void _CollectionOfDaata_v_readText (_CollectionOfDaata* me, MelderReadText text, int formatVersion) { if (formatVersion < 0) { long l_size; autostring8 line = Melder_32to8 (MelderReadText_readLine (text)); if (! line.peek() || ! sscanf (line.peek(), "%ld", & l_size) || l_size < 0) Melder_throw (U"Collection::readText: cannot read size."); my _grow (l_size); for (long i = 1; i <= l_size; i ++) { long itemNumberRead; int n = 0, length, stringsRead; char klas [200], nameTag [2000]; do { line.reset (Melder_32to8 (MelderReadText_readLine (text))); if (! line.peek()) Melder_throw (U"Missing object line."); } while (strncmp (line.peek(), "Object ", 7)); stringsRead = sscanf (line.peek(), "Object %ld: class %s %s%n", & itemNumberRead, klas, nameTag, & n); if (stringsRead < 2) Melder_throw (U"Collection::readText: cannot read header of object ", i, U"."); if (itemNumberRead != i) Melder_throw (U"Collection::readText: read item number ", itemNumberRead, U" while expecting ", i, U"."); if (stringsRead == 3 && ! strequ (nameTag, "name")) Melder_throw (U"Collection::readText: wrong header at object ", i, U"."); my at [i] = (Daata) Thing_newFromClassName (Melder_peek8to32 (klas), nullptr).releaseToAmbiguousOwner(); my size ++; if (! Thing_isa (my at [i], classDaata) || ! Data_canReadText (my at [i])) Melder_throw (U"Cannot read item of class ", Thing_className (my at [i]), U" in collection."); Data_readText (my at [i], text, -1); if (stringsRead == 3) { if (line [n] == U' ') n ++; // skip space character length = strlen (line.peek()+n); if (length > 0 && (line.peek()+n) [length - 1] == '\n') (line.peek()+n) [length - 1] = '\0'; Thing_setName (my at [i], Melder_peek8to32 (line.peek()+n)); } } } else { int32_t l_size = texgeti4 (text); my _grow (l_size); for (int32_t i = 1; i <= l_size; i ++) { autostring32 className = texgetw2 (text); int elementFormatVersion; my at [i] = (Daata) Thing_newFromClassName (className.peek(), & elementFormatVersion).releaseToAmbiguousOwner(); my size ++; if (! Thing_isa (my at [i], classDaata) || ! Data_canReadText (my at [i])) Melder_throw (U"Cannot read item of class ", Thing_className (my at [i]), U" in collection."); autostring32 objectName = texgetw2 (text); Thing_setName (my at [i], objectName.peek()); Data_readText (my at [i], text, elementFormatVersion); } } }
Daata Data_readFromTextFile (MelderFile file) { try { autoMelderReadText text = MelderReadText_createFromFile (file); char32 *line = MelderReadText_readLine (text.peek()); if (! line) Melder_throw (U"No lines."); char32 *end = str32str (line, U"ooTextFile"); // oo format? autoDaata me; int formatVersion; if (end) { autostring32 klas = texgetw2 (text.peek()); me.reset (static_cast <Daata> (Thing_newFromClassName (klas.peek(), & formatVersion))); } else { end = str32str (line, U"TextFile"); if (! end) Melder_throw (U"Not an old-type text file; should not occur."); *end = U'\0'; me.reset (static_cast <Daata> (Thing_newFromClassName (line, nullptr))); formatVersion = -1; // old version } MelderFile_getParentDir (file, & Data_directoryBeingRead); Data_readText (me.peek(), text.peek(), formatVersion); file -> format = structMelderFile :: Format :: text; return me.transfer(); } catch (MelderError) { Melder_throw (U"Data not read from text file ", file, U"."); } }
Strings Strings_readFromRawTextFile (MelderFile file) { try { autoMelderReadText text = MelderReadText_createFromFile (file); /* * Count number of strings. */ long n = MelderReadText_getNumberOfLines (text.peek()); /* * Create. */ autoStrings me = Thing_new (Strings); if (n > 0) my strings = NUMvector <wchar_t *> (1, n); my numberOfStrings = n; /* * Read strings. */ for (long i = 1; i <= n; i ++) { wchar_t *line = MelderReadText_readLine (text.peek()); my strings [i] = Melder_wcsdup (line); } return me.transfer(); } catch (MelderError) { Melder_throw ("Strings not read from raw text file ", file, "."); } }
Any Data_readFromTextFile (MelderFile file) { try { autoMelderReadText text = MelderReadText_createFromFile (file); wchar_t *line = MelderReadText_readLine (text.peek()); if (line == NULL) Melder_throw ("No lines."); wchar_t *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()); return me.transfer(); } catch (MelderError) { Melder_throw ("Data not read from text file ", file, "."); } }