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