-
Notifications
You must be signed in to change notification settings - Fork 0
/
leech_csv.c
368 lines (307 loc) · 10.4 KB
/
leech_csv.c
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
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include "buffer.h"
#include "csv.h"
#include "definitions.h"
#include "files.h"
#include "list.h"
#include "logger.h"
#include "string_lib.h"
#include "utils.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
char *filename;
LCH_List *table;
} CSVconn;
void *LCH_CallbackConnect(const char *const conn_info) {
CSVconn *const conn = (CSVconn *)malloc(sizeof(CSVconn));
if (conn == NULL) {
LCH_LOG_ERROR("malloc(3): Failed to allocate memory: %s", strerror(errno));
return NULL;
}
conn->filename = LCH_StringDuplicate(conn_info);
if (conn->filename == NULL) {
free(conn);
return NULL;
}
conn->table = NULL;
return conn;
}
void LCH_CallbackDisconnect(void *const _conn) {
CSVconn *const conn = (CSVconn *)_conn;
if (conn != NULL) {
free(conn->filename);
LCH_ListDestroy(conn->table);
}
free(conn);
}
bool LCH_CallbackCreateTable(void *const _conn, const char *const table_name,
const LCH_List *const primary_columns,
const LCH_List *const subsidiary_columns) {
CSVconn *const conn = (CSVconn *)_conn;
assert(conn != NULL);
assert(conn->filename != NULL);
assert(primary_columns != NULL);
assert(subsidiary_columns != NULL);
if (LCH_FileIsRegular(conn->filename)) {
LCH_LOG_DEBUG("Skipped creating CSV file '%s': Table \"%s\" already exists",
conn->filename, table_name);
return true;
}
LCH_List *const header = LCH_ListCopy(
primary_columns, (LCH_DuplicateFn)LCH_BufferDuplicate, LCH_BufferDestroy);
if (header == NULL) {
return false;
}
const size_t num_subdidiary = LCH_ListLength(subsidiary_columns);
for (size_t i = 0; i < num_subdidiary; i++) {
const LCH_Buffer *const column_name =
(LCH_Buffer *)LCH_ListGet(subsidiary_columns, i);
if (!LCH_ListAppendBufferDuplicate(header, column_name)) {
LCH_ListDestroy(header);
return false;
}
}
LCH_List *const table = LCH_ListCreate();
if (table == NULL) {
LCH_ListDestroy(header);
return false;
}
if (!LCH_ListAppend(table, header, LCH_ListDestroy)) {
LCH_ListDestroy(table);
LCH_ListDestroy(header);
return false;
}
if (!LCH_CSVComposeFile(table, conn->filename)) {
LCH_ListDestroy(table);
return false;
}
// Print debug info
LCH_Buffer *csv = NULL;
if (LCH_CSVComposeRecord(&csv, header)) {
const char *const str_repr = LCH_BufferData(csv);
LCH_LOG_DEBUG("Created table with header: \n\t%s", str_repr);
LCH_BufferDestroy(csv);
}
LCH_ListDestroy(table);
return true;
}
bool LCH_CallbackTruncateTable(void *const _conn, const char *const table_name,
const char *const uq_column,
const char *const uq_field) {
CSVconn *const conn = (CSVconn *)_conn;
assert(conn != NULL);
assert(conn->filename != NULL);
assert(conn->table != NULL);
assert(uq_column != NULL);
assert(uq_field != NULL);
size_t num_records = LCH_ListLength(conn->table);
assert(num_records > 0);
const LCH_List *const table_header = (LCH_List *)LCH_ListGet(conn->table, 0);
const size_t uq_col_idx =
LCH_ListIndex(table_header, LCH_BufferStaticFromString(uq_column),
(LCH_CompareFn)LCH_BufferCompare);
if (uq_col_idx >= LCH_ListLength(table_header)) {
LCH_LOG_ERROR(
"Missing field name \"%s\" for unique host identifier "
"in table header of table '%s'",
uq_column, table_name);
return false;
}
for (size_t i = 1; i < num_records;) {
const LCH_List *const record = (LCH_List *)LCH_ListGet(conn->table, i);
const LCH_Buffer *const field =
(LCH_Buffer *)LCH_ListGet(record, uq_col_idx);
if (LCH_BufferEqual(LCH_BufferStaticFromString(uq_field), field)) {
// Records with the unqiue host identifier are to be removed
LCH_LOG_DEBUG(
"Deleting record %zu form table \"%s\" because unique host "
"identifier \"%s\" is '%s' ('%s' == '%s')",
i, table_name, uq_column, uq_field, uq_field, LCH_BufferData(field));
LCH_List *const removed = (LCH_List *)LCH_ListRemove(conn->table, i);
LCH_Buffer *str_repr = NULL;
if (LCH_CSVComposeRecord(&str_repr, removed)) {
LCH_LOG_DEBUG("Deleted record contained: %s", LCH_BufferData(str_repr));
LCH_BufferDestroy(str_repr);
}
LCH_ListDestroy(removed);
num_records -= 1;
} else {
i += 1;
}
}
return true;
}
LCH_List *LCH_CallbackGetTable(void *const _conn, const char *const table_name,
LCH_UNUSED const LCH_List *const columns) {
CSVconn *const conn = (CSVconn *)_conn;
assert(conn != NULL);
assert(conn->filename != NULL);
LCH_List *const table = LCH_CSVParseFile(conn->filename);
if (table == NULL) {
return NULL;
}
LCH_LOG_DEBUG("Loaded table \"%s\" from '%s'", table_name, conn->filename);
/**
* TODO: Extract only the fields listed in the columns parameter, and in the
* same order as they appear (see ticket CFE-4339).
*/
return table;
}
bool LCH_CallbackBeginTransaction(void *const _conn) {
CSVconn *const conn = (CSVconn *)_conn;
assert(conn != NULL);
assert(conn->filename != NULL);
LCH_List *const table = LCH_CSVParseFile(conn->filename);
if (table == NULL) {
return false;
}
LCH_LOG_DEBUG("Loaded table from '%s'", conn->filename);
conn->table = table;
return true;
}
bool LCH_CallbackCommitTransaction(void *const _conn) {
CSVconn *const conn = (CSVconn *)_conn;
assert(conn != NULL);
assert(conn->filename != NULL);
assert(conn->table != NULL);
if (!LCH_CSVComposeFile(conn->table, conn->filename)) {
LCH_ListDestroy(conn->table);
conn->table = NULL;
return false;
}
LCH_LOG_DEBUG("Wrote table to '%s'", conn->filename);
LCH_ListDestroy(conn->table);
conn->table = NULL;
return true;
}
bool LCH_CallbackRollbackTransaction(void *const _conn) {
CSVconn *const conn = (CSVconn *)_conn;
assert(conn != NULL);
LCH_ListDestroy(conn->table);
conn->table = NULL;
LCH_LOG_DEBUG("Destroyed table");
return true;
}
bool LCH_CallbackInsertRecord(void *const _conn,
LCH_UNUSED const char *const table_name,
LCH_UNUSED const LCH_List *const columns,
const LCH_List *const values) {
CSVconn *const conn = (CSVconn *)_conn;
assert(conn != NULL);
assert(conn->table != NULL);
LCH_List *const record = LCH_ListCopy(
values, (LCH_DuplicateFn)LCH_BufferDuplicate, LCH_BufferDestroy);
if (record == NULL) {
return false;
}
if (!LCH_ListAppend(conn->table, record, LCH_ListDestroy)) {
LCH_ListDestroy(record);
return false;
}
LCH_Buffer *str_repr = NULL;
if (LCH_CSVComposeRecord(&str_repr, record)) {
LCH_LOG_DEBUG("Inserted record %zu: '%s'", LCH_ListLength(conn->table) - 1,
LCH_BufferData(str_repr));
} else {
LCH_LOG_DEBUG("Inserted record %zu", LCH_ListLength(conn->table) - 1);
}
LCH_BufferDestroy(str_repr);
return true;
}
bool LCH_CallbackDeleteRecord(void *const _conn,
LCH_UNUSED const char *const table_name,
LCH_UNUSED const LCH_List *const primary_columns,
const LCH_List *const primary_values) {
CSVconn *const conn = (CSVconn *)_conn;
assert(conn != NULL);
assert(conn->table != NULL);
const size_t num_records = LCH_ListLength(conn->table);
assert(num_records > 0);
for (size_t i = 1 /* Skip header */; i < num_records; i++) {
const LCH_List *const record =
(const LCH_List *)LCH_ListGet(conn->table, i);
bool found = true;
const size_t num_primary = LCH_ListLength(primary_values);
for (size_t j = 0; j < num_primary; j++) {
const LCH_Buffer *const field = (LCH_Buffer *)LCH_ListGet(record, j);
const LCH_Buffer *const value =
(LCH_Buffer *)LCH_ListGet(primary_values, j);
assert(value != NULL);
if (!LCH_BufferEqual(field, value)) {
found = false;
break;
}
}
if (found) {
LCH_List *const removed = (LCH_List *)LCH_ListRemove(conn->table, i);
LCH_Buffer *str_repr = NULL;
if (LCH_CSVComposeRecord(&str_repr, removed)) {
LCH_LOG_DEBUG("Deleted record %zu: '%s'", i + 1,
LCH_BufferData(str_repr));
} else {
LCH_LOG_DEBUG("Deleted record %zu", i + 1);
}
LCH_BufferDestroy(str_repr);
LCH_ListDestroy(removed);
return true;
}
}
return false;
}
bool LCH_CallbackUpdateRecord(
void *const _conn, LCH_UNUSED const char *const table_name,
LCH_UNUSED const LCH_List *const primary_columns,
const LCH_List *const primary_values,
LCH_UNUSED const LCH_List *const subsidiary_columns,
const LCH_List *const subsidiary_values) {
CSVconn *const conn = (CSVconn *)_conn;
assert(conn != NULL);
assert(conn->table != NULL);
const size_t num_records = LCH_ListLength(conn->table);
assert(num_records > 0);
for (size_t i = 1 /* Skip header */; i < num_records; i++) {
LCH_List *const record = (LCH_List *)LCH_ListGet(conn->table, i);
bool found = true;
size_t j;
const size_t num_primary = LCH_ListLength(primary_values);
for (j = 0; j < num_primary; j++) {
const LCH_Buffer *const field = (LCH_Buffer *)LCH_ListGet(record, j);
const LCH_Buffer *const value =
(LCH_Buffer *)LCH_ListGet(primary_values, j);
if (!LCH_BufferEqual(field, value)) {
found = false;
break;
}
}
if (found) {
const size_t num_subsidiary = LCH_ListLength(subsidiary_values);
for (size_t k = 0; k < num_subsidiary; k++) {
const LCH_Buffer *const value =
(LCH_Buffer *)LCH_ListGet(subsidiary_values, k);
LCH_Buffer *const duplicate = LCH_BufferDuplicate(value);
if (duplicate == NULL) {
return false;
}
LCH_ListSet(record, j + k, duplicate, LCH_BufferDestroy);
}
LCH_Buffer *str_repr = NULL;
if (LCH_CSVComposeRecord(&str_repr, record)) {
LCH_LOG_DEBUG("Updated record %zu: '%s'", i + 1,
LCH_BufferData(str_repr));
} else {
LCH_LOG_DEBUG("Updated record %zu", i + 1);
}
LCH_BufferDestroy(str_repr);
return true;
}
}
return false;
}
#ifdef __cplusplus
}
#endif