ecere/gui/Window: Prevent uninitialized values if base Window methods not overridden...
[sdk] / extras / CSVDataParser.ec
1 public import "ecere"
2 public import "CSVParser"
3
4 import "timeTools"
5
6 public enum ColumnType { null = 1, string = 2, number = 4, floater = 8, date = 12 };
7 public const String columnTypeNames[ColumnType] = { "", "Null", "String", "", "Number", "", "", "", "Float", "", "", "", "Date" };
8
9 public class Column : struct
10 {
11    ColumnType type;
12    char * s;
13    union
14    {
15       int i;
16       float f;
17       DateTime d;
18    };
19
20    ~Column()
21    {
22       delete s;
23    }
24 }
25
26 public class CSVDataParser : public CSVParser
27 {
28 public:
29    ColumnType * columnsTypes;
30    int rowCount;
31    Array<Column> columns { };
32
33    virtual bool OnRow();
34
35    void Process()
36    {
37       rowCount = 0;
38       CSVParser::Process();
39    }
40
41    bool OnRowStrings(Array<String> strings)
42    {
43       bool status = true;
44       int c = 0;
45
46       for(s : strings)
47       {
48          Column column;
49          if(s)
50          {
51             int len;
52             char * str;
53             buffer.size = strlen(s) * 3 + 1;
54             len = ISO8859_1toUTF8(s, buffer.array, buffer.size);
55             str = new char[len + 1];
56             memcpy(str, buffer.array, len + 1);
57             column = Column { s = str };
58          }
59          else
60             column = Column { s = CopyString("") };
61          switch(columnsTypes[c])
62          {
63             case date:
64             {
65                DateTime d { };
66                d.OnGetDataFromString(s);
67                column.type = date;
68                if(d.month >= january && d.month <= december)
69                   column.d = d;
70                else
71                {
72                   ::Print/*Ln*/("bdate"/*"BAD DATE -- FIXME FIXME -- ", s*/);
73                   column.d = { };
74                }
75                break;
76             }
77             case floater:
78                column.type = floater;
79                column.f = (float)strtod(s, null);
80                break;
81             case number:
82                column.type = number;
83                column.i = strtol(s, null, 10);
84                break;
85             case null|string:
86             case string:
87                column.type = string;
88                break;
89          }
90          switch(columnsTypes[c])
91          {
92             case date:
93             case floater:
94             case number:
95             case null|string:
96             case null:
97             case string:
98                columns.Add(column);
99                break;
100             default:
101                ::PrintLn("WHAT'S GOING ON?", s);
102                break;
103          }
104          if(++c >= options.expectedFieldCount) break;
105       }
106       rowCount++;
107       status = OnRow();
108       columns.Free();
109       return status;
110    }
111
112 private:
113    Array<char> buffer { minAllocSize = 1024 };
114 }