| 1 | /* FileInMemory.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012-2021 David Weenink, 2017,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. See the GNU |
| 13 | * 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 "FileInMemory.h" |
| 20 | #include "Strings_.h" |
| 21 | #include <sys/stat.h> |
| 22 | |
| 23 | #include "oo_DESTROY.h" |
| 24 | #include "FileInMemory_def.h" |
| 25 | #include "oo_COPY.h" |
| 26 | #include "FileInMemory_def.h" |
| 27 | #include "oo_EQUAL.h" |
| 28 | #include "FileInMemory_def.h" |
| 29 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 30 | #include "FileInMemory_def.h" |
| 31 | #include "oo_WRITE_TEXT.h" |
| 32 | #include "FileInMemory_def.h" |
| 33 | #include "oo_READ_TEXT.h" |
| 34 | #include "FileInMemory_def.h" |
| 35 | #include "oo_WRITE_BINARY.h" |
| 36 | #include "FileInMemory_def.h" |
| 37 | #include "oo_READ_BINARY.h" |
| 38 | #include "FileInMemory_def.h" |
| 39 | #include "oo_DESCRIPTION.h" |
| 40 | #include "FileInMemory_def.h" |
| 41 | |
| 42 | Thing_implement (FileInMemory, Daata, 0); |
| 43 | Thing_implement (FileInMemorySet, SortedSetOfString, 0); |
| 44 | |
| 45 | void structFileInMemory :: v1_info () { |
| 46 | our structDaata :: v1_info (); |
| 47 | MelderInfo_writeLine (U"File name: ", our string.get()); |
| 48 | MelderInfo_writeLine (U"Number of bytes: ", our d_numberOfBytes); |
| 49 | } |
| 50 | |
| 51 | autoFileInMemory FileInMemory_create (MelderFile file) { |
| 52 | try { |
| 53 | Melder_require (MelderFile_readable (file), |
| 54 | U"File is not readable."); |
| 55 | const integer length = MelderFile_length (file); |
| 56 | |
| 57 | autoFileInMemory me = Thing_new (FileInMemory); |
| 58 | my string = Melder_dup (MelderFile_peekPath (file)); |
| 59 | my d_numberOfBytes = length; |
| 60 | my _dontOwnData = false; |
| 61 | my d_data = newvectorzero <byte> (my d_numberOfBytes + 1); // includes room for a final null byte in case the file happens to contain only text |
| 62 | MelderFile_open (file); |
| 63 | for (integer i = 1; i <= my d_numberOfBytes; i++) { |
| 64 | const unsigned int number = bingetu8 (file -> filePointer); |
| 65 | my d_data [i] = number; |
| 66 | } |
| 67 | my d_data [my d_numberOfBytes + 1] = 0; // "extra" null byte |
| 68 | MelderFile_close (file); |
| 69 | return me; |
| 70 | } catch (MelderError) { |
| 71 | Melder_throw (U"FileInMemory not created from file ", file, U"."); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | autoFileInMemory FileInMemory_createWithData (integer numberOfBytes, uint8 *data, bool isStaticData, conststring32 path) { |
| 76 | try { |
| 77 | autoFileInMemory me = Thing_new (FileInMemory); |
| 78 | my string = Melder_dup (path); |
| 79 | my d_numberOfBytes = numberOfBytes; |
| 80 | if (isStaticData) { |
| 81 | my _dontOwnData = true; // we cannot dispose of the data! |
| 82 | my d_data.cells = data; |
| 83 | my d_data.size = numberOfBytes + 1; // ... and the `_capacity` stays at zero! |
| 84 | } else { |
| 85 | my _dontOwnData = false; |
| 86 | my d_data = newvectorraw <unsigned char> (numberOfBytes + 1); |
| 87 | memcpy (my d_data.asArgumentToFunctionThatExpectsZeroBasedArray(), data, (size_t) numberOfBytes + 1); |
| 88 | } |
| 89 | return me; |
| 90 | } catch (MelderError) { |
| 91 | Melder_throw (U"FileInMemory not created from data."); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void FileInMemory_showAsCode (FileInMemory me, conststring32 name, integer numberOfBytesPerLine) { |
| 96 | Melder_assert (me); |
| 97 | if (numberOfBytesPerLine < 1) |
| 98 | numberOfBytesPerLine = 20; |
| 99 | |
| 100 | MelderInfo_write (U"\t\tstatic unsigned char ", name, U"_data[", my d_numberOfBytes+1, U"] = \n\""); |
| 101 | for (integer i = 1; i <= my d_numberOfBytes; i ++) { |
| 102 | const unsigned char number = my d_data [i]; |
| 103 | const unsigned char previousNumber = ( i > 1 ? my d_data [i - 1] : '\0' ); |
| 104 | const unsigned char nextNumber = ( i < my d_numberOfBytes ? my d_data [i + 1] : '\0' ); |
| 105 | if (i % numberOfBytesPerLine == 1 && i > 1) |
| 106 | MelderInfo_write (U"\"\n\""); |
| 107 | if (number >= 127) |
| 108 | MelderInfo_write (U"\\", number / 64, (number % 64) / 8, number % 8); |
| 109 | else if (number >= 32) { |
| 110 | if (number == '\"') |
| 111 | MelderInfo_write (U"\\\""); // two characters: \" ... |
| 112 | else if (number == '\\') |
| 113 | MelderInfo_write (U"\\\\"); // two characters: \\ ... |
| 114 | else if (number == '?') { |
| 115 | if (previousNumber == '?') // prevent trigraphs |
| 116 | MelderInfo_write (U"\\?"); // two characters: \? ... |
| 117 | else |
| 118 | MelderInfo_write (U"?"); |
| 119 | } else { |
| 120 | char32 buffer [2] = { number, U'\0' }; |
| 121 | MelderInfo_write (buffer); |
| 122 | } |
| 123 | } else if (number >= 14) { |
| 124 | if (nextNumber >= '0' && nextNumber <= '7' && i % numberOfBytesPerLine != 0) |
| 125 | MelderInfo_write (U"\\0", number / 8, number % 8); |
| 126 | else |
| 127 | MelderInfo_write (U"\\", number / 8, number % 8); |
| 128 | } else if (number >= 7) { |
| 129 | static char32 escapes [7] [3] = { U"\\a", U"\\b", U"\\t", U"\\n", U"\\v", U"\\f", U"\\r" }; |
| 130 | MelderInfo_write (escapes [number - 7]); |
| 131 | } else { |
| 132 | if (nextNumber >= '0' && nextNumber <= '7' && i % numberOfBytesPerLine != 0) |
| 133 | MelderInfo_write (U"\\00", number); |
| 134 | else |
| 135 | MelderInfo_write (U"\\", number); |
| 136 | } |
| 137 | } |
| 138 | MelderInfo_writeLine (U"\"\n\t\t;"); |
| 139 | MelderInfo_write (U"\t\tautoFileInMemory ", name, U" = FileInMemory_createWithData ("); |
| 140 | MelderInfo_writeLine (my d_numberOfBytes, U", ", name, U"_data, true, U\"", my string.get(), U"\");"); |
| 141 | } |
| 142 | |
| 143 | FileInMemory FileInMemory_fopen (FileInMemory me, const char *mode) { |
| 144 | Melder_assert (mode [0] == 'r'); // i.e. "r" or "rb"; only reading has been implemented; cannot write ('w') or append ('a') |
| 145 | my d_position = 0; // after opening, start at the beginning of the file |
| 146 | my d_errno = 0; |
| 147 | my d_eof = false; |
| 148 | my ungetChar = -1; |
| 149 | my isOpen = true; |
| 150 | return me; |
| 151 | } |
| 152 | |
| 153 | FileInMemory FileInMemorySet_fopen (FileInMemorySet me, const char *fileName, const char *mode) { |
| 154 | const integer index = my lookUp (Melder_peek8to32_u (fileName)); |
| 155 | if (index == 0) { |
| 156 | errno = ENOENT; // "no such file or directory" |
| 157 | return nullptr; |
| 158 | } |
| 159 | return FileInMemory_fopen (my at [index], mode); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | From http://www.cplusplus.com/reference/cstdio 20171028 |
| 164 | void rewind ( FILE * stream ); |
| 165 | |
| 166 | Set position of stream to the beginning |
| 167 | Sets the position indicator associated with stream to the beginning of the file. |
| 168 | |
| 169 | The end-of-file and error internal indicators associated to the stream are cleared after a successful call to this function, |
| 170 | and all effects from previous calls to ungetc on this stream are dropped. |
| 171 | |
| 172 | On streams open for update (read+write), a call to rewind allows to switch between reading and writing. |
| 173 | |
| 174 | Parameters |
| 175 | |
| 176 | stream |
| 177 | Pointer to a FILE object that identifies the stream. |
| 178 | |
| 179 | |
| 180 | Return Value |
| 181 | none |
| 182 | */ |
| 183 | void FileInMemory_rewind (FileInMemory me) { |
| 184 | Melder_assert (me); |
| 185 | Melder_assert (my isOpen); |
| 186 | my d_position = 0; // this was successful... |
| 187 | my d_eof = my d_errno = 0; // ...so we clear the end-of-file indicator and the error indicator |
| 188 | my ungetChar = -1; // we also drop any effects from previous calls to `ungetc` on this stream |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | From http://www.cplusplus.com/reference/cstdio 20171028 |
| 193 | int fclose ( FILE * stream ); |
| 194 | |
| 195 | Close file |
| 196 | Closes the file associated with the stream and disassociates it. |
| 197 | |
| 198 | All internal buffers associated with the stream are disassociated from it and flushed: |
| 199 | the content of any unwritten output buffer is written and the content of any unread input buffer is discarded. |
| 200 | |
| 201 | Even if the call fails, the stream passed as parameter will no longer be associated with the file nor its buffers. |
| 202 | |
| 203 | Parameters |
| 204 | |
| 205 | stream |
| 206 | Pointer to a FILE object that specifies the stream to be closed. |
| 207 | |
| 208 | |
| 209 | Return Value |
| 210 | If the stream is successfully closed, a zero value is returned. |
| 211 | On failure, EOF is returned. |
| 212 | */ |
| 213 | int FileInMemory_fclose (FileInMemory me) { |
| 214 | Melder_assert (me); |
| 215 | Melder_assert (my isOpen); |
| 216 | my d_position = 0; |
| 217 | my d_errno = 0; |
| 218 | my d_eof = false; |
| 219 | my ungetChar = -1; |
| 220 | my isOpen = false; |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | From http://www.cplusplus.com/reference/cstdio 20171028 |
| 226 | int feof ( FILE * stream ); |
| 227 | |
| 228 | Check end-of-file indicator |
| 229 | Checks whether the end-of-File indicator associated with stream is set, returning a value different from zero if it is. |
| 230 | |
| 231 | This indicator is generally set by a previous operation on the stream that attempted to read at or past the end-of-file. |
| 232 | |
| 233 | Notice that stream's internal position indicator may point to the end-of-file for the next operation, |
| 234 | but still, the end-of-file indicator may not be set until an operation attempts to read at that point. |
| 235 | |
| 236 | This indicator is cleared by a call to clearerr, rewind, fseek, fsetpos or freopen. |
| 237 | Although if the position indicator is not repositioned by such a call, the next i/o operation is likely to set the indicator again. |
| 238 | |
| 239 | Parameters |
| 240 | |
| 241 | stream |
| 242 | Pointer to a FILE object that identifies the stream. |
| 243 | |
| 244 | |
| 245 | Return Value |
| 246 | A non-zero value is returned in the case that the end-of-file indicator associated with the stream is set. |
| 247 | Otherwise, zero is returned. |
| 248 | */ |
| 249 | int FileInMemory_feof (FileInMemory me) { |
| 250 | Melder_assert (me); |
| 251 | Melder_assert (my isOpen); |
| 252 | return my d_eof; |
| 253 | } |
| 254 | |
| 255 | int FileInMemory_ferror (FileInMemory stream) { |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | From http://www.cplusplus.com/reference/cstdio 20171028 |
| 261 | int fseek ( FILE * stream, long int offset, int origin ); |
| 262 | |
| 263 | Reposition stream position indicator |
| 264 | Sets the position indicator associated with the stream to a new position. |
| 265 | |
| 266 | For streams open in binary mode, the new position is defined by adding offset to a reference position specified by origin. |
| 267 | |
| 268 | For streams open in text mode, offset shall either be zero or a value returned by a previous call to ftell, |
| 269 | and origin shall necessarily be SEEK_SET. |
| 270 | |
| 271 | If the function is called with other values for these arguments, |
| 272 | support depends on the particular system and library implementation (non-portable). |
| 273 | |
| 274 | The end-of-file internal indicator of the stream is cleared after a successful call to this function, |
| 275 | and all effects from previous calls to ungetc on this stream are dropped. |
| 276 | |
| 277 | On streams open for update (read+write), a call to fseek allows to switch between reading and writing. |
| 278 | |
| 279 | Parameters |
| 280 | |
| 281 | stream |
| 282 | Pointer to a FILE object that identifies the stream. |
| 283 | offset |
| 284 | Binary files: Number of bytes to offset from origin. |
| 285 | Text files: Either zero, or a value returned by ftell. |
| 286 | origin |
| 287 | Position used as reference for the offset. It is specified by one of the following constants defined in <cstdio> exclusively to be used as arguments for this function: |
| 288 | Constant Reference position |
| 289 | SEEK_SET Beginning of file |
| 290 | SEEK_CUR Current position of the file pointer |
| 291 | SEEK_END End of file * |
| 292 | * Library implementations are allowed to not meaningfully support SEEK_END (therefore, code using it has no real standard portability). |
| 293 | |
| 294 | |
| 295 | Return Value |
| 296 | If successful, the function returns zero. |
| 297 | Otherwise, it returns non-zero value. |
| 298 | If a read or write error occurs, the error indicator (ferror) is set. |
| 299 | */ |
| 300 | int FileInMemory_fseek (FileInMemory me, integer offset, int origin) { |
| 301 | Melder_assert (me); |
| 302 | Melder_assert (my isOpen); |
| 303 | my d_errno = 0; // set the error indicator to nonzero only if there is an error |
| 304 | integer newPosition = 0; |
| 305 | if (origin == SEEK_SET) |
| 306 | newPosition = offset; |
| 307 | else if (origin == SEEK_CUR) |
| 308 | newPosition = my d_position + offset; |
| 309 | else if (origin == SEEK_END) |
| 310 | newPosition = my d_numberOfBytes + offset; |
| 311 | else |
| 312 | Melder_throw (U"FileInMemory_fseek: undefined behaviour (origin ", origin, U")."); |
| 313 | |
| 314 | if (newPosition < 0) |
| 315 | return -1; // deemed "unsuccessful" |
| 316 | |
| 317 | // deemed "successful" |
| 318 | my d_position = newPosition; // even when greater than numberOfBytes |
| 319 | my ungetChar = -1; // drop all effects of previous calls to ungetc on this stream |
| 320 | my d_eof = false; // successful, so clear the end-of-file indicator |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | From http://www.cplusplus.com/reference/cstdio 20171028 |
| 326 | long int ftell ( FILE * stream ); |
| 327 | |
| 328 | Get current position in stream |
| 329 | Returns the current value of the position indicator of the stream. |
| 330 | |
| 331 | For binary streams, this is the number of bytes from the beginning of the file. |
| 332 | |
| 333 | For text streams, the numerical value may not be meaningful but can still be used to restore the position to the same position later using fseek |
| 334 | (if there are characters put back using ungetc still pending of being read, the behavior is undefined). |
| 335 | |
| 336 | Parameters |
| 337 | |
| 338 | stream |
| 339 | Pointer to a FILE object that identifies the stream. |
| 340 | |
| 341 | |
| 342 | Return Value |
| 343 | On success, the current value of the position indicator is returned. |
| 344 | On failure, -1L is returned, and errno is set to a system-specific positive value. |
| 345 | */ |
| 346 | integer FileInMemory_ftell (FileInMemory me) { |
| 347 | Melder_assert (me); |
| 348 | Melder_assert (my isOpen); |
| 349 | return my d_position; // FIXME: what about EBDF? |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | From http://www.cplusplus.com/reference/cstdio 20171028 |
| 354 | char * fgets ( char * str, int num, FILE * stream ); |
| 355 | |
| 356 | Get string from stream |
| 357 | Reads characters from stream and stores them as a C string into str until (num-1) characters have been read |
| 358 | or either a newline or the end-of-file is reached, whichever happens first. |
| 359 | |
| 360 | A newline character makes fgets stop reading, |
| 361 | but it is considered a valid character by the function and included in the string copied to str. |
| 362 | |
| 363 | A terminating null character is automatically appended after the characters copied to str. |
| 364 | |
| 365 | Notice that fgets is quite different from gets: not only fgets accepts a stream argument, |
| 366 | but also allows to specify the maximum size of str and includes in the string any ending newline character. |
| 367 | |
| 368 | Parameters |
| 369 | |
| 370 | str |
| 371 | Pointer to an array of chars where the string read is copied. |
| 372 | num |
| 373 | Maximum number of characters to be copied into str (including the terminating null-character). |
| 374 | stream |
| 375 | Pointer to a FILE object that identifies an input stream. |
| 376 | stdin can be used as argument to read from the standard input. |
| 377 | |
| 378 | |
| 379 | Return Value |
| 380 | On success, the function returns str. |
| 381 | If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). |
| 382 | If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged). |
| 383 | If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned |
| 384 | (but the contents pointed by str may have changed). |
| 385 | */ |
| 386 | char *FileInMemory_fgets (char *buffer, int bufferSize, FileInMemory me) { |
| 387 | Melder_assert (me); |
| 388 | Melder_assert (my isOpen); |
| 389 | if (bufferSize <= 0) |
| 390 | return nullptr; // "Undefined Behavior" |
| 391 | if (bufferSize == 1) { |
| 392 | buffer [0] = '\0'; |
| 393 | return buffer; // the usual interpretation of this edge case |
| 394 | } |
| 395 | if (my d_eof) // end-of-file will be encountered while attempting to read a character? |
| 396 | // then set the end-of-file indicator (but it has already been set)... |
| 397 | return nullptr; //... and as no characters could be read, we return a null pointer, leaving the contents of str unchanged |
| 398 | Melder_assert (bufferSize >= 2); // so we will be reading at least one character |
| 399 | integer startingPosition = my d_position; |
| 400 | if (startingPosition >= my d_numberOfBytes) { // this includes the case of an empty file |
| 401 | /* |
| 402 | We already know that the first character that we will attempt to read lies past the end of the file. |
| 403 | */ |
| 404 | my d_eof = true; |
| 405 | my d_errno = 0; // this flag is only for other errors than end-of-file, so we don't set it (FIXME: should we indeed clear it?) |
| 406 | return nullptr; // as no characters could be read, we return a null pointer, leaving the contents of str unchanged |
| 407 | } |
| 408 | Melder_assert (startingPosition < my d_numberOfBytes); |
| 409 | const integer maximumNumberOfCharactersToCopy = std::min ((integer) bufferSize - 1, my d_numberOfBytes - startingPosition); |
| 410 | Melder_assert (maximumNumberOfCharactersToCopy >= 1); |
| 411 | const uint8 *source = my d_data.asArgumentToFunctionThatExpectsZeroBasedArray() + startingPosition; |
| 412 | if (my ungetChar >= 0) { // this includes stray null bytes |
| 413 | buffer [0] = (char) (unsigned char) (unsigned int) my ungetChar; // guarded sign conversion from int to unsigned int |
| 414 | my ungetChar = -1; |
| 415 | } else |
| 416 | buffer [0] = (char) source [0]; |
| 417 | integer numberOfCharactersCopied = 1; |
| 418 | if (buffer [0] != '\n') |
| 419 | while (numberOfCharactersCopied < maximumNumberOfCharactersToCopy) { |
| 420 | char kar = (char) source [numberOfCharactersCopied]; |
| 421 | buffer [numberOfCharactersCopied] = kar; |
| 422 | numberOfCharactersCopied ++; |
| 423 | if (kar == '\n') |
| 424 | break; |
| 425 | } |
| 426 | buffer [numberOfCharactersCopied] = '\0'; |
| 427 | if (buffer [numberOfCharactersCopied - 1] != '\n') |
| 428 | my d_eof = true; // this seems to be the somewhat weird behaviour of `fgets` on all platforms checked |
| 429 | my d_position += numberOfCharactersCopied; |
| 430 | Melder_assert (my d_position <= my d_numberOfBytes); |
| 431 | return buffer; // everything OK |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | From http://www.cplusplus.com/reference/cstdio 20171028 |
| 436 | int fgetc ( FILE * stream ); |
| 437 | |
| 438 | Get character from stream |
| 439 | Returns the character currently pointed by the internal file position indicator of the specified stream. |
| 440 | The internal file position indicator is then advanced to the next character. |
| 441 | |
| 442 | If the stream is at the end-of-file when called, the function returns EOF and sets the end-of-file indicator for the stream (feof). |
| 443 | |
| 444 | If a read error occurs, the function returns EOF and sets the error indicator for the stream (ferror). |
| 445 | |
| 446 | fgetc and getc are equivalent, except that getc may be implemented as a macro in some libraries. |
| 447 | |
| 448 | Parameters |
| 449 | |
| 450 | stream |
| 451 | Pointer to a FILE object that identifies an input stream. |
| 452 | |
| 453 | |
| 454 | Return Value |
| 455 | On success, the character read is returned (promoted to an int value). |
| 456 | The return type is int to accommodate for the special value EOF, which indicates failure: |
| 457 | If the position indicator was at the end-of-file, the function returns EOF and sets the eof indicator (feof) of stream. |
| 458 | If some other reading error happens, the function also returns EOF, but sets its error indicator (ferror) instead. |
| 459 | */ |
| 460 | int FileInMemory_fgetc (FileInMemory me) { |
| 461 | Melder_assert (me); |
| 462 | Melder_assert (my isOpen); |
| 463 | char kar; |
| 464 | (void) FileInMemory_fread (& kar, 1, 1, me); |
| 465 | return FileInMemory_feof (me) ? EOF : (int) (unsigned int) (unsigned char) (kar); |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | From http://www.cplusplus.com/reference/cstdio 20171028 |
| 470 | size_t fread ( void * ptr, size_t size, size_t count, FILE * stream ); |
| 471 | |
| 472 | Read block of data from stream |
| 473 | Reads an array of count elements, each one with a size of size bytes, |
| 474 | from the stream and stores them in the block of memory specified by ptr. |
| 475 | |
| 476 | The position indicator of the stream is advanced by the total amount of bytes read. |
| 477 | |
| 478 | The total amount of bytes read if successful is (size*count). |
| 479 | |
| 480 | Parameters |
| 481 | |
| 482 | ptr |
| 483 | Pointer to a block of memory with a size of at least (size*count) bytes, converted to a void*. |
| 484 | size |
| 485 | Size, in bytes, of each element to be read. |
| 486 | size_t is an unsigned integral type. |
| 487 | count |
| 488 | Number of elements, each one with a size of size bytes. |
| 489 | size_t is an unsigned integral type. |
| 490 | stream |
| 491 | Pointer to a FILE object that specifies an input stream. |
| 492 | |
| 493 | Return Value |
| 494 | The total number of elements successfully read is returned. |
| 495 | If this number differs from the count parameter, either a reading error occurred or the end-of-file was reached while reading. In |
| 496 | both cases, the proper indicator is set, which can be checked with ferror and feof, respectively. |
| 497 | If either size or count is zero, the function returns zero and both the stream state and the content pointed by ptr remain unchanged. |
| 498 | size_t is an unsigned integral type. |
| 499 | */ |
| 500 | size_t FileInMemory_fread (void *void_buffer, size_t elementSize_unsigned, size_t numberOfElementsThatShouldBeRead_unsigned, FileInMemory me) { |
| 501 | Melder_assert (me); |
| 502 | Melder_assert (my isOpen); |
| 503 | const integer elementSize = uinteger_to_integer_a (elementSize_unsigned); |
| 504 | const integer numberOfElementsThatShouldBeRead = uinteger_to_integer_a (numberOfElementsThatShouldBeRead_unsigned); |
| 505 | const double numberOfBytesThatShouldBeRead_real = (double) elementSize * (double) numberOfElementsThatShouldBeRead; |
| 506 | integer numberOfBytesThatShouldBeRead; |
| 507 | if (numberOfBytesThatShouldBeRead_real > (double) INTEGER_MAX) { |
| 508 | my d_eof = true; |
| 509 | numberOfBytesThatShouldBeRead = INTEGER_MAX; |
| 510 | } else |
| 511 | numberOfBytesThatShouldBeRead = elementSize * numberOfElementsThatShouldBeRead; |
| 512 | if (numberOfBytesThatShouldBeRead == 0) |
| 513 | return 0; |
| 514 | const integer numberOfBytesLeftInFile = my d_numberOfBytes - my d_position; |
| 515 | if (numberOfBytesLeftInFile <= 0) { |
| 516 | my d_eof = true; |
| 517 | return 0; |
| 518 | } |
| 519 | Melder_assert (numberOfBytesThatShouldBeRead > 0); |
| 520 | const integer numberOfElementsThatCanBeRead = numberOfBytesLeftInFile / elementSize; // integer division, rounding down |
| 521 | const integer numberOfElementsThatWillBeRead = std::min (numberOfElementsThatShouldBeRead, numberOfElementsThatCanBeRead); |
| 522 | if (numberOfElementsThatWillBeRead < numberOfElementsThatShouldBeRead) |
| 523 | my d_eof = true; |
| 524 | #if 0 |
| 525 | const integer numberOfBytesThatWillBeRead = elementSize * numberOfElementsThatWillBeRead; |
| 526 | #else |
| 527 | const integer numberOfBytesThatWillBeRead = std::min (numberOfBytesThatShouldBeRead, numberOfBytesLeftInFile); |
| 528 | #endif |
| 529 | Melder_assert (numberOfBytesThatWillBeRead <= numberOfBytesLeftInFile); |
| 530 | if (numberOfBytesThatWillBeRead == 0) |
| 531 | return 0; |
| 532 | |
| 533 | integer startingPosition = my d_position; |
| 534 | if (startingPosition >= my d_numberOfBytes) { // this includes the case of an empty file |
| 535 | /* |
| 536 | We already know that the first character that we will attempt to read lies past the end of the file. |
| 537 | */ |
| 538 | my d_eof = true; |
| 539 | my d_errno = 0; // this flag is only for other errors than end-of-file, so we don't set it (FIXME: should we indeed clear it?) |
| 540 | return 0; // as no characters could be read, we return a null pointer, leaving the contents of str unchanged |
| 541 | } |
| 542 | Melder_assert (startingPosition < my d_numberOfBytes); |
| 543 | const uint8 *source = my d_data.asArgumentToFunctionThatExpectsZeroBasedArray() + startingPosition; |
| 544 | uint8 *buffer = (uint8 *) void_buffer; |
| 545 | if (my ungetChar >= 0) { // this includes stray null bytes |
| 546 | buffer [0] = (char) (unsigned char) (unsigned int) my ungetChar; // guarded sign conversion from int to unsigned int |
| 547 | my ungetChar = -1; |
| 548 | } else |
| 549 | buffer [0] = (char) source [0]; |
| 550 | integer numberOfBytesCopied = 1; |
| 551 | while (numberOfBytesCopied < numberOfBytesThatWillBeRead) { |
| 552 | char kar = (char) source [numberOfBytesCopied]; |
| 553 | buffer [numberOfBytesCopied] = kar; |
| 554 | numberOfBytesCopied ++; |
| 555 | } |
| 556 | my d_position += numberOfBytesCopied; |
| 557 | Melder_assert (my d_position <= my d_numberOfBytes); |
| 558 | return integer_to_uinteger_a (numberOfElementsThatWillBeRead); |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | From http://www.cplusplus.com/reference/cstdio 20171028 |
| 563 | int ungetc ( int character, FILE * stream ); |
| 564 | |
| 565 | Unget character from stream |
| 566 | A character is virtually put back into an input stream, decreasing its internal file position as if a previous getc operation was undone. |
| 567 | |
| 568 | This character may or may not be the one read from the stream in the preceding input operation. In any case, the next character retrieved from stream is the character passed to this function, independently of the original one. |
| 569 | |
| 570 | Notice though, that this only affects further input operations on that stream, and not the content of the physical file associated with it, which is not modified by any calls to this function. |
| 571 | |
| 572 | Some library implementations may support this function to be called multiple times, making the characters available in the reverse order in which they were put back. Although this behavior has no standard portability guarantees, and further calls may simply fail after any number of calls beyond the first. |
| 573 | |
| 574 | If successful, the function clears the end-of-file indicator of stream (if it was currently set), and decrements its internal file position indicator if it operates in binary mode; In text mode, the position indicator has unspecified value until all characters put back with ungetc have been read or discarded. |
| 575 | |
| 576 | A call to fseek, fsetpos or rewind on stream will discard any characters previously put back into it with this function. |
| 577 | |
| 578 | If the argument passed for the character parameter is EOF, the operation fails and the input stream remains unchanged. |
| 579 | |
| 580 | Parameters |
| 581 | |
| 582 | character |
| 583 | The int promotion of the character to be put back. |
| 584 | The value is internally converted to an unsigned char when put back. |
| 585 | stream |
| 586 | Pointer to a FILE object that identifies an input stream. |
| 587 | |
| 588 | |
| 589 | Return Value |
| 590 | On success, the character put back is returned. |
| 591 | If the operation fails, EOF is returned. |
| 592 | */ |
| 593 | |
| 594 | int FileInMemory_ungetc (int character, FileInMemory me) { |
| 595 | Melder_assert (me); |
| 596 | Melder_assert (my isOpen); |
| 597 | int result = EOF; |
| 598 | if (character != EOF) { |
| 599 | -- my d_position; |
| 600 | result = my ungetChar = character; |
| 601 | } |
| 602 | return result; |
| 603 | } |
| 604 | |
| 605 | int FileInMemorySet_stat (FileInMemorySet me, const char *path, struct stat *buf) { |
| 606 | conststring32 path32 = Melder_peek8to32_u (path); |
| 607 | integer position = my lookUp (path32); |
| 608 | if (position > 0) { |
| 609 | buf -> st_mode = S_IFREG; |
| 610 | buf -> st_size = my at [position] -> d_numberOfBytes; |
| 611 | return 0; |
| 612 | } |
| 613 | if (FileInMemorySet_hasDirectory (me, path32)) { |
| 614 | buf -> st_mode = S_IFDIR; |
| 615 | buf -> st_size = 0; |
| 616 | } |
| 617 | errno = ENOENT; |
| 618 | return 1; |
| 619 | } |
| 620 | |
| 621 | void structFileInMemorySet :: v1_info () { |
| 622 | MelderInfo_writeLine (U"Number of files: ", size); |
| 623 | MelderInfo_writeLine (U"Total number of bytes: ", FileInMemorySet_getTotalNumberOfBytes (this)); |
| 624 | } |
| 625 | |
| 626 | integer FileInMemorySet_getTotalNumberOfBytes (FileInMemorySet me) { |
| 627 | integer numberOfBytes = 0; |
| 628 | for (integer ifile = 1; ifile <= my size; ifile ++) { |
| 629 | const FileInMemory fim = my at [ifile]; |
| 630 | numberOfBytes += fim -> d_numberOfBytes; |
| 631 | } |
| 632 | return numberOfBytes; |
| 633 | } |
| 634 | |
| 635 | autoFileInMemorySet FileInMemorySets_merge (OrderedOf<structFileInMemorySet>& list) { |
| 636 | try { |
| 637 | autoFileInMemorySet thee = Data_copy (list.at [1]); |
| 638 | for (integer iset = 1; iset <= list.size; iset ++) |
| 639 | thy merge (list.at [iset]); |
| 640 | return thee; |
| 641 | } catch (MelderError) { |
| 642 | Melder_throw (U"FileInMemorySets not merged."); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | autoFileInMemorySet FileInMemorySet_createFromDirectoryContents (conststring32 dirpath, conststring32 fileGlobber) { |
| 647 | try { |
| 648 | structMelderFolder parent { }; |
| 649 | Melder_pathToFolder (dirpath, & parent); // not Melder_relativePathToFolder! Because if dirpath is relative, so should be the fims |
| 650 | autoStrings thee = Strings_createAsFileList (Melder_cat (dirpath, U"/", fileGlobber)); |
| 651 | Melder_require (thy numberOfStrings > 0, |
| 652 | U"No files found."); |
| 653 | |
| 654 | autoFileInMemorySet me = FileInMemorySet_create (); |
| 655 | for (integer i = 1; i <= thy numberOfStrings; i ++) { |
| 656 | structMelderFile file { }; |
| 657 | //MelderFolder_getFile (& parent, thy strings [i].get(), & file); // this would convert forward to backward slashes on Windows |
| 658 | conststring32 filePath = Melder_cat (dirpath, U"/", thy strings [i].get()); // this conserves forward slashes, even on Windows |
| 659 | Melder_pathToFile (filePath, & file); // this conserves forward slashes, even on Windows |
| 660 | my addItem_move (FileInMemory_create (& file)); |
| 661 | } |
| 662 | return me; |
| 663 | } catch (MelderError) { |
| 664 | Melder_throw (U"FileInMemorySet not created from directory \"", dirpath, U"\" for files that match \"", fileGlobber, U"\"."); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | autoFileInMemorySet FilesInMemory_to_FileInMemorySet (OrderedOf<structFileInMemory>& list) { |
| 669 | try { |
| 670 | autoFileInMemorySet thee = FileInMemorySet_create (); |
| 671 | for (integer ifile = 1; ifile <= list.size; ifile ++) |
| 672 | thy addItem_move (Data_copy (list.at [ifile])); |
| 673 | return thee; |
| 674 | } catch (MelderError) { |
| 675 | Melder_throw (U"FilesInMemory not collected in FileInMemorySet."); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | autoFileInMemorySet FileInMemorySet_extractFiles (FileInMemorySet me, kMelder_string which, conststring32 criterion) { |
| 680 | try { |
| 681 | autoFileInMemorySet thee = Thing_new (FileInMemorySet); |
| 682 | for (integer ifile = 1; ifile <= my size; ifile ++) { |
| 683 | const FileInMemory fim = my at [ifile]; |
| 684 | if (Melder_stringMatchesCriterion (fim -> string.get(), which, criterion, true)) |
| 685 | thy addItem_move (Data_copy (fim)); |
| 686 | } |
| 687 | return thee; |
| 688 | } catch (MelderError) { |
| 689 | Melder_throw (me, U": cannot extract files."); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | autoFileInMemorySet FileInMemorySet_removeFiles (FileInMemorySet me, kMelder_string which, conststring32 criterion) { |
| 694 | try { |
| 695 | autoFileInMemorySet thee = Thing_new (FileInMemorySet); |
| 696 | for (integer ifile = 1; ifile <= my size; ifile ++) { |
| 697 | const FileInMemory fim = my at [ifile]; |
| 698 | if (Melder_stringMatchesCriterion (fim -> string.get(), which, criterion, true)) |
| 699 | thy addItem_move (my subtractItem_move (ifile)); |
| 700 | } |
| 701 | return thee; |
| 702 | } catch (MelderError) { |
| 703 | Melder_throw (me, U": cannot extract files."); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | autoFileInMemorySet FileInMemorySet_listFiles (FileInMemorySet me, kMelder_string which, conststring32 criterion) { |
| 708 | try { |
| 709 | autoFileInMemorySet thee = Thing_new (FileInMemorySet); |
| 710 | for (integer ifile = 1; ifile <= my size; ifile ++) { |
| 711 | const FileInMemory fim = my at [ifile]; |
| 712 | if (Melder_stringMatchesCriterion (fim -> string.get(), which, criterion, true)) |
| 713 | thy addItem_ref (fim); |
| 714 | } |
| 715 | return thee; |
| 716 | } catch (MelderError) { |
| 717 | Melder_throw (me, U": cannot extract files."); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | void FileInMemorySet_showAsCode (FileInMemorySet me, conststring32 functionName, integer numberOfBytesPerLine) { |
| 722 | autoMelderString fimName; |
| 723 | MelderInfo_writeLine (U"#include \"FileInMemory.h\"\n"); |
| 724 | MelderInfo_writeLine (U"void ", functionName, U" (FileInMemorySet me) {"); |
| 725 | MelderInfo_writeLine (U"\ttry {"); |
| 726 | for (integer ifile = 1; ifile <= my size; ifile ++) { |
| 727 | const FileInMemory fim = my at [ifile]; |
| 728 | MelderString_copy (& fimName, U"fim", ifile); |
| 729 | FileInMemory_showAsCode (fim, fimName.string, numberOfBytesPerLine); |
| 730 | MelderInfo_writeLine (U"\t\tmy addItem_move (", fimName.string, U".move());\n"); |
| 731 | } |
| 732 | MelderInfo_writeLine (U"\t} catch (MelderError) {"); |
| 733 | MelderInfo_writeLine (U"\t\tMelder_throw (U\"Not everything was added to the FileInMemorySet.\");"); |
| 734 | MelderInfo_writeLine (U"\t}"); |
| 735 | MelderInfo_writeLine (U"}\n"); |
| 736 | } |
| 737 | |
| 738 | void FileInMemorySet_showOneFileAsCode (FileInMemorySet me, integer index, conststring32 name, integer numberOfBytesPerLine) { |
| 739 | if (index < 1 || index > my size) |
| 740 | return; |
| 741 | MelderInfo_writeLine (U"#include \"FileInMemory.h\"\n"); |
| 742 | MelderInfo_writeLine (U"static autoFileInMemory create_new_object () {"); |
| 743 | MelderInfo_writeLine (U"\ttry {"); |
| 744 | autoMelderString one_fim; |
| 745 | const FileInMemory fim = my at [index]; |
| 746 | MelderString_append (& one_fim, name, index); |
| 747 | FileInMemory_showAsCode (fim, U"me", numberOfBytesPerLine); |
| 748 | MelderInfo_writeLine (U"\t\treturn me;"); |
| 749 | MelderInfo_writeLine (U"\t} catch (MelderError) {"); |
| 750 | MelderInfo_writeLine (U"\t\tMelder_throw (U\"FileInMemory not created.\");"); |
| 751 | MelderInfo_writeLine (U"\t}"); |
| 752 | MelderInfo_writeLine (U"}\n\n"); |
| 753 | MelderInfo_writeLine (U"autoFileInMemory ", name, U" = create_new_object ();"); |
| 754 | } |
| 755 | |
| 756 | integer FileInMemorySet_findNumberOfMatches_path (FileInMemorySet me, kMelder_string which, conststring32 criterion) { |
| 757 | integer numberOfMatches = 0; |
| 758 | for (integer ifile = 1; ifile <= my size; ifile ++) { |
| 759 | const FileInMemory fim = my at [ifile]; |
| 760 | if (Melder_stringMatchesCriterion (fim -> string.get(), which, criterion, true)) |
| 761 | numberOfMatches ++; |
| 762 | } |
| 763 | return numberOfMatches; |
| 764 | } |
| 765 | |
| 766 | bool FileInMemorySet_hasDirectory (FileInMemorySet me, conststring32 name) { |
| 767 | autoMelderString searchString; |
| 768 | MelderString_append (& searchString, U"/", name, U"/"); |
| 769 | for (integer i = 1; i <= my size; i ++) { |
| 770 | const FileInMemory fim = my at [i]; |
| 771 | if (str32str (fim -> string.get(), searchString.string)) |
| 772 | return true; |
| 773 | } |
| 774 | return false; |
| 775 | } |
| 776 | |
| 777 | autoStrings FileInMemorySet_to_Strings_path (FileInMemorySet me) { |
| 778 | try { |
| 779 | autoStrings thee = Thing_new (Strings); |
| 780 | thy strings = autoSTRVEC (my size); |
| 781 | thy numberOfStrings = 0; |
| 782 | for (integer ifile = 1; ifile <= my size; ifile ++) { |
| 783 | const FileInMemory fim = my at [ifile]; |
| 784 | thy strings [ifile] = Melder_dup_f (fim -> string.get()); |
| 785 | thy numberOfStrings ++; |
| 786 | } |
| 787 | return thee; |
| 788 | } catch (MelderError) { |
| 789 | Melder_throw (U"No Strings created from FilesinMemory."); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | The following test is meant to be called only from `test/dwsys/FileInMemory.praat`, |
| 795 | because that is where the subfolder `examples/FileInMemory` resides, |
| 796 | which contains the files that are tested. |
| 797 | |
| 798 | If the files aren't there yet, they can be created by setting `WRITE` to true |
| 799 | (writing is not entirely trivial, because the files may contain null bytes |
| 800 | and high bytes, all of which may be difficult to type from a text editor. |
| 801 | Note that `WRITE` should normally be false, for security reasons. |
| 802 | |
| 803 | For the content, we use char8 instead of char32, |
| 804 | because otherwise we would have to use `Melder_peek32to8`, which on Windows would convert newlines. |
| 805 | */ |
| 806 | |
| 807 | static conststring32 theTestSubfolder = U"examples/FileInMemory"; // constant |
| 808 | static autoFileInMemorySet theTestFileInMemorySet; // singleton |
| 809 | |
| 810 | static structMelderFile theTestFile; // we make this "global" in order to still be able to do things with it after running `testOneFile`... |
| 811 | static FILE *theTestFilePointer; // ... and this as well... |
| 812 | static FileInMemory theTestFim; // ... and this as well... |
| 813 | |
| 814 | static void testOneFile ( |
| 815 | conststring32 fileName, // usually relative to test/dwsys |
| 816 | const char content [40], // this is not a string, because it can contain null bytes |
| 817 | const integer length, // this is the length of `contents` (note that this cannot be determined by `strlen` if there are null bytes) |
| 818 | const char lineContents [5][40], // the intended contents of the (maximally) five lines |
| 819 | const integer lineLengths [5], // the intended lengths of the (maximally) five lines |
| 820 | const bool endsInNewline |
| 821 | ) { |
| 822 | try { |
| 823 | static MelderString relativePath; |
| 824 | MelderString_copy (& relativePath, Melder_cat (theTestSubfolder, U"/", fileName)); |
| 825 | Melder_relativePathToFile (relativePath.string, & theTestFile); |
| 826 | |
| 827 | constexpr bool WRITE = false; // this should normally be false!! |
| 828 | if constexpr (WRITE) { |
| 829 | Melder_warning ( |
| 830 | U"YOU ARE USING AN UNSAFE VERSION OF PRAAT, " |
| 831 | U"which is meant to be used only by Praat’s authors.\n" |
| 832 | U"If you are not a Praat author (but instead a user or maintainer), " |
| 833 | U"please notify the authors at [email protected], " |
| 834 | U"telling them to “set WRITE to false in the source code of the FileInMemory test”.\n\n" |
| 835 | U"If you proceed to click OK, Praat will create the file ", & theTestFile, U". ", |
| 836 | U"If you don’t want this, force-quit Praat now." |
| 837 | ); // FIXME: to be replaced with permission window |
| 838 | FILE *fout = Melder_fopen (& theTestFile, "wb"); |
| 839 | fwrite (content, 1, (size_t) length, fout); |
| 840 | Melder_fclose (& theTestFile, fout); |
| 841 | } |
| 842 | /* |
| 843 | Initialize. |
| 844 | */ |
| 845 | errno = 0; |
| 846 | integer numberOfLines = 0, numberOfBytes = 0; |
| 847 | while (lineLengths [numberOfLines] > 0) // no lines of length 0 can exist (there's always at least a newline character) |
| 848 | numberOfBytes += lineLengths [numberOfLines ++]; |
| 849 | MelderInfo_writeLine (U"Testing file \"", fileName, U"\" with ", numberOfLines, U" lines and ", numberOfBytes, U" bytes."); |
| 850 | if (theTestFilePointer) { |
| 851 | Melder_fclose (& theTestFile, theTestFilePointer); |
| 852 | theTestFilePointer = nullptr; |
| 853 | } |
| 854 | if (theTestFim) { |
| 855 | FileInMemory_fclose (theTestFim); |
| 856 | theTestFim = nullptr; |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | fopen should return NULL if the file doesn't exist. |
| 861 | Full paths should not work: |
| 862 | */ |
| 863 | MelderInfo_writeLine (U"Testing fopen..."); |
| 864 | MelderInfo_writeLine (U"\tTrying to open file-in-memory via full path:\n\t\t\"", MelderFile_peekPath (& theTestFile), U"\"..."); |
| 865 | theTestFim = FileInMemorySet_fopen (theTestFileInMemorySet.get(), MelderFile_peekPath8 (& theTestFile), "rb"); |
| 866 | Melder_require (theTestFim == nullptr, |
| 867 | U"FileInMemory should have been null."); |
| 868 | /* |
| 869 | If the returned file is NULL, errno should contain a relevant message. |
| 870 | */ |
| 871 | Melder_require (errno == ENOENT, |
| 872 | U"It should have been reported that the file ", & theTestFile, U" does not exist."); |
| 873 | MelderInfo_writeLine (U"\t\t\t... not opened"); |
| 874 | /* |
| 875 | Raw file names should not work: |
| 876 | */ |
| 877 | MelderInfo_writeLine (U"\tTrying to open file-in-memory via raw file name:\n\t\t\"", fileName, U"\"..."); |
| 878 | theTestFim = FileInMemorySet_fopen (theTestFileInMemorySet.get(), Melder_peek32to8_fileSystem (fileName), "rb"); |
| 879 | Melder_require (theTestFim == nullptr, |
| 880 | U"FileInMemory should still have been null."); |
| 881 | /* |
| 882 | If the returned file is NULL, errno should contain a relevant message. |
| 883 | */ |
| 884 | Melder_require (errno == ENOENT, |
| 885 | U"It should still have been reported that the file ", fileName, U" does not exist."); |
| 886 | MelderInfo_writeLine (U"\t\t\t... not opened"); |
| 887 | /* |
| 888 | fopen should return a valid pointer if the file does exist. |
| 889 | Relative paths that were used for creating the FileInMemorySet should work: |
| 890 | */ |
| 891 | MelderInfo_writeLine (U"\tTrying to open file-in-memory via relative path:\n\t\t\"", relativePath.string, U"\"..."); |
| 892 | theTestFilePointer = Melder_fopen (& theTestFile, "rb"); |
| 893 | Melder_require (theTestFilePointer != nullptr, |
| 894 | U"FILE* should exist."); |
| 895 | theTestFim = FileInMemorySet_fopen (theTestFileInMemorySet.get(), Melder_peek32to8_fileSystem (relativePath.string), "rb"); |
| 896 | Melder_require (theTestFim != nullptr, |
| 897 | U"FileInMemory should exist."); |
| 898 | MelderInfo_writeLine (U"\t\t\t... opened"); |
| 899 | /* |
| 900 | fopen should never clear errno. |
| 901 | */ |
| 902 | Melder_require (errno == ENOENT, |
| 903 | U"The old error number should still exist."); |
| 904 | |
| 905 | /* |
| 906 | Now that both files are open, |
| 907 | we can measure their sizes with `fseek` and `fgets`. |
| 908 | */ |
| 909 | MelderInfo_writeLine (U"Testing fseek, ftell, feof..."); |
| 910 | fseek (theTestFilePointer, 0, SEEK_END); |
| 911 | Melder_require (ftell (theTestFilePointer) == length, |
| 912 | U"The file should have ", length, U" bytes, not ", ftell (theTestFilePointer)); |
| 913 | FileInMemory_fseek (theTestFim, 0, SEEK_END); |
| 914 | Melder_require (FileInMemory_ftell (theTestFim) == length, |
| 915 | U"The file-in-memory should have ", length, U" bytes, not ", FileInMemory_ftell (theTestFim)); |
| 916 | |
| 917 | /* |
| 918 | Now that both files are open, |
| 919 | we can compare their contents by `fgets`. |
| 920 | */ |
| 921 | MelderInfo_writeLine (U"Testing fgets, rewind, ftell, feof..."); |
| 922 | rewind (theTestFilePointer); |
| 923 | FileInMemory_rewind (theTestFim); |
| 924 | constexpr integer bufferSize = 200; |
| 925 | char fbuffer [bufferSize], fimbuffer [bufferSize], *fline, *fimline; |
| 926 | integer intendedPosition = 0; |
| 927 | for (integer iline = 1; iline <= numberOfLines; iline ++) { |
| 928 | Melder_assert (iline-1 >= 0); // bounds-checking |
| 929 | const char *lineContent = lineContents [iline-1]; |
| 930 | const integer lineLength = lineLengths [iline-1]; |
| 931 | fline = fgets (fbuffer, bufferSize, theTestFilePointer); |
| 932 | fimline = FileInMemory_fgets (fimbuffer, bufferSize, theTestFim); |
| 933 | Melder_require (!! fline, |
| 934 | U"File-line ", iline, U" should exist."); |
| 935 | Melder_require (!! fimline, |
| 936 | U"Fim-line ", iline, U" should exist."); |
| 937 | Melder_require (fline [lineLength] == '\0', |
| 938 | U"File-line ", iline, U" should have a null byte after it, not ", (integer) fline [lineLength], U"."); |
| 939 | Melder_require (fimline [lineLength] == '\0', |
| 940 | U"Fm-line ", iline, U" should have a null byte after it, not ", (integer) fimline [lineLength], U"."); |
| 941 | Melder_require (memcmp (fline, lineContent, (size_t) lineLength) == 0, // not strcmp, because of potential null bytes |
| 942 | U"File-line ", iline, U" (\"", Melder_peek8to32_u (fline), U"\") should have been \"", Melder_peek8to32_u (lineContent), U"\"."); |
| 943 | Melder_require (memcmp (fimline, lineContent, (size_t) lineLength) == 0, // not strcmp, because of potential null bytes |
| 944 | U"Fim-line ", iline, U" (\"", Melder_peek8to32_u (fimline), U"\") should have been \"", Melder_peek8to32_u (lineContent), U"\"."); |
| 945 | intendedPosition += lineLength; |
| 946 | integer fpos = ftell (theTestFilePointer); |
| 947 | integer fimpos = FileInMemory_ftell (theTestFim); |
| 948 | Melder_require (fpos == intendedPosition, |
| 949 | U"After line ", iline, U" the file should be at position ", intendedPosition, U"."); |
| 950 | Melder_require (fimpos == intendedPosition, |
| 951 | U"After line ", iline, U" the file-in-memory should be at position ", intendedPosition, U", not ", fimpos, U"."); |
| 952 | if (iline == numberOfLines) { |
| 953 | /* |
| 954 | We are at the end of the files, but no end-of-file indicator should have been set, |
| 955 | because we haven't attempted to read *beyond* the end. |
| 956 | |
| 957 | Well, this turns out to be true only if there was no final newline character (who can document this behaviour?). |
| 958 | */ |
| 959 | Melder_assert (lineLength-1 >= 0); // bounds-checking |
| 960 | if (endsInNewline) { |
| 961 | Melder_require (feof (theTestFilePointer) == 0, |
| 962 | U"At the end of the file, we haven't yet read beyond it."); |
| 963 | Melder_require (FileInMemory_feof (theTestFim) == 0, |
| 964 | U"At the end of the file-in-memory, we haven't yet read beyond it."); |
| 965 | /* |
| 966 | `fgets` should have included the last newline character. |
| 967 | */ |
| 968 | Melder_assert (fline [lineLength-1] == '\n'); |
| 969 | Melder_require (fimline [lineLength-1] == '\n', |
| 970 | U"The last character should be a newline."); |
| 971 | } else { |
| 972 | /* |
| 973 | `fgets` has not included any trailing newline character, |
| 974 | and apparently this should lead to an end-of-file indication. |
| 975 | */ |
| 976 | Melder_require (fline [lineLength-1] != '\n', |
| 977 | U"The last character of the file should not be a newline."); |
| 978 | Melder_require (fimline [lineLength-1] != '\n', |
| 979 | U"The last character of the file-in-memory should not be a newline."); |
| 980 | Melder_require (feof (theTestFilePointer) != 0, |
| 981 | U"At the end of the file, we haven't yet read beyond it."); |
| 982 | Melder_require (FileInMemory_feof (theTestFim) != 0, |
| 983 | U"At the end of the file-in-memory, we haven't yet read beyond it."); |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | /* |
| 988 | But now we read beyond the end of file, switching on the marker. |
| 989 | */ |
| 990 | fline = fgets (fbuffer, bufferSize, theTestFilePointer); |
| 991 | fimline = FileInMemory_fgets (fimbuffer, bufferSize, theTestFim); |
| 992 | Melder_require (! fline, |
| 993 | U"File-line ", numberOfLines + 1, U" should not exist."); |
| 994 | Melder_require (! fimline, |
| 995 | U"Fim-line ", numberOfLines + 1, U" should not exist."); |
| 996 | Melder_require (feof (theTestFilePointer) != 0, |
| 997 | U"Past the end of the file, the end-of-file indicator should have been set."); |
| 998 | Melder_require (FileInMemory_feof (theTestFim) != 0, |
| 999 | U"Past the end of the file-in-memory, the end-of-file indicator should have been set."); |
| 1000 | |
| 1001 | /* |
| 1002 | Rewinding to the start of the files should clear their end-of-file markers. |
| 1003 | */ |
| 1004 | MelderInfo_writeLine (U"Testing rewind, ftell, feof..."); |
| 1005 | rewind (theTestFilePointer); |
| 1006 | FileInMemory_rewind (theTestFim); |
| 1007 | Melder_assert (ftell (theTestFilePointer) == 0); |
| 1008 | Melder_assert (FileInMemory_ftell (theTestFim) == 0); |
| 1009 | Melder_assert (feof (theTestFilePointer) == 0); |
| 1010 | Melder_assert (FileInMemory_feof (theTestFim) == 0); |
| 1011 | |
| 1012 | /* |
| 1013 | Testing fread. |
| 1014 | */ |
| 1015 | MelderInfo_writeLine (U"Testing fread, ftell, feof..."); |
| 1016 | rewind (theTestFilePointer); |
| 1017 | FileInMemory_rewind (theTestFim); |
| 1018 | // |
| 1019 | integer size = 1, count = 14; |
| 1020 | size_t fnread = fread (fbuffer, (size_t) size, (size_t) count, theTestFilePointer); |
| 1021 | size_t fimnread = FileInMemory_fread (fimbuffer, (size_t) size, (size_t) count, theTestFim); |
| 1022 | MelderInfo_writeLine (U"\tRead ", fnread, U" and ", fimnread, U" elements of size ", size, U"."); |
| 1023 | integer expectedNumberOfElementsRead = std::min (count, numberOfBytes / size); |
| 1024 | integer expectedNumberOfBytesRead = size * expectedNumberOfElementsRead; |
| 1025 | Melder_require ((integer) fnread == expectedNumberOfElementsRead, |
| 1026 | U"Expected to read ", expectedNumberOfElementsRead, U" elements from file, not ", fnread, U"."); |
| 1027 | Melder_require ((integer) fimnread == expectedNumberOfElementsRead, |
| 1028 | U"Expected to read ", expectedNumberOfElementsRead, U" elements from file-in-memory, not ", fnread, U"."); |
| 1029 | Melder_require (ftell (theTestFilePointer) == expectedNumberOfBytesRead, |
| 1030 | U"Expected file position ", expectedNumberOfBytesRead, U", not ", ftell (theTestFilePointer), U"."); |
| 1031 | Melder_require (FileInMemory_ftell (theTestFim) == expectedNumberOfBytesRead, |
| 1032 | U"Expected file-in-memory position ", expectedNumberOfBytesRead, U", not ", FileInMemory_ftell (theTestFim), U"."); |
| 1033 | if (expectedNumberOfElementsRead < count) { |
| 1034 | Melder_require (feof (theTestFilePointer), |
| 1035 | U"fread should have its end-of-file indicator set (size ", size, U", count ", count, U")."); |
| 1036 | Melder_require (FileInMemory_feof (theTestFim), |
| 1037 | U"FileInMemory_fread should have its end-of-file indicator set (size ", size, U", count ", count, U")."); |
| 1038 | } else { |
| 1039 | Melder_require (! feof (theTestFilePointer), |
| 1040 | U"fread should not have its end-of-file indicator set (size ", size, U", count ", count, U")."); |
| 1041 | Melder_require (! FileInMemory_feof (theTestFim), |
| 1042 | U"FileInMemory_fread should not have its end-of-file indicator set (size ", size, U", count ", count, U")."); |
| 1043 | } |
| 1044 | // |
| 1045 | size = 1; |
| 1046 | count = 195; |
| 1047 | fnread = fread (fbuffer, (size_t) size, (size_t) count, theTestFilePointer); |
| 1048 | fimnread = FileInMemory_fread (fimbuffer, (size_t) size, (size_t) count, theTestFim); |
| 1049 | MelderInfo_writeLine (U"Read ", fnread, U" and ", fimnread, U" elements of size ", size, U"."); |
| 1050 | Melder_require (ftell (theTestFilePointer) == numberOfBytes, |
| 1051 | U"Expected file position ", expectedNumberOfBytesRead, U", not ", ftell (theTestFilePointer), U"."); |
| 1052 | Melder_require (FileInMemory_ftell (theTestFim) == numberOfBytes, |
| 1053 | U"Expected file-in-memory position ", expectedNumberOfBytesRead, U", not ", FileInMemory_ftell (theTestFim), U"."); |
| 1054 | Melder_require (feof (theTestFilePointer), |
| 1055 | U"fread should have its end-of-file indicator set after trying to read more characters than there are in the file."); |
| 1056 | Melder_require (FileInMemory_feof (theTestFim), |
| 1057 | U"FileInMemory_fread should have its end-of-file indicator set after trying to read more characters than there are in the file-in-memory."); |
| 1058 | |
| 1059 | rewind (theTestFilePointer); |
| 1060 | FileInMemory_rewind (theTestFim); |
| 1061 | |
| 1062 | fnread = fread (fbuffer, 1, (size_t) numberOfBytes, theTestFilePointer); |
| 1063 | fimnread = FileInMemory_fread (fimbuffer, 1, (size_t) numberOfBytes, theTestFim); |
| 1064 | MelderInfo_writeLine (U"Read ", fnread, U" and ", fimnread, U" bytes"); |
| 1065 | Melder_require (ftell (theTestFilePointer) == numberOfBytes, |
| 1066 | U"fread: at the end of the file we should be at position ", numberOfBytes, U", not ", FileInMemory_ftell (theTestFim)); |
| 1067 | Melder_require (FileInMemory_ftell (theTestFim) == numberOfBytes, |
| 1068 | U"FileInMemory_fread: at the end of the file-in-memory we should be at position ", numberOfBytes, U", not ", FileInMemory_ftell (theTestFim)); |
| 1069 | Melder_require (! feof (theTestFilePointer), |
| 1070 | U"fread should not have its end-of-file indicator set after reading precisely the content of the file."); |
| 1071 | Melder_require (! FileInMemory_feof (theTestFim), |
| 1072 | U"FileInMemory_fread should not have its end-of-file indicator set after reading precisely the content of the file-in-memory."); |
| 1073 | |
| 1074 | rewind (theTestFilePointer); |
| 1075 | FileInMemory_rewind (theTestFim); |
| 1076 | Melder_require (! FileInMemory_feof (theTestFim), |
| 1077 | U"FileInMemory_fread should not have its end-of-file indicator set just after rewinding."); |
| 1078 | |
| 1079 | size = 7; |
| 1080 | count = 2; |
| 1081 | fnread = fread (fbuffer, (size_t) size, (size_t) count, theTestFilePointer); |
| 1082 | fimnread = FileInMemory_fread (fimbuffer, (size_t) size, (size_t) count, theTestFim); |
| 1083 | MelderInfo_writeLine (U"Read ", fnread, U" and ", fimnread, U" elements of size ", size, U"."); |
| 1084 | expectedNumberOfElementsRead = std::min (count, numberOfBytes / size); |
| 1085 | //expectedNumberOfBytesRead = size * expectedNumberOfElementsRead; |
| 1086 | expectedNumberOfBytesRead = std::min (count * size, numberOfBytes); |
| 1087 | Melder_require ((integer) fnread == expectedNumberOfElementsRead, |
| 1088 | U"Expected to read ", expectedNumberOfElementsRead, U" elements from file, not ", fnread, U"."); |
| 1089 | Melder_require ((integer) fimnread == expectedNumberOfElementsRead, |
| 1090 | U"Expected to read ", expectedNumberOfElementsRead, U" elements from file-in-memory, not ", fnread, U"."); |
| 1091 | Melder_require (ftell (theTestFilePointer) == expectedNumberOfBytesRead, |
| 1092 | U"Expected file position ", expectedNumberOfBytesRead, U", not ", ftell (theTestFilePointer), U"."); |
| 1093 | Melder_require (FileInMemory_ftell (theTestFim) == expectedNumberOfBytesRead, |
| 1094 | U"Expected file-in-memory position ", expectedNumberOfBytesRead, U", not ", FileInMemory_ftell (theTestFim), U"."); |
| 1095 | if (expectedNumberOfElementsRead < count) { |
| 1096 | Melder_require (feof (theTestFilePointer), |
| 1097 | U"fread should have its end-of-file indicator set (size ", size, U", count ", count, U")."); |
| 1098 | Melder_require (FileInMemory_feof (theTestFim), |
| 1099 | U"FileInMemory_fread should have its end-of-file indicator set (size ", size, U", count ", count, U")."); |
| 1100 | } else { |
| 1101 | Melder_require (! feof (theTestFilePointer), |
| 1102 | U"fread should not have its end-of-file indicator set (size ", size, U", count ", count, U")."); |
| 1103 | Melder_require (! FileInMemory_feof (theTestFim), |
| 1104 | U"FileInMemory_fread should not have its end-of-file indicator set (size ", size, U", count ", count, U")."); |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | Testing fgetc. |
| 1109 | */ |
| 1110 | MelderInfo_writeLine (U"Testing fgetc..."); |
| 1111 | rewind (theTestFilePointer); |
| 1112 | FileInMemory_rewind (theTestFim); |
| 1113 | Melder_assert (FileInMemory_ftell (theTestFim) == 0); |
| 1114 | Melder_assert (FileInMemory_feof (theTestFim) == 0); |
| 1115 | for (integer i = 1; i <= numberOfBytes; i ++) { |
| 1116 | int fkar = fgetc (theTestFilePointer); |
| 1117 | Melder_require (fkar == content [i-1], |
| 1118 | U"fgetc: character #", i, U" should be ", content [i-1], U" but is ", fkar, U"."); |
| 1119 | Melder_require (! feof (theTestFilePointer), |
| 1120 | U"fgetc: we should not be past the end yet."); |
| 1121 | int fimkar = FileInMemory_fgetc (theTestFim); |
| 1122 | Melder_require (fimkar == content [i-1], |
| 1123 | U"FileInMemory_fgetc: character #", i, U" should be ", content [i-1], U" but is ", fimkar, U"."); |
| 1124 | Melder_require (! FileInMemory_feof (theTestFim), |
| 1125 | U"FileInMemory_fgetc: we should not be past the end yet."); |
| 1126 | } |
| 1127 | int fkar = fgetc (theTestFilePointer); |
| 1128 | Melder_require (fkar == EOF, |
| 1129 | U"fgetc: character should be EOF (-1) but is ", fkar, U"."); |
| 1130 | Melder_require (feof (theTestFilePointer), |
| 1131 | U"fgetc: we should be past the end now."); |
| 1132 | int fimkar = FileInMemory_fgetc (theTestFim); |
| 1133 | Melder_require (fimkar == EOF, |
| 1134 | U"FIM_fgetc: character should be EOF (-1) but is ", fimkar, U"."); |
| 1135 | Melder_require (FileInMemory_feof (theTestFim), |
| 1136 | U"FIM_fgetc: we should not be past the end yet."); |
| 1137 | |
| 1138 | /* |
| 1139 | Testing fseek. |
| 1140 | */ |
| 1141 | MelderInfo_writeLine (U"Testing fseek, ftell..."); |
| 1142 | // |
| 1143 | int fresult = fseek (theTestFilePointer, 0, SEEK_SET); |
| 1144 | long_not_integer fposition = ftell (theTestFilePointer); |
| 1145 | Melder_require (fresult == 0, |
| 1146 | U"fseek to position 0: result should be OK: the start of the file."); |
| 1147 | Melder_require (fposition == 0, |
| 1148 | U"fseek to position 0: position should be 0 (at the start of the file), not ", fposition, U"."); |
| 1149 | int fimresult = FileInMemory_fseek (theTestFim, 0, SEEK_SET); |
| 1150 | long_not_integer fimposition = FileInMemory_ftell (theTestFim); |
| 1151 | Melder_require (fimresult == 0, |
| 1152 | U"FIM_fseek to position 0: result should be OK: the start of the file."); |
| 1153 | Melder_require (fimposition == 0, |
| 1154 | U"FIM_fseek to position 0: position should be 0 (at the start of the file), not ", fimposition, U"."); |
| 1155 | // |
| 1156 | fresult = fseek (theTestFilePointer, numberOfBytes, SEEK_SET); |
| 1157 | fposition = ftell (theTestFilePointer); |
| 1158 | Melder_require (fresult == 0, |
| 1159 | U"fseek to the last position: result should be OK: the end of the file."); |
| 1160 | Melder_require (fposition == numberOfBytes, |
| 1161 | U"fseek to the last position: position should be ", numberOfBytes, U" (at the end of the file), not ", fposition, U"."); |
| 1162 | fimresult = FileInMemory_fseek (theTestFim, numberOfBytes, SEEK_SET); |
| 1163 | fimposition = FileInMemory_ftell (theTestFim); |
| 1164 | Melder_require (fimresult == 0, |
| 1165 | U"FIM_fseek to the last position: result should be OK: the end of the file."); |
| 1166 | Melder_require (fimposition == numberOfBytes, |
| 1167 | U"FIM_fseek to the last position: position should be ", numberOfBytes, U" (at the end of the file), not ", fimposition, U"."); |
| 1168 | // |
| 1169 | fresult = fseek (theTestFilePointer, 14, SEEK_SET); |
| 1170 | fposition = ftell (theTestFilePointer); |
| 1171 | if (numberOfBytes >= 14) { |
| 1172 | Melder_require (fresult == 0, |
| 1173 | U"fseek to position 14: result should be OK."); |
| 1174 | Melder_require (fposition == 14, |
| 1175 | U"fseek to position 14: position should be 14."); |
| 1176 | } else { |
| 1177 | Melder_require (fresult == 0, |
| 1178 | U"fseek to position 14: result should be OK, even after seeking past the end."); |
| 1179 | Melder_require (fposition == 14, |
| 1180 | U"fseek to position 14: position should be at 14, although that is past the end of the file."); |
| 1181 | } |
| 1182 | fimresult = FileInMemory_fseek (theTestFim, 14, SEEK_SET); |
| 1183 | fimposition = FileInMemory_ftell (theTestFim); |
| 1184 | if (numberOfBytes >= 14) { |
| 1185 | Melder_require (fimresult == 0, |
| 1186 | U"FileInMemory_fseek to position 14: result should be OK."); |
| 1187 | Melder_require (fimposition == 14, |
| 1188 | U"FileInMemory_fseek to position 14: position should be 14."); |
| 1189 | } else { |
| 1190 | Melder_require (fimresult == 0, |
| 1191 | U"FileInMemory_fseek to position 14: result should be OK, even after seeking past the end."); |
| 1192 | Melder_require (fimposition == 14, |
| 1193 | U"FileInMemory_fseek to position 14: position should be at 14, although that is past the end of the file."); |
| 1194 | } |
| 1195 | // |
| 1196 | fresult = fseek (theTestFilePointer, 1000, SEEK_SET); |
| 1197 | fposition = ftell (theTestFilePointer); |
| 1198 | Melder_require (fresult == 0, |
| 1199 | U"fseek to position 1000: result should be OK, even after seeking past the end."); |
| 1200 | Melder_require (fposition == 1000, |
| 1201 | U"fseek to position 1000: position should be at 1000, although that is past the end of the file."); |
| 1202 | fimresult = FileInMemory_fseek (theTestFim, 1000, SEEK_SET); |
| 1203 | fimposition = FileInMemory_ftell (theTestFim); |
| 1204 | Melder_require (fimresult == 0, |
| 1205 | U"FileInMemory_fseek to position 1000: result should be OK, even after seeking past the end."); |
| 1206 | Melder_require (fimposition == 1000, |
| 1207 | U"FileInMemory_fseek to position 1000: position should be at 1000, although that is past the end of the file."); |
| 1208 | /* |
| 1209 | With a negative position argument, the internal position should not change. |
| 1210 | */ |
| 1211 | fresult = fseek (theTestFilePointer, -1000, SEEK_SET); |
| 1212 | fposition = ftell (theTestFilePointer); |
| 1213 | Melder_require (fresult == -1, |
| 1214 | U"fseek to position -1000: result should be failure."); |
| 1215 | Melder_require (fposition == 1000, |
| 1216 | U"fseek to position -1000: position is at ", fposition, U" instead of still 1000."); |
| 1217 | Melder_require (! ferror (theTestFilePointer), |
| 1218 | U"fseek to position -1000 should not be an error."); |
| 1219 | fimresult = FileInMemory_fseek (theTestFim, -1000, SEEK_SET); |
| 1220 | fimposition = FileInMemory_ftell (theTestFim); |
| 1221 | Melder_require (fimresult == -1, |
| 1222 | U"FileInMemory_fseek to position -1000: result should be failure."); |
| 1223 | Melder_require (fimposition == 1000, |
| 1224 | U"FileInMemory_fseek to position -1000: position is at ", fimposition, U" instead of still 1000."); |
| 1225 | Melder_require (! FileInMemory_ferror (theTestFim), |
| 1226 | U"FileInMemory_fseek to position -1000 should not be an error."); |
| 1227 | // |
| 1228 | fresult = fseek (theTestFilePointer, -1, SEEK_SET); |
| 1229 | fposition = ftell (theTestFilePointer); |
| 1230 | Melder_require (fresult == -1, |
| 1231 | U"fseek to position -1: result should be failure."); |
| 1232 | Melder_require (fposition == 1000, |
| 1233 | U"fseek to position -1: position is at ", fposition, U" instead of still 1000."); |
| 1234 | Melder_require (! ferror (theTestFilePointer), |
| 1235 | U"fseek to position -1 should not be an error."); |
| 1236 | fimresult = FileInMemory_fseek (theTestFim, -1, SEEK_SET); |
| 1237 | fimposition = FileInMemory_ftell (theTestFim); |
| 1238 | Melder_require (fimresult == -1, |
| 1239 | U"fseek to position -1: result should be failure."); |
| 1240 | Melder_require (fimposition == 1000, |
| 1241 | U"fseek to position -1: position is at ", fimposition, U" instead of still 1000."); |
| 1242 | Melder_require (! FileInMemory_ferror (theTestFim), |
| 1243 | U"FileInMemory_fseek to position -1 should not be an error."); |
| 1244 | |
| 1245 | MelderInfo_writeLine (U"File \"", fileName, U"\" was handled correctly\n"); |
| 1246 | } catch (MelderError) { |
| 1247 | Melder_throw (U"Test on file \"", fileName, U"\" failed."); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | void test_FileInMemory_io () { |
| 1252 | try { |
| 1253 | /* |
| 1254 | Clean up the rubbish left over from any previous failed call. |
| 1255 | */ |
| 1256 | if (theTestFilePointer) { |
| 1257 | Melder_fclose (& theTestFile, theTestFilePointer); // last chance |
| 1258 | theTestFilePointer = nullptr; |
| 1259 | } |
| 1260 | if (theTestFim) { |
| 1261 | FileInMemory_fclose (theTestFim); // will dangle |
| 1262 | theTestFim = nullptr; |
| 1263 | } |
| 1264 | |
| 1265 | theTestFileInMemorySet = FileInMemorySet_createFromDirectoryContents (theTestSubfolder, U"*"); |
| 1266 | |
| 1267 | {// scope |
| 1268 | const char content [200] = "abcd\n" "ef\n" "ghijk\n"; |
| 1269 | const integer length = 14 ; |
| 1270 | const char lineContents [5][40] = { "abcd\n", "ef\n", "ghijk\n" }; |
| 1271 | const integer lineLengths [5] = { 5 , 3 , 6 }; |
| 1272 | testOneFile (U"threeLinesWithFinalNewline.txt", content, length, lineContents, lineLengths, true); |
| 1273 | } |
| 1274 | { |
| 1275 | const char content [200] = "lmno\n" "\n" "pqrstuvwxyz"; |
| 1276 | const integer length = 17 ; |
| 1277 | const char lineContents [5][40] = { "lmno\n", "\n", "pqrstuvwxyz" }; |
| 1278 | const integer lineLengths [5] = { 5 , 1 , 11 }; |
| 1279 | testOneFile (U"threeLinesWithoutFinalNewline.txt", content, length, lineContents, lineLengths, false); |
| 1280 | } |
| 1281 | { |
| 1282 | const char content [200] = "lmn\0\n" "\n" "pqrst\0vwx\0z"; |
| 1283 | const integer length = 17 ; |
| 1284 | const char lineContents [5][40] = { "lmn\0\n", "\n", "pqrst\0vwx\0z" }; |
| 1285 | const integer lineLengths [5] = { 5 , 1 , 11 }; |
| 1286 | testOneFile (U"threeLinesWithThreeNulls.txt", content, length, lineContents, lineLengths, false); |
| 1287 | } |
| 1288 | { |
| 1289 | const char content [200] = "\n" "\n" "\n"; |
| 1290 | const integer length = 3 ; |
| 1291 | const char lineContents [5][40] = { "\n", "\n", "\n" }; |
| 1292 | const integer lineLengths [5] = { 1 , 1 , 1 }; |
| 1293 | testOneFile (U"threeEmptyLines.txt", content, length, lineContents, lineLengths, true); |
| 1294 | } |
| 1295 | { |
| 1296 | const char content [200] = "abcdefghijk\n"; |
| 1297 | const integer length = 12 ; |
| 1298 | const char lineContents [5][40] = { "abcdefghijk\n" }; |
| 1299 | const integer lineLengths [5] = { 12 }; |
| 1300 | testOneFile (U"oneLineWithFinalNewline.txt", content, length, lineContents, lineLengths, true); |
| 1301 | } |
| 1302 | { |
| 1303 | const char content [200] = "abcdefghijk"; |
| 1304 | const integer length = 11 ; |
| 1305 | const char lineContents [5][40] = { "abcdefghijk" }; |
| 1306 | const integer lineLengths [5] = { 11 }; |
| 1307 | testOneFile (U"oneLineWithoutFinalNewline.txt", content, length, lineContents, lineLengths, false); |
| 1308 | } |
| 1309 | { |
| 1310 | const char content [200] = "\n"; |
| 1311 | const integer length = 1 ; |
| 1312 | const char lineContents [5][40] = { "\n" }; |
| 1313 | const integer lineLengths [5] = { 1 }; |
| 1314 | testOneFile (U"oneEmptyLine.txt", content, length, lineContents, lineLengths, true); |
| 1315 | } |
| 1316 | { |
| 1317 | /* |
| 1318 | We should be able to handle an empty file (remember CrowdStrike in July 2024) |
| 1319 | */ |
| 1320 | const char content [200] = ""; |
| 1321 | const integer length = 0; |
| 1322 | const char lineContents [5][40] = { "" }; |
| 1323 | const integer lineLengths [5] = { 0 }; |
| 1324 | testOneFile (U"emptyFile.txt", content, length, lineContents, lineLengths, false); |
| 1325 | } |
| 1326 | |
| 1327 | /* |
| 1328 | Clean up. |
| 1329 | */ |
| 1330 | if (theTestFilePointer) { |
| 1331 | Melder_fclose (& theTestFile, theTestFilePointer); |
| 1332 | theTestFilePointer = nullptr; |
| 1333 | } |
| 1334 | if (theTestFim) { |
| 1335 | FileInMemory_fclose (theTestFim); |
| 1336 | theTestFim = nullptr; |
| 1337 | } |
| 1338 | MelderInfo_writeLine (U"test_FileInMemory_io: OK"); |
| 1339 | } catch (MelderError) { |
| 1340 | Melder_throw (U"FileInMemory test failed."); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | |
| 1345 | /* End of file FileInMemory.cpp */ |