File: CSVDataParser.ec

package info (click to toggle)
ecere-sdk 0.44.15-1
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 97,712 kB
  • ctags: 54,695
  • sloc: ansic: 593,042; makefile: 12,250; yacc: 4,955; lex: 707; objc: 259; python: 252; xml: 102
file content (114 lines) | stat: -rw-r--r-- 2,671 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public import "ecere"
public import "CSVParser"

import "timeTools"

public enum ColumnType { null = 1, string = 2, number = 4, floater = 8, date = 12 };
public const String columnTypeNames[ColumnType] = { "", "Null", "String", "", "Number", "", "", "", "Float", "", "", "", "Date" };

public class Column : struct
{
   ColumnType type;
   char * s;
   union
   {
      int i;
      float f;
      DateTime d;
   };

   ~Column()
   {
      delete s;
   }
}

public class CSVDataParser : public CSVParser
{
public:
   ColumnType * columnsTypes;
   int rowCount;
   Array<Column> columns { };

   virtual bool OnRow();

   void Process()
   {
      rowCount = 0;
      CSVParser::Process();
   }

   bool OnRowStrings(Array<String> strings)
   {
      bool status = true;
      int c = 0;

      for(s : strings)
      {
         Column column;
         if(s)
         {
            int len;
            char * str;
            buffer.size = strlen(s) * 3 + 1;
            len = ISO8859_1toUTF8(s, buffer.array, buffer.size);
            str = new char[len + 1];
            memcpy(str, buffer.array, len + 1);
            column = Column { s = str };
         }
         else
            column = Column { s = CopyString("") };
         switch(columnsTypes[c])
         {
            case date:
            {
               DateTime d { };
               d.OnGetDataFromString(s);
               column.type = date;
               if(d.month >= january && d.month <= december)
                  column.d = d;
               else
               {
                  ::Print/*Ln*/("bdate"/*"BAD DATE -- FIXME FIXME -- ", s*/);
                  column.d = { };
               }
               break;
            }
            case floater:
               column.type = floater;
               column.f = (float)strtod(s, null);
               break;
            case number:
               column.type = number;
               column.i = strtol(s, null, 10);
               break;
            case null|string:
            case string:
               column.type = string;
               break;
         }
         switch(columnsTypes[c])
         {
            case date:
            case floater:
            case number:
            case null|string:
            case null:
            case string:
               columns.Add(column);
               break;
            default:
               ::PrintLn("WHAT'S GOING ON?", s);
               break;
         }
         if(++c >= options.expectedFieldCount) break;
      }
      rowCount++;
      status = OnRow();
      columns.Free();
      return status;
   }

private:
   Array<char> buffer { minAllocSize = 1024 };
}