/usr/share/ecere/extras/CSVParser.ec is in ecere-extras 0.44.09.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <stdarg.h>
public import "ecere"
// to be moved in ecere?
public class FileHandler
{
public File file;
~FileHandler()
{
delete file;
}
}
public struct CSVParserParameters
{
char fieldSeparator;
char valueQuotes;
int expectedFieldCount;
bool tolerateNewLineInValues;
//bool checkNulls;
//bool checkCurlies;
};
CSVParserParameters classicParameters = { ',', '\"', 0, false };
public struct CSVParserState
{
uint lineNum;
uint charNum;
uint rowNum;
uint fieldNum;
};
public class CSVParser : public FileHandler
{
public:
CSVParserParameters options { ',', '\"', 0, false };
CSVParserState info;
void PrintMessage(typed_object object, ...)
{
va_list args;
char buffer[4096];
va_start(args, object);
PrintStdArgsToBuffer(buffer, sizeof(buffer), object, args);
va_end(args);
OnMessage(buffer);
}
virtual bool OnMessage(String message)
{
::PrintLn(this._class.name, ": ", message,
" lineNum=", info.lineNum,
" charNum=", info.charNum,
" rowNum=", info.rowNum,
" fieldNum=", info.fieldNum);
}
virtual bool OnRowStrings(Array<String> strings);
virtual void Process()
{
bool quoted = false, status = true;
Array<String> values { };
bool started = false;
int start = 0, end = 0;
int readCount = 0;
Array<char> buffer { minAllocSize = 4096 };
//info.charNum = 0;
info.lineNum = 0;
info.rowNum = 0;
info.fieldNum = 0;
while(!file.Eof() && status)
{
int c, offset = 0;
if(started)
{
offset = readCount - start;
if(offset > buffer.minAllocSize / 2)
buffer.minAllocSize += 4096;
memmove(&buffer[0], &buffer[start], offset);
end -= start;
start = 0;
}
readCount = offset + file.Read(&buffer[offset], 1, buffer.minAllocSize - offset);
for(c = offset; c < readCount && status; c++)
{
char ch = buffer[c];
if(quoted)
{
if(ch == options.valueQuotes)
{
quoted = false;
end = c;
}
}
else
{
if(ch == options.valueQuotes)
{
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)))
{
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')
{
info.lineNum++;
info.rowNum++;
status = OnRowStrings(values);
values.Free();
//info.charNum = 0;
info.fieldNum = 0;
}
}
else if(ch == '\r');
else
{
if(!started)
{
start = c;
started = true;
}
end = c+1;
}
}
//info.charNum++;
}
}
if(end > start)
{
int len = end-start;
String value = new char[len+1];
memcpy(value, &buffer[start], len);
value[len] = 0;
values.Add(value);
}
if(values.count && status)
{
status = OnRowStrings(values);
info.rowNum++;
}
values.Free();
delete values;
}
}
|