lorid

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

commit 75cd6db74b20fedf630def9b014d46d45dc5dd5d
parent 281f02fdb2a6f942fe42586869b684d10120228a
Author: nibo <nibo@relim.de>
Date:   Fri,  3 Jan 2025 08:18:51 +0100

Replace most while loops with for loops

Refactor what came up

Diffstat:
Mchord_diagram.c | 28++++++++++------------------
Mchordpro.c | 278++++++++++++++++++++++++++++++++-----------------------------------------------
Mconfig.c | 65+++++++++++++++++++++++++++++------------------------------------
Mconfig.h | 2+-
Mfontconfig.c | 18++----------------
Mout_pdf.c | 275++++++++++++++++++++++++++++---------------------------------------------------
Mutil.c | 59++++++++++++++++++++++++-----------------------------------
7 files changed, 274 insertions(+), 451 deletions(-)

diff --git a/chord_diagram.c b/chord_diagram.c @@ -307,10 +307,8 @@ string_diagram_new(void) static int string_diagram_string_count(struct StringDiagram *d) { - int i = 0; - while (d->frets[i] != -2 && i < 12) { - i++; - } + int i; + for (i = 0; d->frets[i] != -2 && i < 12; i++); return i; } @@ -379,10 +377,8 @@ string_diagram_is_valid(struct StringDiagram *d) static size_t string_diagram_fret_count(struct StringDiagram *d) { - int i = 0; - while (d->frets[i] != -2 && i < 12) { - i++; - } + int i; + for (i = 0; d->frets[i] != -2 && i < 12; i++); return i; } @@ -589,10 +585,9 @@ chord_diagrams_free(struct ChordDiagram **diagrams) if (!diagrams) { return; } - struct ChordDiagram **d = diagrams; - while (*d) { + struct ChordDiagram **d; + for (d = diagrams; *d; d++) { chord_diagram_free(*d); - d++; } free(diagrams); } @@ -637,14 +632,13 @@ chord_diagrams_create( { struct ChordDiagram **diagrams = NULL; struct ChordDiagram **cd; - struct ChoChord **c = *chords; + struct ChoChord **c; int d = 0; size_t i; switch (config->output->diagram->instrument) { case INS_GUITAR: - while (*c) { - cd = custom_diagrams; - while (*cd) { + for (c = *chords; *c; c++) { + for (cd = custom_diagrams; *cd; cd++) { if ( (*cd)->is_string_instrument && string_diagram_fret_count((*cd)->u.sd) == 6 && @@ -659,7 +653,6 @@ chord_diagrams_create( d++; goto NEXT_CHORD; } - cd++; } for (i = 0; i<LENGTH(guitar_diagrams); i++) { if (!strcmp((*c)->name, guitar_diagrams[i].name)) { @@ -671,8 +664,7 @@ chord_diagrams_create( d++; } } - NEXT_CHORD: - c++; + NEXT_CHORD: ; } break; case INS_KEYBOARD: diff --git a/chordpro.c b/chordpro.c @@ -576,12 +576,11 @@ cho_fonts_free(struct Font **fonts) if (!fonts) { return; } - struct Font **start = fonts; - while (*fonts) { - cho_font_free(*fonts); - fonts++; + struct Font **f; + for (f = fonts; *f; f++) { + cho_font_free(*f); } - free(start); + free(fonts); } enum FontFamily @@ -925,16 +924,17 @@ cho_style_font_desc_parse(const char *str) char **words = emalloc(sizeof(char *)); int w = 0; words[w] = NULL; - int i = 0; int k = 0; - while (str[i] != 0) { + int i; + for (i = 0; str[i]; i++) { if (str[i] == ' ') { words[w] = erealloc(words[w], (k+1) * sizeof(char)); words[w][k] = 0; - if (strlen(words[w]) == 0) + if (strlen(words[w]) == 0) { free(words[w]); - else + } else { w++; + } k = 0; words = erealloc(words, (w+1) * sizeof(char *)); words[w] = NULL; @@ -943,38 +943,41 @@ cho_style_font_desc_parse(const char *str) words[w][k] = str[i]; k++; } - i++; } words[w] = erealloc(words[w], (k+1) * sizeof(char)); words[w][k] = 0; w++; words = erealloc(words, (w+1) * sizeof(char *)); words[w] = NULL; - w = 0; int stop_at = EMPTY_INT; - while (words[w] != NULL) { + for (w = 0; words[w]; w++) { if (strcasecmp(words[w], "italic") == 0) { font->style = FS_ITALIC; - if (stop_at == EMPTY_INT) + if (stop_at == EMPTY_INT) { stop_at = w; + } } else if (strcasecmp(words[w], "bold") == 0) { font->weight = FW_BOLD; - if (stop_at == EMPTY_INT) + if (stop_at == EMPTY_INT) { stop_at = w; + } } else if (strcasecmp(words[w], "oblique") == 0) { font->style = FS_OBLIQUE; - if (stop_at == EMPTY_INT) + if (stop_at == EMPTY_INT) { stop_at = w; + } // TODO: Is that smart? } else if (strcasecmp(words[w], "regular") == 0) { font->weight = FW_REGULAR; - if (stop_at == EMPTY_INT) + if (stop_at == EMPTY_INT) { stop_at = w; + } // TODO: Is that smart? } else if (strcasecmp(words[w], "normal") == 0) { font->style = FS_ROMAN; - if (stop_at == EMPTY_INT) + if (stop_at == EMPTY_INT) { stop_at = w; + } /* Commented because the family name sometimes contains 'sans' or 'serif' */ /* } else if (strcasecmp(words[w], "sans") == 0) { font->family = FF_SANS; @@ -986,42 +989,37 @@ cho_style_font_desc_parse(const char *str) stop_at = w; */ } else if (strcasecmp(words[w], "monospace") == 0) { font->family = FF_MONOSPACE; - if (stop_at == EMPTY_INT) + if (stop_at == EMPTY_INT) { stop_at = w; + } } else { size = strtod(words[w], NULL); - if (size == 0.0) - goto SKIP; + if (size == 0.0) { + continue; + } font->size = size; - if (stop_at == EMPTY_INT) + if (stop_at == EMPTY_INT) { stop_at = w; + } } - SKIP: - w++; } if (stop_at == EMPTY_INT) { stop_at = w; } - k = 0; int n = 0; - for (int i=0; i<stop_at; i++) { - while (words[i][k] != 0) { + for (i = 0; i<stop_at; i++) { + for (k = 0; words[i][k]; n++, k++) { font->name = erealloc(font->name, (n+1) * sizeof(char)); font->name[n] = words[i][k]; - n++; - k++; } font->name = erealloc(font->name, (n+1) * sizeof(char)); font->name[n] = ' '; n++; - k = 0; } n--; font->name[n] = 0; - w = 0; - while (words[w] != NULL) { + for (w = 0; words[w]; w++) { free(words[w]); - w++; } free(words); return font; @@ -1039,8 +1037,8 @@ cho_style_parse(const char *tag_name, struct Attr **attrs, struct ChoStyle *inhe else style = cho_style_new_default(); if (!strcmp(tag_name, "span")) { - int a = 0; - while (attrs[a] != NULL) { + int a; + for (a = 0; attrs[a]; a++) { if (!strcmp(attrs[a]->name, "font_desc")) { font = cho_style_font_desc_parse(attrs[a]->value); if (font) { @@ -1271,7 +1269,6 @@ cho_style_parse(const char *tag_name, struct Attr **attrs, struct ChoStyle *inhe cho_log(LOG_ERR, "Invalid attribute '%s'.", attrs[a]->name); return NULL; } - a++; } } else if (!strcmp(tag_name, "b")) { style->font->weight = FW_BOLD; @@ -1410,12 +1407,11 @@ cho_tag_attrs_free(struct Attr **attrs) if (!attrs) { return; } - struct Attr **start = attrs; - while (*attrs) { - cho_tag_attr_free(*attrs); - attrs++; + struct Attr **a; + for (a = attrs; *a; a++) { + cho_tag_attr_free(*a); } - free(start); + free(attrs); } static struct Tag * @@ -1439,21 +1435,21 @@ cho_tag_free(struct Tag *tag) if (tag->style) { cho_style_free(tag->style); } - if (tag->attrs) + if (tag->attrs) { cho_tag_attrs_free(tag->attrs); + } free(tag); } static bool cho_tag_close_last_unclosed(const char *tag_name, struct Tag **tags, int last_index) { - int i = last_index; - while (i >= 0) { + int i; + for (i = last_index; i >= 0; i--) { if (!strcmp(tags[i]->name, tag_name) && !tags[i]->is_closed) { tags[i]->is_closed = true; return true; } - i--; } cho_log(LOG_ERR, "Didn't find a start tag for the end tag '%s'.", tag_name); return false; @@ -1462,12 +1458,11 @@ cho_tag_close_last_unclosed(const char *tag_name, struct Tag **tags, int last_in static struct ChoStyle * cho_tag_style_inherit(struct Tag **tags, int prev_index) { - int i = prev_index; - while (i >= 0) { + int i; + for (i = prev_index; i >= 0; i--) { if (!tags[i]->is_closed) { return tags[i]->style; } - i--; } /* Doesn't mean there is an error. @@ -1517,12 +1512,12 @@ static struct ChoMetadata * cho_metadata_split(const char *directive_value) { struct ChoMetadata *meta = cho_metadata_new(); + bool is_name = true; char *value = str_remove_leading_whitespace(directive_value); - int i = 0; int n = 0; int v = 0; - bool is_name = true; - while (value[i] != 0) { + int i; + for (i = 0; value[i]; i++) { if (value[i] == ' ') { meta->name = erealloc(meta->name, (n+1) * sizeof(char)); meta->name[n] = 0; @@ -1538,7 +1533,6 @@ cho_metadata_split(const char *directive_value) v++; } } - i++; } meta->value = erealloc(meta->value, (v+1) * sizeof(char)); meta->value[v] = 0; @@ -1615,7 +1609,7 @@ transposition_calc_chord_root(int index, enum NoteType type) } } } else { - for (i=transpose; i<0; i++) { + for (i = transpose; i<0; i++) { switch (note_type) { case NT_NOTE: switch (new_index) { @@ -1718,8 +1712,8 @@ cho_chords_has(struct ChoChord **chords, struct ChoChord *chord) if (!chords) { return false; } - struct ChoChord **c = chords; - while (*c) { + struct ChoChord **c; + for (c = chords; *c; c++) { if ( !str_compare((*c)->name, chord->name) && !str_compare((*c)->root, chord->root) && @@ -1729,7 +1723,6 @@ cho_chords_has(struct ChoChord **chords, struct ChoChord *chord) ) { return true; } - c++; } return false; } @@ -1748,10 +1741,8 @@ cho_chords_len(struct ChoChord **chords) if (!chords) { return 0; } - int i = 0; - while (chords[i]) { - i++; - } + int i; + for (i = 0; chords[i]; i++); return i; } @@ -1760,11 +1751,8 @@ cho_chords_add(struct ChoChord ***chords, struct ChoChord *chord) { int i = 0; if (*chords) { - struct ChoChord **c = *chords; - while (*c) { - i++; - c++; - } + struct ChoChord **c; + for (c = *chords; *c; c++, i++); } *chords = erealloc(*chords, (i+2) * sizeof(struct ChoChord *)); (*chords)[i] = cho_chord_copy(chord); @@ -1777,10 +1765,9 @@ cho_chords_free(struct ChoChord **chords) if (!chords) { return; } - struct ChoChord **c = chords; - while (*c) { + struct ChoChord **c; + for (c = chords; *c; c++) { cho_chord_free(*c); - c++; } free(chords); } @@ -2031,40 +2018,18 @@ cho_image_free(struct ChoImage *image) } free(image->id); free(image->src); - if (image->width) { - free(image->width); - } - if (image->height) { - free(image->height); - } - if (image->width_scale) { - free(image->width_scale); - } - if (image->height_scale) { - free(image->height_scale); - } - if (image->spread_space) { - free(image->spread_space); - } + free(image->width); + free(image->height); + free(image->width_scale); + free(image->height_scale); + free(image->spread_space); free(image->href); - if (image->x) { - free(image->x); - } - if (image->y) { - free(image->y); - } - if (image->dx) { - free(image->dx); - } - if (image->dy) { - free(image->dy); - } - if (image->w) { - free(image->w); - } - if (image->h) { - free(image->h); - } + free(image->x); + free(image->y); + free(image->dx); + free(image->dy); + free(image->w); + free(image->h); free(image); } @@ -2362,7 +2327,7 @@ cho_image_directive_parse(const char *str) memset(value, 0, sizeof(value)); int option_count = 0; int i; - for (i = 0; str[i] != 0; i++) { + for (i = 0; str[i]; i++) { c = str[i]; switch (state) { case OS_NAME: @@ -2680,7 +2645,7 @@ cho_chord_diagram_parse(const char *str) int finger_count = 0; int8_t number = -2; long l; - for (i = 0; str[i] != 0; i++) { + for (i = 0; str[i]; i++) { c = str[i]; // printf("c '%c' state '%d'\n", c, state); switch (state) { @@ -2935,9 +2900,8 @@ cho_text_copy(struct ChoText *text) int cho_text_above_count(struct ChoLineItemAbove **text_above) { - int i = 0; - while (text_above[i] != NULL) - i++; + int i; + for (i = 0; text_above[i]; i++); return i; } @@ -3024,18 +2988,16 @@ cho_line_free(struct ChoLine *line) if (!line) { return; } - struct ChoLineItem **start_items = line->items; - while (*line->items) { - cho_line_item_free(*line->items); - line->items++; + struct ChoLineItem **it; + for (it = line->items; *it; it++) { + cho_line_item_free(*it); } - struct ChoLineItemAbove **start_text_above = line->text_above; - while (*line->text_above) { - cho_text_above_free(*line->text_above); - line->text_above++; + struct ChoLineItemAbove **above; + for (above = line->text_above; *above; above++) { + cho_text_above_free(*above); } - free(start_items); - free(start_text_above); + free(line->items); + free(line->text_above); free(line); } @@ -3046,13 +3008,12 @@ cho_line_compute_text_above_position(struct ChoLine *line, int ly, int te) if (ly == 0) { return te; } - ly--; size_t lyrics_len = 0; - while (ly >= 0) { - if (line->items[ly]->is_text) { - lyrics_len += strlen(line->items[ly]->u.text->text); + int i; + for (i = ly-1; i >= 0; i--) { + if (line->items[i]->is_text) { + lyrics_len += strlen(line->items[i]->u.text->text); } - ly--; } return lyrics_len + te; } @@ -3076,12 +3037,11 @@ cho_section_free(struct ChoSection *section) if (section->label) { cho_text_free(section->label); } - struct ChoLine **start_lines = section->lines; - while (*section->lines) { - cho_line_free(*section->lines); - section->lines++; + struct ChoLine **li; + for (li = section->lines; *li; li++) { + cho_line_free(*li); } - free(start_lines); + free(section->lines); free(section); } @@ -3132,9 +3092,8 @@ cho_song_new(void) int cho_song_count(struct ChoSong **songs) { - int i = 0; - while (songs[i] != NULL) - i++; + int i; + for (i = 0; songs[i]; i++); return i; } @@ -3142,12 +3101,10 @@ const char * cho_song_get_title(struct ChoSong *song) { struct ChoMetadata **m; - m = song->metadata; - while (*m) { + for (m = song->metadata; *m; m++) { if (!strcmp((*m)->name, "title")) { return (*m)->value; } - m++; } return NULL; } @@ -3166,24 +3123,21 @@ cho_song_free(struct ChoSong *song) if (!song) { return; } - struct ChoMetadata **start_meta = song->metadata; - struct ChoSection **start_section = song->sections; - struct ChordDiagram **start_diagram = song->diagrams; - while (*song->metadata) { - cho_metadata_free(*song->metadata); - song->metadata++; + struct ChoMetadata **m; + struct ChoSection **se; + struct ChordDiagram **dia; + for (m = song->metadata; *m; m++) { + cho_metadata_free(*m); } - while (*song->sections) { - cho_section_free(*song->sections); - song->sections++; + for (se = song->sections; *se; se++) { + cho_section_free(*se); } - while (*song->diagrams) { - chord_diagram_free(*song->diagrams); - song->diagrams++; + for (dia = song->diagrams; *dia; dia++) { + chord_diagram_free(*dia); } - free(start_diagram); - free(start_meta); - free(start_section); + free(song->metadata); + free(song->sections); + free(song->diagrams); free(song); } @@ -3193,12 +3147,11 @@ cho_songs_free(struct ChoSong **songs) if (!songs) { return; } - struct ChoSong **start_song = songs; - while (*songs) { - cho_song_free(*songs); - songs++; + struct ChoSong **so; + for (so = songs; *so; so++) { + cho_song_free(*so); } - free(start_song); + free(songs); } static struct ChoSection * @@ -3593,7 +3546,7 @@ cho_directive_label_parse(const char *directive_name, const char *str) memset(name, 0, sizeof(name)); memset(value, 0, sizeof(value)); int i; - for (i = 0; str[i] != 0; i++) { + for (i = 0; str[i]; i++) { c = str[i]; switch (state) { case OS_NAME: @@ -4659,6 +4612,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) if (tags[ta]->attrs[at-1]->value) { cho_tag_attr_free(tags[ta]->attrs[at]); tags[ta]->attrs[at] = NULL; + atn = 0; if (!strcmp(tags[ta]->name, "img")) { cho_text_free((*lines)[li]->items[ly]->u.text); (*lines)[li]->items[ly]->is_text = false; @@ -4903,22 +4857,19 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config) void cho_debug_songs_print(struct ChoSong **songs) { - struct ChoSong **s = songs; + struct ChoSong **s; struct ChoSection **se; struct ChoLine **li; struct ChoLineItem **it; struct ChoLineItemAbove **above; char *name; - while (*s) { - se = (*s)->sections; - while (*se) { + for (s = songs; *s; s++) { + for (se = (*s)->sections; *se; se++) { printf("## Section\n"); - li = (*se)->lines; - while (*li) { + for (li = (*se)->lines; *li; li++) { printf("## Line\n"); - above = (*li)->text_above; it = (*li)->items; - while (*above) { + for (above = (*li)->text_above; *above; above++) { if ((*above)->is_chord) { name = cho_chord_name_generate((*above)->u.chord); printf("chord: %s\n", name); @@ -4926,21 +4877,16 @@ cho_debug_songs_print(struct ChoSong **songs) } else { printf("annotation: %s\n", (*above)->u.annot->text); } - above++; } - while (*it) { + for (it = (*li)->items; *it; it++) { if ((*it)->is_text) { printf("text: %s\n", (*it)->u.text->text); } else { printf("image: %s\n", (*it)->u.image->src); } - it++; } - li++; } - se++; } - s++; } } #endif /* DEBUG */ diff --git a/config.c b/config.c @@ -130,14 +130,13 @@ config_output_style_free(struct OutputStyle *item) } struct OutputStyle * -config_output_style_get(struct OutputStyle **items, const char *name) +config_output_style_get(struct OutputStyle **styles, const char *name) { - int i = 0; - while (items[i] != NULL) { - if (!strcmp(items[i]->name, name)) { - return items[i]; + struct OutputStyle **os; + for (os = styles; *os; os++) { + if (!strcmp((*os)->name, name)) { + return *os; } - i++; } return NULL; } @@ -213,12 +212,11 @@ config_note_free(struct Note *note) static void config_notes_free(struct Note **notes) { - struct Note **start = notes; - while (*notes) { - config_note_free(*notes); - notes++; + struct Note **no; + for (no = notes; *no; no++) { + config_note_free(*no); } - free(start); + free(notes); } static struct Note ** @@ -259,7 +257,7 @@ config_notes_new_default(enum NamingSystem system) notes_default[i]->flat = strdup(notes[i].flat); } } - notes_default[7] = NULL; + notes_default[7] = NULL; // TODO: This is probably needless return notes_default; } @@ -449,10 +447,9 @@ config_print_default(void) printf("label = \"Chorus\"\n"); printf("quote = false\n\n"); printf("[output.styles]\n"); - int i = 0; - while (config->output->styles[i] != NULL) { - config_output_style_print_as_toml(config->output->styles[i]); - i++; + struct OutputStyle **os; + for (os = config->output->styles; *os; os++) { + config_output_style_print_as_toml(*os); } printf("[parser]\n"); printf("[parser.chords]\n"); @@ -642,11 +639,10 @@ config_load_style(struct ChoStyle *style, toml_table_t *table, const char *key_n static bool config_is_style(const char *str) { - int i = 0; - while (g_valid_styles[i] != NULL) { - if (!strcmp(g_valid_styles[i], str)) + const char **s; + for (s = g_valid_styles; *s; s++) { + if (!strcmp(*s, str)) return true; - i++; } return false; } @@ -751,7 +747,8 @@ config_load(const char *filepath) const char *key_name; toml_table_t *key; struct OutputStyle *item; - for (int i=0; i<toml_table_len(styles); i++) { + int i; + for (i = 0; i<toml_table_len(styles); i++) { key_name = toml_table_key(styles, i, &unused); if (config_is_style(key_name)) { key = toml_table_table(styles, key_name); @@ -819,31 +816,27 @@ config_load(const char *filepath) void config_free(struct Config *config) { - struct OutputStyle **start_items = config->output->styles; - struct Note **start_notes = config->output->notes; + struct OutputStyle **os; + struct Note **no; free(config->output->toc->title); free(config->output->toc); free(config->output->chorus->label); free(config->output->chorus); - while (*config->output->styles) { - config_output_style_free(*config->output->styles); - config->output->styles++; + for (os = config->output->styles; *os; os++) { + config_output_style_free(*os); } - free(start_items); - while (*config->output->notes) { - config_note_free(*config->output->notes); - config->output->notes++; + free(config->output->styles); + for (no = config->output->notes; *no; no++) { + config_note_free(*no); } - free(start_notes); + free(config->output->notes); free(config->output->diagram); free(config->output); free(config->parser->chords); - start_notes = config->parser->notes; - while (*config->parser->notes) { - config_note_free(*config->parser->notes); - config->parser->notes++; + for (no = config->parser->notes; *no; no++) { + config_note_free(*no); } - free(start_notes); + free(config->parser->notes); free(config->parser); free(config); } diff --git a/config.h b/config.h @@ -89,6 +89,6 @@ struct Config { struct Config *config_load(const char *filepath); void config_free(struct Config *config); void config_print_default(void); -struct OutputStyle *config_output_style_get(struct OutputStyle **items, const char *name); +struct OutputStyle *config_output_style_get(struct OutputStyle **styles, const char *name); #endif /* _CONFIG_H_ */ diff --git a/fontconfig.c b/fontconfig.c @@ -10,12 +10,11 @@ static bool file_extension_is_ttc(const char *filepath) { int mark = -1; - int i = 0; - while (filepath[i] != 0) { + int i; + for (i = 0; filepath[i]; i++) { if (filepath[i] == '.') { mark = i; } - i++; } if (!strcmp(&filepath[mark+1], "ttc")) return true; @@ -115,16 +114,3 @@ fontconfig_fontpath_find(struct Font *font, enum FontType font_type) FcFontSetDestroy(set); return filepath; } - -/* int main(int argc, char *argv[]) -{ - struct Font font = { - .name = "LiberationMono", - .family = FF_NORMAL, - .style = FS_ITALIC, - .weight = FW_BOLD, - .size = 0.0 - }; - fontconfig_fontpath_find(&font); - return 0; -} */ diff --git a/out_pdf.c b/out_pdf.c @@ -24,11 +24,11 @@ static pdfio_file_t *g_pdf_file = NULL; pdfio_obj_t * out_pdf_fnt_obj_get_by_name(const char *name) { - int i = 0; - while (g_fonts[i] != NULL) { - if (!strcmp(g_fonts[i]->name, name)) + int i; + for (i = 0; g_fonts[i]; i++) { + if (!strcmp(g_fonts[i]->name, name)) { return g_fonts[i]->value; - i++; + } } return NULL; } @@ -36,22 +36,22 @@ out_pdf_fnt_obj_get_by_name(const char *name) static bool out_pdf_font_add_if_not_in(struct Font *font, struct Font ***array) { - int a = 0; if (!*array) { *array = erealloc(*array, 2 * sizeof(struct Font *)); (*array)[0] = font; (*array)[1] = NULL; return true; } else { - while ((*array)[a] != NULL) { + int a; + for (a = 0; (*array)[a]; a++) { if ( !strcmp((*array)[a]->name, font->name) && (*array)[a]->family == font->family && (*array)[a]->style == font->style && (*array)[a]->weight == font->weight - ) + ) { return false; - a++; + } } *array = erealloc(*array, (a+2) * sizeof(struct Font *)); (*array)[a] = font; @@ -66,10 +66,10 @@ out_pdf_add_fonts(struct Font *font, struct Font ***fonts) bool added = false; char part[100]; char *trimmed = NULL; - int i = 0; int p = 0; + int i; struct Font *new_font = NULL; - while (font->name[i] != 0) { + for (i = 0; font->name[i]; i++) { if (font->name[i] == ',') { part[p] = 0; trimmed = str_trim(part); @@ -90,7 +90,6 @@ out_pdf_add_fonts(struct Font *font, struct Font ***fonts) part[p] = font->name[i]; p++; } - i++; } part[p] = 0; trimmed = str_trim(part); @@ -110,53 +109,44 @@ out_pdf_font_get_all(struct ChoSong **songs, struct Config *config) { struct Font **fonts = NULL; struct Font *font; - bool added = false; - int i = 0; - while (config->output->styles[i] != NULL) { + bool added; + int i; + for (i = 0; config->output->styles[i]; i++) { font = cho_font_copy(config->output->styles[i]->style->font); added = out_pdf_font_add_if_not_in(font, &fonts); - if (!added) + if (!added) { cho_font_free(font); - i++; + } } - int so = 0; - int se = 0; - int li = 0; - int ly = 0; - int ch = 0; + struct ChoSong **so; + struct ChoSection **se; + struct ChoLine **li; + struct ChoLineItemAbove **above; + struct ChoLineItem **it; struct ChoStyle *style; - while (songs[so] != NULL) { - while (songs[so]->sections[se] != NULL) { - while (songs[so]->sections[se]->lines[li]) { - while (songs[so]->sections[se]->lines[li]->text_above[ch] != NULL) { - if (songs[so]->sections[se]->lines[li]->text_above[ch]->is_chord) { - style = songs[so]->sections[se]->lines[li]->text_above[ch]->u.chord->style; + for (so = songs; *so; so++) { + for (se = (*so)->sections; *se; se++) { + for (li = (*se)->lines; *li; li++) { + for (above = (*li)->text_above; *above; above++) { + if ((*above)->is_chord) { + style = (*above)->u.chord->style; } else { - style = songs[so]->sections[se]->lines[li]->text_above[ch]->u.annot->style; + style = (*above)->u.annot->style; } if (style->font->name) { out_pdf_add_fonts(style->font, &fonts); } - ch++; } - ch = 0; - while (songs[so]->sections[se]->lines[li]->items[ly] != NULL) { - if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) { - style = songs[so]->sections[se]->lines[li]->items[ly]->u.text->style; + for (it = (*li)->items; *it; it++) { + if ((*it)->is_text) { + style = (*it)->u.text->style; if (style->font->name) { out_pdf_add_fonts(style->font, &fonts); } } - ly++; } - ly = 0; - li++; } - li = 0; - se++; } - se = 0; - so++; } return fonts; } @@ -253,36 +243,29 @@ out_pdf_fnt_name_create(struct Font *font) const char *style = cho_font_style_to_config_string(font->style); const char *weight = cho_font_weight_to_config_string(font->weight); int n = 0; - int i = 0; + int i; char *fnt_name = emalloc((strlen(name) + strlen(family) + strlen(style) + strlen(weight) + 4) * sizeof(char)); - while (name[i] != 0) { + for (i = 0; name[i]; i++) { fnt_name[n] = name[i]; n++; - i++; } - i = 0; fnt_name[n] = '-'; n++; - while (family[i] != 0) { + for (i = 0; family[i]; i++) { fnt_name[n] = family[i]; n++; - i++; } - i = 0; fnt_name[n] = '-'; n++; - while (style[i] != 0) { + for (i = 0; style[i]; i++) { fnt_name[n] = style[i]; n++; - i++; } - i = 0; fnt_name[n] = '-'; n++; - while (weight[i] != 0) { + for (i = 0; weight[i]; i++) { fnt_name[n] = weight[i]; n++; - i++; } fnt_name[n] = 0; free(name); @@ -611,7 +594,7 @@ annot_url_link_add(struct PDFContext *ctx, struct ChoStyle *style, double width) static bool out_pdf_set_title(pdfio_file_t *pdf, struct ChoSong **songs) { - // INFO: Set pdf title only if single song exist + // INFO: Set pdf title only if a single song exist if (songs[0] && !songs[1]) { const char *title; title = cho_metadata_get(songs[0]->metadata, "title"); @@ -689,38 +672,23 @@ objs_free(struct Obj **objs) return; } struct Obj **start = objs; - while (*objs) { + for (; *objs; objs++) { obj_free(*objs); - objs++; } free(start); } -static bool -objs_has(struct Obj **objs, const char *name) -{ - if (!objs) { - return false; - } - struct Obj **o = objs; - while (*o) { - if (!strcmp((*o)->name, name)) { - return true; - } - o++; - } - return false; -} - static pdfio_obj_t * objs_get_obj(struct Obj **objs, const char *name) { - struct Obj **o = objs; - while (*o) { + if (!objs) { + return NULL; + } + struct Obj **o; + for (o = objs; *o; o++) { if (!strcmp((*o)->name, name)) { return (*o)->value; } - o++; } return NULL; } @@ -728,14 +696,13 @@ objs_get_obj(struct Obj **objs, const char *name) static void objs_add_obj(struct Obj ***array, struct Obj *obj) { - int a = 0; if (!*array) { *array = erealloc(*array, 2 * sizeof(struct Obj *)); (*array)[0] = obj; (*array)[1] = NULL; } else { - while ((*array)[a]) - a++; + int a; + for (a = 0; (*array)[a]; a++); *array = erealloc(*array, (a+2) * sizeof(struct Obj *)); (*array)[a] = obj; (*array)[a+1] = NULL; @@ -809,37 +776,34 @@ image_name(struct ChoImage *image) static bool out_pdf_load_images(struct Obj ***images, pdfio_file_t *file, struct ChoSong **songs) { - struct ChoSong **s = songs; + struct ChoSong **s; struct ChoSection **se; struct ChoLine **li; - struct ChoLineItem **items; + struct ChoLineItem **it; int i = 0; char filepath[PATH_MAX]; char *name; char *image_filepath; - while (*s) { - se = (*s)->sections; - while (*se) { - li = (*se)->lines; - while (*li) { - items = (*li)->items; - while (*items) { - if (!(*items)->is_text) { + for (s = songs; *s; s++) { + for (se = (*s)->sections; *se; se++) { + for (li = (*se)->lines; *li; li++) { + for (it = (*li)->items; *it; it++) { + if (!(*it)->is_text) { memset(filepath, 0, PATH_MAX); strcpy((char *)&filepath, g_cho_dirpath); strcat((char *)&filepath, "/"); - strcat((char *)&filepath, (*items)->u.image->src); - name = image_name((*items)->u.image); + strcat((char *)&filepath, (*it)->u.image->src); + name = image_name((*it)->u.image); if (!name) { LOG_DEBUG("image_name failed."); return false; } - if (!objs_has(*images, name)) { + if (!objs_get_obj(*images, name)) { *images = erealloc(*images, (i+2) * sizeof(struct Obj *)); (*images)[i] = obj_new(); (*images)[i]->name = strdup(name); - if (strchr((*items)->u.image->src, '/')) { - image_filepath = (*items)->u.image->src; + if (strchr((*it)->u.image->src, '/')) { + image_filepath = (*it)->u.image->src; } else { image_filepath = (char *)&filepath; } @@ -853,13 +817,9 @@ out_pdf_load_images(struct Obj ***images, pdfio_file_t *file, struct ChoSong **s free(name); i++; } - items++; } - li++; } - se++; } - s++; } return true; } @@ -885,22 +845,16 @@ out_pdf_get_chords(struct ChoSong *song, struct ChoChord ***chords) struct ChoSection **se; struct ChoLine **li; struct ChoLineItemAbove **above; - se = song->sections; - while (*se) { - li = (*se)->lines; - while (*li) { - above = (*li)->text_above; - while (*above) { + for (se = song->sections; *se; se++) { + for (li = (*se)->lines; *li; li++) { + for (above = (*li)->text_above; *above; above++) { if ((*above)->is_chord) { if (!cho_chords_has(*chords, (*above)->u.chord)) { cho_chords_add(chords, (*above)->u.chord); } } - above++; } - li++; } - se++; } return true; } @@ -1049,27 +1003,21 @@ pdf_page_free(struct PDFPage *page) struct PDFText **t; struct PDFImage **i; struct ChordDiagram **d; - t = page->texts; - if (t) { - while (*t) { + if ((t = page->texts)) { + for (; *t; t++) { pdf_text_free(*t); - t++; } free(page->texts); } - i = page->images; - if (i) { - while (*i) { + if ((i = page->images)) { + for (; *i; i++) { pdf_image_free(*i); - i++; } free(page->images); } - d = page->diagrams; - if (d) { - while (*d) { + if ((d = page->diagrams)) { + for (; *d; d++) { chord_diagram_free(*d); - d++; } free(page->diagrams); } @@ -1093,17 +1041,13 @@ pdf_content_free(struct PDFContent *content) } struct PDFPage **p; struct TocEntry **toc; - p = content->pages; - while (*p) { + for (p = content->pages; *p; p++) { pdf_page_free(*p); - p++; } free(content->pages); - toc = content->toc; - if (toc) { - while (*toc) { + if ((toc = content->toc)) { + for (; *toc; toc++) { toc_entry_free(*toc); - toc++; } free(content->toc); } @@ -1116,10 +1060,9 @@ spaces_free(struct SpaceNeeded **spaces) if (!spaces) { return; } - struct SpaceNeeded **s = spaces; - while (*s) { + struct SpaceNeeded **s; + for (s = spaces; *s; s++) { free(*s); - s++; } free(spaces); } @@ -1185,19 +1128,18 @@ static double item_width(struct ChoLineItem *item, int i, struct SpaceNeeded **spaces, struct Obj **img_objs) { double width; - struct SpaceNeeded **s = spaces; if (item->is_text) { width = text_width(item->u.text->text, item->u.text->style); if (width == ERROR) { LOG_DEBUG("text_width failed."); return ERROR; } - if (s) { - while (*s) { + struct SpaceNeeded **s; + if ((s = spaces)) { + for (; *s; s++) { if ((*s)->line_item_index == i) { width += (*s)->amount; } - s++; } } } else { @@ -1267,7 +1209,7 @@ items_find_position_to_break_line( static double images_find_biggest_height(struct ChoLineItem **left_items, int line_item_index, struct Obj **img_objs) { - struct ChoLineItem **items = left_items; + struct ChoLineItem **it; char *name; pdfio_obj_t *obj; int i = 0; @@ -1277,9 +1219,9 @@ images_find_biggest_height(struct ChoLineItem **left_items, int line_item_index, if (end == -1) { end = 10000; } - while (*items && i <= end) { - if (!(*items)->is_text) { - name = image_name((*items)->u.image); + for (it = left_items; *it && i <= end; it++, i++) { + if (!(*it)->is_text) { + name = image_name((*it)->u.image); if (!name) { LOG_DEBUG("image_name failed."); return ERROR; @@ -1289,14 +1231,12 @@ images_find_biggest_height(struct ChoLineItem **left_items, int line_item_index, LOG_DEBUG("objs_get_obj failed."); return ERROR; } - height = image_height((*items)->u.image, obj); + height = image_height((*it)->u.image, obj); if (height > biggest) { biggest = height; } free(name); } - items++; - i++; } return biggest; } @@ -1343,9 +1283,8 @@ static void text_above_update_positions(struct ChoLineItemAbove **aboves, size_t consumed_lyrics) { struct ChoLineItemAbove **a = aboves; - while (*a) { + for (a = aboves; *a; a++) { (*a)->position -= consumed_lyrics + 1; // why plus one? - a++; } } @@ -1393,8 +1332,7 @@ pdf_texts_add_lyrics( (*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) { + for (sp = ctx->spaces; *sp; sp++) { if ((*sp)->line_item_index == i && (*sp)->text_index == c) { // TODO: This code splits multibyte characters which leads to invalid UTF-8 (*texts)[ctx->text]->text = erealloc((*texts)[ctx->text]->text, (t+1) * sizeof(char)); @@ -1421,7 +1359,6 @@ pdf_texts_add_lyrics( *texts = erealloc(*texts, (ctx->text+1) * sizeof(struct PDFText *)); (*texts)[ctx->text] = pdf_text_new(); } - sp++; } } (*texts)[ctx->text]->text = erealloc((*texts)[ctx->text]->text, (t+1) * sizeof(char)); @@ -1566,8 +1503,7 @@ pdf_toc_page_count( double y = MEDIABOX_HEIGHT - MARGIN_TOP; double width; struct TocEntry **toc; - toc = entries; - while (*toc) { + for (toc = entries; *toc; toc++) { if (y < MARGIN_BOTTOM) { y = MEDIABOX_HEIGHT - MARGIN_TOP; page++; @@ -1602,7 +1538,6 @@ pdf_toc_page_count( } else { y -= 8.0 + style->font->size; } - toc++; } return page + 1; } @@ -1745,7 +1680,7 @@ pdf_toc_create( return false; } cho_style_free(toc_title_style); */ - while (*toc) { + for (; *toc; toc++) { if (!pdf_texts_add_toc_entry(&ctx, *toc, toc_style->style, max_title_width, toc_page_count)) { LOG_DEBUG("pdf_texts_add_toc_entry failed."); return false; @@ -1772,7 +1707,6 @@ pdf_toc_create( LOG_DEBUG("pdf_texts_add_text failed."); return false; } */ - toc++; } *texts = erealloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); (*texts)[ctx.text] = NULL; @@ -1841,8 +1775,7 @@ pdf_content_create( } cho_chords_free(chords); } - m = songs[s]->metadata; - while (*m) { + for (m = songs[s]->metadata; *m; m++) { if (!strcmp((*m)->name, "title")) { ctx.content->toc = erealloc(ctx.content->toc, (ctx.toc_entry+1) * sizeof(struct TocEntry *)); ctx.content->toc[ctx.toc_entry] = emalloc(sizeof(struct TocEntry)); @@ -1855,10 +1788,8 @@ pdf_content_create( return false; } } - m++; } - m = songs[s]->metadata; - while (*m) { + for (m = songs[s]->metadata; *m; m++) { if (!strcmp((*m)->name, "subtitle")) { /* INFO: (*m)->style will be ignored and the config style will be @@ -1876,19 +1807,16 @@ pdf_content_create( return false; } } - m++; } ctx.y -= 30.0; - se = songs[s]->sections; - while (*se) { + for (se = songs[s]->sections; *se; se++) { if ((*se)->label) { if (!pdf_texts_add_text(&ctx, (*se)->label->text, (*se)->label->style, A_LEFT)) { LOG_DEBUG("pdf_texts_add_text failed."); return false; } } - li = (*se)->lines; - while (*li) { + for (li = (*se)->lines; *li; li++) { int item_index; int text_above_index; struct CharPosition *pos; @@ -1928,13 +1856,12 @@ pdf_content_create( return false; } ctx.x = MARGIN_HORIZONTAL + width + ctx.prev_added_space; - struct SpaceNeeded **sp = ctx.spaces; - while (*sp) { + struct SpaceNeeded **sp; + for (sp = ctx.spaces; *sp; sp++) { if ((*sp)->text_above_index == i) { ctx.x += (*sp)->amount; ctx.prev_added_space += (*sp)->amount; } - sp++; } *texts = erealloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); (*texts)[ctx.text] = pdf_text_new(); @@ -2141,10 +2068,8 @@ pdf_content_create( diagrams = &ctx.content->pages[ctx.page]->diagrams; ctx.y = MEDIABOX_HEIGHT - MARGIN_TOP; } - li++; } ctx.y -= SECTION_GAP_WIDTH; - se++; } } *texts = erealloc(*texts, (ctx.text+1) * sizeof(struct PDFText *)); @@ -2172,14 +2097,12 @@ pdf_toc_render(struct PDFContent *content, pdfio_file_t *file) int p; for (p = 0; pages[p]; p++) { g_current_page_index = p; - texts = pages[p]->texts; stream = out_pdf_page_create(file, NULL, pages[p]->annots); - while (*texts) { + for (texts = pages[p]->texts; *texts; texts++) { if (!out_pdf_text_show(stream, *texts)) { LOG_DEBUG("out_pdf_text_show failed."); return false; } - texts++; } if (!pdfioStreamClose(stream)) { LOG_DEBUG("pdfioStreamClose failed."); @@ -2200,21 +2123,18 @@ pdf_content_render(struct PDFContent *content, pdfio_file_t *file) pages = content->pages; for (p = 0; pages[p]; p++) { g_current_page_index = p; - texts = pages[p]->texts; - imgs = pages[p]->images; - stream = out_pdf_page_create(file, imgs, pages[p]->annots); + stream = out_pdf_page_create(file, pages[p]->images, pages[p]->annots); if (!stream) { LOG_DEBUG("out_pdf_page_create failed."); return false; } - while (*texts) { + for (texts = pages[p]->texts; *texts; texts++) { if (!out_pdf_text_show(stream, *texts)) { LOG_DEBUG("out_pdf_text_show failed."); return false; } - texts++; } - while (*imgs) { + for (imgs = pages[p]->images; *imgs; imgs++) { if ( !pdfioContentDrawImage( stream, @@ -2228,22 +2148,20 @@ pdf_content_render(struct PDFContent *content, pdfio_file_t *file) LOG_DEBUG("pdfioContentDrawImage failed."); return false; } - imgs++; } if (pages[p]->diagrams) { double x = MARGIN_HORIZONTAL; double y = 40.0; double size = 50.0; double padding = 30.0; - struct ChordDiagram **d = pages[p]->diagrams; + struct ChordDiagram **d; /* TODO: Handle line break when too long */ - while (*d) { + for (d = pages[p]->diagrams; *d; d++) { if (!chord_diagram_draw(stream, *d, x, y, size)) { LOG_DEBUG("chord_diagram_draw failed."); return false; } x += size + padding; - d++; } } if (!pdfioStreamClose(stream)) { @@ -2269,7 +2187,6 @@ out_pdf_create( struct PDFContent *toc_content = NULL; pdfio_rect_t media_box_a4 = { 0.0, 0.0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT }; pdfio_rect_t crop_box = { 0.0, 0.0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT }; - int f = 0; char *dirpath, *fontpath, *pdf_filepath; memset(&g_current_font_name, 0, sizeof(g_current_font_name)); @@ -2296,8 +2213,9 @@ out_pdf_create( LOG_DEBUG("out_pdf_set_title failed."); goto CLEAN; } + int f; needed_fonts = out_pdf_font_get_all(songs, config); - while (needed_fonts[f] != NULL) { + for (f = 0; needed_fonts[f]; f++) { fontpath = fontconfig_fontpath_find(needed_fonts[f], FT_TTF); if (fontpath) { fnt = obj_new(); @@ -2319,7 +2237,6 @@ out_pdf_create( goto CLEAN; } } - f++; } cho_fonts_free(needed_fonts); if (config->output->diagram->show) { diff --git a/util.c b/util.c @@ -103,10 +103,10 @@ char * str_normalize(const char *str) { char *normalized = NULL; - int i = 0; - int n = 0; char c; - for (; str[i] != 0; i++) { + int n = 0; + int i; + for (i = 0; str[i]; i++) { if (str[i] == ' ' || str[i] == '/' || str[i] == '.') { normalized = erealloc(normalized, (n+1) * sizeof(char)); normalized[n] = '-'; @@ -201,12 +201,11 @@ strs_has(char **strs, const char *str) if (!strs) { return false; } - char **s = strs; - while (*s) { + char **s; + for (s = strs; *s; s++) { if (!strcmp(*s, str)) { return true; } - s++; } return false; } @@ -216,11 +215,8 @@ strs_add(char ***strs, const char *str) { int i = 0; if (*strs) { - char **s = *strs; - while (*s) { - i++; - s++; - } + char **s; + for (s = *strs; *s; s++, i++); } *strs = erealloc(*strs, (i+2) * sizeof(char *)); (*strs)[i] = strdup(str); @@ -233,10 +229,9 @@ strs_free(char **strs) if (!strs) { return; } - char **begin = strs; - while (*begin) { - free(*begin); - begin++; + char **s; + for (s = strs; *s; s++) { + free(*s); } free(strs); } @@ -306,38 +301,32 @@ file_extension_replace_or_add(const char *old, const char *extension) size_t extension_len = strlen(extension); char *new = NULL; int mark = -1; - int i = 0; - int e = 0; + int i, k; int old_len; - while (old[i] != 0) { - if (old[i] == '.') + for (i = 0; old[i]; i++) { + if (old[i] == '.') { mark = i; - i++; + } } - i = 0; if (mark == -1) { old_len = (int)strlen(old); new = emalloc((old_len+2+extension_len) * sizeof(char)); - while (i < old_len) { + for (i = 0; i < old_len; i++) { new[i] = old[i]; - i++; } new[i] = '.'; - while (extension[e] != 0) { - new[++i] = extension[e]; - e++; + i++; + for (k = 0; extension[k]; k++, i++) { + new[i] = extension[k]; } - new[++i] = 0; + new[i] = 0; } else { new = emalloc((mark+2+extension_len) * sizeof(char)); - while (i <= mark) { + for (i = 0; i <= mark; i++) { new[i] = old[i]; - i++; } - while (extension[e] != 0) { - new[i] = extension[e]; - i++; - e++; + for (k = 0; extension[k]; k++, i++) { + new[i] = extension[k]; } new[i] = 0; } @@ -364,7 +353,7 @@ filepath_basename(const char *path) { int begin = 0; int i; - for (i = 0; path[i] != 0; i++) { + for (i = 0; path[i]; i++) { if (path[i] == '/') { begin = i+1; } @@ -378,7 +367,7 @@ filepath_dirname(const char *path) char *dirname; int end = 0; int i; - for (i = 0; path[i] != 0; i++) { + for (i = 0; path[i]; i++) { if (path[i] == '/') { end = i; }