libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmFile.h 8.7KB

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