FreeCalypso > hg > freecalypso-tools
view rvinterf/ctracedec/readtab.c @ 295:79434b9de159
c1xx-calextr will need mkdir and librftab
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 18 Nov 2017 17:15:54 +0000 |
parents | e7502631a0f9 |
children |
line wrap: on
line source
/* * This module handles the reading and parsing of str2ind.tab */ #include <stdio.h> #include <ctype.h> #include <string.h> #include <strings.h> #include <stdlib.h> extern char *str2ind_tab_filename; int str2ind_array_size; char **str2ind_orig_strings; char **str2ind_param_strings; static FILE *readF; static char linebuf[256]; static int lineno; static void read_line() { char *cp; if (!fgets(linebuf, sizeof linebuf, readF)) { fprintf(stderr, "error: premature EOF reading from %s\n", str2ind_tab_filename); exit(1); } lineno++; cp = index(linebuf, '\n'); if (!cp) { fprintf(stderr, "error: %s line %d is too long or unterminated\n", str2ind_tab_filename, lineno); exit(1); } *cp = '\0'; if (cp > linebuf && cp[-1] == '\r') cp[-1] = '\0'; } static void line_pure_num() { char *cp; for (cp = linebuf; isspace(*cp); cp++) ; if (!isdigit(*cp)) { inv: fprintf(stderr, "%s line %d: pure number expected\n", str2ind_tab_filename, lineno); exit(1); } while (isdigit(*cp)) cp++; if (*cp) goto inv; } static char * copystr(str) char *str; { static char null = '\0'; char *buf; if (str[0]) { buf = malloc(strlen(str) + 1); if (!buf) { perror("malloc"); exit(1); } strcpy(buf, str); return(buf); } else return(&null); } static void process_record_line(idx) { char *cp, *cp2; int i; for (cp = linebuf; isspace(*cp); cp++) ; if (!isdigit(*cp)) { syntaxerr: fprintf(stderr, "%s line %d: unexpected syntax\n", str2ind_tab_filename, lineno); exit(1); } while (isdigit(*cp)) cp++; if (*cp++ != ',') goto syntaxerr; i = atoi(linebuf); if (i != idx) { fprintf(stderr, "%s line %d lists wrong index (expected %d)\n", str2ind_tab_filename, lineno, idx); exit(1); } cp2 = index(cp, ','); if (!cp2) goto syntaxerr; *cp2++ = '\0'; str2ind_param_strings[idx] = copystr(cp); str2ind_orig_strings[idx] = copystr(cp2); } read_str2ind_tab() { int idx; readF = fopen(str2ind_tab_filename, "r"); if (!readF) { perror(str2ind_tab_filename); exit(1); } /* skip the timestamp line: the user is responsible for matching */ read_line(); line_pure_num(); /* read the line with the array size */ read_line(); line_pure_num(); str2ind_array_size = atoi(linebuf); if (str2ind_array_size < 1) { fprintf(stderr, "error: %s gives index array size < 1\n", str2ind_tab_filename); exit(1); } str2ind_orig_strings = malloc(sizeof(char *) * str2ind_array_size); str2ind_param_strings = malloc(sizeof(char *) * str2ind_array_size); if (!str2ind_orig_strings || !str2ind_param_strings) { perror("malloc"); exit(1); } for (idx = 0; idx < str2ind_array_size; idx++) { read_line(); process_record_line(idx); } fclose(readF); return(0); }