github.com/256dpi/max-go@v0.7.0/lib/max/ext_sysfile.h (about) 1 #ifndef _EXT_SYSFILE_H_ 2 #define _EXT_SYSFILE_H_ 3 4 #include "ext_prefix.h" 5 #include "ext_mess.h" 6 7 typedef struct _filestruct t_filestruct; 8 9 /** A t_filehandle is a cross-platform way of referring to an open file. 10 It is an opaque structure, meaning you don’t have access to the individual 11 elements of the data structure. You can use a t_filehandle only 12 with the file routines in the Sysfile API. Do not use other platform- 13 specific file functions in conjunction with these functions. 14 The perm parameter can be either READ_PERM, WRITE_PERM, or RW_PERM. 15 16 @ingroup files */ 17 typedef t_filestruct *t_filehandle; 18 19 /** Modes used by sysfile_setpos() 20 @ingroup files */ 21 typedef enum { 22 SYSFILE_ATMARK = 0, ///< ? 23 SYSFILE_FROMSTART = 1, ///< Calculate the file position from the start of the file. 24 SYSFILE_FROMLEOF = 2, ///< Calculate the file position from the logical end of the file. 25 SYSFILE_FROMMARK = 3 ///< Calculate the file position from the current file position. 26 } t_sysfile_pos_mode; 27 28 29 typedef enum { 30 SYSFILE_SUBFILE = 1, 31 SYSFILE_HANDLE = 2, // can call sysmem_freehandle on close 32 SYSFILE_RESOURCE = 4, // can call ReleaseResource on close 33 SYSFILE_MEMORY = 6, // either a handle or a resource 34 SYSFILE_RESFILE = 8, // a resource file 35 SYSFILE_OPENRESFILE = 16, // an already-open resource file 36 SYSFILE_EXTERNALDATA = 32, // don't do anything to f_data 37 SYSFILE_JUSTAPOINTER = 64, // f_data is really a 4-byte pointer 38 SYSFILE_EXTERNALDATA_CANWRITE = 128, // can sysfile_write() to external data 39 SYSFILE_EXTERNALDATA_CANGROW = 256, // if write goes beyond data, grow the handle (not valid with SYSFILE_JUSTAPOINTER) 40 SYSFILE_EXTERNALDATA_FREE = 512, // data is external, but free it anyway on file close 41 SYSFILE_EXTERNALDATA_LATEFREE = 1024 42 } t_sysfile_flags; 43 44 /** Flags used reading and writing text files. 45 @ingroup files */ 46 typedef enum { 47 TEXT_LB_NATIVE = 0x00000001L, ///< Use the linebreak format native to the current platform. 48 TEXT_LB_MAC = 0x00000002L, ///< Use Macintosh line breaks 49 TEXT_LB_PC = 0x00000004L, ///< Use Windows line breaks 50 TEXT_LB_UNIX = 0x00000008L, ///< Use Unix line breaks 51 TEXT_LB_MASK = 0x0000000FL, // can use to mask out non-LB related flags 52 TEXT_ENCODING_USE_FILE = 0x00000100L, ///< If this flag is not set then the encoding is forced to UTF8 53 TEXT_NULL_TERMINATE = 0x00000200L ///< Terminate memory returned from sysfile_readtextfile() with a NULL character 54 } t_sysfile_text_flags; 55 56 BEGIN_USING_C_LINKAGE 57 58 /** Close a file opened with sysfile_open(). 59 This function is similar to FSClose() or fclose(). 60 It should be used instead of system-specific file closing routines in order to make max external 61 code that will compile cross-platform. 62 63 @ingroup files 64 @param f The #t_filehandle structure of the file the user wants to close. 65 @return An error code. 66 */ 67 t_max_err sysfile_close(t_filehandle f); 68 69 70 /** Read a file from disk. 71 This function is similar to FSRead() or fread(). It should be used instead of 72 these functions (or other system-specific file reading routines) in order 73 to make max external code that will compile cross-platform. It reads 74 "count" bytes from file handle at current file position into "bufptr". 75 The byte count actually read is set in "count", and the file position is 76 updated by the actual byte count read. 77 78 @ingroup files 79 @param f The #t_filehandle structure of the file the user wants to open. 80 @param count Pointer to the number of bytes that will be read from the file at the current file position. 81 The byte count actually read is returned to this value. 82 @param bufptr Pointer to the buffer that the data will be read into. 83 @return An error code. 84 */ 85 t_max_err sysfile_read( t_filehandle f, t_ptr_size *count, void *bufptr); 86 87 88 /** Read the contents of a file into a handle. 89 @ingroup files 90 @param f The open #t_filehandle structure to read into the handle. 91 @param h The address of a handle into which the file will be read. 92 @return An error code. 93 @remark You should free the pointer, when you are done with it, using sysmem_freehandle(). 94 */ 95 t_max_err sysfile_readtohandle(t_filehandle f, char ***h); 96 97 98 /** Read the contents of a file into a pointer. 99 @ingroup files 100 @param f The open #t_filehandle structure to read into the handle. 101 @param p The address of a pointer into which the file will be read. 102 @return An error code. 103 @remark You should free the pointer, when you are done with it, using sysmem_freeptr(). 104 */ 105 t_max_err sysfile_readtoptr(t_filehandle f, char **p); 106 107 108 /** Write part of a file to disk. 109 This function is similar to FSWrite() or fwrite(). It should be used instead 110 of these functions (or other system-specific file reading routines) in 111 order to make max external code that will compile cross-platform. The 112 function writes "count" bytes from "bufptr" into file handle at current 113 file position. The byte count actually written is set in "count", and the 114 file position is updated by the actual byte count written. 115 116 @ingroup files 117 @param f The t_filehandle structure of the file to which the user wants to write. 118 @param count Pointer to the number of bytes that will be written to the file at the current file position. 119 The byte count actually written is returned to this value. 120 @param bufptr Pointer to the buffer that the data will be read from. 121 @return An error code. 122 */ 123 t_max_err sysfile_write(t_filehandle f, t_ptr_size *count, const void *bufptr); 124 125 126 /** Set the size of a file handle. 127 This function is similar to and should be used instead of SetEOF(). 128 The function sets the size of file handle in bytes, specified by �“logeof?. 129 130 @ingroup files 131 @param f The file's #t_filehandle structure. 132 @param logeof The file size in bytes. 133 @return An error code. 134 */ 135 t_max_err sysfile_seteof(t_filehandle f, t_ptr_size logeof); 136 137 138 /** Get the size of a file handle. 139 This function is similar to and should be used instead of GetEOF(). 140 The function gets the size of file handle in bytes, and places it in �“logeof?. 141 142 @ingroup files 143 @param f The file's #t_filehandle structure. 144 @param logeof The file size in bytes is returned to this value. 145 @return An error code. 146 */ 147 t_max_err sysfile_geteof(t_filehandle f, t_ptr_size *logeof); 148 149 150 /** Set the current file position of a file handle. 151 This function is similar to and should be used instead of SetFPos(). 152 It is used to set the current file position of file handle to by the given 153 number of offset bytes relative to the mode used, as defined in #t_sysfile_pos_mode. 154 155 @ingroup files 156 @param f The file's #t_filehandle structure. 157 @param mode Mode from which the offset will be calculated, as defined in #t_sysfile_pos_mode. 158 @param offset The offset in bytes relative to the mode. 159 @return An error code. 160 */ 161 t_max_err sysfile_setpos(t_filehandle f, t_sysfile_pos_mode mode, t_ptr_int offset); 162 163 164 /** Get the current file position of a file handle. 165 This function is similar to and should be used instead of GetFPos(). 166 The function gets the current file position of file handle in bytes, and places it in "filepos". 167 168 @ingroup files 169 @param f The file's #t_filehandle structure. 170 @param filepos The address of a variable to hold the current file position of file handle in bytes. 171 @return An error code. 172 */ 173 t_max_err sysfile_getpos(t_filehandle f, t_ptr_size *filepos); 174 175 176 /** Copy the contents of one file handle to another file handle. 177 @ingroup files 178 @param src The file handle from which to copy. 179 @param dst The file handle to which the copy is written. 180 @param size The number of bytes to copy. If 0 the size of src will be used. 181 @return An error code. 182 */ 183 t_max_err sysfile_spoolcopy(t_filehandle src, t_filehandle dst, t_ptr_size size); 184 185 186 // private 187 void sysfile_setobject(t_filehandle f, t_object *o); 188 189 190 191 /** Read a text file from disk. 192 This function reads up to the maximum number of bytes given by 193 maxlen from file handle at current file position into the htext 194 handle, performing linebreak translation if set in flags. 195 196 @ingroup files 197 @param f The #t_filehandle structure of the text file the user wants to open. 198 @param htext Handle that the data will be read into. 199 @param maxlen The maximum length in bytes to be read into the handle. 200 Passing the value 0L indicates no maximum (i.e. read the entire file). 201 @param flags Flags to set linebreak translation as defined in #t_sysfile_text_flags. 202 @return An error code. 203 */ 204 t_max_err sysfile_readtextfile(t_filehandle f, t_handle htext, t_ptr_size maxlen, t_sysfile_text_flags flags); 205 206 207 /** Write a text file to disk. 208 This function writes a text handle to a text file performing linebreak 209 translation if set in flags. 210 211 @ingroup files 212 @param f The #t_filehandle structure of the text file to which the user wants to write. 213 @param htext Handle that the data that will be read from. 214 @param flags Flags to set linebreak translation as defined in #t_sysfile_text_flags. 215 @return An error code. 216 */ 217 t_max_err sysfile_writetextfile(t_filehandle f, t_handle htext, t_sysfile_text_flags flags); 218 219 220 /** Create a #t_filehandle from a pre-existing handle. 221 @ingroup files 222 @param h A handle for some data, data is *not* copied and *not* freed on file close. 223 @param flags Pass 0 (additional flags are private). 224 @param fh The address of a #t_filehandle which will be allocated. 225 @return An error code. 226 */ 227 t_max_err sysfile_openhandle(char **h, t_sysfile_flags flags, t_filehandle *fh); 228 229 230 /** Create a #t_filehandle from a pre-existing pointer. 231 @ingroup files 232 @param p A pointer to some data. Data is *not* copied and *not* freed on file close. 233 @param length The size of p. 234 @param flags Pass 0 (additional flags are private). 235 @param fh The address of a #t_filehandle which will be allocated. 236 @return An error code. 237 */ 238 t_max_err sysfile_openptrsize(char *p, t_ptr_size length, t_sysfile_flags flags, t_filehandle *fh); 239 240 END_USING_C_LINKAGE 241 242 #endif // _EXT_SYSFILE_H_ 243