stream.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. _ATTRIBUTE((__format__(__printf__, 2, 3)));
  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, FuriString* 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. _ATTRIBUTE((__format__(__printf__, 2, 3)));
  180. /**
  181. * Insert formatted string to the stream, va_list version
  182. * @param stream Stream instance
  183. * @param format
  184. * @param args
  185. * @return true if the operation was successful
  186. * @return false on error
  187. */
  188. bool stream_insert_vaformat(Stream* stream, const char* format, va_list args);
  189. /**
  190. * Delete N chars from the stream and insert char to the stream
  191. * @param stream Stream instance
  192. * @param delete_size size of data to be deleted
  193. * @param c char value
  194. * @return true if the operation was successful
  195. * @return false on error
  196. */
  197. bool stream_delete_and_insert_char(Stream* stream, size_t delete_size, char c);
  198. /**
  199. * Delete N chars from the stream and insert string to the stream
  200. * @param stream Stream instance
  201. * @param delete_size size of data to be deleted
  202. * @param string string value
  203. * @return true if the operation was successful
  204. * @return false on error
  205. */
  206. bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, FuriString* string);
  207. /**
  208. * Delete N chars from the stream and insert const char* to the stream
  209. * @param stream Stream instance
  210. * @param delete_size size of data to be deleted
  211. * @param string c-string value
  212. * @return true if the operation was successful
  213. * @return false on error
  214. */
  215. bool stream_delete_and_insert_cstring(Stream* stream, size_t delete_size, const char* string);
  216. /**
  217. * Delete N chars from the stream and insert formatted string to the stream
  218. * @param stream Stream instance
  219. * @param delete_size size of data to be deleted
  220. * @param format
  221. * @param ...
  222. * @return true if the operation was successful
  223. * @return false on error
  224. */
  225. bool stream_delete_and_insert_format(Stream* stream, size_t delete_size, const char* format, ...)
  226. _ATTRIBUTE((__format__(__printf__, 3, 4)));
  227. /**
  228. * Delete N chars from the stream and insert formatted string to the stream, va_list version
  229. * @param stream Stream instance
  230. * @param delete_size size of data to be deleted
  231. * @param format
  232. * @param args
  233. * @return true if the operation was successful
  234. * @return false on error
  235. */
  236. bool stream_delete_and_insert_vaformat(
  237. Stream* stream,
  238. size_t delete_size,
  239. const char* format,
  240. va_list args);
  241. /**
  242. * Remove N chars from the stream, starting at the current pointer.
  243. * The size may be larger than stream size, the stream will be cleared from current rw pointer to the end.
  244. * @param stream Stream instance
  245. * @param size how many chars need to be deleted
  246. * @return true if the operation was successful
  247. * @return false on error
  248. */
  249. bool stream_delete(Stream* stream, size_t size);
  250. /**
  251. * Copy data from one stream to another. Data will be copied from current rw pointer and to current rw pointer.
  252. * @param stream_from
  253. * @param stream_to
  254. * @param size
  255. * @return size_t
  256. */
  257. size_t stream_copy(Stream* stream_from, Stream* stream_to, size_t size);
  258. /**
  259. * Copy data from one stream to another. Data will be copied from start of one stream and to start of other stream.
  260. * @param stream_from
  261. * @param stream_to
  262. * @return size_t
  263. */
  264. size_t stream_copy_full(Stream* stream_from, Stream* stream_to);
  265. /**
  266. * Splits one stream into two others. The original stream will remain untouched.
  267. * @param stream
  268. * @param stream_left
  269. * @param stream_right
  270. * @return true
  271. * @return false
  272. */
  273. bool stream_split(Stream* stream, Stream* stream_left, Stream* stream_right);
  274. /**
  275. * 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.
  276. * @param stream Stream instance
  277. * @param storage
  278. * @param path
  279. * @return size_t
  280. */
  281. size_t stream_load_from_file(Stream* stream, Storage* storage, const char* path);
  282. /**
  283. * 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.
  284. * @param stream Stream instance
  285. * @param storage
  286. * @param path
  287. * @param mode
  288. * @return size_t
  289. */
  290. size_t stream_save_to_file(Stream* stream, Storage* storage, const char* path, FS_OpenMode mode);
  291. /**
  292. * Dump stream inner data (size, RW positiot, content)
  293. * @param stream Stream instance
  294. */
  295. void stream_dump_data(Stream* stream);
  296. #ifdef __cplusplus
  297. }
  298. #endif