lorid

convert chordpro to pdf
git clone git://git.relim.de/lorid.git
Log | Files | Refs | README | LICENSE

commit a393a0c67771b3e32cf2849f125945918bdc6ce1
parent 7e1f05491cd3e20a14df5da83468f0a20389bc72
Author: nibo <nibo@relim.de>
Date:   Tue, 19 Nov 2024 11:26:18 +0100

Quit program if malloc or realloc fails

Diffstat:
Mchordpro.c | 298++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mconfig.c | 20++++++++++----------
Mlorid.1 | 2+-
Mlorid.c | 4++--
Mout_pdf.c | 90++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mutil.c | 82++++++++++++++++++++++++++++++++-----------------------------------------------
Mutil.h | 3++-
7 files changed, 242 insertions(+), 257 deletions(-)

diff --git a/chordpro.c b/chordpro.c @@ -385,7 +385,7 @@ is_whitespace(char c) struct RGBColor * cho_rgbcolor_new(uint8_t red, uint8_t green, uint8_t blue) { - struct RGBColor *color = malloc(sizeof(struct RGBColor)); + struct RGBColor *color = emalloc(sizeof(struct RGBColor)); color->red = red; color->green = green; color->blue = blue; @@ -395,7 +395,7 @@ cho_rgbcolor_new(uint8_t red, uint8_t green, uint8_t blue) struct RGBColor * cho_rgbcolor_copy(struct RGBColor *color) { - struct RGBColor *copy = malloc(sizeof(struct RGBColor)); + struct RGBColor *copy = emalloc(sizeof(struct RGBColor)); copy->red = color->red; copy->green = color->green; copy->blue = color->blue; @@ -414,7 +414,7 @@ cho_rgbcolor_to_string(struct RGBColor *color) struct RGBColor * cho_rgbcolor_parse(const char *str) { - struct RGBColor *color = malloc(sizeof(struct RGBColor)); + struct RGBColor *color = emalloc(sizeof(struct RGBColor)); size_t len = strlen(str); char tmp[3]; long primary_color; @@ -517,7 +517,7 @@ cho_rgbcolor_parse(const char *str) struct RGBColor * cho_color_parse(const char *str) { - struct RGBColor *color = malloc(sizeof(struct RGBColor)); + struct RGBColor *color = emalloc(sizeof(struct RGBColor)); if (str[0] == '#') { struct RGBColor *rgb_color = cho_rgbcolor_parse(str); if (rgb_color) { @@ -575,7 +575,7 @@ cho_color_parse(const char *str) struct Font * cho_font_new(void) { - struct Font *font = malloc(sizeof(struct Font)); + struct Font *font = emalloc(sizeof(struct Font)); font->name = NULL; font->family = FF_NORMAL; font->style = FS_ROMAN; @@ -587,7 +587,7 @@ cho_font_new(void) struct Font * cho_font_copy(struct Font *font) { - struct Font *copy = malloc(sizeof(struct Font)); + struct Font *copy = emalloc(sizeof(struct Font)); if (font->name) copy->name = strdup(font->name); else @@ -858,7 +858,7 @@ cho_style_apply_default(enum SongFragmentType current_ftype, struct ChoStyle *st struct ChoStyle * cho_style_new(void) { - struct ChoStyle *style = malloc(sizeof(struct ChoStyle)); + struct ChoStyle *style = emalloc(sizeof(struct ChoStyle)); style->font = cho_font_new(); style->foreground_color = cho_rgbcolor_new(20, 20, 20); style->background_color = cho_rgbcolor_new(255, 255, 255); @@ -878,7 +878,7 @@ cho_style_new(void) struct ChoStyle * cho_style_copy(struct ChoStyle *style) { - struct ChoStyle *copy = malloc(sizeof(struct ChoStyle)); + struct ChoStyle *copy = emalloc(sizeof(struct ChoStyle)); copy->font = cho_font_copy(style->font); copy->foreground_color = cho_rgbcolor_copy(style->foreground_color); copy->background_color = cho_rgbcolor_copy(style->background_color); @@ -960,33 +960,33 @@ cho_style_font_desc_parse(const char *str) } struct Font *font = cho_font_new(); double size = -1.0; - char **words = malloc(sizeof(char *)); + char **words = emalloc(sizeof(char *)); int w = 0; words[w] = NULL; int i = 0; int k = 0; while (str[i] != 0) { if (str[i] == ' ') { - words[w] = realloc(words[w], (k+1) * sizeof(char)); + words[w] = erealloc(words[w], (k+1) * sizeof(char)); words[w][k] = 0; if (strlen(words[w]) == 0) free(words[w]); else w++; k = 0; - words = realloc(words, (w+1) * sizeof(char *)); + words = erealloc(words, (w+1) * sizeof(char *)); words[w] = NULL; } else { - words[w] = realloc(words[w], (k+1) * sizeof(char)); + words[w] = erealloc(words[w], (k+1) * sizeof(char)); words[w][k] = str[i]; k++; } i++; } - words[w] = realloc(words[w], (k+1) * sizeof(char)); + words[w] = erealloc(words[w], (k+1) * sizeof(char)); words[w][k] = 0; w++; - words = realloc(words, (w+1) * sizeof(char *)); + words = erealloc(words, (w+1) * sizeof(char *)); words[w] = NULL; w = 0; int stop_at = EMPTY; @@ -1044,12 +1044,12 @@ cho_style_font_desc_parse(const char *str) int n = 0; for (int i=0; i<stop_at; i++) { while (words[i][k] != 0) { - font->name = realloc(font->name, (n+1) * sizeof(char)); + font->name = erealloc(font->name, (n+1) * sizeof(char)); font->name[n] = words[i][k]; n++; k++; } - font->name = realloc(font->name, (n+1) * sizeof(char)); + font->name = erealloc(font->name, (n+1) * sizeof(char)); font->name[n] = ' '; n++; k = 0; @@ -1426,7 +1426,7 @@ cho_style_reset_default(void) static struct Attr * cho_tag_attr_new(void) { - struct Attr *attr = malloc(sizeof(struct Attr)); + struct Attr *attr = emalloc(sizeof(struct Attr)); attr->name = NULL; attr->value = NULL; return attr; @@ -1460,7 +1460,7 @@ cho_tag_attrs_free(struct Attr **attrs) static struct Tag * cho_tag_new(void) { - struct Tag *tag = malloc(sizeof(struct Tag)); + struct Tag *tag = emalloc(sizeof(struct Tag)); tag->name = NULL; tag->style = NULL; tag->attrs = NULL; @@ -1519,7 +1519,7 @@ cho_tag_style_inherit(struct Tag **tags, int prev_index) static struct ChoMetadata * cho_metadata_new(void) { - struct ChoMetadata *meta = malloc(sizeof(struct ChoMetadata)); + struct ChoMetadata *meta = emalloc(sizeof(struct ChoMetadata)); meta->name = NULL; meta->value = NULL; meta->style = NULL; @@ -1563,23 +1563,23 @@ cho_metadata_split(const char *directive_value) bool is_name = true; while (value[i] != 0) { if (value[i] == ' ') { - meta->name = realloc(meta->name, (n+1) * sizeof(char)); + meta->name = erealloc(meta->name, (n+1) * sizeof(char)); meta->name[n] = 0; is_name = false; } else { if (is_name) { - meta->name = realloc(meta->name, (n+1) * sizeof(char)); + meta->name = erealloc(meta->name, (n+1) * sizeof(char)); meta->name[n] = value[i]; n++; } else { - meta->value = realloc(meta->value, (v+1) * sizeof(char)); + meta->value = erealloc(meta->value, (v+1) * sizeof(char)); meta->value[v] = value[i]; v++; } } i++; } - meta->value = realloc(meta->value, (v+1) * sizeof(char)); + meta->value = erealloc(meta->value, (v+1) * sizeof(char)); meta->value[v] = 0; free(value); if (v > 1) { @@ -1693,7 +1693,7 @@ transposition_calc_chord_root(int index, enum NoteType type) static struct ChoChord * cho_chord_new(void) { - struct ChoChord *chord = malloc(sizeof(struct ChoChord)); + struct ChoChord *chord = emalloc(sizeof(struct ChoChord)); chord->style = cho_style_new_default(); chord->name = NULL; chord->is_canonical = false; @@ -1740,7 +1740,7 @@ cho_chord_free(struct ChoChord *chord) static struct ChoChord * cho_chord_copy(struct ChoChord *chord) { - struct ChoChord *copy = malloc(sizeof(struct ChoChord)); + struct ChoChord *copy = emalloc(sizeof(struct ChoChord)); copy->style = cho_style_copy(chord->style); copy->is_canonical = chord->is_canonical; copy->qual = chord->qual; @@ -1924,21 +1924,21 @@ cho_chord_name_generate(struct ChoChord *chord) char *name = NULL; size_t name_len = 0; name_len += strlen(chord->root); - name = realloc(name, name_len * sizeof(char)); + name = erealloc(name, name_len * sizeof(char)); for (i = 0; chord->root[i]; i++) { name[n] = chord->root[i]; n++; } const char *qual = chord_qualifiers[chord->qual]; name_len += strlen(qual); - name = realloc(name, name_len * sizeof(char)); + name = erealloc(name, name_len * sizeof(char)); for (i = 0; qual[i]; i++) { name[n] = qual[i]; n++; } if (chord->ext) { name_len += strlen(chord->ext); - name = realloc(name, name_len * sizeof(char)); + name = erealloc(name, name_len * sizeof(char)); for (i = 0; chord->ext[i]; i++) { name[n] = chord->ext[i]; n++; @@ -1947,7 +1947,7 @@ cho_chord_name_generate(struct ChoChord *chord) if (chord->bass) { name_len++; name_len += strlen(chord->bass); - name = realloc(name, name_len * sizeof(char)); + name = erealloc(name, name_len * sizeof(char)); name[n] = '/'; n++; for (i = 0; chord->bass[i]; i++) { @@ -1956,7 +1956,7 @@ cho_chord_name_generate(struct ChoChord *chord) } } name_len++; - name = realloc(name, name_len * sizeof(char)); + name = erealloc(name, name_len * sizeof(char)); name[n] = 0; return name; } @@ -1966,7 +1966,7 @@ cho_chord_name_generate(struct ChoChord *chord) static struct ChoImage * cho_image_new(void) { - struct ChoImage *image = malloc(sizeof(struct ChoImage)); + struct ChoImage *image = emalloc(sizeof(struct ChoImage)); image->is_asset = false; image->id = NULL; image->src = NULL; @@ -2037,7 +2037,7 @@ cho_image_free(struct ChoImage *image) static struct ChoImage * cho_image_copy(struct ChoImage *image) { - struct ChoImage *copy = malloc(sizeof(struct ChoImage)); + struct ChoImage *copy = emalloc(sizeof(struct ChoImage)); copy->is_asset = image->is_asset; copy->id = strdup(image->id); copy->src = strdup(image->src); @@ -2602,7 +2602,7 @@ cho_image_tag_parse(struct Attr **attrs) static struct ChoText * cho_text_new(void) { - struct ChoText *text = malloc(sizeof(struct ChoText)); + struct ChoText *text = emalloc(sizeof(struct ChoText)); text->style = cho_style_new_default(); text->text = NULL; return text; @@ -2622,7 +2622,7 @@ cho_text_free(struct ChoText *text) static struct ChoText * cho_text_copy(struct ChoText *text) { - struct ChoText *copy = malloc(sizeof(struct ChoText)); + struct ChoText *copy = emalloc(sizeof(struct ChoText)); copy->style = cho_style_copy(text->style); copy->text = strdup(text->text); return copy; @@ -2654,7 +2654,7 @@ cho_text_above_free(struct ChoLineItemAbove *text_above) static struct ChoLineItemAbove * cho_text_above_copy(struct ChoLineItemAbove *text_above) { - struct ChoLineItemAbove *copy = malloc(sizeof(struct ChoLineItemAbove)); + struct ChoLineItemAbove *copy = emalloc(sizeof(struct ChoLineItemAbove)); copy->position = text_above->position; copy->is_chord = text_above->is_chord; if (copy->is_chord) { @@ -2668,7 +2668,7 @@ cho_text_above_copy(struct ChoLineItemAbove *text_above) static struct ChoLineItem * cho_line_item_new(void) { - struct ChoLineItem *item = malloc(sizeof(struct ChoLineItem)); + struct ChoLineItem *item = emalloc(sizeof(struct ChoLineItem)); item->is_text = true; item->u.text = cho_text_new(); return item; @@ -2693,7 +2693,7 @@ cho_line_item_free(struct ChoLineItem *item) static struct ChoLineItem * cho_line_item_copy(struct ChoLineItem *item) { - struct ChoLineItem *copy = malloc(sizeof(struct ChoLineItem)); + struct ChoLineItem *copy = emalloc(sizeof(struct ChoLineItem)); if (item->is_text) { copy->is_text = true; copy->u.text = cho_text_copy(item->u.text); @@ -2707,7 +2707,7 @@ cho_line_item_copy(struct ChoLineItem *item) static struct ChoLine * cho_line_new(void) { - struct ChoLine *line = malloc(sizeof(struct ChoLine)); + struct ChoLine *line = emalloc(sizeof(struct ChoLine)); line->text_above = NULL; line->items = NULL; line->btype = BT_LINE; @@ -2756,7 +2756,7 @@ cho_line_compute_chord_position(struct ChoLine *line, int ly, int te) static struct ChoSection * cho_section_new(void) { - struct ChoSection *section = malloc(sizeof(struct ChoSection)); + struct ChoSection *section = emalloc(sizeof(struct ChoSection)); section->type = ST_EMPTY; section->label = NULL; section->lines = NULL; @@ -2784,7 +2784,7 @@ cho_section_free(struct ChoSection *section) static struct ChoSection * cho_section_copy(struct ChoSection *section) { - struct ChoSection *copy = malloc(sizeof(struct ChoSection)); + struct ChoSection *copy = emalloc(sizeof(struct ChoSection)); copy->type = section->type; if (section->label) { copy->label = cho_text_copy(section->label); @@ -2794,23 +2794,23 @@ cho_section_copy(struct ChoSection *section) copy->lines = NULL; int li, c, ly; for (li = 0; section->lines[li]; li++) { - copy->lines = realloc(copy->lines, (li+1) * sizeof(struct ChoLine *)); + copy->lines = erealloc(copy->lines, (li+1) * sizeof(struct ChoLine *)); copy->lines[li] = cho_line_new(); copy->lines[li]->btype = section->lines[li]->btype; for (c = 0; section->lines[li]->text_above[c]; c++) { - copy->lines[li]->text_above = realloc(copy->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + copy->lines[li]->text_above = erealloc(copy->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); copy->lines[li]->text_above[c] = cho_text_above_copy(section->lines[li]->text_above[c]); } - copy->lines[li]->text_above = realloc(copy->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + copy->lines[li]->text_above = erealloc(copy->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); copy->lines[li]->text_above[c] = NULL; for (ly = 0; section->lines[li]->items[ly]; ly++) { - copy->lines[li]->items = realloc(copy->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + copy->lines[li]->items = erealloc(copy->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); copy->lines[li]->items[ly] = cho_line_item_copy(section->lines[li]->items[ly]); } - copy->lines[li]->items = realloc(copy->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + copy->lines[li]->items = erealloc(copy->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); copy->lines[li]->items[ly] = NULL; } - copy->lines = realloc(copy->lines, (li+1) * sizeof(struct ChoLine *)); + copy->lines = erealloc(copy->lines, (li+1) * sizeof(struct ChoLine *)); copy->lines[li] = NULL; return copy; } @@ -2818,7 +2818,7 @@ cho_section_copy(struct ChoSection *section) static struct ChoSong * cho_song_new(void) { - struct ChoSong *song = malloc(sizeof(struct ChoSong)); + struct ChoSong *song = emalloc(sizeof(struct ChoSong)); song->metadata = NULL; song->sections = NULL; return song; @@ -2883,7 +2883,7 @@ cho_find_previous_chorus(struct ChoSection **sections, int se) static struct ChoDirective * cho_directive_new(void) { - struct ChoDirective *directive = malloc(sizeof(struct ChoDirective)); + struct ChoDirective *directive = emalloc(sizeof(struct ChoDirective)); directive->dtype = DT_EMPTY; directive->stype = ST_EMPTY; directive->position = POS_EMPTY; @@ -3312,20 +3312,20 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) int transpose; int th = 0; size_t read; - g_transpose_history = realloc(g_transpose_history, (th+1) * sizeof(int *)); + g_transpose_history = erealloc(g_transpose_history, (th+1) * sizeof(int *)); g_transpose_history[th] = 0; g_transpose = &g_transpose_history[th]; th++; enum AttrValueSyntax avs = AVS_NO; struct ChoDirective *directive = NULL; struct ChoMetadata *metadata = NULL; - struct ChoSong **songs = malloc(sizeof(struct ChoSong *)); + struct ChoSong **songs = emalloc(sizeof(struct ChoSong *)); songs[so] = cho_song_new(); - songs[so]->sections = malloc((se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = emalloc((se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_new(); - songs[so]->sections[se]->lines = malloc(sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = emalloc(sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = malloc(sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = emalloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); struct Tag **tags = NULL; struct ChoStyle *tag_style; @@ -3360,7 +3360,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } if (buf == '<') { if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; if (strlen(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text) == 0) { cho_line_item_free(songs[so]->sections[se]->lines[li]->items[ly]); @@ -3371,7 +3371,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) ly++; } te = 0; - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); prev_state = STATE_LYRICS; state = STATE_MARKUP_TAG; @@ -3388,7 +3388,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) return NULL; } if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; if (strlen(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text) == 0) { cho_line_item_free(songs[so]->sections[se]->lines[li]->items[ly]); @@ -3399,9 +3399,9 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) ) { free(songs[so]->sections[se]->lines[li]->items); free(songs[so]->sections[se]->lines[li]); - songs[so]->sections[se]->lines = realloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = erealloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); break; } @@ -3412,21 +3412,21 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } else { ly++; } - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = NULL; ly = 0; te = 0; - songs[so]->sections[se]->lines[li]->text_above = realloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + songs[so]->sections[se]->lines[li]->text_above = erealloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); songs[so]->sections[se]->lines[li]->text_above[c] = NULL; c = 0; li++; - songs[so]->sections[se]->lines = realloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = erealloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); break; } - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = buf; te++; break; @@ -3454,13 +3454,13 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_new(); songs[so]->sections[se]->type = directive->stype; li = 0; - songs[so]->sections[se]->lines = malloc(sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = emalloc(sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = malloc(sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = emalloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); break; case POS_END: @@ -3476,12 +3476,12 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_new(); li = 0; - songs[so]->sections[se]->lines = malloc(sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = emalloc(sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = malloc(sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = emalloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); } break; @@ -3491,29 +3491,29 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) if (chorus) { if (config->output->chorus->quote) { if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; } ly++; - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = NULL; ly = 0; te = 0; - songs[so]->sections[se]->lines[li]->text_above = realloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + songs[so]->sections[se]->lines[li]->text_above = erealloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); songs[so]->sections[se]->lines[li]->text_above[c] = NULL; c = 0; cho_line_free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; li = 0; se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_copy(chorus); se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_new(); - songs[so]->sections[se]->lines = malloc(sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = emalloc(sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = malloc(sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = emalloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); } else { if (chorus->label) { @@ -3522,7 +3522,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) label = strdup(config->output->chorus->label); } if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; if (strlen(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text) == 0) { cho_line_item_free(songs[so]->sections[se]->lines[li]->items[ly]); @@ -3532,7 +3532,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } else { ly++; } - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); cho_style_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text->style); g_current_ftype = SF_LABEL; @@ -3546,7 +3546,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } label = strdup(config->output->chorus->label); if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; if (strlen(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text) == 0) { cho_line_item_free(songs[so]->sections[se]->lines[li]->items[ly]); @@ -3556,7 +3556,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } else { ly++; } - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); cho_style_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text->style); g_current_ftype = SF_LABEL; @@ -3572,7 +3572,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) break; case DT_METADATA: cho_log(LOG_INFO, "Metadata directive '%s' has no value.", directive_name); - songs[so]->metadata = realloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); + songs[so]->metadata = erealloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); songs[so]->metadata[m] = cho_metadata_new(); songs[so]->metadata[m]->name = strdup(directive_name); m++; @@ -3590,27 +3590,27 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) free(songs[so]->sections[se]->lines[li]->items); free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; - songs[so]->metadata = realloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); + songs[so]->metadata = erealloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); songs[so]->metadata[m] = NULL; se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = NULL; if (!cho_style_reset_default()) { LOG_DEBUG("cho_style_reset_default failed."); return NULL; } so++; - songs = realloc(songs, (so+1) * sizeof(struct ChoSong *)); + songs = erealloc(songs, (so+1) * sizeof(struct ChoSong *)); songs[so] = cho_song_new(); se = 0; li = 0; ly = 0; m = 0; - songs[so]->sections = malloc((se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = emalloc((se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_new(); - songs[so]->sections[se]->lines = realloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = erealloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = malloc(sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = emalloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); break; case DT_FONT: @@ -3699,10 +3699,10 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_new(); songs[so]->sections[se]->type = directive->stype; - songs[so]->sections[se]->label = malloc(sizeof(struct ChoText)); + songs[so]->sections[se]->label = emalloc(sizeof(struct ChoText)); if (strstr(directive_value, "=")) { label = cho_directive_label_parse(directive_name, directive_value); if (!label) { @@ -3716,9 +3716,9 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } songs[so]->sections[se]->label->style = cho_style_new_from_config(SF_LABEL); li = 0; - songs[so]->sections[se]->lines = malloc(sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = emalloc(sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = malloc(sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = emalloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); break; case POS_END: @@ -3729,12 +3729,12 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_new(); li = 0; - songs[so]->sections[se]->lines = malloc(sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = emalloc(sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = malloc(sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = emalloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); } break; @@ -3745,42 +3745,42 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) if (chorus) { if (config->output->chorus->quote) { if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; } ly++; - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = NULL; ly = 0; te = 0; - songs[so]->sections[se]->lines[li]->text_above = realloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + songs[so]->sections[se]->lines[li]->text_above = erealloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); songs[so]->sections[se]->lines[li]->text_above[c] = NULL; c = 0; cho_line_free(songs[so]->sections[se]->lines[li]); - // songs[so]->sections[se]->lines = realloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); + // songs[so]->sections[se]->lines = erealloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = NULL; li = 0; se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_copy(chorus); if (songs[so]->sections[se]->label) { free(songs[so]->sections[se]->label->text); songs[so]->sections[se]->label->text = label; } else { - songs[so]->sections[se]->label = malloc(sizeof(struct ChoText)); + songs[so]->sections[se]->label = emalloc(sizeof(struct ChoText)); g_current_ftype = SF_LABEL; songs[so]->sections[se]->label->style = cho_style_new_default(); } se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = cho_section_new(); - songs[so]->sections[se]->lines = malloc(sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = emalloc(sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = cho_line_new(); - songs[so]->sections[se]->lines[li]->items = malloc(sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = emalloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); } else { if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; if (strlen(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text) == 0) { cho_line_item_free(songs[so]->sections[se]->lines[li]->items[ly]); @@ -3790,7 +3790,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } else { ly++; } - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); cho_style_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text->style); g_current_ftype = SF_LABEL; @@ -3803,7 +3803,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) cho_log(LOG_WARN, "Can't quote chorus because it's not defined previously."); } if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; if (strlen(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text) == 0) { cho_line_item_free(songs[so]->sections[se]->lines[li]->items[ly]); @@ -3813,7 +3813,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } else { ly++; } - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); cho_style_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text->style); g_current_ftype = SF_LABEL; @@ -3831,7 +3831,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) if (!strcmp(directive_name, "meta")) { metadata = cho_metadata_split(directive_value); if (metadata) { - songs[so]->metadata = realloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); + songs[so]->metadata = erealloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); songs[so]->metadata[m] = metadata; m++; } else { @@ -3839,7 +3839,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) return NULL; } } else { - songs[so]->metadata = realloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); + songs[so]->metadata = erealloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); songs[so]->metadata[m] = cho_metadata_new(); songs[so]->metadata[m]->name = strdup(directive_name); songs[so]->metadata[m]->value = str_remove_leading_whitespace(directive_value); @@ -3859,7 +3859,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) break; case DT_FORMATTING: if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; if (strlen(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text) == 0) { cho_line_item_free(songs[so]->sections[se]->lines[li]->items[ly]); @@ -3869,7 +3869,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } else { ly++; } - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); cho_style_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text->style); songs[so]->sections[se]->lines[li]->items[ly]->u.text->style = cho_style_copy(directive->style); @@ -3889,17 +3889,17 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) image->src = str_remove_leading_whitespace(directive_value); } if (image->is_asset) { - g_image_assets = realloc(g_image_assets, (g_ia+1) * sizeof(struct ChoImage *)); + g_image_assets = erealloc(g_image_assets, (g_ia+1) * sizeof(struct ChoImage *)); g_image_assets[g_ia] = image; g_ia++; } else { if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; te = 0; } ly++; - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); cho_text_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text); songs[so]->sections[se]->lines[li]->items[ly]->is_text = false; @@ -3914,7 +3914,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) char *dir_value = str_remove_leading_whitespace(directive_value); switch (directive->sprop) { case SPT_FONT: - sprop.u.font_name = malloc((strlen(dir_value)+1) * sizeof(char)); + sprop.u.font_name = emalloc((strlen(dir_value)+1) * sizeof(char)); strcpy(sprop.u.font_name, dir_value); sprop.type = SPT_FONT; break; @@ -3956,7 +3956,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) cho_log(LOG_ERR, "Directive 'transpose' has an invalid value."); return NULL; } - g_transpose_history = realloc(g_transpose_history, (th+1) * sizeof(int *)); + g_transpose_history = erealloc(g_transpose_history, (th+1) * sizeof(int *)); g_transpose_history[th] = g_transpose_history[th-1] + transpose; g_transpose = &g_transpose_history[th]; th++; @@ -4009,8 +4009,8 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) cho_chord_free(tmp_chord); is_chord_already_initialized = false; } else { - songs[so]->sections[se]->lines[li]->text_above = realloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); - songs[so]->sections[se]->lines[li]->text_above[c] = malloc(sizeof(struct ChoLineItemAbove)); + songs[so]->sections[se]->lines[li]->text_above = erealloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + songs[so]->sections[se]->lines[li]->text_above[c] = emalloc(sizeof(struct ChoLineItemAbove)); songs[so]->sections[se]->lines[li]->text_above[c]->is_chord = true; chord_pos = cho_line_compute_chord_position(songs[so]->sections[se]->lines[li], ly, te); songs[so]->sections[se]->lines[li]->text_above[c]->position = chord_pos; @@ -4028,8 +4028,8 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) if (prev_buf == '[' && buf == '*') { g_prev_ftype = g_current_ftype; g_current_ftype = SF_ANNOT; - songs[so]->sections[se]->lines[li]->text_above = realloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); - songs[so]->sections[se]->lines[li]->text_above[c] = malloc(sizeof(struct ChoLineItemAbove)); + songs[so]->sections[se]->lines[li]->text_above = erealloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + songs[so]->sections[se]->lines[li]->text_above[c] = emalloc(sizeof(struct ChoLineItemAbove)); songs[so]->sections[se]->lines[li]->text_above[c]->is_chord = false; chord_pos = cho_line_compute_chord_position(songs[so]->sections[se]->lines[li], ly, te); songs[so]->sections[se]->lines[li]->text_above[c]->position = chord_pos; @@ -4047,8 +4047,8 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } if (buf == '<') { if (prev_buf == '[') { - songs[so]->sections[se]->lines[li]->text_above = realloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); - songs[so]->sections[se]->lines[li]->text_above[c] = malloc(sizeof(struct ChoLineItemAbove)); + songs[so]->sections[se]->lines[li]->text_above = erealloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + songs[so]->sections[se]->lines[li]->text_above[c] = emalloc(sizeof(struct ChoLineItemAbove)); songs[so]->sections[se]->lines[li]->text_above[c]->is_chord = true; songs[so]->sections[se]->lines[li]->text_above[c]->u.chord = cho_chord_new(); is_chord_already_initialized = true; @@ -4062,7 +4062,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) break; case STATE_ANNOTATION: if (buf == ']') { - songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text = realloc(songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text, (ann+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text = erealloc(songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text, (ann+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text[ann] = 0; ann = 0; c++; @@ -4079,7 +4079,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) cho_log(LOG_ERR, "Newline character inside an annotation is invalid."); return NULL; } - songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text = realloc(songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text, (ann+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text = erealloc(songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text, (ann+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text[ann] = buf; ann++; break; @@ -4110,12 +4110,12 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) break; case STATE_ANNOTATION: if (ann > 0) { - songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text = realloc(songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text, (ann+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text = erealloc(songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text, (ann+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->text_above[c]->u.annot->text[ann] = 0; ann = 0; c++; - songs[so]->sections[se]->lines[li]->text_above = realloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); - songs[so]->sections[se]->lines[li]->text_above[c] = malloc(sizeof(struct ChoLineItemAbove)); + songs[so]->sections[se]->lines[li]->text_above = erealloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + songs[so]->sections[se]->lines[li]->text_above[c] = emalloc(sizeof(struct ChoLineItemAbove)); songs[so]->sections[se]->lines[li]->text_above[c]->is_chord = false; songs[so]->sections[se]->lines[li]->text_above[c]->position = chord_pos; songs[so]->sections[se]->lines[li]->text_above[c]->u.annot = cho_text_new(); @@ -4135,7 +4135,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) tag_start[t] = 0; t = 0; tags[ta]->name = strdup(tag_start); - tags[ta]->attrs = realloc(tags[ta]->attrs, (at+1) * sizeof(struct Attr *)); + tags[ta]->attrs = erealloc(tags[ta]->attrs, (at+1) * sizeof(struct Attr *)); tags[ta]->attrs[at] = cho_tag_attr_new(); state = STATE_MARKUP_ATTR_NAME; break; @@ -4157,7 +4157,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) break; } ta++; - tags = realloc(tags, (ta+1) * sizeof(struct Tag *)); + tags = erealloc(tags, (ta+1) * sizeof(struct Tag *)); tags[ta] = cho_tag_new(); state = STATE_MARKUP_TAG_START; // INFO: If we don't use 'goto' we loose the first character of the start tag name @@ -4187,7 +4187,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) break; case STATE_MARKUP_ATTR_NAME: if (buf == '=') { - tags[ta]->attrs[at]->name = realloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); + tags[ta]->attrs[at]->name = erealloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); tags[ta]->attrs[at]->name[atn] = 0; atn = 0; state = STATE_MARKUP_ATTR_VALUE; @@ -4198,7 +4198,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) if (!tags[ta]->attrs[at]->name) { break; } else { - tags[ta]->attrs[at]->name = realloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); + tags[ta]->attrs[at]->name = erealloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); tags[ta]->attrs[at]->name[atn] = 0; cho_log(LOG_ERR, "Attribute with name '%s' of tag '%s' has no value.", tags[ta]->attrs[at]->name, tag_start); return NULL; @@ -4210,7 +4210,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) if (!tags[ta]->attrs[at-1]->name && !tags[ta]->attrs[at-1]->value) { break; } - tags[ta]->attrs[at]->name = realloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); + tags[ta]->attrs[at]->name = erealloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); tags[ta]->attrs[at]->name[atn] = 0; cho_log(LOG_ERR, "Attribute with name '%s' of tag '%s' has no value.", tags[ta]->attrs[at]->name, tag_start); return NULL; @@ -4229,7 +4229,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } songs[so]->sections[se]->lines[li]->items[ly]->u.image = image; ly++; - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); } else { tag_style = cho_style_parse(tag_start, tags[ta]->attrs, cho_tag_style_inherit(tags, ta-1)); @@ -4261,7 +4261,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) state = prev_state; break; } else { - tags[ta]->attrs[at]->name = realloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); + tags[ta]->attrs[at]->name = erealloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); tags[ta]->attrs[at]->name[atn] = 0; atn = 0; cho_log(LOG_ERR, "Attribute with name '%s' of tag '%s' has no value.", tags[ta]->attrs[at]->name, tag_start); @@ -4272,7 +4272,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) cho_log(LOG_ERR, "Newline character inside an tag attribute name is invalid."); return NULL; } - tags[ta]->attrs[at]->name = realloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); + tags[ta]->attrs[at]->name = erealloc(tags[ta]->attrs[at]->name, (atn+1) * sizeof(char)); tags[ta]->attrs[at]->name[atn] = buf; atn++; break; @@ -4296,18 +4296,18 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) avs = AVS_QUOTATION_MARK; } else { avs = AVS_UNQUOTED; - tags[ta]->attrs[at]->value = realloc(tags[ta]->attrs[at]->value, (atv+1) * sizeof(char)); + tags[ta]->attrs[at]->value = erealloc(tags[ta]->attrs[at]->value, (atv+1) * sizeof(char)); tags[ta]->attrs[at]->value[atv] = buf; atv++; } break; } if (avs == AVS_UNQUOTED && buf == '>') { - tags[ta]->attrs[at]->value = realloc(tags[ta]->attrs[at]->value, (atv+1) * sizeof(char)); + tags[ta]->attrs[at]->value = erealloc(tags[ta]->attrs[at]->value, (atv+1) * sizeof(char)); tags[ta]->attrs[at]->value[atv] = 0; atv = 0; at++; - tags[ta]->attrs = realloc(tags[ta]->attrs, (at+1) * sizeof(struct Attr *)); + tags[ta]->attrs = erealloc(tags[ta]->attrs, (at+1) * sizeof(struct Attr *)); tags[ta]->attrs[at] = NULL; if (!strcmp(tags[ta]->name, "img")) { cho_text_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text); @@ -4319,7 +4319,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) } songs[so]->sections[se]->lines[li]->items[ly]->u.image = image; ly++; - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new(); } else { tag_style = cho_style_parse(tag_start, tags[ta]->attrs, cho_tag_style_inherit(tags, ta-1)); @@ -4357,17 +4357,17 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) (avs == AVS_QUOTATION_MARK && buf == '"') || (avs == AVS_UNQUOTED && (buf == ' ' || buf == '\t')) ) { - tags[ta]->attrs[at]->value = realloc(tags[ta]->attrs[at]->value, (atv+1) * sizeof(char)); + tags[ta]->attrs[at]->value = erealloc(tags[ta]->attrs[at]->value, (atv+1) * sizeof(char)); tags[ta]->attrs[at]->value[atv] = 0; atv = 0; at++; - tags[ta]->attrs = realloc(tags[ta]->attrs, (at+1) * sizeof(struct Attr *)); + tags[ta]->attrs = erealloc(tags[ta]->attrs, (at+1) * sizeof(struct Attr *)); tags[ta]->attrs[at] = cho_tag_attr_new(); avs = AVS_NO; state = STATE_MARKUP_ATTR_NAME; break; } - tags[ta]->attrs[at]->value = realloc(tags[ta]->attrs[at]->value, (atv+1) * sizeof(char)); + tags[ta]->attrs[at]->value = erealloc(tags[ta]->attrs[at]->value, (atv+1) * sizeof(char)); tags[ta]->attrs[at]->value[atv] = buf; atv++; break; @@ -4392,15 +4392,15 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) free(tags); if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { if (songs[so]->sections[se]->lines[li]->items[ly]->u.text->text) { - songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); + songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = erealloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0; ly++; - songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); + songs[so]->sections[se]->lines[li]->items = erealloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->items[ly] = NULL; - songs[so]->sections[se]->lines[li]->text_above = realloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); + songs[so]->sections[se]->lines[li]->text_above = erealloc(songs[so]->sections[se]->lines[li]->text_above, (c+1) * sizeof(struct ChoLineItemAbove *)); songs[so]->sections[se]->lines[li]->text_above[c] = NULL; li++; - songs[so]->sections[se]->lines = realloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); + songs[so]->sections[se]->lines = erealloc(songs[so]->sections[se]->lines, (li+1) * sizeof(struct ChoLine *)); songs[so]->sections[se]->lines[li] = NULL; } else { cho_line_item_free(songs[so]->sections[se]->lines[li]->items[ly]); @@ -4414,13 +4414,13 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) LOG_DEBUG("cho_style_reset_default failed."); return NULL; } - songs[so]->metadata = realloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); + songs[so]->metadata = erealloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); songs[so]->metadata[m] = NULL; se++; - songs[so]->sections = realloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); + songs[so]->sections = erealloc(songs[so]->sections, (se+1) * sizeof(struct ChoSection *)); songs[so]->sections[se] = NULL; so++; - songs = realloc(songs, (so+1) * sizeof(struct ChoSong *)); + songs = erealloc(songs, (so+1) * sizeof(struct ChoSong *)); songs[so] = NULL; free(g_transpose_history); for (e = 0; e<g_ia; e++) { diff --git a/config.c b/config.c @@ -106,7 +106,7 @@ static struct Note notes_nashville[] = { static struct OutputStyle * config_output_style_new(const char *name) { - struct OutputStyle *item = malloc(sizeof(struct OutputStyle)); + struct OutputStyle *item = emalloc(sizeof(struct OutputStyle)); item->name = strdup(name); item->style = cho_style_new(); return item; @@ -162,7 +162,7 @@ config_naming_system_to_config_string(enum NamingSystem system) static struct Note * config_note_new(void) { - struct Note *note = malloc(sizeof(struct Note)); + struct Note *note = emalloc(sizeof(struct Note)); note->note = NULL; note->sharp = NULL; note->flat = NULL; @@ -192,7 +192,7 @@ config_notes_free(struct Note **notes) static struct Note ** config_notes_new_default(enum NamingSystem system) { - struct Note **notes_default = malloc(8 * sizeof(struct Note *)); + struct Note **notes_default = emalloc(8 * sizeof(struct Note *)); struct Note *notes; switch (system) { case NS_GERMAN: @@ -234,7 +234,7 @@ config_notes_new_default(enum NamingSystem system) static struct Note ** config_notes_load(toml_table_t *notes, const char *system) { - struct Note **custom_notes = malloc(8 * sizeof(struct Note *)); + struct Note **custom_notes = emalloc(8 * sizeof(struct Note *)); toml_array_t *arr = toml_table_array(notes, system); int arr_len = toml_array_len(arr); if (arr_len != 7) { @@ -309,13 +309,13 @@ config_parse_mode_to_config_string(enum ParseMode mode) static struct Config * config_load_default(void) { - struct Config *config = malloc(sizeof(struct Config)); - config->output = malloc(sizeof(struct ConfigOutput)); - config->output->chorus = malloc(sizeof(struct ConfigChorus)); + struct Config *config = emalloc(sizeof(struct Config)); + config->output = emalloc(sizeof(struct ConfigOutput)); + config->output->chorus = emalloc(sizeof(struct ConfigChorus)); config->output->chorus->label = strdup("Chorus"); config->output->chorus->quote = false; config->output->system = NS_COMMON; - config->output->styles = malloc(12 * sizeof(struct OutputStyle *)); + config->output->styles = emalloc(12 * sizeof(struct OutputStyle *)); config->output->styles[0] = config_output_style_new("title"); config->output->styles[0]->style->font->name = strdup("Inter"); config->output->styles[0]->style->font->weight = FW_BOLD; @@ -348,8 +348,8 @@ config_load_default(void) config->output->styles[10]->style->font->style = FS_ITALIC; config->output->styles[11] = NULL; config->output->notes = config_notes_new_default(NS_COMMON); - config->parser = malloc(sizeof(struct ConfigParser)); - config->parser->chords = malloc(sizeof(struct ConfigChords)); + config->parser = emalloc(sizeof(struct ConfigParser)); + config->parser->chords = emalloc(sizeof(struct ConfigChords)); config->parser->chords->system = NS_COMMON; config->parser->chords->mode = PM_STRICT; config->parser->notes = config_notes_new_default(NS_COMMON); diff --git a/lorid.1 b/lorid.1 @@ -9,7 +9,7 @@ lorid \- convert chordpro to pdf .B lorid reads text from .I filepath -and if it wasn't provided than from stdin. +and if it wasn't provided then from stdin. The text will be parsed according to the chordpro specification and then converted to pdf. These two steps can be altered by changing the configuration file. diff --git a/lorid.c b/lorid.c @@ -31,11 +31,11 @@ main(int argc, char *argv[]) config_print_default(); return 0; case 'c': - config_filepath = malloc((strlen(optarg)+1) * sizeof(char)); + config_filepath = emalloc((strlen(optarg)+1) * sizeof(char)); strcpy(config_filepath, optarg); break; case 'o': - output = realloc(output, (strlen(optarg)+1) * sizeof(char)); + output = erealloc(output, (strlen(optarg)+1) * sizeof(char)); strcpy(output, optarg); break; case 'v': diff --git a/out_pdf.c b/out_pdf.c @@ -34,7 +34,7 @@ out_pdf_font_add_if_not_in(struct Font *font, struct Font ***array) { int a = 0; if (!*array) { - *array = realloc(*array, 2 * sizeof(struct Font *)); + *array = erealloc(*array, 2 * sizeof(struct Font *)); (*array)[0] = font; (*array)[1] = NULL; return true; @@ -49,7 +49,7 @@ out_pdf_font_add_if_not_in(struct Font *font, struct Font ***array) return false; a++; } - *array = realloc(*array, (a+2) * sizeof(struct Font *)); + *array = erealloc(*array, (a+2) * sizeof(struct Font *)); (*array)[a] = font; (*array)[a+1] = NULL; return true; @@ -69,7 +69,7 @@ out_pdf_add_fonts(struct Font *font, struct Font ***fonts) if (font->name[i] == ',') { part[p] = 0; trimmed = str_trim(part); - new_font = malloc(sizeof(struct Font)); + new_font = emalloc(sizeof(struct Font)); new_font->name = trimmed; new_font->family = font->family; new_font->style = font->style; @@ -90,7 +90,7 @@ out_pdf_add_fonts(struct Font *font, struct Font ***fonts) } part[p] = 0; trimmed = str_trim(part); - new_font = malloc(sizeof(struct Font)); + new_font = emalloc(sizeof(struct Font)); new_font->name = trimmed; new_font->family = font->family; new_font->style = font->style; @@ -220,7 +220,7 @@ out_pdf_filename_create(struct ChoSong **songs, const char *cho_filepath, const return strdup(out); case F_FOLDER: tmp = filepath_add_ending_slash_if_missing(out); - tmp = realloc(tmp, (strlen(tmp)+strlen(pdf_filename)+1) * sizeof(char)); + tmp = erealloc(tmp, (strlen(tmp)+strlen(pdf_filename)+1) * sizeof(char)); strcat(tmp, pdf_filename); free(pdf_filename); free(pdf_filepath); @@ -250,7 +250,7 @@ out_pdf_fnt_name_create(struct Font *font) const char *weight = cho_font_weight_to_config_string(font->weight); int n = 0; int i = 0; - char *fnt_name = malloc((strlen(name) + strlen(family) + strlen(style) + strlen(weight) + 4) * sizeof(char)); + char *fnt_name = emalloc((strlen(name) + strlen(family) + strlen(style) + strlen(weight) + 4) * sizeof(char)); while (name[i] != 0) { fnt_name[n] = name[i]; n++; @@ -601,7 +601,7 @@ out_pdf_page_create(pdfio_file_t *pdf, struct PDFImage **imgs, pdfio_array_t *an static struct Obj * obj_new(void) { - struct Obj *obj = malloc(sizeof(struct Obj)); + struct Obj *obj = emalloc(sizeof(struct Obj)); obj->name = NULL; obj->value = NULL; return obj; @@ -662,13 +662,13 @@ objs_add_obj(struct Obj ***array, struct Obj *obj) { int a = 0; if (!*array) { - *array = realloc(*array, 2 * sizeof(struct Obj *)); + *array = erealloc(*array, 2 * sizeof(struct Obj *)); (*array)[0] = obj; (*array)[1] = NULL; } else { while ((*array)[a]) a++; - *array = realloc(*array, (a+2) * sizeof(struct Obj *)); + *array = erealloc(*array, (a+2) * sizeof(struct Obj *)); (*array)[a] = obj; (*array)[a+1] = NULL; } @@ -762,7 +762,7 @@ out_pdf_load_images(struct Obj ***images, pdfio_file_t *file, struct ChoSong **s strcat((char *)&filepath, (*items)->u.image->src); name = image_name((*items)->u.image); if (!objs_has(*images, name)) { - *images = realloc(*images, (i+2) * sizeof(struct Obj *)); + *images = erealloc(*images, (i+2) * sizeof(struct Obj *)); (*images)[i] = obj_new(); (*images)[i]->name = strdup(name); if (strchr((*items)->u.image->src, '/')) { @@ -864,7 +864,7 @@ line_width_until_text_above( static struct PDFImage * pdf_image_new(void) { - struct PDFImage *image = malloc(sizeof(struct PDFImage)); + struct PDFImage *image = emalloc(sizeof(struct PDFImage)); image->x = 0.0; image->y = 0.0; image->width = 0.0; @@ -887,7 +887,7 @@ pdf_image_free(struct PDFImage *image) static struct PDFText * pdf_text_new(void) { - struct PDFText *t = malloc(sizeof(struct PDFText)); + struct PDFText *t = emalloc(sizeof(struct PDFText)); t->text = NULL; t->style = NULL; t->x = 0.0; @@ -909,7 +909,7 @@ pdf_text_free(struct PDFText *text) static struct PDFPage * pdf_page_new() { - struct PDFPage *page = malloc(sizeof(struct PDFPage)); + struct PDFPage *page = emalloc(sizeof(struct PDFPage)); page->texts = NULL; page->images = NULL; page->annots = pdfioArrayCreate(g_pdf_file); @@ -1002,14 +1002,14 @@ calc_space_between_text_above( if (prev_width >= width_between) { space.text_above_index = i; space.amount = prev_width - width_between + MIN_CHORD_GAP_WIDTH; - *spaces = realloc(*spaces, (sp+1) * sizeof(struct SpaceNeeded *)); - (*spaces)[sp] = malloc(sizeof(struct SpaceNeeded)); + *spaces = erealloc(*spaces, (sp+1) * sizeof(struct SpaceNeeded *)); + (*spaces)[sp] = emalloc(sizeof(struct SpaceNeeded)); *((*spaces)[sp]) = space; sp++; } else { } } - *spaces = realloc(*spaces, (sp+1) * sizeof(struct SpaceNeeded *)); + *spaces = erealloc(*spaces, (sp+1) * sizeof(struct SpaceNeeded *)); (*spaces)[sp] = NULL; return true; } @@ -1059,7 +1059,7 @@ items_find_position_to_break_line( struct Obj **img_objs ) { - struct CharPosition *pos = malloc(sizeof(struct CharPosition)); + struct CharPosition *pos = emalloc(sizeof(struct CharPosition)); pos->line_item_index = -1; pos->text_index = -1; double width = 0.0; @@ -1190,7 +1190,7 @@ pdf_texts_add_lyrics( int t = 0; int c; texts = &ctx->content->pages[ctx->page]->texts; - *texts = realloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); if (!ctx->spaces) { (*texts)[ctx->text]->text = strdup(item->u.text->text); @@ -1217,13 +1217,13 @@ pdf_texts_add_lyrics( return true; } for (c = 0; item->u.text->text[c]; c++) { - (*texts)[ctx->text]->text = realloc((*texts)[ctx->text]->text, (t+1) * sizeof(char)); + (*texts)[ctx->text]->text = erealloc((*texts)[ctx->text]->text, (t+1) * sizeof(char)); (*texts)[ctx->text]->text[t] = item->u.text->text[c]; t++; sp = ctx->spaces; while (*sp) { if ((*sp)->line_item_index == i && (*sp)->text_index == c) { - (*texts)[ctx->text]->text = realloc((*texts)[ctx->text]->text, (t+1) * sizeof(char)); + (*texts)[ctx->text]->text = erealloc((*texts)[ctx->text]->text, (t+1) * sizeof(char)); (*texts)[ctx->text]->text[t] = 0; (*texts)[ctx->text]->style = cho_style_copy(item->u.text->style); (*texts)[ctx->text]->x = ctx->x; @@ -1244,13 +1244,13 @@ pdf_texts_add_lyrics( t = 0; ctx->consumed_lyrics += strlen((*texts)[ctx->text]->text); ctx->text++; - *texts = realloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); } sp++; } } - (*texts)[ctx->text]->text = realloc((*texts)[ctx->text]->text, (t+1) * sizeof(char)); + (*texts)[ctx->text]->text = erealloc((*texts)[ctx->text]->text, (t+1) * sizeof(char)); (*texts)[ctx->text]->text[t] = 0; (*texts)[ctx->text]->style = cho_style_copy(item->u.text->style); (*texts)[ctx->text]->x = ctx->x; @@ -1318,7 +1318,7 @@ pdf_texts_add_text( return false; } ctx->x = calc_x(width, align); - *texts = realloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); (*texts)[ctx->text]->text = strdup(t); (*texts)[ctx->text]->style = cho_style_copy(style); @@ -1345,7 +1345,7 @@ pdf_texts_add_text( return false; } ctx->x = calc_x(width, align); - *texts = realloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); (*texts)[ctx->text]->text = strdup(t); (*texts)[ctx->text]->style = cho_style_copy(style); @@ -1361,7 +1361,7 @@ pdf_texts_add_text( ctx->y -= 8.0 + style->font->size; } else { ctx->x = calc_x(width, align); - *texts = realloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); (*texts)[ctx->text]->text = strdup(text); (*texts)[ctx->text]->style = cho_style_copy(style); @@ -1401,8 +1401,8 @@ pdf_content_create( ctx.image = 0; ctx.page = 0; ctx.spaces = NULL; - ctx.content = malloc(sizeof(struct PDFContent)); - ctx.content->pages = malloc(sizeof(struct PDFPage *)); + ctx.content = emalloc(sizeof(struct PDFContent)); + ctx.content->pages = emalloc(sizeof(struct PDFPage *)); ctx.content->pages[ctx.page] = pdf_page_new(); s = songs; texts = &ctx.content->pages[ctx.page]->texts; @@ -1496,7 +1496,7 @@ pdf_content_create( } sp++; } - *texts = realloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); (*texts)[ctx.text] = pdf_text_new(); (*texts)[ctx.text]->x = ctx.x; (*texts)[ctx.text]->y = ctx.y; @@ -1550,24 +1550,24 @@ pdf_content_create( ctx.y = MEDIABOX_HEIGHT - MARGIN_TOP; for (int p = ctx.text-1; prev_y == (*texts)[p]->y; p--) { (*texts)[p]->y = ctx.y; - tmp = realloc(tmp, (tm+1) * sizeof(struct PDFText *)); + tmp = erealloc(tmp, (tm+1) * sizeof(struct PDFText *)); tmp[tm] = (*texts)[p]; tm++; - *texts = realloc(*texts, (--ctx.text) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (--ctx.text) * sizeof(struct PDFText *)); } - *texts = realloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); (*texts)[ctx.text] = NULL; - *imgs = realloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); + *imgs = erealloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); (*imgs)[ctx.image] = NULL; ctx.text = 0; ctx.image = 0; ctx.page++; - ctx.content->pages = realloc(ctx.content->pages, (ctx.page+1) * sizeof(struct PDFPage *)); + ctx.content->pages = erealloc(ctx.content->pages, (ctx.page+1) * sizeof(struct PDFPage *)); ctx.content->pages[ctx.page] = pdf_page_new(); texts = &ctx.content->pages[ctx.page]->texts; imgs = &ctx.content->pages[ctx.page]->images; for (int i=0; i<tm; i++) { - *texts = realloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); (*texts)[ctx.text] = tmp[i]; ctx.text++; } @@ -1589,7 +1589,7 @@ pdf_content_create( if ((*left_items)->is_text) { pdf_texts_add_lyrics(*left_items, &ctx, i); } else { - *imgs = realloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); + *imgs = erealloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); (*imgs)[ctx.image] = pdf_image_new(); (*imgs)[ctx.image]->name = image_name((*left_items)->u.image); (*imgs)[ctx.image]->obj = objs_get_obj(img_objs, (*imgs)[ctx.image]->name); @@ -1619,7 +1619,7 @@ pdf_content_create( } else if (pos->line_item_index != -1 && pos->text_index == -1) { if (!(*left_items)->is_text) { - *imgs = realloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); + *imgs = erealloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); (*imgs)[ctx.image] = pdf_image_new(); (*imgs)[ctx.image]->name = image_name((*left_items)->u.image); (*imgs)[ctx.image]->obj = objs_get_obj(img_objs, (*imgs)[ctx.image]->name); @@ -1637,14 +1637,14 @@ pdf_content_create( } ctx.y -= 8.0 + ctx.biggest_font_size; if (ctx.y < MARGIN_BOTTOM) { - *texts = realloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); (*texts)[ctx.text] = NULL; - *imgs = realloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); + *imgs = erealloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); (*imgs)[ctx.image] = NULL; ctx.text = 0; ctx.image = 0; ctx.page++; - ctx.content->pages = realloc(ctx.content->pages, (ctx.page+1) * sizeof(struct PDFPage *)); + ctx.content->pages = erealloc(ctx.content->pages, (ctx.page+1) * sizeof(struct PDFPage *)); ctx.content->pages[ctx.page] = pdf_page_new(); texts = &ctx.content->pages[ctx.page]->texts; imgs = &ctx.content->pages[ctx.page]->images; @@ -1657,14 +1657,14 @@ pdf_content_create( text_above_update_positions(left_aboves, ctx.consumed_lyrics); } if ((*li)->btype == BT_PAGE) { - *texts = realloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); (*texts)[ctx.text] = NULL; - *imgs = realloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); + *imgs = erealloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); (*imgs)[ctx.image] = NULL; ctx.text = 0; ctx.image = 0; ctx.page++; - ctx.content->pages = realloc(ctx.content->pages, (ctx.page+1) * sizeof(struct PDFPage *)); + ctx.content->pages = erealloc(ctx.content->pages, (ctx.page+1) * sizeof(struct PDFPage *)); ctx.content->pages[ctx.page] = pdf_page_new(); texts = &ctx.content->pages[ctx.page]->texts; imgs = &ctx.content->pages[ctx.page]->images; @@ -1677,12 +1677,12 @@ pdf_content_create( } s++; } - *texts = realloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); + *texts = erealloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); (*texts)[ctx.text] = NULL; - *imgs = realloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); + *imgs = erealloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *)); (*imgs)[ctx.image] = NULL; ctx.page++; - ctx.content->pages = realloc(ctx.content->pages, (ctx.page+1) * sizeof(struct PDFPage *)); + ctx.content->pages = erealloc(ctx.content->pages, (ctx.page+1) * sizeof(struct PDFPage *)); ctx.content->pages[ctx.page] = NULL; *out = ctx.content; return true; diff --git a/util.c b/util.c @@ -8,6 +8,28 @@ #include <errno.h> #include "util.h" +void * +emalloc(size_t size) +{ + void *ptr = malloc(size); + if (!ptr) { + perror("malloc failed"); + exit(1); + } + return ptr; +} + +void * +erealloc(void *ptr, size_t size) +{ + void *tmp = realloc(ptr, size); + if (!tmp) { + perror("realloc failed"); + exit(1); + } + return tmp; +} + void util_log(enum LogLevel level, const char *msg, ...) { @@ -84,7 +106,7 @@ str_normalize(const char *str) char c; for (; str[i] != 0; i++) { if (str[i] == ' ' || str[i] == '/' || str[i] == '.') { - normalized = realloc(normalized, (n+1) * sizeof(char)); + normalized = erealloc(normalized, (n+1) * sizeof(char)); normalized[n] = '-'; n++; continue; @@ -92,11 +114,11 @@ str_normalize(const char *str) if (str[i] == '\'' || str[i] == ',') continue; c = (char)tolower(str[i]); - normalized = realloc(normalized, (n+1) * sizeof(char)); + normalized = erealloc(normalized, (n+1) * sizeof(char)); normalized[n] = c; n++; } - normalized = realloc(normalized, (n+1) * sizeof(char)); + normalized = erealloc(normalized, (n+1) * sizeof(char)); normalized[n] = 0; return normalized; } @@ -133,12 +155,12 @@ str_trim(const char *str) int k = 0; for (int i=0; i<len; i++) { if (i >= begin && i < len - end) { - trimmed = realloc(trimmed, (k+1) * sizeof(char)); + trimmed = erealloc(trimmed, (k+1) * sizeof(char)); trimmed[k] = str[i]; k++; } } - trimmed = realloc(trimmed, (k+1) * sizeof(char)); + trimmed = erealloc(trimmed, (k+1) * sizeof(char)); trimmed[k] = 0; return trimmed; } @@ -153,27 +175,6 @@ str_remove_leading_whitespace(const char *str) return strdup(&str[i]); } -short -str_parse_as_percent(const char *str) -{ - if (strlen(str) > 4) { - return -1; - } - long l; - char *endptr; - l = strtol(str, &endptr, 10); - if (str == endptr) { - return -1; - } - if (errno == ERANGE) { - return -1; - } - if (l < 1 || l > 100) { - return -1; - } - return (short)l; -} - enum FileType file_type(const char *path) { @@ -207,7 +208,7 @@ file_extension_replace_or_add(const char *old, const char *extension) i = 0; if (mark == -1) { old_len = (int)strlen(old); - new = malloc((old_len+2+extension_len) * sizeof(char)); + new = emalloc((old_len+2+extension_len) * sizeof(char)); while (i < old_len) { new[i] = old[i]; i++; @@ -219,7 +220,7 @@ file_extension_replace_or_add(const char *old, const char *extension) } new[++i] = 0; } else { - new = malloc((mark+2+extension_len) * sizeof(char)); + new = emalloc((mark+2+extension_len) * sizeof(char)); while (i <= mark) { new[i] = old[i]; i++; @@ -241,7 +242,7 @@ filepath_add_ending_slash_if_missing(const char *path) if (path[len-1] == '/') { return strdup(path); } else { - char *path_with_slash = malloc((len+2) * sizeof(char)); + char *path_with_slash = emalloc((len+2) * sizeof(char)); strcpy(path_with_slash, path); path_with_slash[len] = '/'; path_with_slash[len+1] = 0; @@ -273,7 +274,7 @@ filepath_dirname(const char *path) end = i; } } - dirname = malloc((end+1)* sizeof(char)); + dirname = emalloc((end+1)* sizeof(char)); i = 0; while (i<end) { dirname[i] = path[i]; @@ -302,7 +303,7 @@ struct Size * size_create(const char *str) { size_t len = strlen(str); - struct Size *size = malloc(sizeof(struct Size)); + struct Size *size = emalloc(sizeof(struct Size)); char *endptr; double d; d = strtod(str, &endptr); @@ -332,7 +333,7 @@ size_create(const char *str) struct Size * size_copy(struct Size *size) { - struct Size *copy = malloc(sizeof(struct Size)); + struct Size *copy = emalloc(sizeof(struct Size)); copy->type = size->type; copy->d = size->d; return copy; @@ -349,20 +350,3 @@ size_to_string(struct Size *size) } return str; } - -/* double -size_as_points(struct Size *size) -{ - // TODO: which font size should be used here? An image has no font. - double font_size = 14.0; - switch (size->type) { - case ST_POINT: - return size->d; - case ST_PERCENT: - return size->d / 100.0; - case ST_EM: - return size->d * font_size; - case ST_EX: - return size->d * font_size * 0.5; - } -} */ diff --git a/util.h b/util.h @@ -36,13 +36,14 @@ struct Size { double d; }; +void *emalloc(size_t size); +void *erealloc(void *ptr, size_t size); void util_log(enum LogLevel level, const char *msg, ...); bool str_starts_with(const char *str, const char *part); char *str_normalize(const char *str); char *str_trim(const char *str); char *str_remove_leading_whitespace(const char *str); -short str_parse_as_percent(const char *str); enum FileType file_type(const char *path); char *file_extension_replace_or_add(const char *old, const char *extension);