2013-03-31 14:09:38 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
2022-11-22 21:36:01 +01:00
|
|
|
|
namespace ChanSort.Api;
|
|
|
|
|
|
|
|
|
|
|
|
public static class CsvFile
|
2013-03-31 14:09:38 +02:00
|
|
|
|
{
|
2022-11-22 21:36:01 +01:00
|
|
|
|
public static IList<string> Parse(string line, char separator)
|
2013-03-31 14:09:38 +02:00
|
|
|
|
{
|
2022-11-22 21:36:01 +01:00
|
|
|
|
if (line.EndsWith("\n")) line = line.Substring(0, line.Length - 1);
|
|
|
|
|
|
if (line.EndsWith("\r")) line = line.Substring(0, line.Length - 1);
|
2013-03-31 14:09:38 +02:00
|
|
|
|
|
2022-11-22 21:36:01 +01:00
|
|
|
|
List<string> tokens = new List<string>();
|
|
|
|
|
|
if (line.Length == 0)
|
|
|
|
|
|
return tokens;
|
2013-03-31 14:09:38 +02:00
|
|
|
|
|
2022-11-22 21:36:01 +01:00
|
|
|
|
bool inQuote = false;
|
|
|
|
|
|
StringBuilder token = new StringBuilder();
|
|
|
|
|
|
for(int i = 0, len=line.Length; i<len; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
char ch = line[i];
|
|
|
|
|
|
if (ch == separator && !inQuote)
|
2013-03-31 14:09:38 +02:00
|
|
|
|
{
|
2022-11-22 21:36:01 +01:00
|
|
|
|
tokens.Add(token.ToString());
|
|
|
|
|
|
token.Remove(0, token.Length);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ch == '"')
|
|
|
|
|
|
{
|
|
|
|
|
|
if (inQuote && i+1 < len && line[i+1] == '"')
|
2013-03-31 14:09:38 +02:00
|
|
|
|
{
|
2022-11-22 21:36:01 +01:00
|
|
|
|
token.Append('"');
|
|
|
|
|
|
++i;
|
2013-04-03 23:26:09 +02:00
|
|
|
|
continue;
|
2013-03-31 14:09:38 +02:00
|
|
|
|
}
|
2022-11-22 21:36:01 +01:00
|
|
|
|
inQuote = !inQuote;
|
|
|
|
|
|
continue;
|
2013-03-31 14:09:38 +02:00
|
|
|
|
}
|
2022-11-22 21:36:01 +01:00
|
|
|
|
token.Append(ch);
|
|
|
|
|
|
}
|
|
|
|
|
|
tokens.Add(token.ToString());
|
|
|
|
|
|
return tokens;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string ToValue(object obj, bool needQuotes = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (obj == null)
|
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
|
|
var text = obj.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
foreach (var c in text)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (c == '\0' || c == '\x1D') // skip end-of-string and end-of-file
|
|
|
|
|
|
continue;
|
|
|
|
|
|
if (c == '\"') // double the double-quote
|
|
|
|
|
|
sb.Append('"');
|
|
|
|
|
|
if (!needQuotes && "\",;\n\r\t".IndexOf(c) >= 0) // characters that require the value to be quoted
|
|
|
|
|
|
needQuotes = true;
|
|
|
|
|
|
sb.Append(c);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (needQuotes)
|
|
|
|
|
|
sb.Insert(0, "\"").Append("\"");
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string ToLine(IEnumerable<object> values, char separator, bool needQuotes = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (values == null)
|
|
|
|
|
|
return "";
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
foreach (var value in values)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sb.Length > 0)
|
|
|
|
|
|
sb.Append(separator);
|
|
|
|
|
|
sb.Append(ToValue(value, needQuotes));
|
2013-03-31 14:09:38 +02:00
|
|
|
|
}
|
2022-11-22 21:36:01 +01:00
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
2013-03-31 14:09:38 +02:00
|
|
|
|
}
|
2022-11-22 21:36:01 +01:00
|
|
|
|
|
|
|
|
|
|
}
|