| 1 | /* Collection.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2012,2014-2022,2024,2026 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "Collection.h" |
| 20 | #include <string> |
| 21 | |
| 22 | /********** class Collection **********/ |
| 23 | |
| 24 | void _CollectionOfDaata_v1_copy (const _CollectionOfDaata* me, _CollectionOfDaata* thee) { |
| 25 | thy at._elements = nullptr; // set to null in case the inherited v1_copy crashes |
| 26 | my structDaata :: v1_copy (thee); |
| 27 | thy _ownershipInitialized = my _ownershipInitialized; |
| 28 | thy _ownItems = my _ownItems; |
| 29 | thy _capacity = my _capacity; |
| 30 | thy size = my size; |
| 31 | if (my _capacity > 0) { |
| 32 | thy at._elements = Melder_calloc (Daata, my _capacity); // filled with null pointers |
| 33 | thy at._elements --; // immediately turn from base-0 into base-1 // BUG use MelderArray |
| 34 | } |
| 35 | for (integer i = 1; i <= my size; i ++) { |
| 36 | Daata item = my at [i]; |
| 37 | if (my _ownItems) { |
| 38 | Melder_require (Thing_isa (item, classDaata), |
| 39 | U"Cannot copy item of class ", Thing_className (item), U"."); |
| 40 | thy at [i] = Data_copy (item).releaseToAmbiguousOwner(); |
| 41 | } else { |
| 42 | thy at [i] = item; // reference copy: if me doesn't own the items, then thee shouldn't either // NOTE: the items don't have to be Daata |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | bool _CollectionOfDaata_v1_equal (_CollectionOfDaata* me, _CollectionOfDaata* thee) { |
| 48 | if (! my structDaata :: v1_equal (thee)) |
| 49 | return false; |
| 50 | if (my size != thy size) |
| 51 | return false; |
| 52 | for (integer i = 1; i <= my size; i ++) { |
| 53 | Melder_require (Thing_isa (my at [i], classDaata), |
| 54 | U"Collection::equal: cannot compare items of class ", Thing_className (my at [i]), U"."); |
| 55 | Melder_require (Thing_isa (thy at [i], classDaata), |
| 56 | U"Collection::equal: cannot compare items of class ", Thing_className (thy at [i]), U"."); |
| 57 | bool equal = Data_equal (my at [i], thy at [i]); |
| 58 | //Melder_casual (U"classCollection_equal: ", equal, |
| 59 | // U", item ", i, |
| 60 | // U", types ", Thing_className (my item [i]), U" and ", Thing_className (thy item [i])); |
| 61 | if (! equal) |
| 62 | return false; |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | bool _CollectionOfDaata_v1_canWriteAsEncoding (_CollectionOfDaata* me, int encoding) { |
| 68 | for (integer i = 1; i <= my size; i ++) { |
| 69 | Daata data = my at [i]; |
| 70 | if (data -> name && ! Melder_isEncodable (data -> name.get(), encoding)) |
| 71 | return false; |
| 72 | if (! Data_canWriteAsEncoding (data, encoding)) |
| 73 | return false; |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | void _CollectionOfDaata_v1_writeText (_CollectionOfDaata* me, MelderFile file) { |
| 79 | texputi32 (file, my size, U"size"); |
| 80 | texputintro (file, U"item []: ", my size ? nullptr : U"(empty)"); |
| 81 | for (integer i = 1; i <= my size; i ++) { |
| 82 | Daata thing = my at [i]; |
| 83 | ClassInfo classInfo = thing -> classInfo; |
| 84 | texputintro (file, U"item [", Melder_integer (i), U"]:"); |
| 85 | Melder_require (Thing_isa (thing, classDaata) && Data_canWriteText (thing), |
| 86 | U"Objects of class ", classInfo -> className, U" cannot be written."); |
| 87 | texputw16 (file, |
| 88 | classInfo -> version > 0 ? |
| 89 | Melder_cat (classInfo -> className, U" ", classInfo -> version) : |
| 90 | classInfo -> className, |
| 91 | U"class", 0,0,0,0,0); |
| 92 | texputw16 (file, thing -> name.get(), U"name"); |
| 93 | Data_writeText (thing, file); |
| 94 | texexdent (file); |
| 95 | } |
| 96 | texexdent (file); |
| 97 | } |
| 98 | |
| 99 | void _CollectionOfDaata_v1_readText (_CollectionOfDaata* me, MelderReadText text, int formatVersion) { |
| 100 | if (formatVersion < 0) { |
| 101 | autostring8 line = Melder_32to8 (MelderReadText_readLine (text)); |
| 102 | integer l_size; |
| 103 | Melder_require (line && sscanf (line.get(), "%td", & l_size) == 1 && l_size >= 0, |
| 104 | U"Collection::readText: cannot read size."); |
| 105 | my _grow (l_size); |
| 106 | for (integer i = 1; i <= l_size; i ++) { |
| 107 | do { |
| 108 | line = Melder_32to8 (MelderReadText_readLine (text)); |
| 109 | if (! line) |
| 110 | Melder_throw (U"Missing object line."); |
| 111 | } while (! strnequ (line.get(), "Object ", 7)); |
| 112 | |
| 113 | integer itemNumberRead; |
| 114 | char klas [200], nameTag [2000]; |
| 115 | int_not_integer n = 0; // %n |
| 116 | integer stringsRead = sscanf (line.get(), "Object %td: class %199s %1999s%n", & itemNumberRead, klas, nameTag, & n); |
| 117 | Melder_require (stringsRead >= 2, |
| 118 | U"Collection::readText: cannot read header of object ", i, U"."); |
| 119 | Melder_require (itemNumberRead == i, |
| 120 | U"Collection::readText: read item number ", itemNumberRead, U" while expecting ", i, U"."); |
| 121 | Melder_require (stringsRead < 3 || strequ (nameTag, "name"), |
| 122 | U"Collection::readText: wrong header at object ", i, U"."); |
| 123 | |
| 124 | my at [i] = (Daata) Thing_newFromClassName (Melder_peek8to32_u (klas), nullptr).releaseToAmbiguousOwner(); |
| 125 | my size ++; |
| 126 | Melder_require (Thing_isa (my at [i], classDaata) && Data_canReadText (my at [i]), |
| 127 | U"Cannot read item of class ", Thing_className (my at [i]), U" in collection."); |
| 128 | Data_readText (my at [i], text, -1); |
| 129 | if (stringsRead == 3) { |
| 130 | char *location = & line [n]; |
| 131 | if (*location == ' ') |
| 132 | n ++; // skip space character |
| 133 | const integer length = Melder8_length (location); |
| 134 | if (length > 0 && location [length - 1] == '\n') |
| 135 | location [length - 1] = '\0'; |
| 136 | Thing_setName (my at [i], Melder_peek8to32_u (line.get()+n)); |
| 137 | } |
| 138 | } |
| 139 | } else { |
| 140 | int32 l_size = texgeti32 (text); |
| 141 | my _grow (l_size); |
| 142 | for (int32 i = 1; i <= l_size; i ++) { |
| 143 | autostring32 className = texgetw16 (text); |
| 144 | int elementFormatVersion; |
| 145 | my at [i] = (Daata) Thing_newFromClassName (className.get(), & elementFormatVersion).releaseToAmbiguousOwner(); |
| 146 | my size ++; |
| 147 | Melder_require (Thing_isa (my at [i], classDaata) && Data_canReadText (my at [i]), |
| 148 | U"Cannot read item of class ", Thing_className (my at [i]), U" in collection."); |
| 149 | autostring32 objectName = texgetw16 (text); |
| 150 | Thing_setName (my at [i], objectName.get()); |
| 151 | Data_readText (my at [i], text, elementFormatVersion); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void _CollectionOfDaata_v1_writeBinary (_CollectionOfDaata* me, FILE *f) { |
| 157 | binputi32 (my size, f); |
| 158 | for (integer i = 1; i <= my size; i ++) { |
| 159 | Daata thing = my at [i]; |
| 160 | ClassInfo classInfo = thing -> classInfo; |
| 161 | Melder_require (Thing_isa (thing, classDaata) && Data_canWriteBinary (thing), |
| 162 | U"Objects of class ", classInfo -> className, U" cannot be written."); |
| 163 | binputw8 (classInfo -> version > 0 ? |
| 164 | Melder_cat (classInfo -> className, U" ", classInfo -> version) : classInfo -> className, f); |
| 165 | binputw16 (thing -> name.get(), f); |
| 166 | Data_writeBinary ((Daata) thing, f); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void _CollectionOfDaata_v1_readBinary (_CollectionOfDaata* me, FILE *f, int formatVersion) { |
| 171 | if (formatVersion < 0) { |
| 172 | int32 l_size = bingeti32 (f); |
| 173 | if (l_size < 0) |
| 174 | Melder_throw (U"Empty collection."); |
| 175 | my _grow (l_size); |
| 176 | for (int32 i = 1; i <= l_size; i ++) { |
| 177 | char klas [200], name [2000]; |
| 178 | Melder_require (fscanf (f, "%199s%1999s", klas, name) == 2, |
| 179 | U"Cannot read class and name."); |
| 180 | my at [i] = (Daata) Thing_newFromClassName (Melder_peek8to32_u (klas), nullptr).releaseToAmbiguousOwner(); |
| 181 | my size ++; |
| 182 | Melder_require (Thing_isa (my at [i], classDaata), |
| 183 | U"Cannot read item of class ", Thing_className (my at [i]), U"."); |
| 184 | Melder_require (fgetc (f) == ' ', |
| 185 | U"Cannot read space."); |
| 186 | Data_readBinary (my at [i], f, -1); |
| 187 | if (strcmp (name, "?")) |
| 188 | Thing_setName (my at [i], Melder_peek8to32_u (name)); |
| 189 | } |
| 190 | } else { |
| 191 | int32 l_size = bingeti32 (f); |
| 192 | if (Melder_debug == 44) |
| 193 | Melder_casual (U"structCollection :: v1_readBinary: Reading ", l_size, U" objects"); |
| 194 | my _grow (l_size); |
| 195 | for (int32 i = 1; i <= l_size; i ++) { |
| 196 | autostring8 klas = bingets8 (f); |
| 197 | if (Melder_debug == 44) |
| 198 | Melder_casual (U"structCollection :: v1_readBinary: Reading object of type ", Melder_peek8to32_u (klas.get())); |
| 199 | int elementFormatVersion; |
| 200 | my at [i] = (Daata) Thing_newFromClassName (Melder_peek8to32_u (klas.get()), & elementFormatVersion).releaseToAmbiguousOwner(); |
| 201 | my size ++; |
| 202 | Melder_require (Thing_isa (my at [i], classDaata) && Data_canReadBinary (my at [i]), |
| 203 | U"Objects of class ", Thing_className (my at [i]), U" cannot be read."); |
| 204 | autostring32 name = bingetw16 (f); |
| 205 | if (Melder_debug == 44) |
| 206 | Melder_casual (U"structCollection :: v1_readBinary: Reading object with name ", name.get()); |
| 207 | Thing_setName (my at [i], name.get()); |
| 208 | Data_readBinary (my at [i], f, elementFormatVersion); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* a global that cannot be marked inline (see Collection.h) */ |
| 214 | structData_Description theCollectionOfDaata_v_description [] = { |
| 215 | { U"size", integerwa, Melder_offsetof (CollectionOf<structThing>*, size), sizeof (integer), nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr }, |
| 216 | { U"items", objectwa, Melder_offsetof (CollectionOf<structThing>*, at), sizeof (Daata), U"Daata", & theClassInfo_Daata, 1, nullptr, U"size", nullptr, nullptr }, |
| 217 | { } |
| 218 | }; |
| 219 | |
| 220 | #define _Collection_implement(klas,genericClass,itemClass,parentClass,version) \ |
| 221 | static Thing _##klas##_new () { return new genericClass<struct##itemClass>; } \ |
| 222 | struct structClassInfo theClassInfo_##klas = { U"" #klas, & theClassInfo_##parentClass, \ |
| 223 | sizeof (genericClass<struct##itemClass>), _##klas##_new, version, 0, nullptr}; \ |
| 224 | ClassInfo class##klas = & theClassInfo_##klas |
| 225 | |
| 226 | _Collection_implement (Collection, CollectionOf, Thing, Daata, 0); |
| 227 | _Collection_implement (Ordered, OrderedOf, Daata, Collection, 0); |
| 228 | _Collection_implement (Sorted, SortedOf, Daata, Collection, 0); |
| 229 | _Collection_implement (SortedSet, SortedSetOf, Daata, Sorted, 0); |
| 230 | _Collection_implement (SortedSetOfString, SortedSetOfStringOf, SimpleString, SortedSet, 0); |
| 231 | |
| 232 | Thing_implement (StringList, Ordered, 0); |
| 233 | Thing_implement (StringSet, SortedSet, 0); |
| 234 | |
| 235 | /* End of file Collection.cpp */ |