7 #if !defined (ASCIIFILE_INCLUDED) 12 enum mytypes { STRING, FLOAT, DOUBLE,
13 SHORTSHORT, SHORT, INT, LONG, LONGLONG,
14 USHORTSHORT, USHORT, UINT, ULONG, ULONGLONG };
25 int compile_regex (regex_t * r,
const char * regex_text)
27 int status = regcomp (r, regex_text, REG_EXTENDED|REG_NEWLINE);
29 char error_message[LINE_SIZE_MAX];
30 regerror (status, r, error_message, LINE_SIZE_MAX);
31 printf (
"Regex error compiling '%s': %s\n",
32 regex_text, error_message);
48 int count_matches(
const char *regex_text,
const char *to_match) {
53 ret = compile_regex(&r, regex_text);
57 const char * p = to_match;
58 const int n_sub_matches = 10;
59 regmatch_t m[n_sub_matches];
61 int nomatch = regexec(&r, p, n_sub_matches, m, 0);
79 int count_formats(
const char* fmt_str) {
80 const char * fmt_regex =
"%([[:digit:]]+\\$)?[+-]?([ 0]|\'.{1})?-?[[:digit:]]*(\\.[[:digit:]]+)?[lhjztL]*[bcdeEufFgGosxX]";
81 int ret = count_matches(fmt_regex, fmt_str);
105 return af_open(&((*t).f));
127 int ret = 0, com = 1;
128 size_t nread = LINE_SIZE_MAX;
129 char *line = (
char*)malloc(nread);
130 while ((ret >= 0) && (com == 1)) {
131 ret = af_readline_full(t.
f, &line, &nread);
134 com = af_is_comment(t.
f, line);
137 if (sret != t.
ncols) {
138 printf(
"at_vreadline: %d arguments filled, but %d were expected\n",
170 int ret = at_vreadline(t, ap);
186 int ret = at_vwriteline(t, ap);
199 if (af_is_open(t.
f) == 1) {
216 int ret = at_open(t);
219 size_t nread = LINE_SIZE_MAX;
220 char *line = (
char*)malloc(nread);
222 while (getline(&line, &nread, (*t).f.fd) >= 0) {
223 if (af_is_comment((*t).f, line) == 1) {
224 if (count_formats(line) > 0) {
225 strcpy((*t).format_str, line + strlen((*t).f.comment));
245 count = count_formats((*t).format_str);
258 (*t).format_typ = (
int*)malloc((*t).ncols*
sizeof(
int));
259 (*t).format_siz = (
int*)malloc((*t).ncols*
sizeof(
int));
264 while (beg < strlen((*t).format_str)) {
265 if ((*t).format_str[beg] ==
'%') {
267 while (end < strlen((*t).format_str)) {
269 if ((strncmp((*t).format_str + end, (*t).column, strlen((*t).column)) == 0) ||
270 (strncmp((*t).format_str + end, (*t).f.newline, strlen((*t).f.newline)) == 0)) {
271 strncpy(ifmt, &((*t).format_str)[beg], end-beg);
272 ifmt[end-beg] =
'\0';
273 if ((*t).format_str[end-1] ==
's') {
275 (*t).format_typ[icol] = STRING;
277 strncpy(len_fmt, &((*t).format_str)[beg+1], end-beg-2);
278 sscanf(len_fmt,
"%d", &((*t).format_siz[icol]));
279 }
else if (((*t).format_str[end-1] ==
'f') ||
280 ((*t).format_str[end-1] ==
'e') ||
281 ((*t).format_str[end-1] ==
'E') ||
282 ((*t).format_str[end-1] ==
'g') ||
283 ((*t).format_str[end-1] ==
'G')) {
284 (*t).format_typ[icol] = DOUBLE;
285 (*t).format_siz[icol] =
sizeof(double);
294 }
else if (((*t).format_str[end-1] ==
'd') ||
295 ((*t).format_str[end-1] ==
'i')) {
297 if ((*t).format_str[end-2] ==
'h') {
298 if ((*t).format_str[end-3] ==
'h') {
299 (*t).format_typ[icol] = SHORTSHORT;
300 (*t).format_siz[icol] =
sizeof(char);
302 (*t).format_typ[icol] = SHORT;
303 (*t).format_siz[icol] =
sizeof(short);
305 }
else if ((*t).format_str[end-2] ==
'l') {
306 if ((*t).format_str[end-3] ==
'l') {
307 (*t).format_typ[icol] = LONGLONG;
308 (*t).format_siz[icol] =
sizeof(
long long);
310 (*t).format_typ[icol] = LONG;
311 (*t).format_siz[icol] =
sizeof(long);
314 (*t).format_typ[icol] = INT;
315 (*t).format_siz[icol] =
sizeof(int);
317 }
else if (((*t).format_str[end-1] ==
'u') ||
318 ((*t).format_str[end-1] ==
'o') ||
319 ((*t).format_str[end-1] ==
'x') ||
320 ((*t).format_str[end-1] ==
'X')) {
322 if ((*t).format_str[end-2] ==
'h') {
323 if ((*t).format_str[end-3] ==
'h') {
324 (*t).format_typ[icol] = USHORTSHORT;
325 (*t).format_siz[icol] =
sizeof(
unsigned char);
327 (*t).format_typ[icol] = USHORT;
328 (*t).format_siz[icol] =
sizeof(
unsigned short);
330 }
else if ((*t).format_str[end-2] ==
'l') {
331 if ((*t).format_str[end-3] ==
'l') {
332 (*t).format_typ[icol] = ULONGLONG;
333 (*t).format_siz[icol] =
sizeof(
unsigned long long);
335 (*t).format_typ[icol] = ULONG;
336 (*t).format_siz[icol] =
sizeof(
unsigned long);
339 (*t).format_typ[icol] = UINT;
340 (*t).format_siz[icol] =
sizeof(
unsigned int);
343 printf(
"Could not parse format string: %s\n", ifmt);
348 (*t).row_siz += (*t).format_siz[icol];
371 int at_vbytes_to_array(
const asciiTable_t t,
const char *data,
372 const int data_siz, va_list ap) {
375 if ((data_siz % t.
row_siz) != 0) {
376 printf(
"Data size (%d) not an even number of rows (row size is %d)\n",
381 int nrows = data_siz / t.
row_siz;
382 int cur_pos = 0, col_siz;
383 for (
int i = 0; i < t.
ncols; i++) {
386 temp = va_arg(ap,
char**);
387 *temp = (
char*)malloc(col_siz);
394 memcpy(*temp, data+cur_pos, col_siz);
412 int at_varray_to_bytes(
const asciiTable_t t,
char **data,
int nrows, va_list ap) {
414 *data = (
char*)realloc(*data, nrows*t.
row_siz);
416 int cur_pos = 0, col_siz;
418 for (
int i = 0; i < t.
ncols; i++) {
420 temp = va_arg(ap,
char*);
421 memcpy(*data+cur_pos, temp, col_siz);
438 int at_bytes_to_array(
const asciiTable_t t,
char *data,
int data_siz, ...) {
440 va_start(ap, data_siz);
441 int ret = at_vbytes_to_array(t, data, data_siz, ap);
455 int at_array_to_bytes(
const asciiTable_t t,
char **data,
int nrows, ...) {
458 int ret = at_varray_to_bytes(t, data, nrows, ap);
470 free((*t).format_typ);
472 free((*t).format_siz);
473 (*t).format_typ = NULL;
474 (*t).format_siz = NULL;
494 asciiTable_t asciiTable(
const char *filepath,
const char *io_mode,
496 const char *
column,
const char *newline) {
504 t.
f = asciiFile(filepath, io_mode, comment, newline);
511 if (format_str == NULL) {
512 if (strcmp(io_mode,
"r") == 0) {
513 t.
status = at_discover_format_str(&t);
522 t.
status = at_set_ncols(&t);
524 t.
status = at_set_format_typ(&t);
int row_siz
Size of an entire row in bytes.
Definition: AsciiTable.h:94
int * format_typ
Array of ncols integers specifying column types.
Definition: AsciiTable.h:92
char comment[64]
Character(s) indicating a comment.
Definition: AsciiFile.h:16
char format_str[LINE_SIZE_MAX]
Format string for rows.
Definition: AsciiTable.h:89
asciiFile_t f
ASCII file structure.
Definition: AsciiTable.h:88
int * format_siz
Array of ncols sizes for elements in each column.
Definition: AsciiTable.h:93
char column[64]
Character(s) used to seperate columns.
Definition: AsciiTable.h:90
int ncols
Number of columns in the table.
Definition: AsciiTable.h:91
Structure containing information about an ASCII text file.
Definition: AsciiFile.h:13
FILE * fd
File identifier for ASCII file when open.
Definition: AsciiFile.h:18
Structure containing information about an ASCII table.
Definition: AsciiTable.h:87
int status
Negative if format_str has not been set yet.
Definition: AsciiTable.h:95