GCC Code Coverage Report


Directory: src/gate/
File: src/gate/encode/csv.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 178 185 96.2%
Functions: 10 10 100.0%
Branches: 84 116 72.4%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/encode/csv.h"
30 #include "gate/results.h"
31 #include "gate/debugging.h"
32
33 4 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)
34 {
35
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (NULL == reader || NULL == src_stream)
36 {
37 ✗ return GATE_RESULT_NULLPOINTER;
38 }
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (separator_char == 0) separator_char = ',';
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (quote_char == 0) quote_char = '\"';
41
42 4 gate_mem_clear(reader, sizeof(gate_csv_reader_t));
43 4 reader->source_stream = src_stream;
44 4 gate_object_retain(reader->source_stream);
45 4 reader->separator_char = separator_char;
46 4 reader->quote_char = quote_char;
47 4 return GATE_RESULT_OK;
48 }
49
50 3 void gate_csv_reader_destroy(gate_csv_reader_t* reader)
51 {
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_DEBUG_ASSERT(reader != NULL);
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_DEBUG_ASSERT(reader->source_stream != NULL);
54 3 gate_object_release(reader->source_stream);
55 3 gate_mem_clear(reader, sizeof(gate_csv_reader_t));
56 3 }
57
58 25 static gate_result_t csv_read_into_strbuilder(gate_csv_reader_t* reader, gate_strbuilder_t* sb, gate_bool_t* is_end_of_record)
59 {
60 25 gate_result_t ret = GATE_RESULT_OK;
61 char current_chr;
62 gate_size_t retrieved;
63 25 unsigned quote_mode = 0;
64
1/2
✓ Branch 1 taken 165 times.
✗ Branch 2 not taken.
165 while (GATE_RESULT_OK == (ret = gate_stream_read(reader->source_stream, &current_chr, 1, &retrieved)))
65 {
66
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 162 times.
165 if (retrieved == 0)
67 {
68 /* end of stream reached, no further bytes to parse */
69 3 ret = GATE_RESULT_ENDOFSTREAM;
70 3 break;
71 }
72
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 88 times.
162 if (quote_mode == 1)
73 {
74 /* we are inside quote mode */
75
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 66 times.
74 if (current_chr == reader->quote_char)
76 {
77 /* possible end-of-quote reached */
78 8 quote_mode = 2;
79 }
80 else
81 {
82 /* add any non-quote char directly */
83 66 gate_strbuilder_append_chars(sb, 1, current_chr);
84 }
85 74 continue;
86 }
87
88
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 80 times.
88 if (quote_mode == 2)
89 {
90 /* previous char was a quote too */
91
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (current_chr == reader->quote_char)
92 {
93 /* double-quote detected, add one quote to output field */
94 4 gate_strbuilder_append_chars(sb, 1, '\"');
95 4 quote_mode = 1; /* still inside quote block */
96 4 continue;
97 }
98 else
99 {
100 /* we are already outside quote block, process char normally */
101 4 quote_mode = 0;
102 }
103 }
104
105 /* we are outside quote-mode */
106
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 77 times.
84 if (current_chr == '\n')
107 {
108 /* end of line detected */
109 7 const gate_size_t field_len = gate_strbuilder_length(sb);
110
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 4 times.
7 if (field_len > 0 && *gate_strbuilder_ptr(sb, field_len -1) == '\r')
111 {
112 /* remove CR-char, if line ended with CR-LF */
113 3 gate_strbuilder_discard_back(sb, 1);
114 }
115 7 *is_end_of_record = true;
116 /* end of field reached */
117 7 break;
118 }
119
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 62 times.
77 else if (current_chr == reader->separator_char)
120 {
121 15 *is_end_of_record = false;
122 /* end of field reached */
123 15 break;
124 }
125
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 58 times.
62 else if (current_chr == reader->quote_char)
126 {
127 /* a quote block starts */
128 4 quote_mode = 1;
129 }
130 else
131 {
132 /* regular char -> appended to field */
133 58 gate_strbuilder_append_chars(sb, 1, current_chr);
134 }
135 }
136
137
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 22 times.
25 if (GATE_FAILED(ret))
138 {
139
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if (gate_strbuilder_length(sb) > 0)
140 {
141 /* stream-read failed but we have something in buffer,
142 so let's return success to read content,
143 but further read-requests will fail due to an empty buffer
144 */
145 1 *is_end_of_record = true;
146 1 ret = GATE_RESULT_OK;
147 }
148 }
149 25 return ret;
150 }
151
152 24 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)
153 {
154 24 gate_result_t ret = GATE_RESULT_FAILED;
155 gate_strbuilder_t sb;
156 24 gate_strbuilder_create(&sb, 0);
157
158 24 ret = csv_read_into_strbuilder(reader, &sb, is_end_of_record);
159
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
24 if (GATE_SUCCEEDED(ret))
160 {
161 22 gate_strbuilder_to_string(&sb, get_next_field);
162 }
163 24 gate_strbuilder_release(&sb);
164 24 return ret;
165 }
166
167 1 gate_result_t gate_csv_reader_get_next_buffer(gate_csv_reader_t* reader,
168 char* buffer, gate_size_t buffer_capacity, gate_size_t* buffer_used, gate_bool_t* is_end_of_record)
169 {
170 1 gate_result_t ret = GATE_RESULT_FAILED;
171 gate_strbuilder_t sb;
172 1 gate_strbuilder_create_static(&sb, buffer, buffer_capacity, 0);
173
174 1 ret = csv_read_into_strbuilder(reader, &sb, is_end_of_record);
175 1 *buffer_used = gate_strbuilder_length(&sb);
176 1 return ret;
177 }
178
179
180
181
182 2 gate_result_t gate_csv_writer_create(gate_csv_writer_t* writer,
183 gate_stream_t* dest_stream, gate_char8_t separator_char, gate_char8_t quote_char)
184 {
185
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (writer == NULL || dest_stream == NULL)
186 {
187 ✗ return GATE_RESULT_NULLPOINTER;
188 }
189 2 gate_mem_clear(writer, sizeof(gate_csv_writer_t));
190 2 writer->dest_stream = dest_stream;
191 2 gate_object_retain(writer->dest_stream);
192 2 writer->separator_char = separator_char;
193 2 writer->quote_char = quote_char;
194 2 return GATE_RESULT_OK;
195 }
196
197 2 void gate_csv_writer_destroy(gate_csv_writer_t* writer)
198 {
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_DEBUG_ASSERT(writer != NULL);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_DEBUG_ASSERT(writer->dest_stream != NULL);
201 2 gate_object_release(writer->dest_stream);
202 2 gate_mem_clear(writer, sizeof(gate_csv_writer_t));
203 2 }
204
205 17 gate_result_t gate_csv_writer_write_next(gate_csv_writer_t* writer,
206 gate_string_t const* next_field, gate_bool_t is_end_of_record)
207 {
208
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 12 times.
17 const char separator = is_end_of_record ? '\n' : writer->separator_char;
209 17 gate_size_t pos = gate_string_char_pos(next_field, '\n', 0);
210 17 gate_bool_t need_quote = pos != GATE_STR_NPOS;
211 17 gate_size_t dummy = 0;
212
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
17 if (!need_quote)
213 {
214 16 pos = gate_string_char_pos(next_field, writer->quote_char, 0);
215 16 need_quote = pos != GATE_STR_NPOS;
216 }
217
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
17 if (!need_quote)
218 {
219 16 pos = gate_string_char_pos(next_field, writer->separator_char, 0);
220 16 need_quote = pos != GATE_STR_NPOS;
221 }
222
223
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 15 times.
17 if (need_quote)
224 {
225 2 char const* const str = gate_string_ptr(next_field, 0);
226 2 gate_size_t const len = gate_string_length(next_field);
227 2 gate_size_t startAt = 0;
228 2 gate_result_t ret = gate_stream_write(writer->dest_stream, &writer->quote_char, 1, &dummy);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_RETURN_IF_FAILED(ret);
230
231
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 while (startAt < len)
232 {
233 4 pos = gate_string_char_pos(next_field, writer->quote_char, startAt);
234
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (GATE_STR_NPOS == pos)
235 {
236 2 ret = gate_stream_write_block(writer->dest_stream, &str[startAt], len - startAt, &dummy);
237 2 startAt = len;
238 }
239 else
240 {
241
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (pos != startAt)
242 {
243 /* write block before quote */
244 2 ret = gate_stream_write_block(writer->dest_stream, &str[startAt], pos - startAt, &dummy);
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_RETURN_IF_FAILED(ret);
246 }
247 /* write double-quote */
248 2 ret = gate_stream_write(writer->dest_stream, &writer->quote_char, 1, &dummy);
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_RETURN_IF_FAILED(ret);
250 2 ret = gate_stream_write(writer->dest_stream, &writer->quote_char, 1, &dummy);
251 2 startAt = pos + 1;
252 }
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_RETURN_IF_FAILED(ret);
254 }
255 2 ret = gate_stream_write_block(writer->dest_stream, &str[startAt], len - startAt, &dummy);
256
257 2 ret = gate_stream_write(writer->dest_stream, &writer->quote_char, 1, &dummy);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_RETURN_IF_FAILED(ret);
259 }
260 else
261 {
262 15 gate_result_t const ret = gate_stream_print_string(writer->dest_stream, next_field);
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 GATE_RETURN_IF_FAILED(ret);
264 }
265 17 return gate_stream_write(writer->dest_stream, &separator, 1, &dummy);
266 }
267
268 1 gate_result_t gate_csv_proptable_load(gate_stream_t* input_stream, gate_proptable_t* table, char separator_char, char quote_char)
269 {
270 1 gate_result_t ret = GATE_RESULT_FAILED;
271 gate_csv_reader_t reader;
272
273 do
274 {
275 1 gate_size_t col = 0;
276 1 gate_size_t row = 0;
277 1 gate_bool_t column_mode = true;
278 1 ret = gate_csv_reader_create(&reader, input_stream, separator_char, quote_char);
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
280
281
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_proptable_create(table))
282 {
283 ✗ ret = GATE_RESULT_OUTOFMEMORY;
284 ✗ break;
285 }
286
287
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 while (GATE_SUCCEEDED(ret))
288 {
289 13 gate_string_t field = GATE_STRING_INIT_EMPTY;
290 13 gate_bool_t is_end_of_line = false;
291 13 ret = gate_csv_reader_get_next(&reader, &field, &is_end_of_line);
292
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 GATE_BREAK_IF_FAILED(ret);
293
294 do
295 {
296
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12 if (column_mode)
297 {
298 /* reader header line -> create colums */
299 4 ret = gate_proptable_insert_column(table, &field, GATE_PROPERTY_TYPE_STRING, col);
300 4 gate_string_release(&field);
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
302 4 ++col;
303
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (is_end_of_line)
304 {
305 1 column_mode = false;
306 1 col = 0;
307 1 row = 0;
308 }
309 }
310 else
311 {
312 8 gate_property_t prop = GATE_INIT_EMPTY;
313
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (col == 0)
314 {
315 2 ret = gate_proptable_insert_row(table, row);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
317 }
318
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (NULL == gate_property_create_string(&prop, &field))
319 {
320 ✗ ret = GATE_RESULT_OUTOFMEMORY;
321 ✗ break;
322 }
323 8 ret = gate_proptable_set_item_at(table, row, col, &prop);
324 8 gate_property_destroy(&prop);
325 8 ++col;
326
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (is_end_of_line)
327 {
328 2 col = 0;
329 2 ++row;
330 }
331 }
332 } while (0);
333 12 gate_string_release(&field);
334 }
335
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ret == GATE_RESULT_ENDOFSTREAM)
336 {
337 1 ret = GATE_RESULT_OK;
338 }
339 } while (0);
340
341 1 return ret;
342 }
343
344 1 gate_result_t gate_csv_proptable_store(gate_proptable_t const* table, gate_stream_t* output_stream, char separator_char, char quote_char)
345 {
346 1 gate_result_t ret = GATE_RESULT_FAILED;
347 1 gate_csv_writer_t writer = GATE_INIT_EMPTY;
348
349 do
350 {
351 gate_size_t row, col;
352 1 const gate_size_t col_count = gate_proptable_get_column_count(table);
353 1 const gate_size_t row_count = gate_proptable_get_row_count(table);
354
355 1 gate_csv_writer_create(&writer, output_stream, separator_char, quote_char);
356
357
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (col = 0; col < col_count; ++col)
358 {
359 4 gate_string_t colname = GATE_STRING_INIT_EMPTY;
360 4 ret = gate_proptable_get_column_name(table, col, &colname);
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
362 4 ret = gate_csv_writer_write_next(&writer, &colname, col + 1 == col_count);
363 4 gate_string_release(&colname);
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
365 }
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
367
368
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (row = 0; row < row_count; ++row)
369 {
370
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (col = 0; col < col_count; ++col)
371 {
372 8 gate_property_t const* prop = gate_proptable_get_item_at(table, row, col);
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (prop == NULL)
374 {
375 static gate_string_t const empty = GATE_STRING_INIT_EMPTY;
376 ✗ ret = gate_csv_writer_write_next(&writer, &empty, col + 1 == col_count);
377 }
378 else
379 {
380 8 gate_string_t proptext = GATE_STRING_INIT_EMPTY;
381 8 ret = gate_property_get_string(prop, &proptext);
382 8 ret = gate_csv_writer_write_next(&writer, &proptext, col + 1 == col_count);
383 8 gate_string_release(&proptext);
384 }
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 GATE_BREAK_IF_FAILED(ret);
386 }
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
388 }
389
390 } while (0);
391 1 gate_csv_writer_destroy(&writer);
392 1 return ret;
393 }
394