| 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"cxx . . $ `<file>/sys/Collection.h`/ |
| 20 | |
| 21 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Daata, Thing, 0); |
| 22 | |
| 23 | autoDaatacxx . . $ autoDaata# _Data_copycxx . . $ _Data_copy(a046ccb3339e7e89). (constDaatacxx . . $ constDaata# me) { |
| 24 | try { |
| 25 | if (! me) |
| 26 | return autoDaatacxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).(); |
| 27 | autoDaatacxx . . $ autoDaata# thee = Thing_newFromClasscxx . . $ Thing_newFromClass(f67a3a815c0fee1d). (mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo.).static_cast_movecxx . . $ autoSomeThing#static_cast_move(8f979c7064dbabcf). <structDaatacxx . . $ structDaata#>(); |
| 28 | mycxx . . $ `melder/melder.h:44:9`! v1_copycxx . . $ structDaata#v1_copy(42659c2335f2eda7). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 29 | Thing_setNamecxx . . $ Thing_setName(b5a42e5e01c267b5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), mycxx . . $ `melder/melder.h:44:9`! namecxx . . $ structThing#name..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 30 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 31 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 32 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not copied."); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | bool Data_equalcxx . . $ Data_equal(6df46d860ee4054f). (Daatacxx . . $ Daata# me, Daatacxx . . $ Daata# thee) { |
| 37 | if (mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. != thycxx . . $ `melder/melder.h:45:9`! classInfocxx . . $ structThing#classInfo.) |
| 38 | return false; // different class: not equal |
| 39 | int offset = sizeof (struct structDaatacxx . . $ structDaata#); // we already compared the methods, and are going to skip the names |
| 40 | if (! memcmp ((char *) me + offset, (char *) thee + offset, mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> sizecxx . . $ structClassInfo#size. - offset)) // BUG: not necessarily portable |
| 41 | return true; // no shallow differences |
| 42 | return mycxx . . $ `melder/melder.h:44:9`! v1_equalcxx . . $ structDaata#v1_equal(de90b88e0a4bb7f7). (thee); |
| 43 | } |
| 44 | |
| 45 | bool Data_canWriteAsEncodingcxx . . $ Data_canWriteAsEncoding(c7ae84fff4422498). (Daatacxx . . $ Daata# me, int encoding) { |
| 46 | return mycxx . . $ `melder/melder.h:44:9`! v1_canWriteAsEncodingcxx . . $ structDaata#v1_canWriteAsEncoding(83c267b20bac841a). (encoding); |
| 47 | } |
| 48 | |
| 49 | bool Data_canWriteTextcxx . . $ Data_canWriteText(de90b88e0a4bb7f7). (Daatacxx . . $ Daata# me) { |
| 50 | return mycxx . . $ `melder/melder.h:44:9`! v_writablecxx . . $ structDaata#v_writable(c9857a73d68e59ba). (); |
| 51 | } |
| 52 | |
| 53 | void Data_writeTextcxx . . $ Data_writeText(c3dcdea1be00195). (Daatacxx . . $ Daata# me, MelderFilecxx . . $ MelderFile# openFile) { |
| 54 | mycxx . . $ `melder/melder.h:44:9`! v1_writeTextcxx . . $ structDaata#v1_writeText(1145552d769d7b5d). (openFile); |
| 55 | if (ferror (openFile -> filePointercxx . . $ structMelderFile#filePointer.)) |
| 56 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"I/O error."); |
| 57 | } |
| 58 | |
| 59 | MelderFilecxx . . $ MelderFile# Data_createTextFilecxx . . $ Data_createTextFile(e035bb54dc67a3c9). (Daatacxx . . $ Daata# me, MelderFilecxx . . $ MelderFile# file, bool verbose) { |
| 60 | autoMelderFilecxx . . $ autoMelderFile# mfile = MelderFile_createcxx . . $ autoMelderFile#autoMelderFile(1145552d769d7b5d). (file); |
| 61 | #if defined (_WIN32) |
| 62 | file -> requiresCRLF = true; |
| 63 | #endif |
| 64 | file -> verbosecxx . . $ structMelderFile#verbose. = verbose; |
| 65 | file -> outputEncodingcxx . . $ structMelderFile#outputEncoding. = (int) Melder_getOutputEncodingcxx . . $ Melder_getOutputEncoding(581d0d572cba43d7). (); |
| 66 | if (file -> outputEncodingcxx . . $ structMelderFile#outputEncoding. == (int) kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::ASCII_THEN_UTF16cxx . . $ kMelder_textOutputEncoding#ASCII_THEN_UTF16.) |
| 67 | file -> outputEncodingcxx . . $ structMelderFile#outputEncoding. = Data_canWriteAsEncodingcxx . . $ Data_canWriteAsEncoding(c7ae84fff4422498). (me, kMelder_textOutputEncoding_ASCIIcxx . . $ kMelder_textOutputEncoding_ASCII.) ? |
| 68 | kMelder_textOutputEncoding_ASCIIcxx . . $ kMelder_textOutputEncoding_ASCII. : (int) kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::UTF16cxx . . $ kMelder_textOutputEncoding#UTF16.; |
| 69 | else if (file -> outputEncodingcxx . . $ structMelderFile#outputEncoding. == (int) kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::ISO_LATIN1_THEN_UTF16cxx . . $ kMelder_textOutputEncoding#ISO_LATIN1_THEN_UTF16.) |
| 70 | file -> outputEncodingcxx . . $ structMelderFile#outputEncoding. = Data_canWriteAsEncodingcxx . . $ Data_canWriteAsEncoding(c7ae84fff4422498). (me, kMelder_textOutputEncoding_ISO_LATIN1cxx . . $ kMelder_textOutputEncoding_ISO_LATIN1.) ? |
| 71 | kMelder_textOutputEncoding_ISO_LATIN1cxx . . $ kMelder_textOutputEncoding_ISO_LATIN1. : (int) kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::UTF16cxx . . $ kMelder_textOutputEncoding#UTF16.; |
| 72 | if (file -> outputEncodingcxx . . $ structMelderFile#outputEncoding. == (int) kMelder_textOutputEncodingcxx . . $ kMelder_textOutputEncoding#::UTF16cxx . . $ kMelder_textOutputEncoding#UTF16.) |
| 73 | binputu16cxx . . $ binputu16(d4e8792de0db7e67). (0xfeff, file -> filePointercxx . . $ structMelderFile#filePointer.); |
| 74 | return mfile.transfercxx . . $ autoMelderFile#transfer(785485829d202295).(); |
| 75 | } |
| 76 | |
| 77 | static void _Data_writeToTextFilecxx . . $ _Data_writeToTextFile(18295fd794e9f6d2). (Daatacxx . . $ Daata# me, MelderFilecxx . . $ MelderFile# file, bool verbose) { |
| 78 | try { |
| 79 | if (! Data_canWriteTextcxx . . $ Data_canWriteText(de90b88e0a4bb7f7). (me)) |
| 80 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Objects of class ", mycxx . . $ `melder/melder.h:44:9`! classInfo -> className, U" cannot be written to a text file."); |
| 81 | autoMelderFilecxx . . $ autoMelderFile# mfile = Data_createTextFilecxx . . $ autoMelderFile#autoMelderFile(1145552d769d7b5d). (me, file, verbose); |
| 82 | #ifndef _WIN32 |
| 83 | flockfile (file -> filePointercxx . . $ structMelderFile#filePointer.); // BUG |
| 84 | #endif |
| 85 | MelderFile_writecxx . . $ MelderFile_write(291fa987bb2f47e8). (file, U"File type = \"ooTextFile\"\nObject class = \"", mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className.); |
| 86 | if (mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> versioncxx . . $ structClassInfo#version. > 0) |
| 87 | MelderFile_writecxx . . $ MelderFile_write(291fa987bb2f47e8). (file, U" ", mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> versioncxx . . $ structClassInfo#version.); |
| 88 | MelderFile_writecxx . . $ MelderFile_write(291fa987bb2f47e8). (file, U"\"\n"); |
| 89 | Data_writeTextcxx . . $ Data_writeText(c3dcdea1be00195). (me, file); |
| 90 | MelderFile_writeCharactercxx . . $ MelderFile_writeCharacter(a5a5190e8fc7020a). (file, U'\n'); |
| 91 | #ifndef _WIN32 |
| 92 | if (file -> filePointercxx . . $ structMelderFile#filePointer.) |
| 93 | funlockfile (file -> filePointercxx . . $ structMelderFile#filePointer.); |
| 94 | #endif |
| 95 | mfile.closecxx . . $ autoMelderFile#close(49f6e7a06ebc5aa8). (); |
| 96 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 97 | #ifndef _WIN32 |
| 98 | if (file -> filePointercxx . . $ structMelderFile#filePointer.) |
| 99 | funlockfile (file -> filePointercxx . . $ structMelderFile#filePointer.); // the file pointer is null before Data_createTextFile() and after mfile.close() |
| 100 | #endif |
| 101 | throw; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void Data_writeToTextFilecxx . . $ Data_writeToTextFile(c3dcdea1be00195). (Daatacxx . . $ Daata# me, MelderFilecxx . . $ MelderFile# file) { |
| 106 | try { |
| 107 | _Data_writeToTextFilecxx . . $ _Data_writeToTextFile(18295fd794e9f6d2). (me, file, true); |
| 108 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 109 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not written to text file ", file, U"."); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void Data_writeToShortTextFilecxx . . $ Data_writeToShortTextFile(c3dcdea1be00195). (Daatacxx . . $ Daata# me, MelderFilecxx . . $ MelderFile# file) { |
| 114 | try { |
| 115 | _Data_writeToTextFilecxx . . $ _Data_writeToTextFile(18295fd794e9f6d2). (me, file, false); |
| 116 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 117 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not written to short text file ", file, U"."); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | bool Data_canWriteBinarycxx . . $ Data_canWriteBinary(de90b88e0a4bb7f7). (Daatacxx . . $ Daata# me) { |
| 122 | return mycxx . . $ `melder/melder.h:44:9`! v_writablecxx . . $ structDaata#v_writable(c9857a73d68e59ba). (); |
| 123 | } |
| 124 | |
| 125 | void Data_writeBinarycxx . . $ Data_writeBinary(68e9d566a624648b). (Daatacxx . . $ Daata# me, FILE *f) { |
| 126 | mycxx . . $ `melder/melder.h:44:9`! v1_writeBinarycxx . . $ structDaata#v1_writeBinary(8d4262d45fc644ab). (f); |
| 127 | if (ferror (f)) |
| 128 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"I/O error."); |
| 129 | } |
| 130 | |
| 131 | void Data_writeToBinaryFilecxx . . $ Data_writeToBinaryFile(c3dcdea1be00195). (Daatacxx . . $ Daata# me, MelderFilecxx . . $ MelderFile# file) { |
| 132 | try { |
| 133 | if (! Data_canWriteBinarycxx . . $ Data_canWriteBinary(de90b88e0a4bb7f7). (me)) |
| 134 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Objects of class ", mycxx . . $ `melder/melder.h:44:9`! classInfo -> className, U" cannot be written to a generic binary file."); |
| 135 | autoMelderFilecxx . . $ autoMelderFile# mfile = MelderFile_createcxx . . $ autoMelderFile#autoMelderFile(1145552d769d7b5d). (file); |
| 136 | if (fprintf (file -> filePointercxx . . $ structMelderFile#filePointer., "ooBinaryFile") < 0) |
| 137 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot write first bytes of file."); |
| 138 | binputw8cxx . . $ binputw8(43ad66ee48fe33fd). ( |
| 139 | mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> versioncxx . . $ structClassInfo#version. > 0 ? |
| 140 | Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className., U" ", mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> versioncxx . . $ structClassInfo#version.) : |
| 141 | mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className., |
| 142 | file -> filePointercxx . . $ structMelderFile#filePointer.); |
| 143 | Data_writeBinarycxx . . $ Data_writeBinary(68e9d566a624648b). (me, file -> filePointercxx . . $ structMelderFile#filePointer.); |
| 144 | mfile.closecxx . . $ autoMelderFile#close(49f6e7a06ebc5aa8). (); |
| 145 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 146 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not written to binary file ", file, U"."); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | bool Data_canReadTextcxx . . $ Data_canReadText(de90b88e0a4bb7f7). (Daatacxx . . $ Daata# me) { |
| 151 | return mycxx . . $ `melder/melder.h:44:9`! v_writablecxx . . $ structDaata#v_writable(c9857a73d68e59ba). (); |
| 152 | } |
| 153 | |
| 154 | void Data_readTextcxx . . $ Data_readText(e29ade1c14d5e948). (Daatacxx . . $ Daata# me, MelderReadTextcxx . . $ MelderReadText# text, int formatVersion) { |
| 155 | try { |
| 156 | mycxx . . $ `melder/melder.h:44:9`! v1_readTextcxx . . $ structDaata#v1_readText(97c85d3b778168ad). (text, formatVersion); |
| 157 | mycxx . . $ `melder/melder.h:44:9`! v_repaircxx . . $ structDaata#v_repair(49f6e7a06ebc5aa8). (); |
| 158 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 159 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (Thing_className (me), U" not read."); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | autoDaatacxx . . $ autoDaata# Data_readFromTextFilecxx . . $ Data_readFromTextFile(da9ba57d6e09e2a7). (MelderFilecxx . . $ MelderFile# file) { |
| 164 | try { |
| 165 | autoMelderReadTextcxx . . $ autoMelderReadText# text = MelderReadText_createFromFilecxx . . $ MelderReadText_createFromFile(30337d9e1397edb4). (file); |
| 166 | const mutablestring32cxx . . $ mutablestring32# line = MelderReadText_readLinecxx . . $ MelderReadText_readLine(f4aa1d9b94177768). (text.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()); |
| 167 | if (! line) |
| 168 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (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 (str32strcxx . . $ str32str(2de17cd8f7c584a6). (line, U"ooText2File")) |
| 175 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"This Praat version cannot read this Praat file. Please download a newer version of Praat."); |
| 176 | char32cxx . . $ char32# *end = str32strcxx . . $ str32str(2de17cd8f7c584a6). (line, U"ooTextFile"); // oo format? |
| 177 | autoDaatacxx . . $ autoDaata# mecxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).; |
| 178 | int formatVersion; |
| 179 | if (end) { |
| 180 | autostring32cxx . . $ autostring32# klas = texgetw16cxx . . $ texgetw16(6d8daa32ac6455b9). (text.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()); |
| 181 | me =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Thing_newFromClassNamecxx . . $ Thing_newFromClassName(87ac77d99e238079). (klas.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), & formatVersion).static_cast_movecxx . . $ autoSomeThing#static_cast_move(8f979c7064dbabcf). <structDaatacxx . . $ structDaata#>(); |
| 182 | } else { |
| 183 | end = str32strcxx . . $ str32str(2de17cd8f7c584a6). (line, U"TextFile"); |
| 184 | if (! end) |
| 185 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Not an old-type text file; should not occur."); |
| 186 | *end = U'\0'; |
| 187 | me =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Thing_newFromClassNamecxx . . $ Thing_newFromClassName(87ac77d99e238079). (line, nullptr).static_cast_movecxx . . $ autoSomeThing#static_cast_move(8f979c7064dbabcf). <structDaatacxx . . $ structDaata#>(); |
| 188 | formatVersion = -1; // old version |
| 189 | } |
| 190 | MelderFile_getParentFoldercxx . . $ MelderFile_getParentFolder(fd81a6dabee805b). (file, & Data_directoryBeingReadcxx . . $ Data_directoryBeingRead.); |
| 191 | Data_readTextcxx . . $ Data_readText(e29ade1c14d5e948). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), text.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), formatVersion); |
| 192 | file -> formatcxx . . $ structMelderFile#format. = structMelderFilecxx . . $ structMelderFile# :: Formatcxx . . $ structMelderFile#Format# :: textcxx . . $ structMelderFile#Format#text.; |
| 193 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 194 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 195 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Data not read from text file ", file, U"."); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | bool Data_canReadBinarycxx . . $ Data_canReadBinary(de90b88e0a4bb7f7). (Daatacxx . . $ Daata# me) { |
| 200 | return mycxx . . $ `melder/melder.h:44:9`! v_writablecxx . . $ structDaata#v_writable(c9857a73d68e59ba). (); |
| 201 | } |
| 202 | |
| 203 | void Data_readBinarycxx . . $ Data_readBinary(dfb403fb68f74b69). (Daatacxx . . $ Daata# me, FILE *f, int formatVersion) { |
| 204 | try { |
| 205 | mycxx . . $ `melder/melder.h:44:9`! v1_readBinarycxx . . $ structDaata#v1_readBinary(e780f64711fb334f). (f, formatVersion); |
| 206 | if (feof (f)) |
| 207 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Early end of file."); |
| 208 | if (ferror (f)) |
| 209 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"I/O error."); |
| 210 | mycxx . . $ `melder/melder.h:44:9`! v_repaircxx . . $ structDaata#v_repair(49f6e7a06ebc5aa8). (); |
| 211 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 212 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (Thing_className (me), U" not read."); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | autoDaatacxx . . $ autoDaata# Data_readFromBinaryFilecxx . . $ Data_readFromBinaryFile(da9ba57d6e09e2a7). (MelderFilecxx . . $ MelderFile# file) { |
| 217 | try { |
| 218 | autofilecxx . . $ autofile# f = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (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_throwcxx . . $ `melder/melder_error.h:77:9`! (U"This Praat version cannot read this Praat file. Please download a newer version of Praat."); |
| 230 | char *end = strstr (line, "ooBinaryFile"); |
| 231 | autoDaatacxx . . $ autoDaata# mecxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).; |
| 232 | int formatVersion; |
| 233 | if (end) { |
| 234 | fseek (f, strlen ("ooBinaryFile"), 0); |
| 235 | autostring8cxx . . $ autostring8# klas = bingets8cxx . . $ bingets8(5a336ef3276fb3b5). (f); |
| 236 | me =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Thing_newFromClassNamecxx . . $ Thing_newFromClassName(87ac77d99e238079). (Melder_peek8to32_u (klas.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()), & formatVersion).static_cast_movecxx . . $ autoSomeThing#static_cast_move(8f979c7064dbabcf). <structDaatacxx . . $ structDaata#>(); |
| 237 | } else { |
| 238 | end = strstr (line, "BinaryFile"); |
| 239 | if (! end) { |
| 240 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"File ", file, U" is not a Data binary file."); |
| 241 | } |
| 242 | *end = '\0'; |
| 243 | me =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Thing_newFromClassNamecxx . . $ Thing_newFromClassName(87ac77d99e238079). (Melder_peek8to32_u (line), nullptr).static_cast_movecxx . . $ autoSomeThing#static_cast_move(8f979c7064dbabcf). <structDaatacxx . . $ 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_getParentFoldercxx . . $ MelderFile_getParentFolder(fd81a6dabee805b). (file, & Data_directoryBeingReadcxx . . $ Data_directoryBeingRead.); |
| 249 | Data_readBinarycxx . . $ Data_readBinary(dfb403fb68f74b69). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), f, formatVersion); |
| 250 | file -> formatcxx . . $ structMelderFile#format. = structMelderFilecxx . . $ structMelderFile# :: Formatcxx . . $ structMelderFile#Format# :: binarycxx . . $ structMelderFile#Format#binary.; |
| 251 | f.closecxx . . $ autofile#close(1145552d769d7b5d). (file); |
| 252 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 253 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 254 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Data not read from binary file ", file, U"."); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static int defaultPublishcxx . . $ defaultPublish(795020d01765d2fa). (autoDaatacxx . . $ autoDaata# /* me */) { |
| 259 | return 0; // nothing published |
| 260 | } |
| 261 | |
| 262 | static int (*thePublishcxx . . $ thePublish.) (autoDaatacxx . . $ autoDaata#) = defaultPublishcxx . . $ defaultPublish(795020d01765d2fa).; |
| 263 | |
| 264 | int Data_publishcxx . . $ Data_publish(795020d01765d2fa). (autoDaatacxx . . $ autoDaata# me) { |
| 265 | return thePublishcxx . . $ thePublish. (mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).()); |
| 266 | } |
| 267 | |
| 268 | void Data_setPublishProccxx . . $ Data_setPublishProc(222770ccd595104f). (int (*publish) (autoDaatacxx . . $ autoDaata#)) { |
| 269 | thePublishcxx . . $ thePublish. = publish ? publish : defaultPublishcxx . . $ defaultPublish(795020d01765d2fa).; |
| 270 | } |
| 271 | |
| 272 | /* Generic reading. */ |
| 273 | |
| 274 | static int numFileTypeRecognizerscxx . . $ numFileTypeRecognizers. = 0; |
| 275 | static Data_FileTypeRecognizercxx . . $ Data_FileTypeRecognizer# fileTypeRecognizerscxx . . $ fileTypeRecognizers. [100]; |
| 276 | void Data_recognizeFileTypecxx . . $ Data_recognizeFileType(7ed4186908013b3d). (Data_FileTypeRecognizercxx . . $ Data_FileTypeRecognizer# recognizer) { |
| 277 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (numFileTypeRecognizers < 100); |
| 278 | fileTypeRecognizerscxx . . $ fileTypeRecognizers. [++ numFileTypeRecognizerscxx . . $ numFileTypeRecognizers.] = recognizer; |
| 279 | } |
| 280 | |
| 281 | autoDaatacxx . . $ autoDaata# Data_readFromFilecxx . . $ Data_readFromFile(da9ba57d6e09e2a7). (MelderFilecxx . . $ MelderFile# file) { |
| 282 | char header [513]; |
| 283 | autofilecxx . . $ autofile# f = Melder_fopencxx . . $ autofile#autofile(8d4262d45fc644ab). (file, "rb"); |
| 284 | size_t nread_u = fread (& header [0], 1, 512, f); |
| 285 | integercxx . . $ integer# nread = (integercxx . . $ integer#) nread_u; // we know it cannot be more than 512 |
| 286 | f.closecxx . . $ autofile#close(1145552d769d7b5d). (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_readFromTextFilecxx . . $ Data_readFromTextFile(da9ba57d6e09e2a7). (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_readFromTextFilecxx . . $ Data_readFromTextFile(da9ba57d6e09e2a7). (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_readFromTextFilecxx . . $ Data_readFromTextFile(da9ba57d6e09e2a7). (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_readFromBinaryFilecxx . . $ Data_readFromBinaryFile(da9ba57d6e09e2a7). (file); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | Possibility 3: is this file of a type for which a recognizer has been installed? |
| 341 | */ |
| 342 | |
| 343 | MelderFile_getParentFoldercxx . . $ MelderFile_getParentFolder(fd81a6dabee805b). (file, & Data_directoryBeingReadcxx . . $ Data_directoryBeingRead.); |
| 344 | for (int i = 1; i <= numFileTypeRecognizerscxx . . $ numFileTypeRecognizers.; i ++) { |
| 345 | autoDaatacxx . . $ autoDaata# object = fileTypeRecognizerscxx . . $ fileTypeRecognizers. [i] (nread, header, file); |
| 346 | if (object) { |
| 347 | if (object ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). classInfocxx . . $ structThing#classInfo. == classDaatacxx . . $ classDaata.) // dummy object? the recognizer could have had a side effect, such as drawing a picture |
| 348 | return autoDaatacxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8). (); // a null return on recognized non-data! |
| 349 | return objectcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 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_readFromTextFilecxx . . $ Data_readFromTextFile(da9ba57d6e09e2a7). (file); |
| 363 | |
| 364 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"File ", file, U" not recognized."); |
| 365 | } |
| 366 | |
| 367 | /* Recursive routines for working with struct members. */ |
| 368 | |
| 369 | int Data_Description_countMemberscxx . . $ Data_Description_countMembers(e486611939ed5677). (Data_Descriptioncxx . . $ Data_Description# structDescription) { |
| 370 | int count = 0; |
| 371 | for (Data_Descriptioncxx . . $ Data_Description# desc = structDescription; desc -> namecxx . . $ structData_Description#name.; desc ++) |
| 372 | count ++; |
| 373 | if (structDescription [0]. typecxx . . $ structData_Description#type. == inheritwacxx . . $ `sys/Data.h:354:9`!) { |
| 374 | Data_Descriptioncxx . . $ Data_Description# parentDescription = ((Daatacxx . . $ Daata#) _Thing_dummyObjectcxx . . $ _Thing_dummyObject(5a47185acae0d935). ((ClassInfocxx . . $ ClassInfo#) structDescription [0]. tagTypecxx . . $ structData_Description#tagType.)) -> v_descriptioncxx . . $ structDaata#v_description(669317afad7f96e1). (); |
| 375 | if (parentDescription) |
| 376 | return count + Data_Description_countMemberscxx . . $ Data_Description_countMembers(e486611939ed5677). (parentDescription); |
| 377 | } |
| 378 | return count; |
| 379 | } |
| 380 | |
| 381 | Data_Descriptioncxx . . $ Data_Description# Data_Description_findMatchcxx . . $ Data_Description_findMatch(4e4ed69fbd16d6e0). (Data_Descriptioncxx . . $ Data_Description# structDescription, conststring32cxx . . $ conststring32# name) { |
| 382 | for (Data_Descriptioncxx . . $ Data_Description# desc = structDescription; desc -> namecxx . . $ structData_Description#name.; desc ++) |
| 383 | if (str32equcxx . . $ `melder/melder_str32.h:122:9`! (name, desc -> namecxx . . $ structData_Description#name.)) return desc; |
| 384 | if (structDescription [0]. typecxx . . $ structData_Description#type. == inheritwacxx . . $ `sys/Data.h:354:9`!) { |
| 385 | Data_Descriptioncxx . . $ Data_Description# parentDescription = ((Daatacxx . . $ Daata#) _Thing_dummyObjectcxx . . $ _Thing_dummyObject(5a47185acae0d935). ((ClassInfocxx . . $ ClassInfo#) structDescription [0]. tagTypecxx . . $ structData_Description#tagType.)) -> v_descriptioncxx . . $ structDaata#v_description(669317afad7f96e1). (); |
| 386 | if (parentDescription) |
| 387 | return Data_Description_findMatchcxx . . $ Data_Description_findMatch(4e4ed69fbd16d6e0). (parentDescription, name); |
| 388 | } |
| 389 | return nullptr; // not found |
| 390 | } |
| 391 | |
| 392 | Data_Descriptioncxx . . $ Data_Description# Data_Description_findNumberUsecxx . . $ Data_Description_findNumberUse(4e4ed69fbd16d6e0). (Data_Descriptioncxx . . $ Data_Description# structDescription, conststring32cxx . . $ conststring32# string) { |
| 393 | for (Data_Descriptioncxx . . $ Data_Description# desc = structDescription; desc -> namecxx . . $ structData_Description#name.; desc ++) { |
| 394 | if (desc -> max1cxx . . $ structData_Description#max1. && str32equcxx . . $ `melder/melder_str32.h:122:9`! (desc -> max1cxx . . $ structData_Description#max1., string)) return desc; |
| 395 | if (desc -> max2cxx . . $ structData_Description#max2. && str32equcxx . . $ `melder/melder_str32.h:122:9`! (desc -> max2cxx . . $ structData_Description#max2., string)) return desc; |
| 396 | } |
| 397 | if (structDescription [0]. typecxx . . $ structData_Description#type. == inheritwacxx . . $ `sys/Data.h:354:9`!) { |
| 398 | Data_Descriptioncxx . . $ Data_Description# parentDescription = ((Daatacxx . . $ Daata#) _Thing_dummyObjectcxx . . $ _Thing_dummyObject(5a47185acae0d935). ((ClassInfocxx . . $ ClassInfo#) structDescription [0]. tagTypecxx . . $ structData_Description#tagType.)) -> v_descriptioncxx . . $ structDaata#v_description(669317afad7f96e1). (); |
| 399 | if (parentDescription) |
| 400 | return Data_Description_findNumberUsecxx . . $ Data_Description_findNumberUse(4e4ed69fbd16d6e0). (parentDescription, string); |
| 401 | } |
| 402 | return nullptr; |
| 403 | } |
| 404 | |
| 405 | /* Retrieving data from object + description. */ |
| 406 | |
| 407 | int64cxx . . $ int64# Data_Description_integercxx . . $ Data_Description_integer(e5a3eeb60b524453). (void *address, Data_Descriptioncxx . . $ Data_Description# description) { |
| 408 | switch (description -> typecxx . . $ structData_Description#type.) { |
| 409 | case bytewacxx . . $ `sys/Data.h:332:9`!: return * (signed char *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.); |
| 410 | case int16wacxx . . $ `sys/Data.h:333:9`!: return * (int16cxx . . $ int16# *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.); |
| 411 | case intwacxx . . $ `sys/Data.h:334:9`!: return * (int *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.); |
| 412 | case integerwacxx . . $ `sys/Data.h:335:9`!: return * (integercxx . . $ integer# *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.); |
| 413 | case ubytewacxx . . $ `sys/Data.h:336:9`!: return * (unsigned char *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.); |
| 414 | case uintwacxx . . $ `sys/Data.h:337:9`!: return * (unsigned int *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.); |
| 415 | case uintegerwacxx . . $ `sys/Data.h:338:9`!: return (int64cxx . . $ int64#) * (uintegercxx . . $ uinteger# *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.); // ignore numbers above 2^63 - 1 |
| 416 | case questionwacxx . . $ `sys/Data.h:345:9`!: return * (bool *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.); |
| 417 | case objectwacxx . . $ `sys/Data.h:351:9`!: return (* (Collectioncxx . . $ Collection# *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.))->sizecxx . . $ CollectionOf#size.; // FIXME: alignment not guaranteed |
| 418 | case collectionofwacxx . . $ `sys/Data.h:352:9`!: return ( (Collectioncxx . . $ Collection#) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.))->sizecxx . . $ CollectionOf#size.; // FIXME: alignment not guaranteed |
| 419 | case collectionwacxx . . $ `sys/Data.h:353:9`!: return (* (Collectioncxx . . $ Collection# *) ((char *) address + description -> offsetcxx . . $ structData_Description#offset.))->sizecxx . . $ CollectionOf#size.; // FIXME: alignment not guaranteed |
| 420 | default: return 0; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | int Data_Description_evaluateIntegercxx . . $ Data_Description_evaluateInteger(9a36bcb04ff11e98). (void *structAddress, Data_Descriptioncxx . . $ Data_Description# structDescription, |
| 425 | conststring32cxx . . $ conststring32# formula, integercxx . . $ 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 | char32cxx . . $ char32# buffer [100], *minus1, *plus1, *psize; |
| 433 | Data_Descriptioncxx . . $ Data_Description# sizeDescription; |
| 434 | str32cpycxx . . $ str32cpy(6ab934322573b179). (buffer, formula); |
| 435 | if ((minus1 = str32strcxx . . $ str32str(2de17cd8f7c584a6). (buffer, U" - 1")) != nullptr) |
| 436 | *minus1 = U'\0'; // strip trailing " - 1", but remember |
| 437 | if ((plus1 = str32strcxx . . $ str32str(2de17cd8f7c584a6). (buffer, U" + 1")) != nullptr) |
| 438 | *plus1 = U'\0'; // strip trailing " + 1", but remember |
| 439 | if ((psize = str32strcxx . . $ str32str(2de17cd8f7c584a6). (buffer, U" -> size")) != nullptr) |
| 440 | *psize = U'\0'; // strip trailing " -> size" |
| 441 | if (! (sizeDescription = Data_Description_findMatchcxx . . $ Data_Description_findMatch(4e4ed69fbd16d6e0). (structDescription, buffer))) { |
| 442 | *result = 0; |
| 443 | return 0 /*Melder_error ("Cannot find member \"%ls\".", buffer)*/; |
| 444 | } |
| 445 | *result = Data_Description_integercxx . . $ Data_Description_integer(e5a3eeb60b524453). (structAddress, sizeDescription); |
| 446 | if (minus1) *result -= 1; |
| 447 | if (plus1) *result += 1; |
| 448 | } else { |
| 449 | *result = Melder_atoicxx . . $ Melder_atoi(c94c67ff4219bf83). (formula); |
| 450 | } |
| 451 | return 1; |
| 452 | } |
| 453 | |
| 454 | /* End of file Data.cpp */ |