comparison rvinterf/ctracedec/readtab.c @ 858:4c6e7ada647b

compressed trace decoder almost fully implemented
author Space Falcon <falcon@ivan.Harhan.ORG>
date Sat, 02 May 2015 08:08:26 +0000
parents
children
comparison
equal deleted inserted replaced
857:2768b4339275 858:4c6e7ada647b
1 /*
2 * This module handles the reading and parsing of str2ind.tab
3 */
4
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <stdlib.h>
10
11 extern char *str2ind_tab_filename;
12
13 int str2ind_array_size;
14 char **str2ind_orig_strings;
15 char **str2ind_param_strings;
16
17 static FILE *readF;
18 static char linebuf[256];
19 static int lineno;
20
21 static void
22 read_line()
23 {
24 char *cp;
25
26 if (!fgets(linebuf, sizeof linebuf, readF)) {
27 fprintf(stderr, "error: premature EOF reading from %s\n",
28 str2ind_tab_filename);
29 exit(1);
30 }
31 lineno++;
32 cp = index(linebuf, '\n');
33 if (!cp) {
34 fprintf(stderr,
35 "error: %s line %d is too long or unterminated\n",
36 str2ind_tab_filename, lineno);
37 exit(1);
38 }
39 *cp = '\0';
40 if (cp > linebuf && cp[-1] == '\r')
41 cp[-1] = '\0';
42 }
43
44 static void
45 line_pure_num()
46 {
47 char *cp;
48
49 for (cp = linebuf; isspace(*cp); cp++)
50 ;
51 if (!isdigit(*cp)) {
52 inv: fprintf(stderr, "%s line %d: pure number expected\n",
53 str2ind_tab_filename, lineno);
54 exit(1);
55 }
56 while (isdigit(*cp))
57 cp++;
58 if (*cp)
59 goto inv;
60 }
61
62 static char *
63 copystr(str)
64 char *str;
65 {
66 static char null = '\0';
67 char *buf;
68
69 if (str[0]) {
70 buf = malloc(strlen(str) + 1);
71 if (!buf) {
72 perror("malloc");
73 exit(1);
74 }
75 strcpy(buf, str);
76 return(buf);
77 } else
78 return(&null);
79 }
80
81 static void
82 process_record_line(idx)
83 {
84 char *cp, *cp2;
85 int i;
86
87 for (cp = linebuf; isspace(*cp); cp++)
88 ;
89 if (!isdigit(*cp)) {
90 syntaxerr: fprintf(stderr, "%s line %d: unexpected syntax\n",
91 str2ind_tab_filename, lineno);
92 exit(1);
93 }
94 while (isdigit(*cp))
95 cp++;
96 if (*cp++ != ',')
97 goto syntaxerr;
98 i = atoi(linebuf);
99 if (i != idx) {
100 fprintf(stderr, "%s line %d lists wrong index (expected %d)\n",
101 str2ind_tab_filename, lineno, idx);
102 exit(1);
103 }
104 cp2 = index(cp, ',');
105 if (!cp2)
106 goto syntaxerr;
107 *cp2++ = '\0';
108 str2ind_param_strings[idx] = copystr(cp);
109 str2ind_orig_strings[idx] = copystr(cp2);
110 }
111
112 read_str2ind_tab()
113 {
114 int idx;
115
116 readF = fopen(str2ind_tab_filename, "r");
117 if (!readF) {
118 perror(str2ind_tab_filename);
119 exit(1);
120 }
121 /* skip the timestamp line: the user is responsible for matching */
122 read_line();
123 line_pure_num();
124 /* read the line with the array size */
125 read_line();
126 line_pure_num();
127 str2ind_array_size = atoi(linebuf);
128 if (str2ind_array_size < 1) {
129 fprintf(stderr, "error: %s gives index array size < 1\n",
130 str2ind_tab_filename);
131 exit(1);
132 }
133 str2ind_orig_strings = malloc(sizeof(char *) * str2ind_array_size);
134 str2ind_param_strings = malloc(sizeof(char *) * str2ind_array_size);
135 if (!str2ind_orig_strings || !str2ind_param_strings) {
136 perror("malloc");
137 exit(1);
138 }
139 for (idx = 0; idx < str2ind_array_size; idx++) {
140 read_line();
141 process_record_line(idx);
142 }
143 fclose(readF);
144 return(0);
145 }