libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmFile_h
  4. #define cmFile_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. //( { file_desc: "File abstraction class." kw:[file system base]}
  9. //
  10. // The cmFile API extends the C standard file handling routines
  11. // with cm style error handling. All cm file input and output occurs
  12. // through this interface."
  13. //
  14. enum
  15. {
  16. kOkFileRC = cmOkRC,
  17. kInvalidFlagFileRC,
  18. kOpenFailFileRC,
  19. kCloseFailFileRC,
  20. kReadFailFileRC,
  21. kWriteFailFileRC,
  22. kSeekFailFileRC,
  23. kTellFailFileRC,
  24. kPrintFailFileRC,
  25. kObjAllocFailFileRC,
  26. kHandleInvalidFileRC,
  27. kStatFailFileRC,
  28. kBufAllocFailFileRC,
  29. kBufTooSmallFileRC,
  30. kFileSysFailFileRC,
  31. };
  32. typedef unsigned cmFileRC_t;
  33. typedef cmHandle_t cmFileH_t;
  34. extern cmFileH_t cmFileNullHandle;
  35. // Flags for use with cmFileOpen().
  36. enum cmFileOpenFlags_t
  37. {
  38. kReadFileFl = 0x01, //< Open a file for reading
  39. kWriteFileFl = 0x02, //< Create an empty file for writing
  40. kAppendFileFl = 0x04, //< Open a file for writing at the end of the file.
  41. kUpdateFileFl = 0x08, //< Open a file for reading and writing.
  42. kBinaryFileFl = 0x10, //< Open a file for binary (not text) input/output.
  43. kStdoutFileFl = 0x20, //< Ignore fn use 'stdout'
  44. kStderrFileFl = 0x40, //< Ignore fn use 'stderr'
  45. kStdinFileFl = 0x80, //< Ignore fn use 'stdin'
  46. };
  47. // Open or create a file.
  48. // Equivalent to fopen().
  49. // If *hp was not initalized by an earlier call to cmFileOpen() then it should
  50. // be set to cmFileNullHandle prior to calling this function. If *hp is a valid handle
  51. // then it is automatically finalized by an internal call to cmFileClose() prior to
  52. // being re-iniitalized.
  53. //
  54. // If kStdoutFileFl, kStderrFileFl or kStdinFileFl are set then
  55. // file name argument 'fn' is ignored.
  56. cmFileRC_t cmFileOpen(
  57. cmFileH_t* hp, // Pointer to a client supplied cmFileHandle_t to recieve the handle for the new object.
  58. const cmChar_t* fn, // The name of the file to open or create.
  59. enum cmFileOpenFlags_t flags, // See cmFileOpenFlags_t
  60. cmRpt_t* rpt // The cmRpt_t to use for error reporting
  61. );
  62. // Close a file opened with Equivalent to fclose().
  63. cmFileRC_t cmFileClose( cmFileH_t* hp );
  64. // Return true if the file handle is associated with an open file.
  65. bool cmFileIsValid( cmFileH_t h );
  66. // Read a block bytes from a file. Equivalent to fread().
  67. cmFileRC_t cmFileRead( cmFileH_t h, void* buf, unsigned bufByteCnt );
  68. // Write a block of bytes to a file. Equivalent to fwrite().
  69. cmFileRC_t cmFileWrite( cmFileH_t h, const void* buf, unsigned bufByteCnt );
  70. enum cmFileSeekFlags_t
  71. {
  72. kBeginFileFl = 0x01,
  73. kCurFileFl = 0x02,
  74. kEndFileFl = 0x04
  75. };
  76. // Set the file position indicator. Equivalent to fseek().
  77. cmFileRC_t cmFileSeek( cmFileH_t h, enum cmFileSeekFlags_t flags, int offsByteCnt );
  78. // Return the file position indicator. Equivalent to ftell().
  79. cmFileRC_t cmFileTell( cmFileH_t h, long* offsPtr );
  80. // Return true if the file position indicator is at the end of the file.
  81. // Equivalent to feof().
  82. bool cmFileEof( cmFileH_t h );
  83. // Return the length of the file in bytes
  84. unsigned cmFileByteCount( cmFileH_t h );
  85. cmFileRC_t cmFileByteCountFn( const cmChar_t* fn, cmRpt_t* rpt, unsigned* fileByteCntPtr );
  86. // Set *isEqualPtr=true if the two files are identical.
  87. cmFileRC_t cmFileCompare( const cmChar_t* fn0, const cmChar_t* fn1, cmRpt_t* rpt, bool* isEqualFlPtr );
  88. // Return the file name associated with a file handle.
  89. const cmChar_t* cmFileName( cmFileH_t h );
  90. // Write a buffer to a file.
  91. cmFileRC_t cmFileFnWrite( const cmChar_t* fn, cmRpt_t* rpt, const void* buf, unsigned bufByteCnt );
  92. // Allocate and fill a buffer from the file.
  93. // Set *bufByteCntPtr to count of bytes read into the buffer.
  94. // 'bufByteCntPtr' is optional - set it to NULL if it is not required by the caller.
  95. // It is the callers responsibility to delete the returned buffer with a
  96. // call to cmMemFree()
  97. cmChar_t* cmFileToBuf( cmFileH_t h, unsigned* bufByteCntPtr );
  98. // Same as cmFileToBuf() but accepts a file name argument.
  99. // 'rpt' is the report object to use for error reporting.
  100. cmChar_t* cmFileFnToBuf( const cmChar_t* fn, cmRpt_t* rpt, unsigned* bufByteCntPtr );
  101. // Copy the file named in srcDir/srcFn/srcExt to a file named dstDir/dstFn/dstExt.
  102. // Note that srcExt/dstExt may be set to NULL if the file extension is included
  103. // in srcFn/dstFn. Likewise srcFn/dstFn may be set to NULL if the file name
  104. // is included in srcDir/dstDir.
  105. cmFileRC_t cmFileCopy(
  106. const cmChar_t* srcDir,
  107. const cmChar_t* srcFn,
  108. const cmChar_t* srcExt,
  109. const cmChar_t* dstDir,
  110. const cmChar_t* dstFn,
  111. const cmChar_t* dstExt,
  112. cmErr_t* err);
  113. // This function creates a backup copy of the file 'fn' by duplicating it into
  114. // a file named fn_#.ext where # is an integer which makes the file name unique.
  115. // The integers chosen with zero and are incremented until an
  116. // unused file name is found in the same directory as 'fn'.
  117. // If the file identified by 'fn' is not found then the function returns quietly.
  118. cmFileRC_t cmFileBackup( const cmChar_t* dir, const cmChar_t* name, const cmChar_t* ext, cmErr_t* err );
  119. // Allocate and fill a zero terminated string from a file.
  120. // Set *bufByteCntPtr to count of bytes read into the buffer.=
  121. // (the buffer memory size is one byte larger to account for the terminating zero)
  122. // 'bufByteCntPtr' is optional - set it to NULL if it is not required by the caller.
  123. // It is the callers responsibility to delete the returned buffer with a
  124. // call to cmMemFree()
  125. cmChar_t* cmFileToStr( cmFileH_t h, unsigned* bufByteCntPtr );
  126. // Same as cmFileToBuf() but accepts a file name argument.
  127. // 'rpt' is the report object to use for error reporting.
  128. cmChar_t* cmFileFnToStr( const cmChar_t* fn, cmRpt_t* rpt, unsigned* bufByteCntPtr );
  129. // Return the count of lines in a file.
  130. cmFileRC_t cmFileLineCount( cmFileH_t h, unsigned* lineCntPtr );
  131. // Read the next line into buf[bufByteCnt].
  132. // Consider using cmFileGetLineAuto() as an alternative to this function
  133. // to avoid having to use a buffer with an explicit size.
  134. //
  135. // If buf is not long enough to hold the entire string then
  136. //
  137. // 1. The function returns kFileBufTooSmallRC
  138. // 2. *bufByteCntPtr is set to the size of the required buffer.
  139. // 3. The internal file position is left unchanged.
  140. //
  141. // If the buffer is long enough to hold the entire line then
  142. // *bufByteCntPtr is left unchanged.
  143. // See cmFileGetLineTest() in cmProcTest.c or cmFileGetLineAuto()
  144. // in cmFile.c for examples of how to use this function to a
  145. // achieve proper buffer sizing.
  146. cmFileRC_t cmFileGetLine( cmFileH_t h, cmChar_t* buf, unsigned* bufByteCntPtr );
  147. // A version of cmFileGetLine() which eliminates the need to handle buffer
  148. // sizing.
  149. //
  150. // Example usage:
  151. //
  152. // cmChar_t* buf = NULL;
  153. // unsigned bufByteCnt = 0;
  154. // while(cmFileGetLineAuto(h,&buf,&bufByteCnt)==kOkFileRC)
  155. // proc(buf);
  156. // cmMemPtrFree(buf);
  157. //
  158. // On the first call to this function *bufPtrPtr must be set to NULL and
  159. // *bufByteCntPtr must be set to 0.
  160. // Following the last call to this function call cmMemPtrFree(bufPtrptr)
  161. // to be sure the line buffer is fully released. Note this step is not
  162. // neccessary if the last call does not return kOkFileRC.
  163. cmFileRC_t cmFileGetLineAuto( cmFileH_t h, cmChar_t** bufPtrPtr, unsigned* bufByteCntPtr );
  164. // Binary Array Reading Functions
  165. // Each of these functions reads a block of binary data from a file.
  166. // The advantage to using these functions over cmFileRead() is only that they are type specific.
  167. cmFileRC_t cmFileReadChar( cmFileH_t h, char* buf, unsigned cnt );
  168. cmFileRC_t cmFileReadUChar( cmFileH_t h, unsigned char* buf, unsigned cnt );
  169. cmFileRC_t cmFileReadShort( cmFileH_t h, short* buf, unsigned cnt );
  170. cmFileRC_t cmFileReadUShort( cmFileH_t h, unsigned short* buf, unsigned cnt );
  171. cmFileRC_t cmFileReadLong( cmFileH_t h, long* buf, unsigned cnt );
  172. cmFileRC_t cmFileReadULong( cmFileH_t h, unsigned long* buf, unsigned cnt );
  173. cmFileRC_t cmFileReadInt( cmFileH_t h, int* buf, unsigned cnt );
  174. cmFileRC_t cmFileReadUInt( cmFileH_t h, unsigned int* buf, unsigned cnt );
  175. cmFileRC_t cmFileReadFloat( cmFileH_t h, float* buf, unsigned cnt );
  176. cmFileRC_t cmFileReadDouble( cmFileH_t h, double* buf, unsigned cnt );
  177. cmFileRC_t cmFileReadBool( cmFileH_t h, bool* buf, unsigned cnt );
  178. // Binary Array Writing Functions
  179. // Each of these functions writes an array to a binary file.
  180. // The advantage to using functions rather than cmFileWrite() is only that they are type specific.
  181. cmFileRC_t cmFileWriteChar( cmFileH_t h, const char* buf, unsigned cnt );
  182. cmFileRC_t cmFileWriteUChar( cmFileH_t h, const unsigned char* buf, unsigned cnt );
  183. cmFileRC_t cmFileWriteShort( cmFileH_t h, const short* buf, unsigned cnt );
  184. cmFileRC_t cmFileWriteUShort( cmFileH_t h, const unsigned short* buf, unsigned cnt );
  185. cmFileRC_t cmFileWriteLong( cmFileH_t h, const long* buf, unsigned cnt );
  186. cmFileRC_t cmFileWriteULong( cmFileH_t h, const unsigned long* buf, unsigned cnt );
  187. cmFileRC_t cmFileWriteInt( cmFileH_t h, const int* buf, unsigned cnt );
  188. cmFileRC_t cmFileWriteUInt( cmFileH_t h, const unsigned int* buf, unsigned cnt );
  189. cmFileRC_t cmFileWriteFloat( cmFileH_t h, const float* buf, unsigned cnt );
  190. cmFileRC_t cmFileWriteDouble( cmFileH_t h, const double* buf, unsigned cnt );
  191. cmFileRC_t cmFileWriteBool( cmFileH_t h, const bool* buf, unsigned cnt );
  192. // Write a string to a file as <N> <char0> <char1> ... <char(N-1)>
  193. // where N is the count of characters in the string.
  194. cmFileRC_t cmFileWriteStr( cmFileH_t h, const cmChar_t* s );
  195. // Read a string back from a file as written by cmFileWriteStr().
  196. // Note that the string will by string will be dynamically allocated
  197. // and threfore must eventually be released via cmMemFree().
  198. // If maxCharN is set to zero then the default maximum string
  199. // length is 16384. Note that this limit is used to prevent
  200. // corrupt files from generating excessively long strings.
  201. cmFileRC_t cmFileReadStr( cmFileH_t h, cmChar_t** sRef, unsigned maxCharN );
  202. // Formatted Text Output Functions:
  203. // Print formatted text to a file.
  204. cmFileRC_t cmFilePrint( cmFileH_t h, const cmChar_t* text );
  205. cmFileRC_t cmFilePrintf( cmFileH_t h, const cmChar_t* fmt, ... );
  206. cmFileRC_t cmFileVPrintf( cmFileH_t h, const cmChar_t* fmt, va_list vl );
  207. cmFileRC_t cmFileLastRC( cmFileH_t h );
  208. cmFileRC_t cmFileSetRC( cmFileH_t h, cmFileRC_t rc );
  209. //)
  210. #ifdef __cplusplus
  211. }
  212. #endif
  213. #endif