| 1 |
/* |
| 2 |
* Gregorio is a program that translates gabc files to GregorioTeX |
| 3 |
* This header defines the Gregorio data structures and functions. |
| 4 |
* |
| 5 |
* Copyright (C) 2016-2025 The Gregorio Project (see CONTRIBUTORS.md) |
| 6 |
* |
| 7 |
* This file is part of Gregorio. |
| 8 |
* |
| 9 |
* Gregorio is free software: you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation, either version 3 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* Gregorio is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with Gregorio. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* @file |
| 25 |
* The file starts by the definition of all the structures used in |
| 26 |
* gregorio. As it is certainly the most important file for |
| 27 |
* understanding, read it carefully. |
| 28 |
*/ |
| 29 |
|
| 30 |
#ifndef STRUCT_ITER_H |
| 31 |
#define STRUCT_ITER_H |
| 32 |
|
| 33 |
#include "struct.h" |
| 34 |
|
| 35 |
/* This enum defines bitfields selecting the node types that should be passed |
| 36 |
* to end_item in gregorio_from_note_to_note */ |
| 37 |
typedef enum { |
| 38 |
GRESTRUCT_NONE = 0, |
| 39 |
GRESTRUCT_NOTE = 1 << 0, |
| 40 |
GRESTRUCT_GLYPH = 1 << 1, |
| 41 |
GRESTRUCT_ELEMENT = 1 << 2, |
| 42 |
GRESTRUCT_SYLLABLE = 1 << 3 |
| 43 |
} gregorio_note_iter_item_type; |
| 44 |
|
| 45 |
typedef struct { |
| 46 |
gregorio_syllable *syllable; |
| 47 |
gregorio_element *element; |
| 48 |
gregorio_glyph *glyph; |
| 49 |
gregorio_note *note; |
| 50 |
} gregorio_note_iter_position; |
| 51 |
|
| 52 |
static __inline void gregorio_from_note_to_note( |
| 53 |
const gregorio_note_iter_position *const start, |
| 54 |
const gregorio_note_iter_position *const end, bool include_end, |
| 55 |
void (*const visit)(const gregorio_note_iter_position *, void *), |
| 56 |
void (*const end_item)(const gregorio_note_iter_position *, |
| 57 |
gregorio_note_iter_item_type, void *), |
| 58 |
const gregorio_note_iter_item_type desired_end_items, void *data) |
| 59 |
{ |
| 60 |
/* Note: include_end is effectively ignored if end is NULL */ |
| 61 |
|
| 62 |
gregorio_note_iter_position p = *start; |
| 63 |
|
| 64 |
while (p.syllable) { |
| 65 |
if (!p.element) { |
| 66 |
p.element = p.syllable->elements[0]; |
| 67 |
} |
| 68 |
while (p.element) { |
| 69 |
if (p.element->type == GRE_ELEMENT) { |
| 70 |
if (!p.glyph) { |
| 71 |
p.glyph = p.element->u.first_glyph; |
| 72 |
} |
| 73 |
while (p.glyph) { |
| 74 |
if (p.glyph->type == GRE_GLYPH) { |
| 75 |
if (!p.note) { |
| 76 |
p.note = p.glyph->u.notes.first_note; |
| 77 |
} |
| 78 |
while (p.note) { |
| 79 |
bool at_end = (end && p.note == end->note); |
| 80 |
|
| 81 |
if (p.note->type == GRE_NOTE |
| 82 |
&& (!at_end || include_end)) { |
| 83 |
visit(&p, data); |
| 84 |
} |
| 85 |
|
| 86 |
if (at_end) { |
| 87 |
if (end_item) { |
| 88 |
gregorio_fail(gregorio_from_note_to_note, |
| 89 |
"unused code path"); |
| 90 |
if (desired_end_items & GRESTRUCT_NOTE) { |
| 91 |
/* currently unused */ |
| 92 |
/* LCOV_EXCL_START */ |
| 93 |
/* to enable the real code, delete this |
| 94 |
* assertion: */ |
| 95 |
gregorio_fail(gregorio_from_note_to_note, |
| 96 |
"unused code path"); |
| 97 |
end_item(&p, GRESTRUCT_NOTE, data); |
| 98 |
} |
| 99 |
/* LCOV_EXCL_END */ |
| 100 |
if (desired_end_items & GRESTRUCT_GLYPH) { |
| 101 |
/* currently unused */ |
| 102 |
/* LCOV_EXCL_START */ |
| 103 |
/* to enable the real code, delete this |
| 104 |
* assertion: */ |
| 105 |
gregorio_fail(gregorio_from_note_to_note, |
| 106 |
"unused code path"); |
| 107 |
end_item(&p, GRESTRUCT_GLYPH, data); |
| 108 |
} |
| 109 |
/* LCOV_EXCL_END */ |
| 110 |
if (desired_end_items |
| 111 |
& GRESTRUCT_ELEMENT) { |
| 112 |
/* currently unused */ |
| 113 |
/* LCOV_EXCL_START */ |
| 114 |
/* to enable the real code, delete this |
| 115 |
* assertion: */ |
| 116 |
gregorio_fail(gregorio_from_note_to_note, |
| 117 |
"unused code path"); |
| 118 |
end_item(&p, GRESTRUCT_ELEMENT, data); |
| 119 |
} |
| 120 |
/* LCOV_EXCL_END */ |
| 121 |
if (desired_end_items |
| 122 |
& GRESTRUCT_SYLLABLE) { |
| 123 |
end_item(&p, GRESTRUCT_SYLLABLE, data); |
| 124 |
} |
| 125 |
} |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
if (end_item |
| 130 |
&& (desired_end_items & GRESTRUCT_NOTE)) { |
| 131 |
/* in 4.2.0, this code is never hit, as the only |
| 132 |
* usage that gets through to here does not |
| 133 |
* include GRESTRUCT_NOTE in desired_end_items */ |
| 134 |
/* LCOV_EXCL_START */ |
| 135 |
/* to enable the real code, delete this |
| 136 |
* assertion: */ |
| 137 |
gregorio_fail(gregorio_from_note_to_note, |
| 138 |
"unused code path"); |
| 139 |
end_item(&p, GRESTRUCT_NOTE, data); |
| 140 |
|
| 141 |
} |
| 142 |
/* LCOV_EXCL_STOP */ |
| 143 |
p.note = p.note->next; |
| 144 |
} /* note */ |
| 145 |
} |
| 146 |
if (end_item && (desired_end_items & GRESTRUCT_GLYPH)) { |
| 147 |
/* in 4.2.0, this code is never hit, as the only usage |
| 148 |
* that gets through to here does not include |
| 149 |
* GRESTRUCT_GLYPH in desired_end_items */ |
| 150 |
/* LCOV_EXCL_START */ |
| 151 |
/* to enable the real code, delete this assertion: */ |
| 152 |
gregorio_fail(gregorio_from_note_to_note, |
| 153 |
"unused code path"); |
| 154 |
end_item(&p, GRESTRUCT_GLYPH, data); |
| 155 |
} |
| 156 |
/* LCOV_EXCL_STOP */ |
| 157 |
p.glyph = p.glyph->next; |
| 158 |
} /* glyph */ |
| 159 |
} |
| 160 |
if (end_item && (desired_end_items & GRESTRUCT_ELEMENT)) { |
| 161 |
end_item(&p, GRESTRUCT_ELEMENT, data); |
| 162 |
} |
| 163 |
p.element = p.element->next; |
| 164 |
} /* element */ |
| 165 |
if (end_item && (desired_end_items & GRESTRUCT_SYLLABLE)) { |
| 166 |
end_item(&p, GRESTRUCT_SYLLABLE, data); |
| 167 |
} |
| 168 |
p.syllable = p.syllable->next_syllable; |
| 169 |
} /* syllable */ |
| 170 |
} |
| 171 |
|
| 172 |
static __inline void gregorio_for_each_note(const gregorio_score *score, |
| 173 |
void (*const visit)(const gregorio_note_iter_position *, void *), |
| 174 |
void (*const end_item)(const gregorio_note_iter_position *, |
| 175 |
gregorio_note_iter_item_type, void *), |
| 176 |
const gregorio_note_iter_item_type desired_end_items, void *data) |
| 177 |
{ |
| 178 |
gregorio_note_iter_position p = { |
| 179 |
/* .syllable = */ NULL, |
| 180 |
/* .element = */ NULL, |
| 181 |
/* .glyph = */ NULL, |
| 182 |
/* .note = */ NULL |
| 183 |
}; |
| 184 |
|
| 185 |
p.syllable = score->first_syllable; |
| 186 |
|
| 187 |
gregorio_from_note_to_note(&p, NULL, true, visit, end_item, |
| 188 |
desired_end_items, data); |
| 189 |
} |
| 190 |
|
| 191 |
#endif |