| [aad342c] | 1 | #ifndef _CIVL_IO_
|
|---|
| 2 | #define _CIVL_IO_
|
|---|
| 3 | #include<civlc.cvh>
|
|---|
| 4 | #pragma CIVL ACSL
|
|---|
| 5 | /* ******************************* Types ******************************* */
|
|---|
| 6 |
|
|---|
| 7 | /* There are 16 modes, and they must appear in this order to be
|
|---|
| 8 | * consistent with the system function Java code. */
|
|---|
| 9 | typedef enum _CIVL_File_mode {
|
|---|
| 10 | CIVL_FILE_MODE_R, // "r", open text file for reading
|
|---|
| 11 | CIVL_FILE_MODE_W, // "w", truncate to zero length or create text file for writing
|
|---|
| 12 | CIVL_FILE_MODE_WX, // "wx", create text file for writing
|
|---|
| 13 | CIVL_FILE_MODE_A, // append; open or create text file for writing at end-of-file
|
|---|
| 14 | CIVL_FILE_MODE_RB, // open binary file for reading
|
|---|
| 15 | CIVL_FILE_MODE_WB, // truncate to zero length or create binary file for writing
|
|---|
| 16 | CIVL_FILE_MODE_WBX, // create binary file for writing
|
|---|
| 17 | CIVL_FILE_MODE_AB, // append; open or create binary file for writing at end-of-file
|
|---|
| 18 | CIVL_FILE_MODE_RP, // open text file for update (reading and writing)
|
|---|
| 19 | CIVL_FILE_MODE_WP, // truncate to zero length or create text file for update
|
|---|
| 20 | CIVL_FILE_MODE_WPX, // create text file for update
|
|---|
| 21 | CIVL_FILE_MODE_AP, // append; open or create text file for update, writing at end-of-file
|
|---|
| 22 | CIVL_FILE_MODE_RPB, // open binary file for update (reading and writing)
|
|---|
| 23 | CIVL_FILE_MODE_WPB, // truncate to zero length or create binary file for update
|
|---|
| 24 | CIVL_FILE_MODE_WPBX, // create binary file for update
|
|---|
| 25 | CIVL_FILE_MODE_APB // append; open or create binary file for update, writing at end-of-file
|
|---|
| 26 | } CIVL_File_mode;
|
|---|
| 27 |
|
|---|
| 28 | typedef struct FILE FILE;
|
|---|
| 29 |
|
|---|
| 30 | /* Represents an actual file: something with a name and contents.
|
|---|
| 31 | * The name is a string (array of char). The contents is an
|
|---|
| 32 | * array of strings: each entry is a "chunk" of the file; the
|
|---|
| 33 | * file may be viewed as a concatenation of those chunks.
|
|---|
| 34 | *
|
|---|
| 35 | * A "point" in the file is represented by an ordered pair
|
|---|
| 36 | * of integers (chunkIndex, characterIndex).
|
|---|
| 37 | *
|
|---|
| 38 | * The flags all have int type instead of boolean because
|
|---|
| 39 | * CVC3 does not support tuples with boolean components.
|
|---|
| 40 | * Zero is used for false, one for true.
|
|---|
| 41 | * A file is an output file if it is ever written to.
|
|---|
| 42 | * A file is an input file if it is ever read before being written to.
|
|---|
| 43 | * A file can be both an input and an output file.
|
|---|
| 44 | * A file is either a binary or text file. The kind is determined
|
|---|
| 45 | * by how the file is opened.
|
|---|
| 46 | * A file is either an ordinary character file or a wide character
|
|---|
| 47 | * file. The orientation is determined by the first access to the
|
|---|
| 48 | * file.
|
|---|
| 49 | */
|
|---|
| 50 | typedef struct $file $file;
|
|---|
| 51 |
|
|---|
| 52 | /* A file system: a set of files. The files in the array can be
|
|---|
| 53 | * sorted in some canonical way. $filesystem is a handle type. */
|
|---|
| 54 | typedef struct CIVL_filesystem *$filesystem;
|
|---|
| 55 |
|
|---|
| 56 | /* Functions */
|
|---|
| 57 |
|
|---|
| 58 | /* Converts a mode string to the enumerated type CIVL_File_mode.
|
|---|
| 59 | * An exception is thrown if the mode string is not one of those
|
|---|
| 60 | * specified in the C11 Standard. */
|
|---|
| 61 | /*@ depends_on \access(modeString);
|
|---|
| 62 | @ executes_when \true;
|
|---|
| 63 | @*/
|
|---|
| 64 | $system[stdio] CIVL_File_mode CIVL_File_stringToMode(char * modeString);//mode not string-literal
|
|---|
| 65 |
|
|---|
| 66 | /* Creates a new empty file system, returning a handle to it. */
|
|---|
| 67 | /*@ depends_on \nothing;
|
|---|
| 68 | @ executes_when \true;
|
|---|
| 69 | @*/
|
|---|
| 70 | $atomic_f $filesystem $filesystem_create($scope scope);
|
|---|
| 71 |
|
|---|
| 72 | /* Deallocates any data associated to the file system, and makes
|
|---|
| 73 | * the handle undefined */
|
|---|
| 74 | /*@ depends_on \access(fs);
|
|---|
| 75 | @ executes_when \true;
|
|---|
| 76 | @*/
|
|---|
| 77 | $system[stdio] void $filesystem_destroy($filesystem fs);
|
|---|
| 78 |
|
|---|
| 79 | /* Opens a file f, creates a new FILE F pointing into f, and returns
|
|---|
| 80 | * a pointer to F. Will create f if f does not already exist or
|
|---|
| 81 | * if mode is write.
|
|---|
| 82 | *
|
|---|
| 83 | * F is allocated in the heap specified by the file system.
|
|---|
| 84 | * It will be freed by a close operation on F, or when the
|
|---|
| 85 | * scope is exited.
|
|---|
| 86 | *
|
|---|
| 87 | * See C11 Sec. 7.21.5.3. The modes currently supported
|
|---|
| 88 | * are:
|
|---|
| 89 | * "r": open text file for reading; fails if file does
|
|---|
| 90 | * not exist //if fails return NULL
|
|---|
| 91 | * "w": truncate to 0 length if file exists and
|
|---|
| 92 | * open for writing, or create new empty text file
|
|---|
| 93 | * for writing if file does not yet exist
|
|---|
| 94 | * "wx": create text file for writing with exclusive access;
|
|---|
| 95 | * fails if file exists,
|
|---|
| 96 | * "a": open or create text file in append mode; all writes
|
|---|
| 97 | * will append to end of file regardless of calls
|
|---|
| 98 | * to fseek
|
|---|
| 99 | * "r+": open/create text file for reading and writing
|
|---|
| 100 | * access to file must follow rules specified
|
|---|
| 101 | * in Standard par. 7
|
|---|
| 102 | *
|
|---|
| 103 | */
|
|---|
| 104 | /*@ depends_on \access(fs, filename);
|
|---|
| 105 | @ executes_when \true;
|
|---|
| 106 | @*/
|
|---|
| 107 | $system[stdio] FILE * $fopen($filesystem fs, const char * restrict filename,
|
|---|
| 108 | CIVL_File_mode mode);
|
|---|
| 109 |
|
|---|
| 110 | /*@ depends_on \access(file);
|
|---|
| 111 | @ executes_when \true;
|
|---|
| 112 | @*/
|
|---|
| 113 | $system[stdio] int $civl_text_file_length($filesystem fs, char* file);
|
|---|
| 114 |
|
|---|
| 115 | int $text_file_length(char* file);
|
|---|
| 116 |
|
|---|
| 117 | /* This assumes there is some output variable of type
|
|---|
| 118 | * array-of-$file, and copies the output files into that
|
|---|
| 119 | * output array. The parameter output should be a pointer
|
|---|
| 120 | * to element 0 of that array.
|
|---|
| 121 | */
|
|---|
| 122 | /*@ depends_on \access(fs, output);
|
|---|
| 123 | @ executes_when \true;
|
|---|
| 124 | @*/
|
|---|
| 125 | $system[stdio] void $filesystem_copy_output($filesystem fs, $file * output);
|
|---|
| 126 |
|
|---|
| 127 | #endif
|
|---|