| 1 |
/* |
| 2 |
* Gregorio is a program that translates gabc files to GregorioTeX |
| 3 |
* This file implements vowel rule handling (aside from parsing). |
| 4 |
* |
| 5 |
* Copyright (C) 2015-2025 The Gregorio Project (see CONTRIBUTORS.md) |
| 6 |
* |
| 7 |
* This file is part of Gregorio. |
| 8 |
* |
| 9 |
* This program is free software: you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU General Public License as published by the Free |
| 11 |
* Software Foundation, either version 3 of the License, or (at your option) |
| 12 |
* any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 17 |
* more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License along with |
| 20 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <assert.h> |
| 28 |
#include <errno.h> |
| 29 |
#include "bool.h" |
| 30 |
#include "vowel.h" |
| 31 |
#include "unicode.h" |
| 32 |
#include "messages.h" |
| 33 |
#include "support.h" |
| 34 |
|
| 35 |
typedef struct character_set { |
| 36 |
grewchar *table; |
| 37 |
struct character_set **next; |
| 38 |
grewchar mask; |
| 39 |
unsigned int bins; |
| 40 |
unsigned int size; |
| 41 |
bool is_final; |
| 42 |
} character_set; |
| 43 |
|
| 44 |
static character_set *character_set_new(const bool alloc_next) |
| 45 |
{ |
| 46 |
character_set *set = |
| 47 |
(character_set *)gregorio_calloc(1, sizeof(character_set)); |
| 48 |
set->mask = 0x0f; |
| 49 |
set->bins = 16; |
| 50 |
set->size = 0; |
| 51 |
set->table = (grewchar *)gregorio_calloc(set->bins, sizeof(grewchar)); |
| 52 |
if (alloc_next) { |
| 53 |
set->next = (character_set **)gregorio_calloc(set->bins, |
| 54 |
sizeof(character_set *)); |
| 55 |
} |
| 56 |
return set; |
| 57 |
} |
| 58 |
|
| 59 |
static void character_set_free(character_set *const set); |
| 60 |
|
| 61 |
static __inline void character_set_next_elements_free(character_set *const set) |
| 62 |
{ |
| 63 |
unsigned int i; |
| 64 |
assert(set && set->next); |
| 65 |
for (i = 0; i < set->bins; ++i) { |
| 66 |
if (set->next[i]) { |
| 67 |
character_set_free(set->next[i]); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static void character_set_free(character_set *const set) |
| 73 |
{ |
| 74 |
if (set) { |
| 75 |
if (set->next) { |
| 76 |
character_set_next_elements_free(set); |
| 77 |
free(set->next); |
| 78 |
} |
| 79 |
free(set->table); |
| 80 |
free(set); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
static bool character_set_contains(character_set *const set, |
| 85 |
const grewchar character, character_set **const next) |
| 86 |
{ |
| 87 |
unsigned int index; |
| 88 |
assert(set); |
| 89 |
|
| 90 |
if (next) { |
| 91 |
*next = NULL; |
| 92 |
} |
| 93 |
for (index = ((unsigned long)character) & set->mask; set->table[index]; |
| 94 |
index = (index + 1) & set->mask) { |
| 95 |
if (set->table[index] == character) { |
| 96 |
if (next && set->next) { |
| 97 |
*next = set->next[index]; |
| 98 |
} |
| 99 |
return true; |
| 100 |
} |
| 101 |
} |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
static __inline void character_set_put(character_set *const set, |
| 106 |
const grewchar character, character_set *next) |
| 107 |
{ |
| 108 |
unsigned int index; |
| 109 |
|
| 110 |
assert(set); |
| 111 |
|
| 112 |
index = ((unsigned long)character) & set->mask; |
| 113 |
while (set->table[index]) { |
| 114 |
index = (index + 1) & set->mask; |
| 115 |
} |
| 116 |
|
| 117 |
set->table[index] = character; |
| 118 |
if (set->next) { |
| 119 |
set->next[index] = next; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
static __inline void character_set_grow(character_set *const set) { |
| 124 |
static grewchar *old_table; |
| 125 |
static character_set **old_next; |
| 126 |
unsigned int old_bins, i; |
| 127 |
|
| 128 |
assert(set); |
| 129 |
|
| 130 |
if (set->bins >= 0x4000L) { |
| 131 |
/* this should not realistically be reached */ |
| 132 |
/* LCOV_EXCL_START */ |
| 133 |
gregorio_message(_("character set too large"), "character_set_grow", |
| 134 |
VERBOSITY_FATAL, 0); |
| 135 |
return; |
| 136 |
/* LCOV_EXCL_STOP */ |
| 137 |
} |
| 138 |
|
| 139 |
old_table = set->table; |
| 140 |
old_next = set->next; |
| 141 |
old_bins = set->bins; |
| 142 |
|
| 143 |
set->bins <<= 1; |
| 144 |
set->mask = (set->mask << 1) | 0x01; |
| 145 |
set->table = gregorio_calloc(set->bins, sizeof(grewchar)); |
| 146 |
if (old_next) { |
| 147 |
set->next = gregorio_calloc(set->bins, sizeof(character_set *)); |
| 148 |
} |
| 149 |
for (i = 0; i < old_bins; ++i) { |
| 150 |
if (old_table[i]) { |
| 151 |
character_set_put(set, old_table[i], old_next ? old_next[i] : NULL); |
| 152 |
} else { |
| 153 |
assert(!old_next || !old_next[i]); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
free(old_table); |
| 158 |
if (old_next) { |
| 159 |
free(old_next); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
static character_set *character_set_add(character_set *const set, |
| 164 |
const grewchar character) |
| 165 |
{ |
| 166 |
character_set *next = NULL; |
| 167 |
|
| 168 |
if (!character_set_contains(set, character, &next)) { |
| 169 |
if (((++(set->size) * 10) / set->bins) >= 7) { |
| 170 |
character_set_grow(set); |
| 171 |
} |
| 172 |
|
| 173 |
if (set->next) { |
| 174 |
next = character_set_new(true); |
| 175 |
} |
| 176 |
|
| 177 |
character_set_put(set, character, next); |
| 178 |
} |
| 179 |
|
| 180 |
return next; |
| 181 |
} |
| 182 |
|
| 183 |
static __inline void character_set_clear(character_set *const set) |
| 184 |
{ |
| 185 |
if (set) { |
| 186 |
memset(set->table, 0, set->bins * sizeof(grewchar)); |
| 187 |
|
| 188 |
if (set->next) { |
| 189 |
character_set_next_elements_free(set); |
| 190 |
memset(set->next, 0, set->bins * sizeof(character_set *)); |
| 191 |
} |
| 192 |
|
| 193 |
set->size = 0; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
static character_set *vowel_table = NULL; |
| 198 |
static character_set *prefix_table = NULL; |
| 199 |
static character_set *suffix_table = NULL; |
| 200 |
static character_set *secondary_table = NULL; |
| 201 |
static grewchar *prefix_buffer = NULL; |
| 202 |
static size_t prefix_buffer_size; |
| 203 |
static size_t prefix_buffer_mask; |
| 204 |
|
| 205 |
extern FILE *gregorio_vowel_rulefile_in; |
| 206 |
|
| 207 |
static void prefix_buffer_grow(size_t required_size) |
| 208 |
{ |
| 209 |
while (required_size > prefix_buffer_size) { |
| 210 |
prefix_buffer_size <<= 1; |
| 211 |
prefix_buffer_mask = (prefix_buffer_mask << 1) | 0x01; |
| 212 |
} |
| 213 |
prefix_buffer = gregorio_realloc(prefix_buffer, |
| 214 |
prefix_buffer_size * sizeof(grewchar)); |
| 215 |
} |
| 216 |
|
| 217 |
void gregorio_vowel_tables_init(void) |
| 218 |
{ |
| 219 |
if (vowel_table) { |
| 220 |
character_set_clear(vowel_table); |
| 221 |
character_set_clear(prefix_table); |
| 222 |
character_set_clear(suffix_table); |
| 223 |
character_set_clear(secondary_table); |
| 224 |
} else { |
| 225 |
vowel_table = character_set_new(false); |
| 226 |
prefix_table = character_set_new(true); |
| 227 |
suffix_table = character_set_new(true); |
| 228 |
secondary_table = character_set_new(true); |
| 229 |
|
| 230 |
prefix_buffer_size = 16; |
| 231 |
prefix_buffer_mask = 0x0f; |
| 232 |
prefix_buffer = (grewchar *)gregorio_malloc( |
| 233 |
prefix_buffer_size * sizeof(grewchar)); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void gregorio_vowel_tables_load(const char *const filename, |
| 238 |
char **const language, rulefile_parse_status *status) |
| 239 |
{ |
| 240 |
gregorio_check_file_access(read, filename, WARNING, return); |
| 241 |
gregorio_vowel_rulefile_in = fopen(filename, "r"); |
| 242 |
if (gregorio_vowel_rulefile_in) { |
| 243 |
gregorio_vowel_rulefile_parse(filename, language, status); |
| 244 |
fclose(gregorio_vowel_rulefile_in); |
| 245 |
gregorio_vowel_rulefile_in = NULL; |
| 246 |
} else { |
| 247 |
/* tests cannot be expected to simulate the system error that would |
| 248 |
* cause gregorio_vowel_rulefile_in to be NULL */ |
| 249 |
/* LCOV_EXCL_START */ |
| 250 |
gregorio_messagef("gregorio_vowel_tables_load", VERBOSITY_WARNING, 0, |
| 251 |
_("unable to open %s: %s"), filename, strerror(errno)); |
| 252 |
/* LCOV_EXCL_STOP */ |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
void gregorio_vowel_tables_free(void) |
| 257 |
{ |
| 258 |
if (vowel_table) { |
| 259 |
character_set_free(vowel_table); |
| 260 |
} |
| 261 |
if (prefix_table) { |
| 262 |
character_set_free(prefix_table); |
| 263 |
} |
| 264 |
if (suffix_table) { |
| 265 |
character_set_free(suffix_table); |
| 266 |
} |
| 267 |
if (secondary_table) { |
| 268 |
character_set_free(secondary_table); |
| 269 |
} |
| 270 |
if (prefix_buffer) { |
| 271 |
free(prefix_buffer); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
void gregorio_vowel_table_add(const char *vowels) |
| 276 |
{ |
| 277 |
if (vowels) { |
| 278 |
grewchar *str = gregorio_build_grewchar_string_from_buf(vowels), *p; |
| 279 |
for (p = str; *p; ++p) { |
| 280 |
character_set_add(vowel_table, *p); |
| 281 |
} |
| 282 |
free(str); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void gregorio_prefix_table_add(const char *prefix) |
| 287 |
{ |
| 288 |
character_set *set = prefix_table; |
| 289 |
grewchar *str, *p; |
| 290 |
|
| 291 |
/* store prefixes backwards */ |
| 292 |
if (prefix && *prefix) { |
| 293 |
str = gregorio_build_grewchar_string_from_buf(prefix); |
| 294 |
p = str; |
| 295 |
|
| 296 |
while (*(++p)) ; |
| 297 |
|
| 298 |
while (p > str) { |
| 299 |
set = character_set_add(set, *(--p)); |
| 300 |
} |
| 301 |
|
| 302 |
set->is_final = true; |
| 303 |
prefix_buffer_grow(gregorio_wcstrlen(str)); |
| 304 |
free(str); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
void gregorio_suffix_table_add(const char *suffix) |
| 309 |
{ |
| 310 |
character_set *set = suffix_table; |
| 311 |
grewchar *str, *p; |
| 312 |
|
| 313 |
if (suffix && *suffix) { |
| 314 |
str = gregorio_build_grewchar_string_from_buf(suffix); |
| 315 |
p = str; |
| 316 |
|
| 317 |
while (*p) { |
| 318 |
set = character_set_add(set, *(p++)); |
| 319 |
} |
| 320 |
|
| 321 |
set->is_final = true; |
| 322 |
free(str); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
void gregorio_secondary_table_add(const char *secondary) |
| 327 |
{ |
| 328 |
character_set *set = secondary_table; |
| 329 |
grewchar *str, *p; |
| 330 |
|
| 331 |
if (secondary && *secondary) { |
| 332 |
str = gregorio_build_grewchar_string_from_buf(secondary); |
| 333 |
p = str; |
| 334 |
|
| 335 |
while (*p) { |
| 336 |
set = character_set_add(set, *(p++)); |
| 337 |
} |
| 338 |
|
| 339 |
set->is_final = true; |
| 340 |
free(str); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
typedef enum { |
| 345 |
VWL_BEFORE = 0, |
| 346 |
VWL_WITHIN, |
| 347 |
VWL_SUFFIX |
| 348 |
} vowel_group_state; |
| 349 |
|
| 350 |
static __inline bool is_in_prefix(size_t bufpos) { |
| 351 |
character_set *previous = prefix_table, *prefix; |
| 352 |
|
| 353 |
while (character_set_contains(previous, prefix_buffer[bufpos], &prefix)) { |
| 354 |
bufpos = (bufpos + prefix_buffer_size - 1) & prefix_buffer_mask; |
| 355 |
previous = prefix; |
| 356 |
} |
| 357 |
|
| 358 |
return previous->is_final; |
| 359 |
} |
| 360 |
|
| 361 |
bool gregorio_find_vowel_group(const grewchar *const string, int *const start, |
| 362 |
int *const end) |
| 363 |
{ |
| 364 |
size_t bufpos = 0; |
| 365 |
vowel_group_state state = VWL_BEFORE; |
| 366 |
character_set *cset = NULL; |
| 367 |
const grewchar *subject; |
| 368 |
int i; |
| 369 |
|
| 370 |
/* stick the 0 in here to avoid overruns from is_in_prefix */ |
| 371 |
prefix_buffer[0] = 0; |
| 372 |
|
| 373 |
for (i = 0, subject = string; true; ++i, ++subject) { |
| 374 |
switch (state) { |
| 375 |
case VWL_BEFORE: |
| 376 |
bufpos = (bufpos + 1) & prefix_buffer_mask; |
| 377 |
prefix_buffer[bufpos] = *subject; |
| 378 |
if (character_set_contains(vowel_table, *subject, NULL)) { |
| 379 |
/* we found a vowel */ |
| 380 |
if ((!character_set_contains(vowel_table, *(subject + 1), NULL) |
| 381 |
&& *(subject + 1) != GREVOWEL_ELISION_MARK) |
| 382 |
|| !is_in_prefix(bufpos)) { |
| 383 |
/* no vowel/elision after, or not in prefix, so this is the |
| 384 |
* start */ |
| 385 |
*start = i; |
| 386 |
state = VWL_WITHIN; |
| 387 |
} |
| 388 |
} |
| 389 |
break; |
| 390 |
|
| 391 |
case VWL_WITHIN: |
| 392 |
if (!character_set_contains(vowel_table, *subject, NULL)) { |
| 393 |
/* not a vowel; is it a suffix? */ |
| 394 |
*end = i; |
| 395 |
if (character_set_contains(suffix_table, *subject, &cset)) { |
| 396 |
/* it's the start of a suffix */ |
| 397 |
state = VWL_SUFFIX; |
| 398 |
} else { |
| 399 |
/* neither a vowel nor a suffix, so this is the end */ |
| 400 |
return true; |
| 401 |
} |
| 402 |
} |
| 403 |
break; |
| 404 |
|
| 405 |
case VWL_SUFFIX: |
| 406 |
if (cset->is_final) { |
| 407 |
/* remember the last final position */ |
| 408 |
*end = i; |
| 409 |
} |
| 410 |
if (!character_set_contains(cset, *subject, &cset)) { |
| 411 |
/* no longer in valid suffix */ |
| 412 |
return true; |
| 413 |
} |
| 414 |
break; |
| 415 |
|
| 416 |
default: |
| 417 |
/* not reachable unless there's a programming error */ |
| 418 |
/* LCOV_EXCL_START */ |
| 419 |
assert(false); |
| 420 |
break; |
| 421 |
/* LCOV_EXCL_STOP */ |
| 422 |
} |
| 423 |
|
| 424 |
if (!*subject) { |
| 425 |
break; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
/* no vowel found; look for a secondary */ |
| 430 |
*end = -1; |
| 431 |
for (*start = 0; *(subject = string + *start); ++ *start) { |
| 432 |
for (cset = secondary_table, i = *start; |
| 433 |
character_set_contains(cset, *subject, &cset); ++i, ++subject) { |
| 434 |
if (cset->is_final) { |
| 435 |
/* remember the last final position */ |
| 436 |
*end = i + 1; |
| 437 |
} |
| 438 |
} |
| 439 |
if (*end >= 0) { |
| 440 |
return true; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
/* no center found */ |
| 445 |
*start = -1; |
| 446 |
return false; |
| 447 |
} |