extrras/gui/CheckListBox: Fixed NotifyChanged to give proper 'row' for hierarchy
[sdk] / extras / CSVParser.ec
index bd05d8a..d10afa7 100644 (file)
@@ -36,7 +36,7 @@ public struct CSVParserState
 public class CSVParser : public FileHandler
 {
 public:
-   CSVParserParameters options;
+   CSVParserParameters options { ',', '\"', 0, false };
    CSVParserState info;
 
    void PrintMessage(typed_object object, ...)
@@ -64,6 +64,7 @@ public:
    {
       bool quoted = false, status = true;
       Array<String> values { };
+      bool started = false;
       int start = 0, end = 0;
       int readCount = 0;
       Array<char> buffer { minAllocSize = 4096 };
@@ -77,7 +78,7 @@ public:
       {
          int c, offset = 0;
 
-         if(start)
+         if(started)
          {
             offset = readCount - start;
             if(offset > buffer.minAllocSize / 2)
@@ -105,17 +106,22 @@ public:
                {
                   quoted = true;
                   start = c + 1;
+                  started = true;
                }
                //else if(ch == options.fieldSeparator || ch == '\n')
                else if(ch == options.fieldSeparator ||
-                     (ch == '\n' && (!options.tolerateNewLineInValues || info.fieldNum == options.expectedFieldCount-1)))
+                     (ch == '\n' && (!options.tolerateNewLineInValues || info.fieldNum >= options.expectedFieldCount-1)))
                {
-                  int len = end-start;
-                  String value = new char[len+1];
-                  memcpy(value, &buffer[start], len);
-                  value[len] = 0;
-                  values.Add(value);
+                  if(values.count < options.expectedFieldCount)
+                  {
+                     int len = started ? (end-start) : 0;
+                     String value = new char[len+1];
+                     memcpy(value, &buffer[start], len);
+                     value[len] = 0;
+                     values.Add(value);
+                  }
                   start = end = 0;
+                  started = false;
                   info.fieldNum++;
                   if(ch == '\n')
                   {
@@ -130,9 +136,12 @@ public:
                else if(ch == '\r');
                else
                {
-                  if(!start)
+                  if(!started)
+                  {
                      start = c;
-                  end = c;
+                     started = true;
+                  }
+                  end = c+1;
                }
             }
             //info.charNum++;