| 1 | #ifndef _Data_h_ |
| 2 | #define _Data_h_ |
| 3 | /* Data.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2009,2011,2012,2014-2019,2021-2024 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | /* Data inherits from Thing. */ |
| 22 | /* It adds the functionality of reproduction, comparison, reading, and writing. */ |
| 23 | #include "Thing.h" |
| 24 | |
| 25 | typedef struct structData_Description { |
| 26 | conststring32 name; // the name of this field |
| 27 | int type; // bytewa..inheritwa, see below |
| 28 | int offset; // the offset of this field in the enveloping struct |
| 29 | int size; // the size of this field if it is in an array |
| 30 | conststring32 tagName; // for structs: tag; for classes: class name; for enums: type name |
| 31 | void *tagType; // for structs: offset table; for classes: class pointer; for enums: enum pointer |
| 32 | int rank; // 0 = single, 1 = vector, 2 = matrix, 3 = set, -1 = array |
| 33 | conststring32 min1, max1; // for vectors and matrices |
| 34 | conststring32 min2, max2; // for matrices |
| 35 | } *Data_Description; |
| 36 | |
| 37 | /* |
| 38 | * We call the class Daata instead of Data, |
| 39 | * because of a conflict with the use of "Data" in assembler strings in intrin-impl.h on Windows. |
| 40 | */ |
| 41 | |
| 42 | Thing_define (Daata, Thing) { |
| 43 | virtual void v1_copy (Daata /* data_to */) const { } |
| 44 | virtual bool v1_equal (Daata /* otherData */) { |
| 45 | return true; // names of "identical" objects are allowed to be different |
| 46 | } |
| 47 | virtual Data_Description v_description () { return nullptr; } |
| 48 | virtual bool v_writable () { return true; } |
| 49 | virtual bool v1_canWriteAsEncoding (int /* outputEncoding */) { return true; } |
| 50 | virtual void v1_writeText (MelderFile /* openFile */) { } |
| 51 | virtual void v1_readText (MelderReadText /* text */, int /* formatVersion */) { } |
| 52 | virtual void v1_writeBinary (FILE * /* f */) { } |
| 53 | virtual void v1_readBinary (FILE * /* f */, int /* formatVersion */) { } |
| 54 | virtual void v_repair () { } // after reading Praat data files created by others |
| 55 | // methods for scripting: |
| 56 | virtual bool v_hasGetNrow () const { return false; } virtual double v_getNrow () const { return undefined; } |
| 57 | virtual bool v_hasGetNcol () const { return false; } virtual double v_getNcol () const { return undefined; } |
| 58 | virtual bool v_hasGetXmin () const { return false; } virtual double v_getXmin () const { return undefined; } |
| 59 | virtual bool v_hasGetXmax () const { return false; } virtual double v_getXmax () const { return undefined; } |
| 60 | virtual bool v_hasGetYmin () const { return false; } virtual double v_getYmin () const { return undefined; } |
| 61 | virtual bool v_hasGetYmax () const { return false; } virtual double v_getYmax () const { return undefined; } |
| 62 | virtual bool v_hasGetNx () const { return false; } virtual double v_getNx () const { return undefined; } |
| 63 | virtual bool v_hasGetNy () const { return false; } virtual double v_getNy () const { return undefined; } |
| 64 | virtual bool v_hasGetDx () const { return false; } virtual double v_getDx () const { return undefined; } |
| 65 | virtual bool v_hasGetDy () const { return false; } virtual double v_getDy () const { return undefined; } |
| 66 | virtual bool v_hasGetX () const { return false; } virtual double v_getX (integer /* ix */) const { return undefined; } |
| 67 | virtual bool v_hasGetY () const { return false; } virtual double v_getY (integer /* iy */) const { return undefined; } |
| 68 | virtual bool v_hasGetRowStr () const { return false; } virtual conststring32 v_getRowStr (integer /* irow */) const { return nullptr; } |
| 69 | virtual bool v_hasGetColStr () const { return false; } virtual conststring32 v_getColStr (integer /* icol */) const { return nullptr; } |
| 70 | virtual bool v_hasGetCell () const { return false; } virtual double v_getCell () const { return undefined; } |
| 71 | virtual bool v_hasGetCellStr () const { return false; } virtual conststring32 v_getCellStr () const { return nullptr; } |
| 72 | virtual bool v_hasGetVector () const { return false; } virtual double v_getVector (integer /* irow */, integer /* icol */) const { return undefined; } |
| 73 | virtual bool v_hasGetVectorStr () const { return false; } virtual conststring32 v_getVectorStr (integer /* icol */) const { return nullptr; } |
| 74 | virtual bool v_hasGetMatrix () const { return false; } virtual double v_getMatrix (integer /* irow */, integer /* icol */) const { return undefined; } |
| 75 | virtual bool v_hasGetMatrixStr () const { return false; } virtual conststring32 v_getMatrixStr (integer /* irow */, integer /* icol */) const { return nullptr; } |
| 76 | virtual bool v_hasGetFunction0 () const { return false; } virtual double v_getFunction0 () const { return undefined; } |
| 77 | virtual bool v_hasGetFunction1 () const { return false; } virtual double v_getFunction1 (integer /* irow */, double /* x */) const { return undefined; } |
| 78 | virtual bool v_hasGetFunction2 () const { return false; } virtual double v_getFunction2 (double /* x */, double /* y */) const { return undefined; } |
| 79 | virtual bool v_hasGetRowIndex () const { return false; } virtual double v_getRowIndex (conststring32 /* rowLabel */) const { return undefined; } |
| 80 | virtual bool v_hasGetColIndex () const { return false; } virtual double v_getColIndex (conststring32 /* colLabel */) const { return undefined; } |
| 81 | }; |
| 82 | |
| 83 | template <class T> autoSomeThing<T> Data_copy (const T* data) { |
| 84 | return _Data_copy (data).template static_cast_move <T>(); |
| 85 | } |
| 86 | autoDaata _Data_copy (constDaata me); |
| 87 | /* |
| 88 | Message: |
| 89 | "return a deep copy of yourself." |
| 90 | Postconditions: |
| 91 | result -> name == nullptr; // the only attribute NOT copied |
| 92 | */ |
| 93 | |
| 94 | bool Data_equal (Daata data1, Daata data2); |
| 95 | /* |
| 96 | Message: |
| 97 | "return true if the shallow or deep attributes of 'data1' and 'data2' are equal; |
| 98 | otherwise, return false." |
| 99 | Comment: |
| 100 | Data_equal (data, Data_copy (data)) should always return 1; the names are not compared. |
| 101 | */ |
| 102 | |
| 103 | typedef MelderCompareHook<structDaata> Data_CompareHook; |
| 104 | |
| 105 | bool Data_canWriteAsEncoding (Daata me, int outputEncoding); |
| 106 | /* |
| 107 | Message: |
| 108 | "Can you write yourself in that encoding?" |
| 109 | The answer depends on whether all members can be written in that encoding. |
| 110 | */ |
| 111 | |
| 112 | bool Data_canWriteText (Daata me); |
| 113 | /* |
| 114 | Message: |
| 115 | "Can you write yourself as text?" |
| 116 | The answer depends on whether the subclass defines the 'writeText' method. |
| 117 | */ |
| 118 | |
| 119 | MelderFile Data_createTextFile ( |
| 120 | Daata me, |
| 121 | MelderFile file, |
| 122 | bool verbose |
| 123 | ); // returns the input MelderFile in order to be caught by an autoMelderFile |
| 124 | |
| 125 | void Data_writeText (Daata me, MelderFile openFile); |
| 126 | /* |
| 127 | Message: |
| 128 | "try to write yourself as text to an open file." |
| 129 | Return value: |
| 130 | 1 if OK, 0 in case of failure. |
| 131 | Failures: |
| 132 | I/O error. |
| 133 | Disk full. |
| 134 | Description: |
| 135 | The format depends on the 'writeText' method defined by the subclass. |
| 136 | */ |
| 137 | |
| 138 | void Data_writeToTextFile (Daata me, MelderFile file); |
| 139 | /* |
| 140 | Message: |
| 141 | "try to write yourself as text to a file". |
| 142 | Description: |
| 143 | The first line is 'File type = "ooTextFile"'. |
| 144 | Your class name is written in the second line, |
| 145 | e.g., if you are a Person, the second line will be 'Object class = "Person"'. |
| 146 | The format of the lines after the second line is the same as in Data_writeText. |
| 147 | */ |
| 148 | |
| 149 | void Data_writeToShortTextFile (Daata me, MelderFile file); |
| 150 | /* |
| 151 | Message: |
| 152 | "try to write yourself as text to a file". |
| 153 | Description: |
| 154 | The first line is 'File type = "ooTextFile short"'. |
| 155 | Your class name is written in the second line, |
| 156 | e.g., if you are a Person, the second line will be '"Person"'. |
| 157 | The format of the lines after the second line is the same as in Data_writeText. |
| 158 | */ |
| 159 | |
| 160 | bool Data_canWriteBinary (Daata me); |
| 161 | /* |
| 162 | Message: |
| 163 | "Can you write yourself as binary data?" |
| 164 | The answer depends on whether the subclass defines the 'writeBinary' method. |
| 165 | */ |
| 166 | |
| 167 | void Data_writeBinary (Daata me, FILE *f); |
| 168 | /* |
| 169 | Message: |
| 170 | "try to write yourself as binary data to an open file." |
| 171 | Failures: |
| 172 | I/O error. |
| 173 | Disk full. |
| 174 | Description: |
| 175 | The format depends on the 'writeBinary' method defined by the subclass, |
| 176 | but is machine independent because it always uses 'most significant byte first' |
| 177 | and IEEE floating-point format. |
| 178 | */ |
| 179 | |
| 180 | void Data_writeToBinaryFile (Daata me, MelderFile file); |
| 181 | /* |
| 182 | Message: |
| 183 | "try to write yourself as binary data to a file". |
| 184 | Description: |
| 185 | First, your class name is written in the file, |
| 186 | e.g., if you are a Person, the file will start with "PersonBinaryFile". |
| 187 | The format of the file after this is the same as in Data_writeBinary. |
| 188 | */ |
| 189 | |
| 190 | bool Data_canReadText (Daata me); |
| 191 | /* |
| 192 | Message: |
| 193 | "Can you read yourself as text?" |
| 194 | The answer depends on whether the subclass defines a 'readText' method, |
| 195 | but is preferably the same as the answer from Data_canWriteText. |
| 196 | */ |
| 197 | |
| 198 | void Data_readText (Daata me, MelderReadText text, int formatVersion); |
| 199 | /* |
| 200 | Message: |
| 201 | "try to read yourself as text from a string." |
| 202 | Failures: |
| 203 | The 'readText' method of the subclass failed. |
| 204 | I/O error. |
| 205 | Early end of file detected. |
| 206 | Description: |
| 207 | The format depends on the 'readText' method defined by the subclass, |
| 208 | but is preferably the same as the format produced by the 'writeText' method. |
| 209 | */ |
| 210 | |
| 211 | autoDaata Data_readFromTextFile (MelderFile file); |
| 212 | /* |
| 213 | Message: |
| 214 | "try to read a Data as text from a file". |
| 215 | Description: |
| 216 | The Data's class name is read from the first line, |
| 217 | e.g., if the first line is "PersonTextFile", the Data will be a Person. |
| 218 | The format of the lines after the first line is the same as in Data_readText. |
| 219 | Return value: |
| 220 | the new object. |
| 221 | Failures: |
| 222 | Error opening file <fileName>. |
| 223 | The file <fileName> does not contain an object. |
| 224 | (plus those from Data_readText) |
| 225 | */ |
| 226 | |
| 227 | bool Data_canReadBinary (Daata me); |
| 228 | /* |
| 229 | Message: |
| 230 | "Can you read yourself as binary data?" |
| 231 | The answer depends on whether the subclass defines a 'readBinary' method, |
| 232 | but is preferably the same as the answer from Data_canWriteBinary. |
| 233 | */ |
| 234 | |
| 235 | void Data_readBinary (Daata me, FILE *f, int formatVersion); |
| 236 | /* |
| 237 | Message: |
| 238 | "try to read yourself as binary data from the stream <f>." |
| 239 | Failures: |
| 240 | The 'readBinary' method of the subclass throws an error. |
| 241 | I/O error. |
| 242 | Early end of file detected. |
| 243 | Description: |
| 244 | The format depends on the 'readBinary' method defined by the subclass, |
| 245 | but is preferably the same as the format produced by the 'writeBinary' method. |
| 246 | */ |
| 247 | |
| 248 | autoDaata Data_readFromBinaryFile (MelderFile file); |
| 249 | /* |
| 250 | Message: |
| 251 | "try to read a Data as binary data from a file". |
| 252 | Description: |
| 253 | The Data's class name is read from the start of the file, |
| 254 | e.g., if the file starts with is "PersonBinaryFile", the Data will be a Person. |
| 255 | The format of the file after this is the same as in Data_readBinary. |
| 256 | Return value: |
| 257 | the new object. |
| 258 | Failures: |
| 259 | Error opening file <fileName>. |
| 260 | The file <fileName> does not contain an object. |
| 261 | (plus those from Data_readBinary) |
| 262 | */ |
| 263 | |
| 264 | using Data_FileTypeRecognizer = autoDaata (*) (integer numberOfBytesRead, const char *header, MelderFile file); |
| 265 | |
| 266 | void Data_recognizeFileType (Data_FileTypeRecognizer recognizer); |
| 267 | /* |
| 268 | Purpose: |
| 269 | to make sure that a file can be read by Data_readFromFile. |
| 270 | Arguments: |
| 271 | recognizer |
| 272 | a routine that tries to identify a file as being of a certain type, |
| 273 | from the information in the header of the file and/or from the file name. |
| 274 | If the identification succeeds, the recognizer routine should call the routine |
| 275 | that actually reads the file. |
| 276 | nread |
| 277 | the length of the header of the file. The maximum value of nread is 512, |
| 278 | but it is smaller if the file is shorter than 512 bytes. |
| 279 | header |
| 280 | a buffer that contains the first nread bytes of the file; |
| 281 | the data were put there by Data_readFromFile before calling the recognizers. |
| 282 | The first byte is in header [0]. |
| 283 | fileName |
| 284 | the name of the file that is to be recognized. The recognizer routine uses this name |
| 285 | to actually read the file if it positively identifies the file. The recognizer routine |
| 286 | may also use %fileName in the recognition process; e.g. some files with raw sound data |
| 287 | have names that make them recognizable as files with raw sound data. |
| 288 | Defining a file-type recognizer: |
| 289 | You define a file-type recognizer as in the following example, |
| 290 | which tries to identify and read a Sun audio file. |
| 291 | A Sun audio file should contain at least 24 bytes and start with the string ".snd": |
| 292 | autoDaata Sound_sunAudioFileRecognizer (int nread, const char *header, MelderFile file) { |
| 293 | if (nread >= 24 && strnequ (& header [0], ".snd", 4)) |
| 294 | return Sound_readFromSunAudioFile (file); |
| 295 | else |
| 296 | return autoDaata (); |
| 297 | } |
| 298 | From this example, we see that if the file is recognized, it should be read immediately, |
| 299 | and the resulting object (always a descendant of class Data) should be returned. |
| 300 | We also see that the return value nullptr is used for notifying Data_readFromFile |
| 301 | of the fact that the file is not a Sun audio file. |
| 302 | Registering a file-type recognizer: |
| 303 | You would put a statement like the following in the initialization section of your program: |
| 304 | Data_recognizeFileType (Sound_sunAudioFileRecognizer); |
| 305 | After this, Data_readFromFile is able to read Sun audio files. |
| 306 | */ |
| 307 | |
| 308 | autoDaata Data_readFromFile (MelderFile file); |
| 309 | /* |
| 310 | Purpose: |
| 311 | to read a file with data of any kind. |
| 312 | Return value: |
| 313 | the object read from `file`, or null if there was no object (as with Picture_readFromPraatPictureFile). |
| 314 | Failure: |
| 315 | the file was not recognized. |
| 316 | Behaviour: |
| 317 | Data_readFromFile first checks whether the file is a text file |
| 318 | that is readable by Data_readFromTextFile, or a binary file |
| 319 | written by Data_writeToBinaryFile, or a file as written by Data_writeToLispFile. |
| 320 | If one of these succeeds, the file is read and the resulting object is returned. |
| 321 | If not, the recognizers installed with Data_recognizeFileType are tried. |
| 322 | */ |
| 323 | |
| 324 | inline structMelderFolder Data_directoryBeingRead { }; |
| 325 | |
| 326 | int Data_publish (autoDaata me); |
| 327 | |
| 328 | void Data_setPublishProc (int (*publish) (autoDaata)); |
| 329 | |
| 330 | /* The values of 'type' in struct descriptions. */ |
| 331 | |
| 332 | #define bytewa 1 |
| 333 | #define int16wa 2 |
| 334 | #define intwa 3 |
| 335 | #define integerwa 4 |
| 336 | #define ubytewa 5 |
| 337 | #define uintwa 6 |
| 338 | #define uintegerwa 7 |
| 339 | #define floatwa 8 |
| 340 | #define doublewa 9 |
| 341 | #define complexwa 10 |
| 342 | #define enumwa 11 |
| 343 | #define lenumwa 12 |
| 344 | #define booleanwa 13 |
| 345 | #define questionwa 14 |
| 346 | #define stringwa 15 |
| 347 | #define lstringwa 16 |
| 348 | #define maxsingletypewa lstringwa |
| 349 | #define structwa 17 |
| 350 | #define widgetwa 18 |
| 351 | #define objectwa 19 |
| 352 | #define collectionofwa 20 |
| 353 | #define collectionwa 21 |
| 354 | #define inheritwa 22 |
| 355 | |
| 356 | /* Recursive routines for working with struct members. */ |
| 357 | |
| 358 | int Data_Description_countMembers (Data_Description structDescription); |
| 359 | /* Including inherited members. */ |
| 360 | |
| 361 | Data_Description Data_Description_findMatch (Data_Description structDescription, conststring32 member); |
| 362 | /* Find the location of member 'member' in a struct. */ |
| 363 | /* If 'structDescription' describes a class, the ancestor classes are also searched. */ |
| 364 | |
| 365 | Data_Description Data_Description_findNumberUse (Data_Description structDescription, conststring32 string); |
| 366 | /* Find the first member that uses member 'string' in its size description (max1 or max2 fields). */ |
| 367 | |
| 368 | /* Retrieving data from object + description. */ |
| 369 | |
| 370 | int64 Data_Description_integer (void *structAddress, Data_Description description); |
| 371 | /* Convert data found at a certain offset from 'address' to an integer, according to the given 'description'. */ |
| 372 | |
| 373 | int Data_Description_evaluateInteger (void *structAddress, Data_Description structDescription, |
| 374 | conststring32 formula, integer *result); |
| 375 | /* |
| 376 | * Translates a string like '100' or 'numberOfHorses' or 'numberOfCows - 1' to an integer. |
| 377 | * The 'algorithm' does some wild guesses as to the meanings of the 'min1' and 'max1' strings. |
| 378 | * A full-fledged interpretation is preferable... |
| 379 | * Returns 0 if 'formula' cannot be parsed to a number. |
| 380 | */ |
| 381 | |
| 382 | /* End of file Data.h */ |
| 383 | #endif |