KSFileUtils.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // KSFileUtils.h
  3. //
  4. // Created by Karl Stenerud on 2012-01-28.
  5. //
  6. // Copyright (c) 2012 Karl Stenerud. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall remain in place
  16. // in this source code.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. /* Basic file reading/writing functions.
  27. */
  28. #ifndef HDR_KSFileUtils_h
  29. #define HDR_KSFileUtils_h
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include <stdbool.h>
  34. #include <stdarg.h>
  35. #define KSFU_MAX_PATH_LENGTH 500
  36. /** Get the last entry in a file path. Assumes UNIX style separators.
  37. *
  38. * @param path The file path.
  39. *
  40. * @return the last entry in the path.
  41. */
  42. const char* ksfu_lastPathEntry(const char* path);
  43. /** Write bytes to a file descriptor.
  44. *
  45. * @param fd The file descriptor.
  46. *
  47. * @param bytes Buffer containing the bytes.
  48. *
  49. * @param length The number of bytes to write.
  50. *
  51. * @return true if the operation was successful.
  52. */
  53. bool ksfu_writeBytesToFD(const int fd, const char* bytes, int length);
  54. /** Read bytes from a file descriptor.
  55. *
  56. * @param fd The file descriptor.
  57. *
  58. * @param bytes Buffer to store the bytes in.
  59. *
  60. * @param length The number of bytes to read.
  61. *
  62. * @return true if the operation was successful.
  63. */
  64. bool ksfu_readBytesFromFD(const int fd, char* bytes, int length);
  65. /** Read an entire file. Returns a buffer of file size + 1, null terminated.
  66. *
  67. * @param path The path to the file.
  68. *
  69. * @param data Place to store a pointer to the loaded data (must be freed).
  70. *
  71. * @param length Place to store the length of the loaded data (can be NULL).
  72. *
  73. * @param maxLength the maximum amount of bytes to read. It will skip beginning
  74. * bytes if necessary, and always get the latter part of the file.
  75. * 0 = no maximum.
  76. *
  77. * @return true if the operation was successful.
  78. */
  79. bool ksfu_readEntireFile(const char* path, char** data, int* length, int maxLength);
  80. /** Write a string to a file.
  81. *
  82. * @param fd The file descriptor.
  83. *
  84. * @param string The string to write.
  85. *
  86. * @return true if successful.
  87. */
  88. bool ksfu_writeStringToFD(const int fd, const char* string);
  89. /** Write a formatted string to a file.
  90. *
  91. * @param fd The file descriptor.
  92. *
  93. * @param fmt The format specifier, followed by its arguments.
  94. *
  95. * @return true if successful.
  96. */
  97. bool ksfu_writeFmtToFD(const int fd, const char* fmt, ...);
  98. /** Write a formatted string to a file.
  99. *
  100. * @param fd The file descriptor.
  101. *
  102. * @param fmt The format specifier.
  103. *
  104. * @param args The arguments list.
  105. *
  106. * @return true if successful.
  107. */
  108. bool ksfu_writeFmtArgsToFD(const int fd, const char* fmt, va_list args);
  109. /** Read a single line from a file.
  110. *
  111. * @param fd The file descriptor.
  112. *
  113. * @param buffer The buffer to read into.
  114. *
  115. * @param maxLength The maximum length to read.
  116. *
  117. * @return The number of bytes read.
  118. */
  119. int ksfu_readLineFromFD(const int fd, char* buffer, int maxLength);
  120. /** Make all directories in a path.
  121. *
  122. * @param absolutePath The full, absolute path to create.
  123. *
  124. * @return true if successful.
  125. */
  126. bool ksfu_makePath(const char* absolutePath);
  127. /** Remove a file or directory.
  128. *
  129. * @param path Path to the file to remove.
  130. *
  131. * @param mustExist If true, and the path doesn't exist, log an error.
  132. *
  133. * @return true if successful.
  134. */
  135. bool ksfu_removeFile(const char* path, bool mustExist);
  136. /** Delete the contents of a directory.
  137. *
  138. * @param path The path of the directory whose contents to delete.
  139. *
  140. * @return true if successful.
  141. */
  142. bool ksfu_deleteContentsOfPath(const char* path);
  143. /** Buffered writer structure. Everything inside should be considered internal use only. */
  144. typedef struct
  145. {
  146. char* buffer;
  147. int bufferLength;
  148. int position;
  149. int fd;
  150. } KSBufferedWriter;
  151. /** Open a file for buffered writing.
  152. *
  153. * @param writer The writer to initialize.
  154. *
  155. * @param path The path of the file to open.
  156. *
  157. * @param writeBuffer Memory to use as the write buffer.
  158. *
  159. * @param writeBufferLength Length of the memory to use as the write buffer.
  160. *
  161. * @return True if the file was successfully opened.
  162. */
  163. bool ksfu_openBufferedWriter(KSBufferedWriter* writer, const char* const path, char* writeBuffer, int writeBufferLength);
  164. /** Close a buffered writer.
  165. *
  166. * @param writer The writer to close.
  167. */
  168. void ksfu_closeBufferedWriter(KSBufferedWriter* writer);
  169. /** Write to a buffered writer.
  170. *
  171. * @param writer The writer to write to.
  172. *
  173. * @param data The data to write.
  174. *
  175. * @param length The length of the data to write.
  176. *
  177. * @return True if the data was successfully written.
  178. */
  179. bool ksfu_writeBufferedWriter(KSBufferedWriter* writer, const char* restrict const data, const int length);
  180. /** Flush a buffered writer, writing all uncommitted data to disk.
  181. *
  182. * @param writer The writer to flush.
  183. *
  184. * @return True if the buffer was successfully flushed.
  185. */
  186. bool ksfu_flushBufferedWriter(KSBufferedWriter* writer);
  187. /** Buffered reader structure. Everything inside should be considered internal use only. */
  188. typedef struct
  189. {
  190. char* buffer;
  191. int bufferLength;
  192. int dataStartPos;
  193. int dataEndPos;
  194. int fd;
  195. } KSBufferedReader;
  196. /** Open a file for buffered reading.
  197. *
  198. * @param reader The reader to initialize.
  199. *
  200. * @param path The path to the file to open.
  201. *
  202. * @param readBuffer The memory to use for buffered reading.
  203. *
  204. * @param readBufferLength The length of the memory to use for buffered reading.
  205. *
  206. * @return True if the file was successfully opened.
  207. */
  208. bool ksfu_openBufferedReader(KSBufferedReader* reader, const char* const path, char* readBuffer, int readBufferLength);
  209. /** Close a buffered reader.
  210. *
  211. * @param reader The reader to close.
  212. */
  213. void ksfu_closeBufferedReader(KSBufferedReader* reader);
  214. /** Read from a buffered reader.
  215. *
  216. * @param reader The reader to read from.
  217. *
  218. * @param dstBuffer The buffer to read into.
  219. *
  220. * @param byteCount The number of bytes to read.
  221. *
  222. * @return The number of bytes actually read.
  223. */
  224. int ksfu_readBufferedReader(KSBufferedReader* reader, char* dstBuffer, int byteCount);
  225. /** Read from a buffered reader until the specified character is encountered.
  226. * All bytes up to and including the character will be read.
  227. *
  228. * @param reader The reader to read from.
  229. *
  230. * @param ch The character to look for.
  231. *
  232. * @param dstBuffer The buffer to read into.
  233. *
  234. * @param length in: The maximum number of bytes to read before giving up the search.
  235. * out: The actual number of bytes read.
  236. *
  237. * @return True if the character was found before giving up.
  238. */
  239. bool ksfu_readBufferedReaderUntilChar(KSBufferedReader* reader, int ch, char* dstBuffer, int* length);
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif // HDR_KSFileUtils_h