File: Data.cpp

package info (click to toggle)
praat 5.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 40,728 kB
  • sloc: cpp: 333,759; ansic: 237,947; makefile: 731; python: 340
file content (411 lines) | stat: -rw-r--r-- 14,035 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* Data.cpp
 *
 * Copyright (C) 1992-2012 Paul Boersma
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "Collection.h"

Thing_implement (Data, Thing, 0);

structMelderDir Data_directoryBeingRead = { { 0 } };

void structData :: v_copy (thou) {
	thouart (Data);
	(void) thee;
}

bool structData :: v_equal (thou) {
	thouart (Data);
	(void) thee;
	return true;
}   // names of "identical" objects are allowed to be different

bool structData :: v_canWriteAsEncoding (int encoding) {
	(void) encoding;
	return true;
}

void structData :: v_writeText (MelderFile openFile) {
	(void) openFile;
}

void structData :: v_readText (MelderReadText text) {
	(void) text;
}

void structData :: v_writeBinary (FILE *f) {
	(void) f;
}

void structData :: v_readBinary (FILE *f) {
	(void) f;
}

Any _Data_copy (Data me) {
	try {
		if (me == NULL) return NULL;
		autoData thee = (Data) _Thing_new (my classInfo);
		my v_copy (thee.peek()); therror
		Thing_setName (thee.peek(), my name);
		return thee.transfer();
	} catch (MelderError) {
		Melder_throw (me, ": not copied.");
	}
}

bool Data_equal (Data me, Data thee) {
	if (my classInfo != thy classInfo) return false;   // different class: not equal
	int offset = sizeof (struct structData);   // we already compared the methods, and are going to skip the names
	if (! memcmp ((char *) me + offset, (char *) thee + offset, my classInfo -> size - offset))
		return true;   // no shallow differences
	return my v_equal (thee);
}

bool Data_canWriteAsEncoding (Data me, int encoding) {
	return my v_canWriteAsEncoding (encoding);
}

bool Data_canWriteText (Data me) {
	return my v_writable ();
}

void Data_writeText (Data me, MelderFile openFile) {
	my v_writeText (openFile); therror
	if (ferror (openFile -> filePointer))
		Melder_throw ("I/O error.");
}

MelderFile Data_createTextFile (Data me, MelderFile file, bool verbose) {
	autoMelderFile mfile = MelderFile_create (file, L"TEXT", 0, L"txt");
	#if defined (_WIN32)
		file -> requiresCRLF = true;
	#endif
	file -> verbose = verbose;
	file -> outputEncoding = Melder_getOutputEncoding ();
	if (file -> outputEncoding == kMelder_textOutputEncoding_ASCII_THEN_UTF16)
		file -> outputEncoding = Data_canWriteAsEncoding (me, kMelder_textOutputEncoding_ASCII) ? kMelder_textOutputEncoding_ASCII : kMelder_textOutputEncoding_UTF16;
	else if (file -> outputEncoding == kMelder_textOutputEncoding_ISO_LATIN1_THEN_UTF16)
		file -> outputEncoding = Data_canWriteAsEncoding (me, kMelder_textOutputEncoding_ISO_LATIN1) ? kMelder_textOutputEncoding_ISO_LATIN1 : kMelder_textOutputEncoding_UTF16;
	if (file -> outputEncoding == kMelder_textOutputEncoding_UTF16) {
		binputu2 (0xfeff, file -> filePointer);
	}
	return mfile.transfer();
}

static void _Data_writeToTextFile (Data me, MelderFile file, bool verbose) {
	try {
		if (! Data_canWriteText (me))
			Melder_throw ("Objects of class ", my classInfo -> className, " cannot be written to a text file.");
		autoMelderFile mfile = Data_createTextFile (me, file, verbose);
		#ifndef _WIN32
			flockfile (file -> filePointer);   // BUG
		#endif
		MelderFile_write2 (file, L"File type = \"ooTextFile\"\nObject class = \"", my classInfo -> className);
		if (my classInfo -> version > 0)
			MelderFile_write2 (file, L" ", Melder_integer (my classInfo -> version));
		MelderFile_write1 (file, L"\"\n");
		Data_writeText (me, file);
		MelderFile_writeCharacter (file, '\n');
		#ifndef _WIN32
			if (file -> filePointer) funlockfile (file -> filePointer);
		#endif
		mfile.close ();
		MelderFile_setMacTypeAndCreator (file, 'TEXT', 0);
	} catch (MelderError) {
		#ifndef _WIN32
			if (file -> filePointer) funlockfile (file -> filePointer);   // the file pointer is NULL before Data_createTextFile() and after mfile.close()
		#endif
		throw;
	}
}

void Data_writeToTextFile (Data me, MelderFile file) {
	try {
		_Data_writeToTextFile (me, file, true);
	} catch (MelderError) {
		Melder_throw (me, ": not written to text file ", file, ".");
	}
}

void Data_writeToShortTextFile (Data me, MelderFile file) {
	try {
		_Data_writeToTextFile (me, file, false);
	} catch (MelderError) {
		Melder_throw (me, ": not written to short text file ", file, ".");
	}
}

bool Data_canWriteBinary (Data me) {
	return my v_writable ();
}

void Data_writeBinary (Data me, FILE *f) {
	my v_writeBinary (f); therror
	if (ferror (f))
		Melder_throw ("I/O error.");
}

void Data_writeToBinaryFile (Data me, MelderFile file) {
	try {
		if (! Data_canWriteBinary (me))
			Melder_throw ("Objects of class ", my classInfo -> className, L" cannot be written to a generic binary file.");
		autoMelderFile mfile = MelderFile_create (file, 0, 0, 0);
		if (fprintf (file -> filePointer, "ooBinaryFile") < 0)
			Melder_throw ("Cannot write first bytes of file.");
		binputw1 (
			my classInfo -> version > 0 ?
				Melder_wcscat (my classInfo -> className, L" ", Melder_integer (my classInfo -> version)) :
				my classInfo -> className,
			file -> filePointer);
		Data_writeBinary (me, file -> filePointer);
		mfile.close ();
		MelderFile_setMacTypeAndCreator (file, 'BINA', 0);
	} catch (MelderError) {
		Melder_throw (me, ": not written to binary file ", file, ".");
	}
}

bool Data_canReadText (Data me) {
	return my v_writable ();
}

void Data_readText (Data me, MelderReadText text) {
	try {
		my v_readText (text); therror
	} catch (MelderError) {
		Melder_throw (Thing_className (me), " not read.");
	}
}

Any Data_readFromTextFile (MelderFile file) {
	try {
		autoMelderReadText text = MelderReadText_createFromFile (file);
		wchar *line = MelderReadText_readLine (text.peek()); therror
		if (line == NULL)
			Melder_throw ("No lines.");
		wchar *end = wcsstr (line, L"ooTextFile");   // oo format?
		autoData me = NULL;
		if (end) {
			autostring klas = texgetw2 (text.peek());
			me.reset ((Data) Thing_newFromClassName (klas.peek()));
		} else {
			end = wcsstr (line, L"TextFile");
			if (end == NULL)
				Melder_throw ("Not an old-type text file; should not occur.");
			*end = '\0';
			me.reset ((Data) Thing_newFromClassName (line));
			Thing_version = -1;   // old version: override version number, which was set to 0 by newFromClassName
		}
		MelderFile_getParentDir (file, & Data_directoryBeingRead);
		Data_readText (me.peek(), text.peek()); therror
		return me.transfer();
	} catch (MelderError) {
		Melder_throw ("Data not read from text file ", file, ".");
	}
}

bool Data_canReadBinary (Data me) {
	return my v_writable ();
}

void Data_readBinary (Data me, FILE *f) {
	try {
		my v_readBinary (f); therror
		if (feof (f))
			Melder_throw ("Early end of file.");
		if (ferror (f))
			Melder_throw ("I/O error.");
	} catch (MelderError) {
		Melder_throw (Thing_className (me), " not read.");
	}
}

Any Data_readFromBinaryFile (MelderFile file) {
	try {
		autofile f = Melder_fopen (file, "rb");
		char line [200];
		int n = fread (line, 1, 199, f); line [n] = '\0';
		char *end = strstr (line, "ooBinaryFile");
		autoData me = NULL;
		if (end) {
			fseek (f, strlen ("ooBinaryFile"), 0);
			autostring8 klas = bingets1 (f);
			me.reset ((Data) Thing_newFromClassNameA (klas.peek()));
		} else {
			end = strstr (line, "BinaryFile");
			if (! end) {
				Melder_throw ("File ", file, " is not a Data binary file.");
			}
			*end = '\0';
			me.reset ((Data) Thing_newFromClassNameA (line));
			Thing_version = -1;   // old version: override version number, which was set to 0 by newFromClassName
			rewind (f);
			fread (line, 1, end - line + strlen ("BinaryFile"), f);
		}
		MelderFile_getParentDir (file, & Data_directoryBeingRead);
		Data_readBinary (me.peek(), f); therror
		f.close (file);
		return me.transfer();
	} catch (MelderError) {
		Melder_throw ("Data not read from binary file ", file, ".");
	}
}

/* Generic reading. */

static int numFileTypeRecognizers = 0;
static Any (*fileTypeRecognizers [100]) (int nread, const char *header, MelderFile fs);
void Data_recognizeFileType (Any (*recognizer) (int nread, const char *header, MelderFile fs)) {
	Melder_assert (numFileTypeRecognizers < 100);
	fileTypeRecognizers [++ numFileTypeRecognizers] = recognizer;
}

Any Data_readFromFile (MelderFile file) {
	int nread, i;
	char header [513];
	autofile f = Melder_fopen (file, "rb");
	nread = fread (& header [0], 1, 512, f);
	f.close (file);
	header [nread] = 0;

	/***** 1. Is this file a text file as defined in Data.cpp? *****/

	if (nread > 11) {
		char *p = strstr (header, "TextFile");
		if (p != NULL && p - header < nread - 8 && p - header < 40)
			return Data_readFromTextFile (file);
	}
	if (nread > 22) {
		char headerCopy [101];
		memcpy (headerCopy, header, 100);
		headerCopy [100] = '\0';
		for (i = 0; i < 100; i ++)
			if (headerCopy [i] == '\0') headerCopy [i] = '\001';
		char *p = strstr (headerCopy, "T\001e\001x\001t\001F\001i\001l\001e");
		if (p != NULL && p - headerCopy < nread - 15 && p - headerCopy < 80)
			return Data_readFromTextFile (file);
	}

	/***** 2. Is this file a binary file as defined in Data.cpp? *****/

	if (nread > 13) {
		char *p = strstr (header, "BinaryFile");
		if (p != NULL && p - header < nread - 10 && p - header < 40)
			return Data_readFromBinaryFile (file);
	}

	/***** 3. Is this file of a type for which a recognizer has been installed? *****/

	MelderFile_getParentDir (file, & Data_directoryBeingRead);
	for (i = 1; i <= numFileTypeRecognizers; i ++) {
		Data object = (Data) fileTypeRecognizers [i] (nread, header, file);
		if (object) return object;
	}

	/***** 4. Is this a common text file? *****/

	for (i = 0; i < nread; i ++)
		if (header [i] < 32 || header [i] > 126)   /* Not ASCII? */
			break;
	if (i >= nread) return Data_readFromTextFile (file);

	Melder_throw ("File ", file, " not recognized.");
}

/* Recursive routines for working with struct members. */

int Data_Description_countMembers (Data_Description structDescription) {
	int count = 0;
	for (Data_Description desc = structDescription; desc -> name; desc ++)
		count ++;
	if (structDescription [0]. type == inheritwa) {
		Data_Description parentDescription = ((Data) _Thing_dummyObject ((ClassInfo) structDescription [0]. tagType)) -> v_description ();
		if (parentDescription)
			return count + Data_Description_countMembers (parentDescription);
	}
	return count;
}

Data_Description Data_Description_findMatch (Data_Description structDescription, const wchar_t *name) {
	for (Data_Description desc = structDescription; desc -> name; desc ++)
		if (wcsequ (name, desc -> name)) return desc;
	if (structDescription [0]. type == inheritwa) {
		Data_Description parentDescription = ((Data) _Thing_dummyObject ((ClassInfo) structDescription [0]. tagType)) -> v_description ();
		if (parentDescription)
			return Data_Description_findMatch (parentDescription, name);
	}
	return NULL;   /* Not found. */
}

Data_Description Data_Description_findNumberUse (Data_Description structDescription, const wchar_t *string) {
	for (Data_Description desc = structDescription; desc -> name; desc ++) {
		if (desc -> max1 && wcsequ (desc -> max1, string)) return desc;
		if (desc -> max2 && wcsequ (desc -> max2, string)) return desc;
	}
	if (structDescription [0]. type == inheritwa) {
		Data_Description parentDescription = ((Data) _Thing_dummyObject ((ClassInfo) structDescription [0]. tagType)) -> v_description ();
		if (parentDescription)
			return Data_Description_findNumberUse (parentDescription, string);
	}
	return NULL;
}

/* Retrieving data from object + description. */

long Data_Description_integer (void *address, Data_Description description) {
	switch (description -> type) {
		case bytewa:       return * (signed char *)    ((char *) address + description -> offset);
		case intwa:        return * (int *)            ((char *) address + description -> offset);
		case longwa:       return * (long *)           ((char *) address + description -> offset);
		case ubytewa:      return * (unsigned char *)  ((char *) address + description -> offset);
		case uintwa:       return * (unsigned int *)   ((char *) address + description -> offset);
		case ulongwa:      return * (unsigned long *)  ((char *) address + description -> offset);
		case boolwa:       return * (bool *)           ((char *) address + description -> offset);
		case objectwa:     return (* (Collection *)    ((char *) address + description -> offset)) -> size;
		case collectionwa: return (* (Collection *)    ((char *) address + description -> offset)) -> size;
		default: return 0;
	}
}

int Data_Description_evaluateInteger (void *structAddress, Data_Description structDescription,
	const wchar *formula, long *result)
{
	if (formula == NULL) {   // this was a VECTOR_FROM array
		*result = 1;
		return 1;
	}
	if (formula [0] >= 'a' && formula [0] <= 'z') {
		wchar buffer [100], *minus1, *psize;
		Data_Description sizeDescription;
		wcscpy (buffer, formula);
		if ((minus1 = wcsstr (buffer, L" - 1")) != NULL)
			*minus1 = '\0';   // strip trailing " - 1", but remember
		if ((psize = wcsstr (buffer, L" -> size")) != NULL)
			*psize = '\0';   // strip trailing " -> size"
		if (! (sizeDescription = Data_Description_findMatch (structDescription, buffer))) {
			*result = 0;
			return 0 /*Melder_error ("Cannot find member \"%ls\".", buffer)*/;
		}
		*result = Data_Description_integer (structAddress, sizeDescription);
		if (minus1) *result -= 1;
	} else {
		*result = wcstol (formula, NULL, 10);
	}
	return 1;
}

/* End of file Data.cpp */