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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/encode/csv.h"
#include "gate/results.h"
#include "gate/debugging.h"
gate_result_t gate_csv_reader_create(gate_csv_reader_t* reader, gate_stream_t* src_stream, gate_char8_t separator_char, gate_char8_t quote_char)
{
if (NULL == reader || NULL == src_stream)
{
return GATE_RESULT_NULLPOINTER;
}
if (separator_char == 0) separator_char = ',';
if (quote_char == 0) quote_char = '\"';
gate_mem_clear(reader, sizeof(gate_csv_reader_t));
reader->source_stream = src_stream;
gate_object_retain(reader->source_stream);
reader->separator_char = separator_char;
reader->quote_char = quote_char;
return GATE_RESULT_OK;
}
void gate_csv_reader_destroy(gate_csv_reader_t* reader)
{
GATE_DEBUG_ASSERT(reader != NULL);
GATE_DEBUG_ASSERT(reader->source_stream != NULL);
gate_object_release(reader->source_stream);
gate_mem_clear(reader, sizeof(gate_csv_reader_t));
}
static gate_result_t csv_read_into_strbuilder(gate_csv_reader_t* reader, gate_strbuilder_t* sb, gate_bool_t* is_end_of_record)
{
gate_result_t ret = GATE_RESULT_OK;
char current_chr;
gate_size_t retrieved;
unsigned quote_mode = 0;
while (GATE_RESULT_OK == (ret = gate_stream_read(reader->source_stream, ¤t_chr, 1, &retrieved)))
{
if (retrieved == 0)
{
/* end of stream reached, no further bytes to parse */
ret = GATE_RESULT_ENDOFSTREAM;
break;
}
if (quote_mode == 1)
{
/* we are inside quote mode */
if (current_chr == reader->quote_char)
{
/* possible end-of-quote reached */
quote_mode = 2;
}
else
{
/* add any non-quote char directly */
gate_strbuilder_append_chars(sb, 1, current_chr);
}
continue;
}
if (quote_mode == 2)
{
/* previous char was a quote too */
if (current_chr == reader->quote_char)
{
/* double-quote detected, add one quote to output field */
gate_strbuilder_append_chars(sb, 1, '\"');
quote_mode = 1; /* still inside quote block */
continue;
}
else
{
/* we are already outside quote block, process char normally */
quote_mode = 0;
}
}
/* we are outside quote-mode */
if (current_chr == '\n')
{
/* end of line detected */
const gate_size_t field_len = gate_strbuilder_length(sb);
if (field_len > 0 && *gate_strbuilder_ptr(sb, field_len -1) == '\r')
{
/* remove CR-char, if line ended with CR-LF */
gate_strbuilder_discard_back(sb, 1);
}
*is_end_of_record = true;
/* end of field reached */
break;
}
else if (current_chr == reader->separator_char)
{
*is_end_of_record = false;
/* end of field reached */
break;
}
else if (current_chr == reader->quote_char)
{
/* a quote block starts */
quote_mode = 1;
}
else
{
/* regular char -> appended to field */
gate_strbuilder_append_chars(sb, 1, current_chr);
}
}
if (GATE_FAILED(ret))
{
if (gate_strbuilder_length(sb) > 0)
{
/* stream-read failed but we have something in buffer,
so let's return success to read content,
but further read-requests will fail due to an empty buffer
*/
*is_end_of_record = true;
ret = GATE_RESULT_OK;
}
}
return ret;
}
gate_result_t gate_csv_reader_get_next(gate_csv_reader_t* reader, gate_string_t* get_next_field, gate_bool_t* is_end_of_record)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_strbuilder_t sb;
gate_strbuilder_create(&sb, 0);
ret = csv_read_into_strbuilder(reader, &sb, is_end_of_record);
if (GATE_SUCCEEDED(ret))
{
gate_strbuilder_to_string(&sb, get_next_field);
}
gate_strbuilder_release(&sb);
return ret;
}
gate_result_t gate_csv_reader_get_next_buffer(gate_csv_reader_t* reader,
char* buffer, gate_size_t buffer_capacity, gate_size_t* buffer_used, gate_bool_t* is_end_of_record)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_strbuilder_t sb;
gate_strbuilder_create_static(&sb, buffer, buffer_capacity, 0);
ret = csv_read_into_strbuilder(reader, &sb, is_end_of_record);
*buffer_used = gate_strbuilder_length(&sb);
return ret;
}
gate_result_t gate_csv_writer_create(gate_csv_writer_t* writer,
gate_stream_t* dest_stream, gate_char8_t separator_char, gate_char8_t quote_char)
{
if (writer == NULL || dest_stream == NULL)
{
return GATE_RESULT_NULLPOINTER;
}
gate_mem_clear(writer, sizeof(gate_csv_writer_t));
writer->dest_stream = dest_stream;
gate_object_retain(writer->dest_stream);
writer->separator_char = separator_char;
writer->quote_char = quote_char;
return GATE_RESULT_OK;
}
void gate_csv_writer_destroy(gate_csv_writer_t* writer)
{
GATE_DEBUG_ASSERT(writer != NULL);
GATE_DEBUG_ASSERT(writer->dest_stream != NULL);
gate_object_release(writer->dest_stream);
gate_mem_clear(writer, sizeof(gate_csv_writer_t));
}
gate_result_t gate_csv_writer_write_next(gate_csv_writer_t* writer,
gate_string_t const* next_field, gate_bool_t is_end_of_record)
{
const char separator = is_end_of_record ? '\n' : writer->separator_char;
gate_size_t pos = gate_string_char_pos(next_field, '\n', 0);
gate_bool_t need_quote = pos != GATE_STR_NPOS;
gate_size_t dummy = 0;
if (!need_quote)
{
pos = gate_string_char_pos(next_field, writer->quote_char, 0);
need_quote = pos != GATE_STR_NPOS;
}
if (!need_quote)
{
pos = gate_string_char_pos(next_field, writer->separator_char, 0);
need_quote = pos != GATE_STR_NPOS;
}
if (need_quote)
{
char const* const str = gate_string_ptr(next_field, 0);
gate_size_t const len = gate_string_length(next_field);
gate_size_t startAt = 0;
gate_result_t ret = gate_stream_write(writer->dest_stream, &writer->quote_char, 1, &dummy);
GATE_RETURN_IF_FAILED(ret);
while (startAt < len)
{
pos = gate_string_char_pos(next_field, writer->quote_char, startAt);
if (GATE_STR_NPOS == pos)
{
ret = gate_stream_write_block(writer->dest_stream, &str[startAt], len - startAt, &dummy);
startAt = len;
}
else
{
if (pos != startAt)
{
/* write block before quote */
ret = gate_stream_write_block(writer->dest_stream, &str[startAt], pos - startAt, &dummy);
GATE_RETURN_IF_FAILED(ret);
}
/* write double-quote */
ret = gate_stream_write(writer->dest_stream, &writer->quote_char, 1, &dummy);
GATE_RETURN_IF_FAILED(ret);
ret = gate_stream_write(writer->dest_stream, &writer->quote_char, 1, &dummy);
startAt = pos + 1;
}
GATE_RETURN_IF_FAILED(ret);
}
ret = gate_stream_write_block(writer->dest_stream, &str[startAt], len - startAt, &dummy);<--- Variable 'ret' is reassigned a value before the old one has been used.
ret = gate_stream_write(writer->dest_stream, &writer->quote_char, 1, &dummy);<--- Variable 'ret' is reassigned a value before the old one has been used.
GATE_RETURN_IF_FAILED(ret);
}
else
{
gate_result_t const ret = gate_stream_print_string(writer->dest_stream, next_field);
GATE_RETURN_IF_FAILED(ret);
}
return gate_stream_write(writer->dest_stream, &separator, 1, &dummy);
}
gate_result_t gate_csv_proptable_load(gate_stream_t* input_stream, gate_proptable_t* table, char separator_char, char quote_char)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_csv_reader_t reader;
do
{
gate_size_t col = 0;
gate_size_t row = 0;
gate_bool_t column_mode = true;
ret = gate_csv_reader_create(&reader, input_stream, separator_char, quote_char);
GATE_BREAK_IF_FAILED(ret);
if (NULL == gate_proptable_create(table))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
while (GATE_SUCCEEDED(ret))
{
gate_string_t field = GATE_STRING_INIT_EMPTY;
gate_bool_t is_end_of_line = false;
ret = gate_csv_reader_get_next(&reader, &field, &is_end_of_line);
GATE_BREAK_IF_FAILED(ret);
do
{
if (column_mode)
{
/* reader header line -> create colums */
ret = gate_proptable_insert_column(table, &field, GATE_PROPERTY_TYPE_STRING, col);
gate_string_release(&field);
GATE_BREAK_IF_FAILED(ret);
++col;
if (is_end_of_line)
{
column_mode = false;
col = 0;
row = 0;
}
}
else
{
gate_property_t prop = GATE_INIT_EMPTY;
if (col == 0)
{
ret = gate_proptable_insert_row(table, row);
GATE_BREAK_IF_FAILED(ret);
}
if (NULL == gate_property_create_string(&prop, &field))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_proptable_set_item_at(table, row, col, &prop);
gate_property_destroy(&prop);
++col;
if (is_end_of_line)
{
col = 0;
++row;
}
}
} while (0);
gate_string_release(&field);
}
if (ret == GATE_RESULT_ENDOFSTREAM)
{
ret = GATE_RESULT_OK;
}
} while (0);
return ret;
}
gate_result_t gate_csv_proptable_store(gate_proptable_t const* table, gate_stream_t* output_stream, char separator_char, char quote_char)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_csv_writer_t writer = GATE_INIT_EMPTY;
do
{
gate_size_t row, col;
const gate_size_t col_count = gate_proptable_get_column_count(table);
const gate_size_t row_count = gate_proptable_get_row_count(table);
gate_csv_writer_create(&writer, output_stream, separator_char, quote_char);
for (col = 0; col < col_count; ++col)
{
gate_string_t colname = GATE_STRING_INIT_EMPTY;
ret = gate_proptable_get_column_name(table, col, &colname);
GATE_BREAK_IF_FAILED(ret);
ret = gate_csv_writer_write_next(&writer, &colname, col + 1 == col_count);
gate_string_release(&colname);
GATE_BREAK_IF_FAILED(ret);
}
GATE_BREAK_IF_FAILED(ret);
for (row = 0; row < row_count; ++row)
{
for (col = 0; col < col_count; ++col)
{
gate_property_t const* prop = gate_proptable_get_item_at(table, row, col);
if (prop == NULL)
{
static gate_string_t const empty = GATE_STRING_INIT_EMPTY;
ret = gate_csv_writer_write_next(&writer, &empty, col + 1 == col_count);
}
else
{
gate_string_t proptext = GATE_STRING_INIT_EMPTY;
ret = gate_property_get_string(prop, &proptext);<--- Variable 'ret' is reassigned a value before the old one has been used.
ret = gate_csv_writer_write_next(&writer, &proptext, col + 1 == col_count);<--- Variable 'ret' is reassigned a value before the old one has been used.
gate_string_release(&proptext);
}
GATE_BREAK_IF_FAILED(ret);
}
GATE_BREAK_IF_FAILED(ret);
}
} while (0);
gate_csv_writer_destroy(&writer);
return ret;
}
|