stream.h 9.7 KB

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