stream.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #pragma once
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <mlib/m-string.h>
  6. #include <storage/storage.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. typedef struct Stream Stream;
  11. typedef enum {
  12. StreamOffsetFromCurrent,
  13. StreamOffsetFromStart,
  14. StreamOffsetFromEnd,
  15. } StreamOffset;
  16. typedef bool (*StreamWriteCB)(Stream* stream, const void* context);
  17. /**
  18. * Free Stream
  19. * @param stream Stream instance
  20. */
  21. void stream_free(Stream* stream);
  22. /**
  23. * Clean (empty) Stream
  24. * @param stream Stream instance
  25. */
  26. void stream_clean(Stream* stream);
  27. /**
  28. * Indicates that the rw pointer is at the end of the stream
  29. * @param stream Stream instance
  30. * @return true if rw pointer is at the end of the stream
  31. * @return false if rw pointer is not at the end of the stream
  32. */
  33. bool stream_eof(Stream* stream);
  34. /**
  35. * Moves the rw pointer.
  36. * @param stream Stream instance
  37. * @param offset how much to move the pointer
  38. * @param offset_type starting from what
  39. * @return true
  40. * @return false
  41. */
  42. bool stream_seek(Stream* stream, int32_t offset, StreamOffset offset_type);
  43. /**
  44. * Gets the value of the rw pointer
  45. * @param stream Stream instance
  46. * @return size_t value of the rw pointer
  47. */
  48. size_t stream_tell(Stream* stream);
  49. /**
  50. * Gets the size of the stream
  51. * @param stream Stream instance
  52. * @return size_t size of the stream
  53. */
  54. size_t stream_size(Stream* stream);
  55. /**
  56. * Write N bytes to the stream
  57. * @param stream Stream instance
  58. * @param data data to write
  59. * @param size size of data to be written
  60. * @return size_t how many bytes was written
  61. */
  62. size_t stream_write(Stream* stream, const uint8_t* data, size_t size);
  63. /**
  64. * Read N bytes from stream
  65. * @param stream Stream instance
  66. * @param data data to be read
  67. * @param count size of data to be read
  68. * @return size_t how many bytes was read
  69. */
  70. size_t stream_read(Stream* stream, uint8_t* data, size_t count);
  71. /**
  72. * Delete N chars from the stream and write data by calling write_callback(context)
  73. * @param stream Stream instance
  74. * @param delete_size size of data to be deleted
  75. * @param write_callback write callback
  76. * @param context write callback context
  77. * @return true if the operation was successful
  78. * @return false on error
  79. */
  80. bool stream_delete_and_insert(
  81. Stream* stream,
  82. size_t delete_size,
  83. StreamWriteCB write_callback,
  84. const void* context);
  85. /********************************** Some random helpers starts here **********************************/
  86. /**
  87. * Read line from a stream (supports LF and CRLF line endings)
  88. * @param stream
  89. * @param str_result
  90. * @return true
  91. * @return false
  92. */
  93. bool stream_read_line(Stream* stream, string_t str_result);
  94. /**
  95. * Moves the rw pointer to the start
  96. * @param stream Stream instance
  97. */
  98. bool stream_rewind(Stream* stream);
  99. /**
  100. * Write char to the stream
  101. * @param stream Stream instance
  102. * @param c char value
  103. * @return size_t how many bytes was written
  104. */
  105. size_t stream_write_char(Stream* stream, char c);
  106. /**
  107. * Write string to the stream
  108. * @param stream Stream instance
  109. * @param string string value
  110. * @return size_t how many bytes was written
  111. */
  112. size_t stream_write_string(Stream* stream, string_t string);
  113. /**
  114. * Write const char* to the stream
  115. * @param stream Stream instance
  116. * @param string c-string value
  117. * @return size_t how many bytes was written
  118. */
  119. size_t stream_write_cstring(Stream* stream, const char* string);
  120. /**
  121. * Write formatted string to the stream
  122. * @param stream Stream instance
  123. * @param format
  124. * @param ...
  125. * @return size_t how many bytes was written
  126. */
  127. size_t stream_write_format(Stream* stream, const char* format, ...);
  128. /**
  129. * Write formatted string to the stream, va_list version
  130. * @param stream Stream instance
  131. * @param format
  132. * @param args
  133. * @return size_t how many bytes was written
  134. */
  135. size_t stream_write_vaformat(Stream* stream, const char* format, va_list args);
  136. /**
  137. * Insert N chars to the stream, starting at the current pointer.
  138. * Data will be inserted, not overwritteт, so the stream will be increased in size.
  139. * @param stream Stream instance
  140. * @param data data to be inserted
  141. * @param size size of data to be inserted
  142. * @return true if the operation was successful
  143. * @return false on error
  144. */
  145. bool stream_insert(Stream* stream, const uint8_t* data, size_t size);
  146. /**
  147. * Insert char to the stream
  148. * @param stream Stream instance
  149. * @param c char value
  150. * @return true if the operation was successful
  151. * @return false on error
  152. */
  153. bool stream_insert_char(Stream* stream, char c);
  154. /**
  155. * Insert string to the stream
  156. * @param stream Stream instance
  157. * @param string string value
  158. * @return true if the operation was successful
  159. * @return false on error
  160. */
  161. bool stream_insert_string(Stream* stream, string_t string);
  162. /**
  163. * Insert const char* to the stream
  164. * @param stream Stream instance
  165. * @param string c-string value
  166. * @return true if the operation was successful
  167. * @return false on error
  168. */
  169. bool stream_insert_cstring(Stream* stream, const char* string);
  170. /**
  171. * Insert formatted string to the stream
  172. * @param stream Stream instance
  173. * @param format
  174. * @param ...
  175. * @return true if the operation was successful
  176. * @return false on error
  177. */
  178. bool stream_insert_format(Stream* stream, const char* format, ...);
  179. /**
  180. * Insert formatted string to the stream, va_list version
  181. * @param stream Stream instance
  182. * @param format
  183. * @param args
  184. * @return true if the operation was successful
  185. * @return false on error
  186. */
  187. bool stream_insert_vaformat(Stream* stream, const char* format, va_list args);
  188. /**
  189. * Delete N chars from the stream and insert char to the stream
  190. * @param stream Stream instance
  191. * @param delete_size size of data to be deleted
  192. * @param c char value
  193. * @return true if the operation was successful
  194. * @return false on error
  195. */
  196. bool stream_delete_and_insert_char(Stream* stream, size_t delete_size, char c);
  197. /**
  198. * Delete N chars from the stream and insert string to the stream
  199. * @param stream Stream instance
  200. * @param delete_size size of data to be deleted
  201. * @param string string value
  202. * @return true if the operation was successful
  203. * @return false on error
  204. */
  205. bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, string_t string);
  206. /**
  207. * Delete N chars from the stream and insert const char* to the stream
  208. * @param stream Stream instance
  209. * @param delete_size size of data to be deleted
  210. * @param string c-string value
  211. * @return true if the operation was successful
  212. * @return false on error
  213. */
  214. bool stream_delete_and_insert_cstring(Stream* stream, size_t delete_size, const char* string);
  215. /**
  216. * Delete N chars from the stream and insert formatted string to the stream
  217. * @param stream Stream instance
  218. * @param delete_size size of data to be deleted
  219. * @param format
  220. * @param ...
  221. * @return true if the operation was successful
  222. * @return false on error
  223. */
  224. bool stream_delete_and_insert_format(Stream* stream, size_t delete_size, const char* format, ...);
  225. /**
  226. * Delete N chars from the stream and insert formatted string to the stream, va_list version
  227. * @param stream Stream instance
  228. * @param delete_size size of data to be deleted
  229. * @param format
  230. * @param args
  231. * @return true if the operation was successful
  232. * @return false on error
  233. */
  234. bool stream_delete_and_insert_vaformat(
  235. Stream* stream,
  236. size_t delete_size,
  237. const char* format,
  238. va_list args);
  239. /**
  240. * Remove N chars from the stream, starting at the current pointer.
  241. * The size may be larger than stream size, the stream will be cleared from current rw pointer to the end.
  242. * @param stream Stream instance
  243. * @param size how many chars need to be deleted
  244. * @return true if the operation was successful
  245. * @return false on error
  246. */
  247. bool stream_delete(Stream* stream, size_t size);
  248. /**
  249. * Copy data from one stream to another. Data will be copied from current rw pointer and to current rw pointer.
  250. * @param stream_from
  251. * @param stream_to
  252. * @param size
  253. * @return size_t
  254. */
  255. size_t stream_copy(Stream* stream_from, Stream* stream_to, size_t size);
  256. /**
  257. * Copy data from one stream to another. Data will be copied from start of one stream and to start of other stream.
  258. * @param stream_from
  259. * @param stream_to
  260. * @return size_t
  261. */
  262. size_t stream_copy_full(Stream* stream_from, Stream* stream_to);
  263. /**
  264. * Splits one stream into two others. The original stream will remain untouched.
  265. * @param stream
  266. * @param stream_left
  267. * @param stream_right
  268. * @return true
  269. * @return false
  270. */
  271. bool stream_split(Stream* stream, Stream* stream_left, Stream* stream_right);
  272. /**
  273. * Loads data to the stream from a file. Data will be loaded to the current RW pointer. RW pointer will be moved to the end of the stream.
  274. * @param stream Stream instance
  275. * @param storage
  276. * @param path
  277. * @return size_t
  278. */
  279. size_t stream_load_from_file(Stream* stream, Storage* storage, const char* path);
  280. /**
  281. * Writes data from a stream to a file. Data will be saved starting from the current RW pointer. RW pointer will be moved to the end of the stream.
  282. * @param stream Stream instance
  283. * @param storage
  284. * @param path
  285. * @param mode
  286. * @return size_t
  287. */
  288. size_t stream_save_to_file(Stream* stream, Storage* storage, const char* path, FS_OpenMode mode);
  289. /**
  290. * Dump stream inner data (size, RW positiot, content)
  291. * @param stream Stream instance
  292. */
  293. void stream_dump_data(Stream* stream);
  294. #ifdef __cplusplus
  295. }
  296. #endif