static void check_input_clobber(char *input_file_name, char *output_file_name) { if (input_file_name && output_file_name) { char *absolute_input_file_name; char *absolute_output_file_name; char *current_directory; int file_cmp; size_t bufsize = 128; char *buf = gregorio_malloc(bufsize); while ((current_directory = getcwd(buf, bufsize)) == NULL && errno == ERANGE && bufsize < MAX_BUF_GROWTH) { free(buf); bufsize <<= 1; buf = gregorio_malloc(bufsize); } if (current_directory == NULL) { fprintf(stderr, _("can't determine current directory")); free(buf); gregorio_exit(1); } absolute_input_file_name = define_path(current_directory, input_file_name); absolute_output_file_name = define_path(current_directory, output_file_name); file_cmp = strcmp(absolute_input_file_name, absolute_output_file_name); if (file_cmp == 0) { fprintf(stderr, "error: refusing to overwrite the input file\n"); } free(buf); free(absolute_input_file_name); free(absolute_output_file_name); if (file_cmp == 0) { gregorio_exit(1); } } }
/* function that adds the good extension to a basename (without extension) */ static char *get_output_filename(char *fbasename, const char *extension) { char *output_filename = NULL; output_filename = (char *) gregorio_malloc((strlen(extension) + strlen(fbasename) + 2)); output_filename = strcpy(output_filename, fbasename); output_filename = strcat(output_filename, "."); output_filename = strcat(output_filename, extension); return output_filename; }
static char *encode_point_and_click_filename(char *input_file_name) { /* percent-encoding favors capital hex digits */ static const char *const hex = "0123456789ABCDEF"; char *filename, *result = NULL, *r = NULL, *p; filename = gregorio_realpath(input_file_name, NULL); if (!filename) { /* it's not reasonable to generate the system error that would cause * this to fail */ /* LCOV_EXCL_START */ fprintf(stderr, "error: unable to resolve %s\n", input_file_name); gregorio_exit(1); /* LCOV_EXCL_STOP */ } /* 2 extra characters for a possible leading slash and final NUL */ r = result = gregorio_malloc(strlen(filename) * 4 + 2); #ifdef _WIN32 *(r++) = '/'; #endif for (p = filename; *p; ++p) { #ifdef _WIN32 if (*p == '\\') { *p = '/'; } #endif /* note that -, _ and ~ are conspicuously missing from this list * because they cause trouble in TeX; we will percent-encode them */ if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p < 'z') || (*p >= '0' && *p <= '9') || *p == '.' || *p == '/' #ifdef _WIN32 || *p == ':' #endif ) { *(r++) = *p; } else { /* percent-encode anything else */ *(r++) = '\\'; /* must escape it because it's TeX */ *(r++) = '%'; *(r++) = hex[(*p >> 4) & 0x0FU]; *(r++) = hex[*p & 0x0FU]; } } *r = '\0'; free(filename); return result; }
void gregorio_add_syllable(gregorio_syllable **current_syllable, int number_of_voices, gregorio_element *elements[], gregorio_character *first_character, gregorio_character *first_translation_character, gregorio_word_position position, char *abovelinestext, gregorio_tr_centering translation_type, gregorio_nlba no_linebreak_area, gregorio_euouae euouae, const gregorio_scanner_location *const loc, const bool first_word) { gregorio_syllable *next; gregorio_element **tab; int i; if (number_of_voices > MAX_NUMBER_OF_VOICES) { gregorio_message(_("too many voices"), "add_syllable", VERBOSITY_FATAL, 0); return; } next = gregorio_calloc(1, sizeof(gregorio_syllable)); next->type = GRE_SYLLABLE; next->special_sign = _NO_SIGN; next->position = position; next->no_linebreak_area = no_linebreak_area; next->euouae = euouae; next->text = first_character; next->translation = first_translation_character; next->translation_type = translation_type; next->abovelinestext = abovelinestext; next->first_word = first_word; if (loc) { next->src_line = loc->first_line; next->src_column = loc->first_column; next->src_offset = loc->first_offset; } next->next_syllable = NULL; next->previous_syllable = *current_syllable; tab = (gregorio_element **) gregorio_malloc(number_of_voices * sizeof(gregorio_element *)); if (elements) { for (i = 0; i < number_of_voices; i++) { tab[i] = elements[i]; } } else { for (i = 0; i < number_of_voices; i++) { tab[i] = NULL; } } next->elements = tab; if (*current_syllable) { (*current_syllable)->next_syllable = next; } *current_syllable = next; }
/* define_path attempts to canonicalize the pathname of a given string */ static char *define_path(char *current_directory, char *string) { int length; char *file_name; char *temp_name; char *base_name; #ifdef _WIN32 char *last_backslash; #endif temp_name = gregorio_strdup(string); base_name = strrchr(temp_name, '/'); #ifdef _WIN32 last_backslash = strrchr(temp_name, '\\'); if (last_backslash > base_name) { base_name = last_backslash; } #endif if (base_name) { /* some path was supplied */ *base_name = '\0'; base_name++; /* try to resolve it */ file_name = gregorio_realpath(temp_name, NULL); if (!file_name) { /* it's not reasonable to cover this failure in testing */ /* LCOV_EXCL_START */ fprintf(stderr, "the directory %s for %s does not exist\n", temp_name, base_name); gregorio_exit(1); /* LCOV_EXCL_STOP */ } } else { /* no path was supplied */ base_name = string; file_name = gregorio_malloc( strlen(current_directory) + strlen(base_name) + 2); strcpy(file_name, current_directory); } /* build the file name */ length = strlen(file_name); file_name = gregorio_realloc(file_name, length + strlen(base_name) + 2); file_name[length] = '/'; strcpy(file_name + length + 1, base_name); free(temp_name); return file_name; }
static void style_push(det_style **current_style, grestyle_style style) { det_style *element; gregorio_assert(current_style, style_push, "current_style may not be NULL", return); element = (det_style *) gregorio_malloc(sizeof(det_style)); element->style = style; element->previous_style = NULL; element->next_style = (*current_style); if (*current_style) { (*current_style)->previous_style = element; } (*current_style) = element; }
/* function that returns the filename without the extension */ static char *get_base_filename(char *fbasename) { char *p; int l; char *ret; p = strrchr(fbasename, '.'); if (!p) { return NULL; } l = strlen(fbasename) - strlen(p); ret = (char *) gregorio_malloc(l + 1); gregorio_snprintf(ret, l + 1, "%s", fbasename); ret[l] = '\0'; return ret; }
void *_gregorio_grow_buffer(void *buffer, size_t *nmemb, size_t size) { if (buffer == NULL) { return gregorio_malloc(*nmemb * size); } if (*nmemb >= MAX_BUF_GROWTH) { /* it's not realistic to test this case */ /* LCOV_EXCL_START */ gregorio_message(_("buffer too large"), "gregorio_grow_buffer", VERBOSITY_FATAL, 0); gregorio_exit(1); /* LCOV_EXCL_STOP */ } *nmemb <<= 1; return gregorio_realloc(buffer, *nmemb * size); }
void gregorio_add_score_header(gregorio_score *score, char *name, char *value) { gregorio_header *header; if (!score) { gregorio_message(_("function called with NULL argument"), "gregorio_add_score_header", VERBOSITY_WARNING, 0); return; } header = (gregorio_header *)gregorio_malloc(sizeof(gregorio_header)); header->name = name; header->value = value; header->next = NULL; if (score->last_header) { score->last_header->next = header; } else { score->headers = header; } score->last_header = header; }
void gregorio_vowel_tables_init(void) { if (vowel_table) { character_set_clear(vowel_table); character_set_clear(prefix_table); character_set_clear(suffix_table); character_set_clear(secondary_table); } else { vowel_table = character_set_new(false); prefix_table = character_set_new(true); suffix_table = character_set_new(true); secondary_table = character_set_new(true); prefix_buffer_size = 16; prefix_buffer_mask = 0x0f; prefix_buffer = (grewchar *)gregorio_malloc( prefix_buffer_size * sizeof(grewchar)); } }
static void determine_center(gregorio_character *character, int *start, int *end) { int count; grewchar *subject; gregorio_character *ch; bool in_elision; *start = *end = -1; in_elision = false; for (count = 0, ch = character; ch; ch = ch->next_character) { if (ch->is_character) { if (!in_elision) { ++count; } } else if (!handle_elision(ch, &in_elision)) { ch = skip_verbatim_or_special(ch); } } if (count == 0) { return; } in_elision = false; subject = (grewchar *)gregorio_malloc((count + 1) * sizeof(grewchar)); for (count = 0, ch = character; ch; ch = ch->next_character) { if (ch->is_character) { if (!in_elision) { subject[count ++] = ch->cos.character; } } else if (!handle_elision(ch, &in_elision)) { ch = skip_verbatim_or_special(ch); } } subject[count] = (grewchar)0; gregorio_find_vowel_group(subject, start, end); free(subject); }
void gregorio_add_texverb_to_note(gregorio_note *current_note, char *str) { size_t len; char *res; if (str == NULL) { return; } if (current_note) { if (current_note->texverb) { len = strlen(current_note->texverb) + strlen(str) + 1; res = gregorio_malloc(len); strcpy(res, current_note->texverb); strcat(res, str); free(current_note->texverb); free(str); current_note->texverb = res; } else { current_note->texverb = str; } } }