-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.c
More file actions
250 lines (175 loc) · 4.28 KB
/
table.c
File metadata and controls
250 lines (175 loc) · 4.28 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
#include <assert.h>
#include <ctype.h>
#include <libgen.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "table.h"
#include "util.h"
const char *TABLE_SUFFIX = ".tsv";
const int NO_SUCH_COLUMN = 0;
/* FIXME: Should types be enums? We would still need a string to int map. */
const char *DEFAULT_TYPE = "text";
char FIELD_SEPARATOR = '\t';
/* Should match IDENT in tokens.l.
Note that max identifier and type lengths are 100 */
#define IDENT_BUFSIZE 101
const char *HEADER_REGEX =
"^([a-zA-Z][a-zA-Z0-9_]{1,99})(\\{[a-zA-Z]{1,100}\\})?$";
char *
path_to_name(char *path) {
char *base = basename(path);
size_t suffix_index = strlen(base) - strlen(TABLE_SUFFIX);
if (suffix_index < 1) {
fprintf(stderr, "[ERROR] Name too short: %s\n", path);
return NULL;
}
if (strcasecmp(base + suffix_index, TABLE_SUFFIX)) {
fprintf(stderr, "[ERROR] Suffix not .tsv: %s\n", path);
return NULL;
}
char *name = strdup(base);
name[suffix_index] = '\0';
return name;
}
column *
read_column(char *header) {
regex_t rx;
int retval;
if (retval = regcomp(&rx, HEADER_REGEX, REG_EXTENDED)) {
char buf[200];
regerror(retval, &rx, buf, 200);
fprintf(stderr, "Regex error: %s\n", buf);
return NULL;
}
else {
regmatch_t matches[3];
matches[2].rm_so = -1;
if (regexec(&rx, header, 3, matches, 0) == 0) {
char name[IDENT_BUFSIZE];
char type[IDENT_BUFSIZE];
regmatch_t rm = matches[1];
strncpy(name, header + rm.rm_so, rm.rm_eo - rm.rm_so);
name[rm.rm_eo - rm.rm_so + 1] = '\0';
rm = matches[2];
if (matches[2].rm_so != -1) {
/* there was a type in curly braces { } */
strncpy(type, header + rm.rm_so + 1, rm.rm_eo - rm.rm_so - 2);
type[rm.rm_eo - rm.rm_so - 2] = '\0';
}
else {
strcpy(type, "text");
}
column *col = malloc(sizeof(column));
col->name = strdup(name);
col->type = strdup(type);
col->next = NULL;
return col;
}
regfree(&rx);
}
return NULL;
}
column *
read_columns(char *path) {
char *line = NULL;
size_t cap = 0;
ssize_t len;
FILE *f = fopen(path, "r");
if (!f) {
fprintf(stderr, "[ERROR] opening file %s", path);
perror("[ERROR] could not open file");
return NULL;
}
len = getline(&line, &cap, f);
if (len == -1) {
if (ferror(stdin)) {
perror("[ERROR] getline: %s\n");
}
else if (feof(stdin)) {
fprintf(stderr, "[ERROR] end of file\n");
}
return NULL;
}
trim(line);
char **headers = split(line, FIELD_SEPARATOR);
column *columns = NULL;
column *col = NULL;
int i;
for (i = 0; headers[i]; ++i) {
if (!col) {
col = read_column(headers[i]);
}
else {
col->next = read_column(headers[i]);
col = col->next;
}
if (!col) {
return NULL;
}
if (!columns) {
columns = col;
}
}
free(line);
free_split_array(headers);
return columns;
}
table *
new_table_from_path(char *path) {
table *tab = malloc(sizeof(table));
tab->filename = strdup(path);
tab->name = path_to_name(path);
if (!tab->name) {
return NULL;
}
tab->columns = read_columns(path);
tab->next = NULL;
return tab;
}
table *
new_table_with_tmp_path() {
table *tab = (table *)malloc(sizeof(table));
tab->filename = make_tmpfile();
tab->name = NULL;
tab->next = NULL;
tab->columns = NULL;
return tab;
}
void
table_print(table *tbl, FILE *fout) {
assert(tbl);
fprintf(fout, "table: %s (%s)\n", tbl->name, tbl->filename);
column *col;
for (col = tbl->columns; col; col = col->next) {
fprintf(fout, " %s{%s}\n", col->name, col->type);
}
}
/* Columns are numbered 1, 2, ...
*
* Returns NO_SUCH_COLUMN if appropriate.
*/
int
table_column_number(table *tbl, char *column_name) {
column *col;
int col_num;
for (col = tbl->columns, col_num = 1; col; col = col->next, ++col_num) {
if (strcmp(column_name, col->name) == 0) {
return col_num;
}
}
return NO_SUCH_COLUMN;
}
/* Returns NULL if table not found.
*/
table *
table_name_to_table(table *tables, char *table_name) {
table *tbl;
for (tbl = tables; tbl; tbl = tbl->next) {
if (strcmp(table_name, tbl->name) == 0) {
return tbl;
}
}
return NULL;
}