Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include "internal.h" |
| 5 | #include "displayLanguage.h" |
| 6 | |
| 7 | static const char * |
| 8 | displayLanguage(const char *lang) { |
| 9 | return DisplayLanguage((char *)lang); |
| 10 | } |
| 11 | |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 12 | static char * |
| 13 | generateDisplayName(const char *table) { |
| 14 | char *name; |
| 15 | char *language; |
| 16 | char *locale; |
| 17 | char *type; |
| 18 | char *dots; |
| 19 | char *contraction; |
| 20 | char *grade; |
| 21 | char *version; |
| 22 | char *query; |
| 23 | char **matches; |
| 24 | char *n; |
| 25 | char *q; |
| 26 | char **m; |
| 27 | name = (char *)malloc(100 * sizeof(*name)); |
| 28 | n = name; |
| 29 | query = (char *)malloc(100 * sizeof(*query)); |
| 30 | q = query; |
| 31 | locale = lou_getTableInfo(table, "locale"); |
| 32 | if (!locale) |
| 33 | return NULL; |
| 34 | language = displayLanguage(locale); |
| 35 | n += sprintf(n, "%s", language); |
| 36 | q += sprintf(q, "locale:%s", locale); |
| 37 | free(locale); |
| 38 | type = lou_getTableInfo(table, "type"); |
| 39 | if (type) { |
| 40 | q += sprintf(q, " type:%s", type); |
| 41 | if (!strcmp(type, "computer")) { |
| 42 | dots = lou_getTableInfo(table, "dots"); |
| 43 | if (dots) { |
| 44 | if (!strcmp(dots, "6")) { |
| 45 | n += sprintf(n, " %s-dot", dots); |
| 46 | } else { |
| 47 | char *q_save = q; |
| 48 | q += sprintf(q, " dots:6"); |
| 49 | matches = lou_findTables(query); |
| 50 | if (matches) { |
| 51 | n += sprintf(n, " %s-dot", dots); |
| 52 | for (m = matches; *m; m++) free(*m); |
| 53 | free(matches); |
| 54 | } |
| 55 | q = q_save; |
| 56 | } |
| 57 | q += sprintf(q, " dots:%s", dots); |
| 58 | free(dots); |
| 59 | } |
| 60 | n += sprintf(n, " %s", type); |
| 61 | } else if (!strcmp(type, "literary")) { |
| 62 | int uncontracted = 0; |
| 63 | int fullyContracted = 0; |
| 64 | int partiallyContracted = 0; |
| 65 | int otherUncontracted = 0; |
| 66 | int otherFullyContracted = 0; |
| 67 | int otherPartiallyContracted = 0; |
| 68 | int twoOrMorePartiallyContracted = 0; |
| 69 | contraction = lou_getTableInfo(table, "contraction"); |
| 70 | if (contraction) { |
| 71 | char *q_save = q; |
| 72 | uncontracted = !strcmp(contraction, "no"); |
| 73 | fullyContracted = !strcmp(contraction, "full"); |
| 74 | partiallyContracted = !strcmp(contraction, "partial"); |
| 75 | otherUncontracted = 0; |
| 76 | q += sprintf(q, " contraction:no"); |
| 77 | matches = lou_findTables(query); |
| 78 | if (matches) { |
| 79 | if (!uncontracted || matches[0] && matches[1]) |
| 80 | otherUncontracted = 1; |
| 81 | for (m = matches; *m; m++) free(*m); |
| 82 | free(matches); |
| 83 | } |
| 84 | q = q_save; |
| 85 | otherPartiallyContracted = 0; |
| 86 | twoOrMorePartiallyContracted = 0; |
| 87 | grade = NULL; |
| 88 | q += sprintf(q, " contraction:partial"); |
| 89 | matches = lou_findTables(query); |
| 90 | if (matches) { |
| 91 | for (m = matches; *m; m++) { |
| 92 | if (!twoOrMorePartiallyContracted) { |
| 93 | char *g = lou_getTableInfo(*m, "grade"); |
| 94 | if (g) { |
| 95 | if (!grade) |
| 96 | grade = g; |
| 97 | else if (strcmp(grade, g)) |
| 98 | twoOrMorePartiallyContracted = 1; |
| 99 | } |
| 100 | } |
| 101 | free(*m); |
| 102 | } |
| 103 | free(matches); |
| 104 | if (!partiallyContracted || twoOrMorePartiallyContracted) |
| 105 | otherPartiallyContracted = 1; |
| 106 | if (twoOrMorePartiallyContracted) |
| 107 | grade = lou_getTableInfo(table, "grade"); |
| 108 | else |
| 109 | grade = NULL; |
| 110 | } |
| 111 | q = q_save; |
| 112 | otherFullyContracted = 0; |
| 113 | q += sprintf(q, " contraction:full"); |
| 114 | matches = lou_findTables(query); |
| 115 | if (matches) { |
| 116 | if (!fullyContracted || matches[0] && matches[1]) |
| 117 | otherFullyContracted = 1; |
| 118 | for (m = matches; *m; m++) free(*m); |
| 119 | free(matches); |
| 120 | } |
| 121 | q = q_save; |
| 122 | q += sprintf(q, " contraction:%s", contraction); |
| 123 | free(contraction); |
| 124 | } |
| 125 | dots = lou_getTableInfo(table, "dots"); |
| 126 | if (dots) { |
| 127 | int otherDots = 0; |
| 128 | matches = lou_findTables(query); |
| 129 | if (matches) { |
| 130 | for (m = matches; *m; m++) { |
| 131 | if (!otherDots) { |
| 132 | char *d = lou_getTableInfo(*m, "dots"); |
| 133 | if (d && strcmp(dots, d)) |
| 134 | otherDots = 1; |
| 135 | } |
| 136 | free(*m); |
| 137 | } |
Bert Frees | 4c3cedc | 2020-04-30 23:03:12 +0200 | [diff] [blame^] | 138 | free(matches); |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 139 | } |
| 140 | if (otherDots) |
| 141 | n += sprintf(n, " %s-dot", dots); |
| 142 | free(dots); |
| 143 | } |
| 144 | if (uncontracted) { |
| 145 | if (otherFullyContracted || otherPartiallyContracted) |
| 146 | n += sprintf(n, " uncontracted"); |
| 147 | } else if (fullyContracted) { |
| 148 | if (otherPartiallyContracted) { |
| 149 | if (twoOrMorePartiallyContracted && grade) |
| 150 | n += sprintf(n, " grade %s contracted", grade); |
| 151 | else |
Bert Frees | 5edfb61 | 2019-08-25 18:31:17 +0200 | [diff] [blame] | 152 | n += sprintf(n, " contracted"); |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 153 | } else if (otherUncontracted) { |
| 154 | n += sprintf(n, " contracted"); |
| 155 | } |
| 156 | } else if (partiallyContracted) { |
| 157 | if (twoOrMorePartiallyContracted && grade) |
| 158 | n += sprintf(n, " grade %s contracted", grade); |
| 159 | else |
| 160 | n += sprintf(n, " partially contracted"); |
| 161 | } |
| 162 | free(grade); |
| 163 | } |
| 164 | free(type); |
| 165 | } |
| 166 | n += sprintf(n, " braille"); |
| 167 | version = lou_getTableInfo(table, "version"); |
| 168 | if (version) { |
| 169 | n += sprintf(n, " (%s standard)", version); |
| 170 | free(version); |
| 171 | } |
| 172 | return name; |
| 173 | } |
| 174 | |
| 175 | int main(int argc, char **argv) { |
Bert Frees | 4c3cedc | 2020-04-30 23:03:12 +0200 | [diff] [blame^] | 176 | int COLUMN_INDEX_NAME = -1; |
| 177 | int COLUMN_DISPLAY_NAME = -1; |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 178 | int result = 0; |
| 179 | FILE *fp; |
| 180 | char *line = NULL; |
| 181 | size_t len = 0; |
| 182 | if (argc != 2) { |
| 183 | fprintf(stderr, "One argument expected\n"); |
| 184 | exit(EXIT_FAILURE); |
| 185 | } |
| 186 | fp = fopen(argv[1], "rb"); |
| 187 | if (!fp) { |
| 188 | fprintf(stderr, "Could not open file: %s\n", argv[1]); |
| 189 | exit(EXIT_FAILURE); |
| 190 | } |
Christian Egli | 547815d | 2019-03-04 09:46:51 +0100 | [diff] [blame] | 191 | lou_setLogLevel(LOU_LOG_WARN); |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 192 | while (getline(&line, &len, fp) != -1) { |
| 193 | char *cp = line; |
| 194 | int generate = 0; |
| 195 | if (*cp == '*') { |
| 196 | generate = 1; |
| 197 | cp++; |
| 198 | } |
Bert Frees | 4c3cedc | 2020-04-30 23:03:12 +0200 | [diff] [blame^] | 199 | while (*cp == ' ') |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 200 | cp++; |
Bert Frees | 4c3cedc | 2020-04-30 23:03:12 +0200 | [diff] [blame^] | 201 | if (*cp == '\0' || *cp == '\n' || *cp == '#') { |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 202 | if (!generate) |
| 203 | continue; |
| 204 | else |
| 205 | goto parse_error; |
Bert Frees | 4c3cedc | 2020-04-30 23:03:12 +0200 | [diff] [blame^] | 206 | } |
| 207 | char *table = cp; |
| 208 | cp++; |
| 209 | while (*cp != ' ' && *cp != '\0' && *cp != '\n' && *cp != '#') |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 210 | cp++; |
Bert Frees | 4c3cedc | 2020-04-30 23:03:12 +0200 | [diff] [blame^] | 211 | if (*cp != ' ') |
| 212 | goto parse_error; |
| 213 | *cp = '\0'; |
| 214 | cp++; |
| 215 | while (*cp == ' ') |
| 216 | cp++; |
| 217 | if (*cp == '\0' || *cp == '\n' || *cp == '#') |
| 218 | goto parse_error; |
| 219 | char *expectedIndexName = cp; |
| 220 | if (COLUMN_INDEX_NAME < 0) |
| 221 | COLUMN_INDEX_NAME = expectedIndexName - line; |
| 222 | else if (expectedIndexName != line + COLUMN_INDEX_NAME) |
| 223 | goto parse_error; |
| 224 | while (*cp != '\0' && *cp != '\n' && *cp != '#') { |
| 225 | if (*cp == ' ' && cp[1] == ' ') { |
| 226 | *cp = '\0'; |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 227 | cp++; |
Bert Frees | 4c3cedc | 2020-04-30 23:03:12 +0200 | [diff] [blame^] | 228 | break; |
| 229 | } |
| 230 | cp++; |
| 231 | } |
| 232 | if (*cp != ' ') |
| 233 | goto parse_error; |
| 234 | while (*cp == ' ') |
| 235 | cp++; |
| 236 | if (*cp == '\0' || *cp == '\n' || *cp == '#') |
| 237 | goto parse_error; |
| 238 | char *expectedDisplayName = cp; |
| 239 | if (COLUMN_DISPLAY_NAME < 0) |
| 240 | COLUMN_DISPLAY_NAME = expectedDisplayName - line; |
| 241 | else if (expectedDisplayName != line + COLUMN_DISPLAY_NAME) |
| 242 | goto parse_error; |
| 243 | while (*cp != '\0' && *cp != '\n' && *cp != '#') { |
| 244 | if (*cp == ' ' && cp[1] == ' ') |
| 245 | break; |
| 246 | cp++; |
| 247 | } |
| 248 | *cp = '\0'; |
| 249 | const char *actualIndexName = lou_getTableInfo(table, "index-name"); |
| 250 | if (!actualIndexName) { |
| 251 | fprintf(stderr, "No index-name field in table %s\n", table); |
| 252 | result = 1; |
| 253 | } else { |
| 254 | if (strcmp(actualIndexName, expectedIndexName) != 0) { |
| 255 | fprintf(stderr, "%s: %s != %s\n", table, actualIndexName, expectedIndexName); |
| 256 | fprintf(stderr, " cat %s | sed 's/^\\(#-index-name: *\\).*$/\\1%s/g' > %s.tmp\n", table, expectedIndexName, table); |
| 257 | fprintf(stderr, " mv %s.tmp %s\n", table, table); |
| 258 | result = 1; |
| 259 | } |
| 260 | } |
| 261 | const char *actualDisplayName = lou_getTableInfo(table, "display-name"); |
| 262 | if (!actualDisplayName) { |
| 263 | fprintf(stderr, "No display-name field in table %s\n", table); |
| 264 | result = 1; |
| 265 | } else { |
| 266 | if (strcmp(actualDisplayName, expectedDisplayName) != 0) { |
| 267 | fprintf(stderr, "%s: %s != %s\n", table, actualDisplayName, expectedDisplayName); |
| 268 | fprintf(stderr, " cat %s | sed 's/^\\(#-display-name: *\\).*$/\\1%s/g' > %s.tmp\n", table, expectedDisplayName, table); |
| 269 | fprintf(stderr, " mv %s.tmp %s\n", table, table); |
| 270 | result = 1; |
| 271 | } |
| 272 | const char *generatedDisplayName = generateDisplayName(table); |
| 273 | if (!generatedDisplayName || !*generatedDisplayName) { |
| 274 | if (generate) { |
| 275 | fprintf(stderr, "No display-name could be generated for table %s\n", table); |
| 276 | result = 1; |
| 277 | } |
| 278 | } else if (strcmp(actualDisplayName, generatedDisplayName) != 0) { |
| 279 | if (generate) { |
| 280 | fprintf(stderr, "%s: %s != %s\n", table, actualDisplayName, generatedDisplayName); |
| 281 | result = 1; |
| 282 | } |
| 283 | } else { |
| 284 | if (!generate) { |
| 285 | fprintf(stderr, "%s: %s == %s\n", table, actualDisplayName, generatedDisplayName); |
| 286 | result = 1; |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
Bert Frees | 4c3cedc | 2020-04-30 23:03:12 +0200 | [diff] [blame^] | 290 | continue; |
Bert Frees | 84f9dbf | 2017-11-12 22:38:01 +0100 | [diff] [blame] | 291 | parse_error: |
| 292 | fprintf(stderr, "Could not parse line: %s\n", line); |
| 293 | exit(EXIT_FAILURE); |
| 294 | } |
| 295 | free(line); |
| 296 | return result; |
| 297 | } |