Debian Codegraph @

src/parse_table.c

raw
1 /**
2 * This file is part of the Detox package.
3 *
4 * Copyright (c) Doug Harple <[email protected]>
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 */
9
10 #include <sys/stat.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <locale.h>
16
17 #include "table.h"
18 #include "wrapped.h"
19
20 #define LINE_LENGTH 6
21
22 enum {
23 BASE_STATE,
24 INSIDE_STATE
25 };
26
27 table_t *parse_table(char *filename)
28 {
29 FILE *table_file;
30 char *work;
31 int code;
32 int offset;
33 char *parsed;
34 int err;
35 int size;
36 int ret;
37 int state;
38 char *system_ctype;
39
40 table_t *table;
41
42 struct stat table_stat;
43
44 err = stat(filename, &table_stat);
45 if (err == -1) {
46 return NULL;
47 }
48
49 system_ctype = setlocale(LC_CTYPE, "");
50 if (system_ctype == NULL) {
51 system_ctype = ""; // I don't think we can free the return from setlocale()
52 }
53
54 size = 0;
55
56 if (table_stat.st_size > 0) {
57 size = table_stat.st_size / LINE_LENGTH;
58 }
59 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
60 else {
61 size = (512 * table_stat.st_blocks) / LINE_LENGTH;
62 }
63 #endif
64
65 if (size < 500) {
66 size = 500;
67 }
68
69 table = table_init(size);
70 if (table == NULL) {
71 return NULL;
72 }
73
74 table_file = fopen(filename, "r");
75 if (table_file == NULL) {
76 fprintf(stderr, "Unable to open translation table: %s\n", strerror(errno));
77 return NULL;
78 }
79
80 work = wrapped_malloc(1024);
81
82 parsed = wrapped_malloc(1024);
83
84 state = BASE_STATE;
85
86 while (fgets(work, 1024, table_file) != NULL) {
87 if (*work == '#') {
88
89 /*
90 * Don't even bother
91 */
92 continue;
93 }
94
95 parsed[0] = '\0';
96
97 if (state == BASE_STATE) {
98 ret = sscanf(work, " %s %n", parsed, &offset);
99
100 if (ret == 0) {
101 continue;
102 }
103
104 if (strncasecmp(parsed, "start", 5) == 0) {
105 if (work[offset] == '\0') {
106 // All languages
107 state = INSIDE_STATE;
108 continue;
109 }
110
111 // check to see if a language specifier has been set
112
113 if (work[offset] == '"') {
114 sscanf(work + offset + 1, "%[^\"]", parsed);
115 } else if (work[offset] == '\'') {
116 sscanf(work + offset + 1, "%[^']", parsed);
117 } else {
118 sscanf(work + offset, "%s", parsed);
119 }
120
121 if (strncasecmp(parsed, system_ctype, strlen(parsed)) == 0) {
122 state = INSIDE_STATE;
123 }
124 // else ignore this start/end block
125
126 continue;
127 }
128
129 if (strncasecmp(parsed, "default", 7) == 0) {
130
131 if (work[offset] == '\0') {
132 table->default_translation = NULL;
133 continue;
134 }
135
136 if (work[offset] == '"') {
137 sscanf(work + offset + 1, "%[^\"]", parsed);
138 } else if (work[offset] == '\'') {
139 sscanf(work + offset + 1, "%[^']", parsed);
140 } else {
141 sscanf(work + offset, "%s", parsed);
142 }
143
144 table->default_translation = wrapped_strdup(parsed);
145
146 continue;
147 }
148
149 continue;
150 }
151
152 /*
153 * Inside state
154 */
155
156 code = -1;
157
158 ret = sscanf(work, "%i %n", &code, &offset);
159
160 if (ret == 0 || code < 0 || offset < 0) {
161
162 /*
163 * Check for end
164 */
165 ret = sscanf(work, " %s %n", parsed, &offset);
166
167 if (ret > 0 && strncasecmp(parsed, "end", 5) == 0) {
168 state = BASE_STATE;
169 }
170
171 continue;
172 }
173
174 if (work[offset] == '\0') {
175 continue;
176 }
177
178 if (work[offset] == '"') {
179 sscanf(work + offset + 1, "%[^\"]", parsed);
180 } else if (work[offset] == '\'') {
181 sscanf(work + offset + 1, "%[^']", parsed);
182 } else {
183 sscanf(work + offset, "%s", parsed);
184 }
185
186 ret = table_put(table, code, parsed);
187 if (ret == -1) {
188 fprintf(stderr, "Failed to add row 0x%04x \"%s\" to translation table\n", code, parsed);
189 table_free(table);
190 fclose(table_file);
191 free(work);
192 free(parsed);
193 return NULL;
194 }
195 }
196
197 free(work);
198 free(parsed);
199 fclose(table_file);
200
201 return table;
202 }