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.

cmFileSys.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. //( { file_desc:"A collection of file system utility functions." kw:[system file]}
  4. //
  5. // Note that cmFileSysInitialize() creates an internal cmLHeapH_t based
  6. // heap manager from which it allocates memory for some returned objects.
  7. // (e.g. cmFileSysMakeFn(), cmFileSysPathParts(), cmFileSysDirEntries())
  8. // Where possible the client can explicitely free these objects via the
  9. // provided functions. (e.g. cmFileSysFreeFn(), cmFileSysFreePathParts(), cmFileSysDirFreeEntries())
  10. // However if these objects are not free they will be automatically deallocated
  11. // when the internal heap is destroyed by cmFileSysFinalize().
  12. //
  13. //)
  14. #ifndef cmFileSys_h
  15. #define cmFileSys_h
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. //(
  20. // Result codes returned by cmFileSys.h functions
  21. enum
  22. {
  23. kOkFsRC = cmOkRC,
  24. kMemAllocErrFsRC,
  25. kLHeapAllocErrFsRC,
  26. kStatFailFsRC,
  27. kAssertFailFsRC,
  28. kOpenDirFailFsRC,
  29. kFnTooLongFsRC,
  30. kMkDirFailFsRC,
  31. kSysErrFsRC,
  32. kOsxFailFsRC,
  33. kLinuxFailFsRC,
  34. kInvalidDirFsRC,
  35. kGenFileFailFsRC,
  36. kAccessFailFsRC,
  37. kFormFnFailFsRC
  38. };
  39. typedef cmHandle_t cmFileSysH_t; //< Opaque handle type used by all cmFileSys.h functions
  40. typedef unsigned cmFsRC_t; //< Result code used as the return type by many cmFileSys.h functions.
  41. extern cmFileSysH_t cmFileSysNullHandle; // NULL handle to be used for setting cmFileSysH_t type handles to an explicit uninitialized value.
  42. // Initialize a file system object.
  43. // If *hp was not initalized by an earlier call to cmFileSysInitialize() then it should
  44. // be set to cmFileSysNullHandle prior to calling this function. If *hp is a valid handle
  45. // then it is automatically finalized by an internal call to cmFileSysFinalize() prior to
  46. // being re-iniitalized.
  47. // The appNameStr is used to determine the location of the preference and resource directories
  48. // on some platforms
  49. cmFsRC_t cmFileSysInitialize( cmFileSysH_t* hp, cmCtx_t* ctx, const cmChar_t* appNameStr );
  50. // Finalize a file system object.
  51. // Upon successful completion *hp is set to cmFileSysNullHandle.
  52. cmFsRC_t cmFileSysFinalize( cmFileSysH_t* hp );
  53. // Returns true if the file system handle is active and initialized.
  54. bool cmFileSysIsValid( cmFileSysH_t h );
  55. const cmChar_t* cmFileSysAppName( cmFileSysH_t h ); //< Return the application name as passed to cmFileSysInitialize()
  56. const cmChar_t* cmFileSysPrefsDir( cmFileSysH_t h ); //< Return the operating system dependent preference data directory for this application.
  57. const cmChar_t* cmFileSysRsrcDir( cmFileSysH_t h ); //< Return the operating system dependent application resource directory for this application.
  58. const cmChar_t* cmFileSysUserDir( cmFileSysH_t h ); //< Return the operating system dependent user directory for this application.
  59. // Check if a request to create a file will succeed.
  60. bool cmFileSysCanWriteToDir( cmFileSysH_t h, const cmChar_t* dirStr );
  61. // Test the type of a file system object:
  62. //
  63. bool cmFileSysIsDir( cmFileSysH_t h, const cmChar_t* dirStr ); //< Return true if 'dirStr' refers to an existing directory.
  64. bool cmFileSysIsFile( cmFileSysH_t h, const cmChar_t* fnStr ); //< Return true if 'fnStr' refers to an existing file.
  65. bool cmFileSysIsLink( cmFileSysH_t h, const cmChar_t* fnStr ); //< Return true if 'fnStr' refers to a symbolic link.
  66. // Create File Names:
  67. //
  68. // Create a file name by concatenating sub-strings.
  69. //
  70. // Variable arg's. entries are directories inserted between
  71. // 'dirPrefixStr' and the file name.
  72. // Terminate var arg's directory list with a NULL.
  73. //
  74. // The returned string is allocated in a local heap maintained by the cmFileSys object.
  75. // The memory used by the string will exist until it is released with cmFileSysFreeFn()
  76. // or the cmFileSys object is finalized.
  77. const cmChar_t* cmFileSysMakeFn( cmFileSysH_t h, const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
  78. // Same as cmFileSysMakeFn but prefixes the entire file path with the current users
  79. // home directory. (i.e. /home/me/<dirPrefix>/<var-args-dir-0>/<var-args-dir1>/<fn>.<ext>)
  80. const cmChar_t* cmFileSysMakeUserFn( cmFileSysH_t h, const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
  81. // Same as cmFileSysMakeFn but with a va_list argument to accept the var. args. parameters.
  82. const cmChar_t* cmFileSysVMakeFn( cmFileSysH_t h, const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
  83. // Same as cmFileSysMakeUserFn but with a va_list argument to accept the var. args parameters.
  84. const cmChar_t* cmFileSysVMakeUserFn( cmFileSysH_t h, const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
  85. // Equivalent to same named cmFileSysMakeFn() functions but form a directory
  86. // path rather than a file name path.
  87. const cmChar_t* cmFileSysVMakeDir( cmFileSysH_t h, const cmChar_t* dir, va_list vl );
  88. const cmChar_t* cmFileSysMakeDir( cmFileSysH_t h, const cmChar_t* dir, ... );
  89. const cmChar_t* cmFileSysVMakeUserDir( cmFileSysH_t h, const cmChar_t* dir, va_list vl );
  90. const cmChar_t* cmFileSysMakeUserDir( cmFileSysH_t h, const cmChar_t* dir, ... );
  91. // Equivalent to same named cmFileSysMake() functions but assumes a single directory prefix and the file name
  92. // extension is attached to 'fn'.
  93. const cmChar_t* cmFileSysMakeDirFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn );
  94. // Same as cmFileSysMakeDirFn() but prefixes 'dir' with the users home directory.
  95. const cmChar_t* cmFileSysMakeUserDirFn(cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn );
  96. // Release the file name created through an earlier call to cmFileSysMakeFn().
  97. void cmFileSysFreeFn( cmFileSysH_t h, const cmChar_t* fn );
  98. // Generate an unused filename in the directory 'dir' beginning with the prefix 'prefixStr'.
  99. // The returned file name will have the format: <dir>/<prefixStr>nnnn.<extStr> where
  100. // nnn represents 1 or more digits. The returned string must be released with a
  101. // call to cmMemFree().
  102. cmFsRC_t cmFileSysGenFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr );
  103. // Create a directory - where the entire path already exists except for the
  104. // final directory.
  105. cmFsRC_t cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir );
  106. // Same as cmFileSysMkDir() but 'dir' is automatically prefixed with the users home directory.
  107. cmFsRC_t cmFileSysMkUserDir( cmFileSysH_t h, const cmChar_t* dir );
  108. // Create a complete directory path - where any of the path segments may
  109. // not already exist.
  110. cmFsRC_t cmFileSysMkDirAll( cmFileSysH_t h, const cmChar_t* dir );
  111. // Same as cmFileSysMkDir() but 'dir' is automatically prefixed with the users home directory.
  112. cmFsRC_t cmFileSysMkUserDirAll( cmFileSysH_t h, const cmChar_t* dir );
  113. // Parse a path into its parts:
  114. //
  115. // Return record used by cmFileSysParts()
  116. typedef struct
  117. {
  118. const cmChar_t* dirStr;
  119. const cmChar_t* fnStr;
  120. const cmChar_t* extStr;
  121. } cmFileSysPathPart_t;
  122. // Given a file name decompose it into a directory string, file name string and file extension string.
  123. // The cmFileSysPathPart_t record and the memory used by the strings that it references
  124. // are allocated from a local heap maintained by the cmFileSys object. This memory will exist
  125. // until it is released with cmFileSysFreePathParts() or the cmFileSysH_t handle is finalized.
  126. cmFileSysPathPart_t* cmFileSysPathParts( cmFileSysH_t h, const cmChar_t* pathNameStr );
  127. // Free the memory associated with a cmFileSysPathPart_t record returned from an eariler call to cmFileSysPathParts().
  128. void cmFileSysFreePathParts( cmFileSysH_t h, cmFileSysPathPart_t* p );
  129. // Return the parts of a directory path as an array of strings.
  130. // The last element in the array is set to NULL to mark the end of the array.
  131. // Note that all '/' separator characters are removed from the result with
  132. // the exception of the first one - which denotes the root directory.
  133. // The returned array is allocated from the file systems internal heap and will
  134. // be automatically released when the file system is closed by cmFileSysDestroy().
  135. // The caller may optionally release the array memory with a call to
  136. // cmFileSysFreeDirParts().
  137. cmChar_t** cmFileSysDirParts( cmFileSysH_t h, const cmChar_t* dirStr );
  138. void cmFileSysFreeDirParts( cmFileSysH_t h, cmChar_t** dirStrArray );
  139. // Return the count of elements in a directory parts array as returned by
  140. // cmFileSysDirParts().
  141. unsigned cmFileSysDirPartsCount(cmFileSysH_t h, cmChar_t** dirStrArray );
  142. // Form a directory string from a NULL terminated array of strings.
  143. // If the first element in the array is set to '/' then the
  144. // resulting directory will be absolute rather than relative.
  145. // The returned string is allocated from the file systems internal heap and will
  146. // be automatically released when the file system is closed by cmFileSysDestroy().
  147. // The caller may optionally release the array memory with a call to
  148. // cmFileSysFreeDir().
  149. cmChar_t* cmFileSysFormDir( cmFileSysH_t h, cmChar_t** dirStrArray, unsigned n );
  150. void cmFileSysFreeDir( cmFileSysH_t h, const cmChar_t* dir );
  151. // Walk a directory tree:
  152. //
  153. // Flags used by cmFileSysDirEntries 'includeFlags' parameter.
  154. enum
  155. {
  156. kFileFsFl = 0x001, //< include all visible files
  157. kDirFsFl = 0x002, //< include all visible directory
  158. kLinkFsFl = 0x004, //< include all symbolic links
  159. kInvisibleFsFl = 0x008, //< include file/dir name beginning with a '.'
  160. kCurDirFsFl = 0x010, //< include '.' directory
  161. kParentDirFsFl = 0x020, //< include '..' directory
  162. kAllFsFl = 0x02f, //< all type flags
  163. kFullPathFsFl = 0x040, //< return the full path in the 'name' field of cmFileSysDirEntry_t;
  164. kRecurseFsFl = 0x080, //< recurse into directories
  165. kRecurseLinksFsFl = 0x100 //< recurse into symbol link directories
  166. };
  167. // The return type for cmFileSysDirEntries().
  168. typedef struct
  169. {
  170. unsigned flags; //< Entry type flags from kXXXFsFl.
  171. const cmChar_t* name; //< Entry name or full path depending on kFullPathFsFl.
  172. } cmFileSysDirEntry_t;
  173. // Return the file and directory names contained in a given subdirectory.
  174. //
  175. // Set 'includeFlags' with the kXXXFsFl flags of the files to include in the returned
  176. // directory entry array. The value pointed to by dirEntryCntPtr will be set to the
  177. // number of records in the returned array.
  178. cmFileSysDirEntry_t* cmFileSysDirEntries( cmFileSysH_t h, const cmChar_t* dirStr, unsigned includeFlags, unsigned* dirEntryCntPtr );
  179. // Release the memory assoicated with a cmFileSysDirEntry_t array returned from an earlier call to cmFileSysDirEntries().
  180. void cmFileSysDirFreeEntries( cmFileSysH_t h, cmFileSysDirEntry_t* p );
  181. // Return the last error code generated by the file system.
  182. cmFsRC_t cmFileSysErrorCode( cmFileSysH_t h );
  183. //-------------------------------------------------------------------------------------------------
  184. // Global file system functions:
  185. // These functions work using a global cmFileSysH created by cmFsInitialize().
  186. // The functions are otherwise just wrappers for the same named function above.
  187. cmFsRC_t cmFsInitialize( cmCtx_t* ctx, const cmChar_t* appNameStr );
  188. cmFsRC_t cmFsFinalize();
  189. const cmChar_t* cmFsAppName();
  190. const cmChar_t* cmFsPrefsDir();
  191. const cmChar_t* cmFsRsrcDir();
  192. const cmChar_t* cmFsUserDir();
  193. bool cmFsCanWriteToDir( const cmChar_t* dirStr );
  194. bool cmFsIsDir( const cmChar_t* dirStr );
  195. bool cmFsIsFile( const cmChar_t* fnStr );
  196. bool cmFsIsLink( const cmChar_t* fnStr );
  197. const cmChar_t* cmFsVMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
  198. const cmChar_t* cmFsMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
  199. const cmChar_t* cmFsVMakeUserFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl );
  200. const cmChar_t* cmFsMakeUserFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... );
  201. const cmChar_t* cmFsVMakeDir( const cmChar_t* dirPrefix, va_list vl );
  202. const cmChar_t* cmFsMakeDir( const cmChar_t* dirPrefix, ... );
  203. const cmChar_t* cmFsVMakeUserDir( const cmChar_t* dirPrefix, va_list vl );
  204. const cmChar_t* cmFsMakeUserDir( const cmChar_t* dirPrefix, ... );
  205. const cmChar_t* cmFsMakeDirFn( const cmChar_t* dir, const cmChar_t* fn );
  206. const cmChar_t* cmFsMakeUserDirFn(const cmChar_t* dir, const cmChar_t* fn );
  207. void cmFsFreeFn( const cmChar_t* fn );
  208. cmFsRC_t cmFsGenFn( const cmChar_t* dir, const cmChar_t* prefixStr, const cmChar_t* extStr, const cmChar_t** fnPtr );
  209. cmFsRC_t cmFsMkDir( const cmChar_t* dir );
  210. cmFsRC_t cmFsMkUserDir( const cmChar_t* dir );
  211. cmFsRC_t cmFsMkDirAll( const cmChar_t* dir );
  212. cmFsRC_t cmFsMkUserDirAll( const cmChar_t* dir );
  213. cmFileSysPathPart_t* cmFsPathParts( const cmChar_t* pathNameStr );
  214. void cmFsFreePathParts( cmFileSysPathPart_t* p );
  215. cmChar_t** cmFsDirParts( const cmChar_t* dirStr );
  216. void cmFsFreeDirParts( cmChar_t** dirStrArray );
  217. unsigned cmFsDirPartsCount( cmChar_t** dirStrArray );
  218. cmChar_t* cmFsFormDir( cmChar_t** dirStrArray, unsigned n );
  219. void cmFsFreeDir( const cmChar_t* dir );
  220. cmFileSysDirEntry_t* cmFsDirEntries( const cmChar_t* dirStr, unsigned includeFlags, unsigned* dirEntryCntPtr );
  221. void cmFsDirFreeEntries( cmFileSysDirEntry_t* p );
  222. cmFsRC_t cmFsErrorCode();
  223. // Test and example function to demonstrate the use of the functions in cmFileSys.h
  224. cmFsRC_t cmFileSysTest( cmCtx_t* ctx );
  225. //)
  226. #ifdef __cplusplus
  227. }
  228. #endif
  229. #endif