-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_table.c
More file actions
264 lines (245 loc) · 9.3 KB
/
csv_table.c
File metadata and controls
264 lines (245 loc) · 9.3 KB
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
/**********************************************
* libcsv, Version 0.3 Alpha *
* Description: CSV library for C *
* Author: Michael Warren, a.k.a Psycho Cod3r *
* Date: November 2020 *
* License: Michael Warren FSL Version 1.1 *
* Current module: Functions implementing SQL *
* operations on CSV tables *
**********************************************/
#include <stdlib.h>
#include <string.h>
#include "csv.h"
#include "dfloat.h"
// Select next record
// Return NULL if end of table is reached
csv_record *csv_next_record( csv_table *table ){
if( table->cur->next ){
table->cur = table->cur->next;
return table->cur;
}
return NULL;
}
// Rewind to the beginning of the table
void csv_rewind( csv_table *table ){
table->cur = table->start;
}
// Equivelent to DROP TABLE in SQL
void csv_drop_table( csv_table *table ){
csv_record *tmp;
int f;
free( table->header );
// Free table records:
csv_rewind( table );
while( csv_next_record( table ) ){
for( f = 0; f < table->rlen; f++ ){
free( table->cur->record[f] );
}
tmp = table->cur;
table->cur = table->cur->next;
free( tmp );
}
// Next part prevents dangling pointer problems.
table->start = NULL;
table->cur = NULL;
table->header = NULL;
free( table );
table = NULL;
}
// Equivalent to CREATE TABLE in SQL
csv_table *csv_create_table( int rlen, csv_field **field_vector ){
csv_table *table;
int f;
table = (csv_table *) malloc( sizeof( csv_table ) );
table->rlen = rlen;
table->header = (csv_field **) calloc( rlen, sizeof( csv_field * ) );
for( f = 0; f < rlen; f++ ){
table->header[f] = (csv_field *) malloc( sizeof( csv_field ) );
memcpy( table->header[f], field_vector[f], sizeof( csv_field ) );
}
table->start = (csv_record *) malloc( sizeof( csv_record ) );
table->cur = table->start;
return table;
}
// Equivalent to INSERT INTO in SQL
void csv_insert_record( csv_table *table, void **record ){
csv_record *save;
int f;
int len;
save = table->cur;
while( csv_next_record( table ) );
table->cur->next = (csv_record *) calloc( 1, sizeof( csv_record ) );
csv_next_record( table );
table->cur->record = (void **) calloc( table->rlen, sizeof( void * ) );
for( f = 0; f < table->rlen; f++ ){
if( table->header[f]->type == csv_number ){
table->cur->record[f] = malloc( sizeof( dfloat64_t ) );
memcpy( table->cur->record[f], record[f], sizeof( dfloat64_t ) );
}
else if( table->header[f]->type == csv_string ){
len = strlen( (char *) record[f] );
table->cur->record[f] = malloc( len + 1 );
strncpy( table->cur->record[f], record[f], len + 1 );
}
}
table->cur->next = NULL;
table->cur = save;
}
// INSERT a blank record and move to that record
void csv_insert_new_record( csv_table *table ){
int f;
while( csv_next_record( table ) );
table->cur->next = (csv_record *) calloc( 1, sizeof( csv_record ) );
csv_next_record( table );
table->cur->record = (void **) calloc( table->rlen, sizeof( void * ) );
for( f = 0; f < table->rlen; f++ ){
if( table->header[f]->type == csv_number )
table->cur->record[f] = malloc( sizeof( dfloat64_t ) );
else if( table->header[f]->type == csv_string )
table->cur->record[f] = malloc( 1 );
}
table->cur->next = NULL;
}
// Equivalent to DELETE FROM in SQL except it deletes the
// current record rather than those matching a condition
void csv_delete_current_record( csv_table *table ){
csv_record *tmp1;
csv_record *tmp2;
int f;
tmp1 = table->start;
tmp2 = NULL;
while( tmp1->next != table->cur )
tmp1 = tmp1->next;
if( table->cur->next )
tmp2 = table->cur->next;
for( f = 0; f < table->rlen; f++ )
free( table->cur->record[f] );
free( table->cur );
if( tmp2 )
tmp1->next = tmp2;
else
tmp1->next = NULL;
table->cur = tmp1;
}
// Used to retrieve numeric values from the table
dfloat64_t *csv_get_number_field_by_name( csv_table *table, char *name ){
dfloat64_t *df;
int f;
for( f = 0; f < table->rlen; f++ ){
if( !strcmp( table->header[f]->name, name ) )
break;
}
if( f == table->rlen )
// Error: Name not found
return NULL;
if( table->header[f]->type != csv_number )
// Error: Type mismatch
return NULL;
df = (dfloat64_t *) malloc( sizeof( dfloat64_t ) );
memcpy( df, table->cur->record[f], sizeof( dfloat64_t ) );
return df;
}
// Used to retrieve numeric values from the table
dfloat64_t *csv_get_number_field_by_index( csv_table *table, int index ){
dfloat64_t *df;
if( index < 0 || index >= table->rlen )
// Error: Out-of-bounds
return NULL;
if( table->header[index]->type != csv_number )
// Error: Type mismatch
return NULL;
df = (dfloat64_t *) malloc( sizeof( dfloat64_t ) );
memcpy( df, table->cur->record[index], sizeof( dfloat64_t ) );
return df;
}
// Used to retrieve string values from the table
char *csv_get_string_field_by_name( csv_table *table, char *name ){
char *str;
int f;
int len;
for( f = 0; f < table->rlen; f++ ){
if( !strcmp( table->header[f]->name, name ) )
break;
}
if( f == table->rlen )
// Error: Name not found
return NULL;
if( table->header[f]->type != csv_string )
// Error: Type mismatch
return NULL;
len = strlen( table->cur->record[f] );
str = (char *) malloc( len + 1 );
strncpy( str, table->cur->record[f], len + 1 );
return str;
}
// Used to retrieve string values from the table
char *csv_get_string_field_by_index( csv_table *table, int index ){
char *str;
int len;
if( index < 0 || index >= table->rlen )
// Error: Out-of-bounds
return NULL;
if( table->header[index]->type != csv_string )
// Error: Type mismatch
return NULL;
len = strlen( table->cur->record[index] );
str = (char *) malloc( len + 1 );
strncpy( str, table->cur->record[index], len + 1 );
return str;
}
// Used to set the value of a number field
void csv_set_number_field_by_name( csv_table *table, char *name, dfloat64_t *val ){
int f;
for( f = 0; f < table->rlen; f++ ){
if( !strcmp( table->header[f]->name, name ) )
break;
}
if( f == table->rlen )
// Error: Name not found
return;
if( table->header[f]->type != csv_number )
// Error: Type mismatch
return;
memcpy( table->cur->record[f], val, sizeof( dfloat64_t ) );
}
// Used to set the value of a number field
void csv_set_number_field_by_index( csv_table *table, int index, dfloat64_t *val ){
if( index < 0 || index >= table->rlen )
// Error: Out-of-bounds
return;
if( table->header[index]->type != csv_number )
// Error: Type mismatch
return;
memcpy( table->cur->record[index], val, sizeof( dfloat64_t ) );
}
// Used to set the value of a string field
void csv_set_string_field_by_name( csv_table *table, char *name, char *val ){
int f;
int len;
for( f = 0; f < table->rlen; f++ ){
if( !strcmp( table->header[f]->name, name ) )
break;
}
if( f == table->rlen )
// Error: Name not found
return;
if( table->header[f]->type != csv_string )
// Error: Type mismatch
return;
len = strlen( val );
table->cur->record[f] = realloc( table->cur->record[f], len + 1 );
strncpy( table->cur->record[f], val, len + 1 );
}
// Used to set the value of a string field
void csv_set_string_field_by_index( csv_table *table, int index, char *val ){
int len;
if( index < 0 || index >= table->rlen )
// Error: Out-of-bounds
return;
if( table->header[index]->type != csv_string )
// Error: Type mismatch
return;
len = strlen( val );
table->cur->record[index] = realloc( table->cur->record[index], len + 1 );
strncpy( table->cur->record[index], val, len + 1 );
}