commit fc1f6bcd51702c51fde660bb20de3666f19792a8
parent c15e6366cb40046802c102cce987a70409fc9be6
Author: nibo <nibo@relim.de>
Date: Sun, 13 Oct 2024 08:49:16 +0200
Improve *_free functions
Diffstat:
| M | chordpro.c | | | 73 | ++++++++++++++++++++++++++++++++++++++++--------------------------------- |
| M | config.c | | | 34 | ++++++++++++++++++++-------------- |
| M | out_pdf.c | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++---------------------- |
3 files changed, 105 insertions(+), 69 deletions(-)
diff --git a/chordpro.c b/chordpro.c
@@ -571,12 +571,12 @@ cho_font_free(struct Font *font)
void
cho_fonts_free(struct Font **fonts)
{
- int i = 0;
- while (fonts[i] != NULL) {
- cho_font_free(fonts[i]);
- i++;
+ struct Font **start = fonts;
+ while (*fonts) {
+ cho_font_free(*fonts);
+ fonts++;
}
- free(fonts);
+ free(start);
}
enum FontFamily
@@ -1396,12 +1396,12 @@ cho_tag_attr_free(struct Attr *attr)
static void
cho_tag_attrs_free(struct Attr **attrs)
{
- int a = 0;
- while (attrs[a] != NULL) {
- cho_tag_attr_free(attrs[a]);
- a++;
+ struct Attr **start = attrs;
+ while (*attrs) {
+ cho_tag_attr_free(*attrs);
+ attrs++;
}
- free(attrs);
+ free(start);
}
static struct Tag *
@@ -2007,15 +2007,18 @@ cho_line_new(void)
static void
cho_line_free(struct ChoLine *line)
{
- int i;
- for (i = 0; line->lyrics[i]; i++) {
- cho_line_item_free(line->lyrics[i]);
+ struct ChoLineItem **start_lyrics = line->lyrics;
+ while (*line->lyrics) {
+ cho_line_item_free(*line->lyrics);
+ line->lyrics++;
}
- for (i = 0; line->text_above[i]; i++) {
- cho_text_above_free(line->text_above[i]);
+ struct ChoLineItemAbove **start_text_above = line->text_above;
+ while (*line->text_above) {
+ cho_text_above_free(*line->text_above);
+ line->text_above++;
}
- free(line->lyrics);
- free(line->text_above);
+ free(start_lyrics);
+ free(start_text_above);
free(line);
}
@@ -2066,11 +2069,12 @@ cho_section_free(struct ChoSection *section)
if (section->label) {
cho_section_label_free(section->label);
}
- int i;
- for (i = 0; section->lines[i]; i++) {
- cho_line_free(section->lines[i]);
+ struct ChoLine **start_lines = section->lines;
+ while (*section->lines) {
+ cho_line_free(*section->lines);
+ section->lines++;
}
- free(section->lines);
+ free(start_lines);
free(section);
}
@@ -2129,27 +2133,30 @@ cho_song_count(struct ChoSong **songs)
static void
cho_song_free(struct ChoSong *song)
{
- int i;
- for (i = 0; song->metadata[i]; i++) {
- cho_metadata_free(song->metadata[i]);
+ struct ChoMetadata **start_meta = song->metadata;
+ struct ChoSection **start_section = song->sections;
+ while (*song->metadata) {
+ cho_metadata_free(*song->metadata);
+ song->metadata++;
}
- free(song->metadata);
- for (i = 0; song->sections[i]; i++) {
- cho_section_free(song->sections[i]);
+ while (*song->sections) {
+ cho_section_free(*song->sections);
+ song->sections++;
}
- free(song->sections);
+ free(start_meta);
+ free(start_section);
free(song);
}
void
cho_songs_free(struct ChoSong **songs)
{
- int so = 0;
- while (songs[so] != NULL) {
- cho_song_free(songs[so]);
- so++;
+ struct ChoSong **start_song = songs;
+ while (*songs) {
+ cho_song_free(*songs);
+ songs++;
}
- free(songs);
+ free(start_song);
}
static struct ChoImage *
diff --git a/config.c b/config.c
@@ -181,11 +181,12 @@ config_note_free(struct Note *note)
static void
config_notes_free(struct Note **notes)
{
- int n;
- for (n = 0; notes[n]; n++) {
- config_note_free(notes[n]);
+ struct Note **start = notes;
+ while (*notes) {
+ config_note_free(*notes);
+ notes++;
}
- free(notes);
+ free(start);
}
static struct Note **
@@ -714,23 +715,28 @@ config_load(const char *filepath)
void
config_free(struct Config *config)
{
- int i;
+ struct PrintableItem **start_items = config->output->printable_items;
+ struct Note **start_notes = config->output->notes;
free(config->output->chorus->label);
free(config->output->chorus);
- for (i = 0; config->output->printable_items[i]; i++) {
- config_printable_item_free(config->output->printable_items[i]);
+ while (*config->output->printable_items) {
+ config_printable_item_free(*config->output->printable_items);
+ config->output->printable_items++;
}
- free(config->output->printable_items);
- for (i = 0; config->output->notes[i]; i++) {
- config_note_free(config->output->notes[i]);
+ free(start_items);
+ while (*config->output->notes) {
+ config_note_free(*config->output->notes);
+ config->output->notes++;
}
- free(config->output->notes);
+ free(start_notes);
free(config->output);
free(config->parser->chords);
- for (i = 0; config->parser->notes[i]; i++) {
- config_note_free(config->parser->notes[i]);
+ start_notes = config->parser->notes;
+ while (*config->parser->notes) {
+ config_note_free(*config->parser->notes);
+ config->parser->notes++;
}
- free(config->parser->notes);
+ free(start_notes);
free(config->parser);
free(config);
}
diff --git a/out_pdf.c b/out_pdf.c
@@ -299,12 +299,12 @@ out_pdf_fnt_free(struct Fnt *fnt)
static void
out_pdf_fnts_free(struct Fnt **fnts)
{
- int i = 0;
- while (fnts[i] != NULL) {
- out_pdf_fnt_free(fnts[i]);
- i++;
+ struct Fnt **start = fnts;
+ while (*fnts) {
+ out_pdf_fnt_free(*fnts);
+ fnts++;
}
- free(fnts);
+ free(start);
}
static void
@@ -348,6 +348,14 @@ out_pdf_font_set(pdfio_stream_t *stream, struct Font *font)
return true;
}
+static void
+text_line_item_free(struct TextLineItem *item)
+{
+ free(item->text);
+ cho_style_free(item->style);
+ free(item);
+}
+
static double
text_line_set_lineheight(struct TextLine *line, enum SongFragmentType ftype)
{
@@ -383,6 +391,20 @@ text_line_create(const char *str, struct Style *style)
return line;
}
+static void
+text_line_free(struct TextLine *line)
+{
+ if (line->items) {
+ struct TextLineItem **start = line->items;
+ while (*line->items) {
+ text_line_item_free(*line->items);
+ line->items++;
+ }
+ free(start);
+ }
+ free(line);
+}
+
static double
text_width(struct TextLineItem *item)
{
@@ -1124,27 +1146,28 @@ text_create(struct ChoSong **songs, struct Config *config)
}
static void
-text_free(struct Text **text)
+text_free(struct Text *text)
{
- int t, tl, tli;
- for (t = 0; text[t]; t++) {
- for (tl = 0; text[t]->lines[tl]; tl++) {
- if (text[t]->lines[tl]->items) {
- for (tli = 0; text[t]->lines[tl]->items[tli]; tli++) {
- free(text[t]->lines[tl]->items[tli]->text);
- cho_style_free(text[t]->lines[tl]->items[tli]->style);
- free(text[t]->lines[tl]->items[tli]);
- }
- free(text[t]->lines[tl]->items);
- }
- free(text[t]->lines[tl]);
- }
- free(text[t]->lines);
- free(text[t]);
+ struct TextLine **start = text->lines;
+ while (*text->lines) {
+ text_line_free(*text->lines);
+ text->lines++;
}
+ free(start);
free(text);
}
+static void
+texts_free(struct Text **texts)
+{
+ struct Text **start = texts;
+ while (*texts) {
+ text_free(*texts);
+ texts++;
+ }
+ free(start);
+}
+
static pdfio_dict_t *
annot_create(pdfio_file_t *pdf, const char *uri, pdfio_rect_t *rect)
{
@@ -1373,7 +1396,7 @@ out_pdf_new(const char *cho_filepath, const char *output_folder_or_file, struct
LOG_DEBUG("pdfioFileClose failed.");
return NULL;
}
- text_free(text);
+ texts_free(text);
out_pdf_fnts_free(g_fonts);
g_fonts = NULL;
return pdf_filename;