stream.h 9.2 KB

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