86 lines
2.6 KiB
Plaintext
86 lines
2.6 KiB
Plaintext
/// <summary>
|
|
/// Parse CSV line into string array.
|
|
/// </summary>
|
|
/// <param name="line">CSV line.</param>
|
|
/// <returns>List of items in CSV.</returns>
|
|
protected string[] ParseCSVLine(string line)
|
|
{
|
|
string[] rv = new string[0];
|
|
|
|
int quotCount = 0;
|
|
bool ignoreNext = false;
|
|
bool hadQuot = false;
|
|
int startPos = 0;
|
|
|
|
for (int i = 0; i < line.Length; i++)
|
|
{
|
|
|
|
// ignore char
|
|
if (ignoreNext)
|
|
{
|
|
ignoreNext = false;
|
|
continue;
|
|
}
|
|
|
|
// ignore next char
|
|
if (line[i].Equals('\\'))
|
|
{
|
|
ignoreNext = true;
|
|
continue;
|
|
}
|
|
|
|
// push quot
|
|
if (line[i].Equals('"'))
|
|
{
|
|
hadQuot = true;
|
|
|
|
if (quotCount <= 0)
|
|
{
|
|
quotCount++;
|
|
}
|
|
else
|
|
{
|
|
quotCount--;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (line[i].Equals(','))
|
|
{
|
|
if (quotCount <= 0)
|
|
{
|
|
string cell = line.Substring(startPos, (i - startPos));
|
|
if (hadQuot)
|
|
{
|
|
Array.Resize(ref rv, (rv.Length + 1));
|
|
rv[(rv.Length - 1)] = cell.Substring(1, (cell.Length - 2));
|
|
}
|
|
else
|
|
{
|
|
Array.Resize(ref rv, (rv.Length + 1));
|
|
rv[(rv.Length - 1)] = cell;
|
|
}
|
|
startPos = (i + 1);
|
|
hadQuot = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// last cell
|
|
if (startPos <= line.Length)
|
|
{
|
|
string cell = line.Substring(startPos, (line.Length - startPos));
|
|
if (hadQuot)
|
|
{
|
|
Array.Resize(ref rv, (rv.Length + 1));
|
|
rv[(rv.Length - 1)] = cell.Substring(1, (cell.Length - 2));
|
|
}
|
|
else
|
|
{
|
|
Array.Resize(ref rv, (rv.Length + 1));
|
|
rv[(rv.Length - 1)] = cell;
|
|
}
|
|
}
|
|
|
|
return rv;
|
|
} |