| 1 | /* Data.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2018,2021,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 | |
| 21 | Thing_implement (Daata, Thing, 0); |
| 22 | |
| 23 | autoDaata _Data_copy (constDaata me) { |
| 24 | try { |
| 25 | if (! me) |
| 26 | return autoDaata(); |
| 27 | autoDaata thee = Thing_newFromClass (my classInfo).static_cast_move <structDaata>(); |
| 28 | my v1_copy (thee.get()); |
| 29 | Thing_setName (thee.get(), my name.get()); |
| 30 | return thee; |
| 31 | } catch (MelderError) { |
| 32 | Melder_throw (me, U": not copied."); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | bool Data_equal (Daata me, Daata thee) { |
| 37 | if (my classInfo != thy classInfo) |
| 38 | return false; // different class: not equal |
| 39 | int offset = sizeof (struct structDaata); // we already compared the methods, and are going to skip the names |
| 40 | if (! memcmp ((char *) me + offset, (char *) thee + offset, my classInfo -> size - offset)) // BUG: not necessarily portable |
| 41 | return true; // no shallow differences |
| 42 | return my v1_equal (thee); |
| 43 | } |
| 44 | |
| 45 | bool Data_canWriteAsEncoding (Daata me, int encoding) { |
| 46 | return my v1_canWriteAsEncoding (encoding); |
| 47 | } |
| 48 | |
| 49 | bool Data_canWriteText (Daata me) { |
| 50 | return my v_writable (); |
| 51 | } |
| 52 | |
| 53 | void Data_writeText (Daata me, MelderFile openFile) { |
| 54 | my v1_writeText (openFile); |
| 55 | if (ferror (openFile -> filePointer)) |
| 56 | Melder_throw (U"I/O error."); |
| 57 | } |
| 58 | |
| 59 | MelderFile Data_createTextFile (Daata me, MelderFile file, bool verbose) { |
| 60 | autoMelderFile mfile = MelderFile_create (file); |
| 61 | #if defined (_WIN32) |
| 62 | file -> requiresCRLF = true; |
| 63 | #endif |
| 64 | file -> verbose = verbose; |
| 65 | file -> outputEncoding = (int) Melder_getOutputEncoding (); |
| 66 | if (file -> outputEncoding == (int) kMelder_textOutputEncoding::ASCII_THEN_UTF16) |
| 67 | file -> outputEncoding = Data_canWriteAsEncoding (me, kMelder_textOutputEncoding_ASCII) ? |
| 68 | kMelder_textOutputEncoding_ASCII : (int) kMelder_textOutputEncoding::UTF16; |
| 69 | else if (file -> outputEncoding == (int) kMelder_textOutputEncoding::ISO_LATIN1_THEN_UTF16) |
| 70 | file -> outputEncoding = Data_canWriteAsEncoding (me, kMelder_textOutputEncoding_ISO_LATIN1) ? |
| 71 | kMelder_textOutputEncoding_ISO_LATIN1 : (int) kMelder_textOutputEncoding::UTF16; |
| 72 | if (file -> outputEncoding == (int) kMelder_textOutputEncoding::UTF16) |
| 73 | binputu16 (0xfeff, file -> filePointer); |
| 74 | return mfile.transfer(); |
| 75 | } |
| 76 | |
| 77 | static void _Data_writeToTextFile (Daata me, MelderFile file, bool verbose) { |
| 78 | try { |
| 79 | if (! Data_canWriteText (me)) |
| 80 | Melder_throw (U"Objects of class ", my classInfo -> className, U" cannot be written to a text file."); |
| 81 | autoMelderFile mfile = Data_createTextFile (me, file, verbose); |
| 82 | #ifndef _WIN32 |
| 83 | flockfile (file -> filePointer); // BUG |
| 84 | #endif |
| 85 | MelderFile_write (file, U"File type = \"ooTextFile\"\nObject class = \"", my classInfo -> className); |
| 86 | if (my classInfo -> version > 0) |
| 87 | MelderFile_write (file, U" ", my classInfo -> version); |
| 88 | MelderFile_write (file, U"\"\n"); |
| 89 | Data_writeText (me, file); |
| 90 | MelderFile_writeCharacter (file, U'\n'); |
| 91 | #ifndef _WIN32 |
| 92 | if (file -> filePointer) |
| 93 | funlockfile (file -> filePointer); |
| 94 | #endif |
| 95 | mfile.close (); |
| 96 | } catch (MelderError) { |
| 97 | #ifndef _WIN32 |
| 98 | if (file -> filePointer) |
| 99 | funlockfile (file -> filePointer); // the file pointer is null before Data_createTextFile() and after mfile.close() |
| 100 | #endif |
| 101 | throw; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void Data_writeToTextFile (Daata me, MelderFile file) { |
| 106 | try { |
| 107 | _Data_writeToTextFile (me, file, true); |
| 108 | } catch (MelderError) { |
| 109 | Melder_throw (me, U": not written to text file ", file, U"."); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void Data_writeToShortTextFile (Daata me, MelderFile file) { |
| 114 | try { |
| 115 | _Data_writeToTextFile (me, file, false); |
| 116 | } catch (MelderError) { |
| 117 | Melder_throw (me, U": not written to short text file ", file, U"."); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | bool Data_canWriteBinary (Daata me) { |
| 122 | return my v_writable (); |
| 123 | } |
| 124 | |
| 125 | void Data_writeBinary (Daata me, FILE *f) { |
| 126 | my v1_writeBinary (f); |
| 127 | if (ferror (f)) |
| 128 | Melder_throw (U"I/O error."); |
| 129 | } |
| 130 | |
| 131 | void Data_writeToBinaryFile (Daata me, MelderFile file) { |
| 132 | try { |
| 133 | if (! Data_canWriteBinary (me)) |
| 134 | Melder_throw (U"Objects of class ", my classInfo -> className, U" cannot be written to a generic binary file."); |
| 135 | autoMelderFile mfile = MelderFile_create (file); |
| 136 | if (fprintf (file -> filePointer, "ooBinaryFile") < 0) |
| 137 | Melder_throw (U"Cannot write first bytes of file."); |
| 138 | binputw8 ( |
| 139 | my classInfo -> version > 0 ? |
| 140 | Melder_cat (my classInfo -> className, U" ", my classInfo -> version) : |
| 141 | my classInfo -> className, |
| 142 | file -> filePointer); |
| 143 | Data_writeBinary (me, file -> filePointer); |
| 144 | mfile.close (); |
| 145 | } catch (MelderError) { |
| 146 | Melder_throw (me, U": not written to binary file ", file, U"."); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | bool Data_canReadText (Daata me) { |
| 151 | return my v_writable (); |
| 152 | } |
| 153 | |
| 154 | void Data_readText (Daata me, MelderReadText text, int formatVersion) { |
| 155 | try { |
| 156 | my v1_readText (text, formatVersion); |
| 157 | my v_repair (); |
| 158 | } catch (MelderError) { |
| 159 | Melder_throw (Thing_className (me), U" not read."); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | autoDaata Data_readFromTextFile (MelderFile file) { |
| 164 | try { |
| 165 | autoMelderReadText text = MelderReadText_createFromFile (file); |
| 166 | const mutablestring32 line = MelderReadText_readLine (text.get()); |
| 167 | if (! line) |
| 168 | Melder_throw (U"No lines."); |
| 169 | /* |
| 170 | Allow for a future version of text files (we have no plans). |
| 171 | This check was written on 2017-09-10. |
| 172 | See below at `Data_readFromBinaryFile` for a more serious proposal. |
| 173 | */ |
| 174 | if (str32str (line, U"ooText2File")) |
| 175 | Melder_throw (U"This Praat version cannot read this Praat file. Please download a newer version of Praat."); |
| 176 | char32 *end = str32str (line, U"ooTextFile"); // oo format? |
| 177 | autoDaata me; |
| 178 | int formatVersion; |
| 179 | if (end) { |
| 180 | autostring32 klas = texgetw16 (text.get()); |
| 181 | me = Thing_newFromClassName (klas.get(), & formatVersion).static_cast_move <structDaata>(); |
| 182 | } else { |
| 183 | end = str32str (line, U"TextFile"); |
| 184 | if (! end) |
| 185 | Melder_throw (U"Not an old-type text file; should not occur."); |
| 186 | *end = U'\0'; |
| 187 | me = Thing_newFromClassName (line, nullptr).static_cast_move <structDaata>(); |
| 188 | formatVersion = -1; // old version |
| 189 | } |
| 190 | MelderFile_getParentFolder (file, & Data_directoryBeingRead); |
| 191 | Data_readText (me.get(), text.get(), formatVersion); |
| 192 | file -> format = structMelderFile :: Format :: text; |
| 193 | return me; |
| 194 | } catch (MelderError) { |
| 195 | Melder_throw (U"Data not read from text file ", file, U"."); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | bool Data_canReadBinary (Daata me) { |
| 200 | return my v_writable (); |
| 201 | } |
| 202 | |
| 203 | void Data_readBinary (Daata me, FILE *f, int formatVersion) { |
| 204 | try { |
| 205 | my v1_readBinary (f, formatVersion); |
| 206 | if (feof (f)) |
| 207 | Melder_throw (U"Early end of file."); |
| 208 | if (ferror (f)) |
| 209 | Melder_throw (U"I/O error."); |
| 210 | my v_repair (); |
| 211 | } catch (MelderError) { |
| 212 | Melder_throw (Thing_className (me), U" not read."); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | autoDaata Data_readFromBinaryFile (MelderFile file) { |
| 217 | try { |
| 218 | autofile f = Melder_fopen (file, "rb"); |
| 219 | char line [200]; |
| 220 | size_t n = fread (line, 1, 199, f); line [n] = '\0'; |
| 221 | /* |
| 222 | Allow for a future version of binary files, which can handle 64-bit integers |
| 223 | and are perhaps written in little-endian format. |
| 224 | This check was written on 2017-09-10, and should stay for at least a year; |
| 225 | ooBinary2 files can therefore be implemented from some moment after 2018-09-10. |
| 226 | Please compare with `Data_readFromTextFile` above. |
| 227 | */ |
| 228 | if (strstr (line, "ooBinary2File")) |
| 229 | Melder_throw (U"This Praat version cannot read this Praat file. Please download a newer version of Praat."); |
| 230 | char *end = strstr (line, "ooBinaryFile"); |
| 231 | autoDaata me; |
| 232 | int formatVersion; |
| 233 | if (end) { |
| 234 | fseek (f, strlen ("ooBinaryFile"), 0); |
| 235 | autostring8 klas = bingets8 (f); |
| 236 | me = Thing_newFromClassName (Melder_peek8to32_u (klas.get()), & formatVersion).static_cast_move <structDaata>(); |
| 237 | } else { |
| 238 | end = strstr (line, "BinaryFile"); |
| 239 | if (! end) { |
| 240 | Melder_throw (U"File ", file, U" is not a Data binary file."); |
| 241 | } |
| 242 | *end = '\0'; |
| 243 | me = Thing_newFromClassName (Melder_peek8to32_u (line), nullptr).static_cast_move <structDaata>(); |
| 244 | formatVersion = -1; // old version: override version number, which was set to 0 by newFromClassName |
| 245 | rewind (f); |
| 246 | fread (line, 1, (size_t) (end - line) + strlen ("BinaryFile"), f); |
| 247 | } |
| 248 | MelderFile_getParentFolder (file, & Data_directoryBeingRead); |
| 249 | Data_readBinary (me.get(), f, formatVersion); |
| 250 | file -> format = structMelderFile :: Format :: binary; |
| 251 | f.close (file); |
| 252 | return me; |
| 253 | } catch (MelderError) { |
| 254 | Melder_throw (U"Data not read from binary file ", file, U"."); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static int defaultPublish (autoDaata /* me */) { |
| 259 | return 0; // nothing published |
| 260 | } |
| 261 | |
| 262 | static int (*thePublish) (autoDaata) = defaultPublish; |
| 263 | |
| 264 | int Data_publish (autoDaata me) { |
| 265 | return thePublish (me.move()); |
| 266 | } |
| 267 | |
| 268 | void Data_setPublishProc (int (*publish) (autoDaata)) { |
| 269 | thePublish = publish ? publish : defaultPublish; |
| 270 | } |
| 271 | |
| 272 | /* Generic reading. */ |
| 273 | |
| 274 | static int numFileTypeRecognizers = 0; |
| 275 | static Data_FileTypeRecognizer fileTypeRecognizers [100]; |
| 276 | void Data_recognizeFileType (Data_FileTypeRecognizer recognizer) { |
| 277 | Melder_assert (numFileTypeRecognizers < 100); |
| 278 | fileTypeRecognizers [++ numFileTypeRecognizers] = recognizer; |
| 279 | } |
| 280 | |
| 281 | autoDaata Data_readFromFile (MelderFile file) { |
| 282 | char header [513]; |
| 283 | autofile f = Melder_fopen (file, "rb"); |
| 284 | size_t nread_u = fread (& header [0], 1, 512, f); |
| 285 | integer nread = (integer) nread_u; // we know it cannot be more than 512 |
| 286 | f.close (file); |
| 287 | header [nread] = 0; |
| 288 | |
| 289 | /* |
| 290 | Possibility 1: is this file a text file as defined in Data.cpp? |
| 291 | */ |
| 292 | if (nread > 9 && header [0] == 0x1F && header [1] == 0x8B) { |
| 293 | /* |
| 294 | A gzipped file. Assume that it is a text file (e.g. a gzipped TextGrid text file in the Corpus Gesproken Nederlands). |
| 295 | */ |
| 296 | return Data_readFromTextFile (file); |
| 297 | } |
| 298 | if (nread > 11) { |
| 299 | int numberOfBytesInFileType = 0; |
| 300 | char *p = strstr (header, "TextFile"); |
| 301 | if (p) { |
| 302 | numberOfBytesInFileType = 8; |
| 303 | } else { |
| 304 | p = strstr (header, "Text2File"); // future version? |
| 305 | numberOfBytesInFileType = 9; |
| 306 | } |
| 307 | if (p && p - header < nread - numberOfBytesInFileType && p - header < 40) |
| 308 | return Data_readFromTextFile (file); |
| 309 | } |
| 310 | if (nread > 22) { |
| 311 | char headerCopy [101]; |
| 312 | memcpy (headerCopy, header, 100); |
| 313 | headerCopy [100] = '\0'; |
| 314 | for (int i = 0; i < 100; i ++) |
| 315 | if (headerCopy [i] == '\0') |
| 316 | headerCopy [i] = '\001'; |
| 317 | char *p = strstr (headerCopy, "T\001e\001x\001t\001F\001i\001l\001e"); |
| 318 | if (p && p - headerCopy < nread - 15 && p - headerCopy < 80) |
| 319 | return Data_readFromTextFile (file); |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | Possibility 2: is this file a binary file as defined in Data.cpp? |
| 324 | */ |
| 325 | |
| 326 | if (nread > 13) { |
| 327 | int numberOfBytesInFileType = 0; |
| 328 | char *p = strstr (header, "BinaryFile"); |
| 329 | if (p) { |
| 330 | numberOfBytesInFileType = 10; |
| 331 | } else { |
| 332 | p = strstr (header, "Binary2File"); // future version |
| 333 | numberOfBytesInFileType = 11; |
| 334 | } |
| 335 | if (p && p - header < nread - numberOfBytesInFileType && p - header < 40) |
| 336 | return Data_readFromBinaryFile (file); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | Possibility 3: is this file of a type for which a recognizer has been installed? |
| 341 | */ |
| 342 | |
| 343 | MelderFile_getParentFolder (file, & Data_directoryBeingRead); |
| 344 | for (int i = 1; i <= numFileTypeRecognizers; i ++) { |
| 345 | autoDaata object = fileTypeRecognizers [i] (nread, header, file); |
| 346 | if (object) { |
| 347 | if (object -> classInfo == classDaata) // dummy object? the recognizer could have had a side effect, such as drawing a picture |
| 348 | return autoDaata (); // a null return on recognized non-data! |
| 349 | return object; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | Possibility 4: is this a common text file? |
| 355 | */ |
| 356 | |
| 357 | int i = 0; |
| 358 | for (; i < nread; i ++) |
| 359 | if (header [i] < 32 || header [i] > 126) // not ASCII? (note: this expression happens to work correctly for both signed and unsigned char) |
| 360 | break; |
| 361 | if (i >= nread) |
| 362 | return Data_readFromTextFile (file); |
| 363 | |
| 364 | Melder_throw (U"File ", file, U" not recognized."); |
| 365 | } |
| 366 | |
| 367 | /* Recursive routines for working with struct members. */ |
| 368 | |
| 369 | int Data_Description_countMembers (Data_Description structDescription) { |
| 370 | int count = 0; |
| 371 | for (Data_Description desc = structDescription; desc -> name; desc ++) |
| 372 | count ++; |
| 373 | if (structDescription [0]. type == inheritwa) { |
| 374 | Data_Description parentDescription = ((Daata) _Thing_dummyObject ((ClassInfo) structDescription [0]. tagType)) -> v_description (); |
| 375 | if (parentDescription) |
| 376 | return count + Data_Description_countMembers (parentDescription); |
| 377 | } |
| 378 | return count; |
| 379 | } |
| 380 | |
| 381 | Data_Description Data_Description_findMatch (Data_Description structDescription, conststring32 name) { |
| 382 | for (Data_Description desc = structDescription; desc -> name; desc ++) |
| 383 | if (str32equ (name, desc -> name)) return desc; |
| 384 | if (structDescription [0]. type == inheritwa) { |
| 385 | Data_Description parentDescription = ((Daata) _Thing_dummyObject ((ClassInfo) structDescription [0]. tagType)) -> v_description (); |
| 386 | if (parentDescription) |
| 387 | return Data_Description_findMatch (parentDescription, name); |
| 388 | } |
| 389 | return nullptr; // not found |
| 390 | } |
| 391 | |
| 392 | Data_Description Data_Description_findNumberUse (Data_Description structDescription, conststring32 string) { |
| 393 | for (Data_Description desc = structDescription; desc -> name; desc ++) { |
| 394 | if (desc -> max1 && str32equ (desc -> max1, string)) return desc; |
| 395 | if (desc -> max2 && str32equ (desc -> max2, string)) return desc; |
| 396 | } |
| 397 | if (structDescription [0]. type == inheritwa) { |
| 398 | Data_Description parentDescription = ((Daata) _Thing_dummyObject ((ClassInfo) structDescription [0]. tagType)) -> v_description (); |
| 399 | if (parentDescription) |
| 400 | return Data_Description_findNumberUse (parentDescription, string); |
| 401 | } |
| 402 | return nullptr; |
| 403 | } |
| 404 | |
| 405 | /* Retrieving data from object + description. */ |
| 406 | |
| 407 | int64 Data_Description_integer (void *address, Data_Description description) { |
| 408 | switch (description -> type) { |
| 409 | case bytewa: return * (signed char *) ((char *) address + description -> offset); |
| 410 | case int16wa: return * (int16 *) ((char *) address + description -> offset); |
| 411 | case intwa: return * (int *) ((char *) address + description -> offset); |
| 412 | case integerwa: return * (integer *) ((char *) address + description -> offset); |
| 413 | case ubytewa: return * (unsigned char *) ((char *) address + description -> offset); |
| 414 | case uintwa: return * (unsigned int *) ((char *) address + description -> offset); |
| 415 | case uintegerwa: return (int64) * (uinteger *) ((char *) address + description -> offset); // ignore numbers above 2^63 - 1 |
| 416 | case questionwa: return * (bool *) ((char *) address + description -> offset); |
| 417 | case objectwa: return (* (Collection *) ((char *) address + description -> offset))->size; // FIXME: alignment not guaranteed |
| 418 | case collectionofwa: return ( (Collection) ((char *) address + description -> offset))->size; // FIXME: alignment not guaranteed |
| 419 | case collectionwa: return (* (Collection *) ((char *) address + description -> offset))->size; // FIXME: alignment not guaranteed |
| 420 | default: return 0; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | int Data_Description_evaluateInteger (void *structAddress, Data_Description structDescription, |
| 425 | conststring32 formula, integer *result) |
| 426 | { |
| 427 | if (! formula) { // this was a VECTOR_FROM array |
| 428 | *result = 1; |
| 429 | return 1; |
| 430 | } |
| 431 | if (formula [0] >= U'a' && formula [0] <= U'z') { |
| 432 | char32 buffer [100], *minus1, *plus1, *psize; |
| 433 | Data_Description sizeDescription; |
| 434 | str32cpy (buffer, formula); |
| 435 | if ((minus1 = str32str (buffer, U" - 1")) != nullptr) |
| 436 | *minus1 = U'\0'; // strip trailing " - 1", but remember |
| 437 | if ((plus1 = str32str (buffer, U" + 1")) != nullptr) |
| 438 | *plus1 = U'\0'; // strip trailing " + 1", but remember |
| 439 | if ((psize = str32str (buffer, U" -> size")) != nullptr) |
| 440 | *psize = U'\0'; // strip trailing " -> size" |
| 441 | if (! (sizeDescription = Data_Description_findMatch (structDescription, buffer))) { |
| 442 | *result = 0; |
| 443 | return 0 /*Melder_error ("Cannot find member \"%ls\".", buffer)*/; |
| 444 | } |
| 445 | *result = Data_Description_integer (structAddress, sizeDescription); |
| 446 | if (minus1) *result -= 1; |
| 447 | if (plus1) *result += 1; |
| 448 | } else { |
| 449 | *result = Melder_atoi (formula); |
| 450 | } |
| 451 | return 1; |
| 452 | } |
| 453 | |
| 454 | /* End of file Data.cpp */ |