libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmFile.h 8.9KB

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