Data.h 16 KB

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