]>
git.datanom.net - lensdb.git/blob - src/csv.c
4 * Copyright 2024 Michael Rasmussen <mir@datanom.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 #include "csv-parser/csv-parser.h"
26 #include <glib/gstdio.h>
32 #define removechar2 "\"\""
33 #define removechar1 "\""
34 #define removechar "\""
36 //static FILE* csv_file = NULL;
51 bool first_line_header
;
54 static inline const gchar
* stringFromFields(Fields f
)
56 static const char *strings
[] = { "ID", "SN", "MAKER", "LENS", "NOTES", "DATE" };
61 static bool strv_contains(gchar
** array
, const gchar
* s
) {
63 gchar
* s2
= g_ascii_strup(s
, -1);
64 for (gchar
** ptr
= array
; *ptr
; ptr
++) {
65 g_autofree gchar
* s1
= g_ascii_strup(s
, -1);
66 if (g_str_equal(s1
, s2
)) {
76 static gchar
** parse_headers(gchar
** headers
) {
79 GStrvBuilder
* builder
= g_strv_builder_new();
80 for (Fields i
= SN
; i
< FIELD_NUMS
; i
++) {
81 if (strv_contains(headers
, stringFromFields(i
))) {
82 gchar
* h
= g_ascii_strup(headers
[i
], -1);
83 g_strv_builder_add(builder
, h
);
87 order
= g_strv_builder_end(builder
);
88 g_strv_builder_unref(builder
);
93 static Record
* parse(CsvParser
* parser
, gchar
** rows
) {
94 Record
* record
= NULL
;
96 gchar
** fields
= parser
->header_order
;
97 if (fields
== NULL
|| rows
== NULL
|| g_strcmp0(*rows
, "") == 0) return record
;
99 record
= g_new0(Record
, 1);
100 for (int i
= 0; i
< g_strv_length(fields
); i
++) {
101 gchar
* elem
= g_strstrip(rows
[i
]);
102 //fprintf(stderr, "Line: %d - %s\n", i, elem);
105 record
->sn
= g_strdup(elem
);
108 record
->maker
= g_strdup(elem
);
111 record
->data
= g_strdup(elem
);
114 record
->notes
= g_strdup(elem
);
117 record
->date
= g_strdup(elem
);
125 static bool open_file(CsvParser
* parser
, const gchar
* file
, bool write
, bool overwrite
, CvsError
** error
) {
126 if (write
== false) {
127 if (!g_file_test(file
, G_FILE_TEST_IS_REGULAR
|G_FILE_TEST_EXISTS
)) {
129 *error
= csv_error_new();
131 (*error
)->message
= g_strdup_printf("%s: File does not exists or is directory or special file", file
);
135 parser
->csv_file
= g_fopen(file
, "r");
137 if (g_file_test(file
, G_FILE_TEST_EXISTS
) && !overwrite
) {
139 *error
= csv_error_new();
141 (*error
)->message
= g_strdup_printf("%s: File exists", file
);
145 parser
->csv_file
= g_fopen(file
, "w");
148 if (parser
->csv_file
== NULL
) {
150 *error
= csv_error_new();
152 (*error
)->message
= g_strdup_printf("%s: File does not exists or is directory or special file", file
);
160 static Record
* read_next_line(CsvParser
* parser
, CvsError
** error
) {
161 Record
* record
= NULL
;
162 gchar
* buffer
= g_new0(gchar
, MAX_ROW
);
166 if (!parser
->csv_file
) return NULL
;
168 if (feof(parser
->csv_file
)) {
169 fclose(parser
->csv_file
);
170 parser
->csv_file
= NULL
;
175 while (fgets(buffer
, MAX_ROW
, parser
->csv_file
)) {
176 if (strlen(buffer
) == MAX_ROW
- 1) {
180 gchar
* tmp
= g_strdup_printf("%s%s", line
, buffer
);
182 line
= g_strdup(tmp
);
183 //fprintf(stderr, "line: %s\n", line);
186 line
= g_strdup(buffer
);
190 line
= g_strdup(buffer
);
192 gchar
* tmp
= g_strdup_printf("%s%s", line
, buffer
);
194 line
= g_strdup(tmp
);
198 if ((c
= g_strstr_len(line
, -1, "\n")) != NULL
)
199 line
[strlen(line
) - strlen(c
)] = '\0';
204 if (line
== NULL
) return NULL
;
206 if (parser
->first_line_header
) {
207 gchar
** elems
= parse_csv(line
);
212 *error
= g_new0(CvsError
, 1);
214 (*error
)->message
= g_strdup("Missing header row");
219 fields
= g_strv_length(elems
);
220 if (fields
!= FIELD_NUMS
) {
222 *error
= csv_error_new();
224 (*error
)->message
= g_strdup_printf("Row must have %d fields", FIELD_NUMS
);
230 parser
->header_order
= parse_headers(elems
);
233 if (!parser
->header_order
|| !(parser
->header_order
&& g_strv_length(parser
->header_order
))) {
235 *error
= csv_error_new();
237 (*error
)->message
= g_strdup_printf("Row must have %d fields", FIELD_NUMS
);
242 parser
->first_line_header
= false;
244 return read_next_line(parser
, error
);
246 gchar
** elems
= parse_csv(line
);
251 *error
= csv_error_new();
253 (*error
)->message
= g_strdup("No lens info found");
258 if (fields
>= 0 && fields
!= g_strv_length(elems
)) {
260 *error
= csv_error_new();
262 (*error
)->message
= g_strdup("Header and fields mismatch");
268 record
= parse(parser
, elems
);
276 bool csv_parser_dump_row(CsvParser
* parser
, const Record
* row
, CvsError
** error
) {
277 if (!parser
->csv_file
|| row
== NULL
) return false;
279 gchar
* strrow
= g_strdup_printf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n",
280 row
->sn
, row
->maker
, row
->data
, row
->notes
, row
->date
);
281 size_t expected
= strlen(strrow
);
282 size_t actual
= fwrite(strrow
, sizeof(char), expected
, parser
->csv_file
);
285 if (actual
!= expected
)
291 CsvParser
* csv_parser_new(Instructions
* instructions
, CvsError
** error
) {
292 CsvParser
* parser
= g_new0(CsvParser
, 1);
294 parser
->first_line_header
= instructions
->first_line_header
;
295 if (! open_file(parser
, instructions
->file
, instructions
->write
, instructions
->write
, error
)) {
296 csv_parser_free(parser
);
302 Record
* csv_parser_next_line(CsvParser
* parser
, CvsError
** error
) {
303 return read_next_line(parser
, error
);
306 CvsError
* csv_error_new() {
308 err
= g_new0(CvsError
, 1);
312 void csv_parser_free(CsvParser
* parser
) {
313 if (parser
== NULL
) return;
315 if (parser
->csv_file
)
316 fclose(parser
->csv_file
);
317 if (parser
->header_order
)
318 g_strfreev(parser
->header_order
);
322 void csv_error_free(CvsError
* error
) {
324 g_free(error
->message
);
329 void csv_error_clear(CvsError
** error
) {
330 if (error
&& *error
) {
331 csv_error_free(*error
);
336 void csv_record_print(Record
* r
) {
337 fprintf(stdout
, "sn: %s maker: %s lens: %s notes: %s date added: %s\n", r
->sn
, r
->maker
, r
->data
, r
->notes
, r
->date
);
340 void csv_record_free(Record
* r
) {
This page took 1.059762 seconds and 6 git commands to generate.