Welcome to mirror list, hosted at ThFree Co, Russian Federation.

stream.h « stream « toolbox « lib - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0fb355188b26b278250312edd81e4248a559ae77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <mlib/m-string.h>
#include <storage/storage.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct Stream Stream;

typedef enum {
    StreamOffsetFromCurrent,
    StreamOffsetFromStart,
    StreamOffsetFromEnd,
} StreamOffset;

typedef bool (*StreamWriteCB)(Stream* stream, const void* context);

/**
 * Free Stream
 * @param stream Stream instance
 */
void stream_free(Stream* stream);

/**
 * Clean (empty) Stream
 * @param stream Stream instance
 */
void stream_clean(Stream* stream);

/**
 * Indicates that the rw pointer is at the end of the stream
 * @param stream Stream instance
 * @return true if rw pointer is at the end of the stream
 * @return false if rw pointer is not at the end of the stream
 */
bool stream_eof(Stream* stream);

/**
 * Moves the rw pointer.
 * @param stream Stream instance
 * @param offset how much to move the pointer
 * @param offset_type starting from what
 * @return true 
 * @return false 
 */
bool stream_seek(Stream* stream, int32_t offset, StreamOffset offset_type);

/**
 * Gets the value of the rw pointer
 * @param stream Stream instance
 * @return size_t value of the rw pointer
 */
size_t stream_tell(Stream* stream);

/**
 * Gets the size of the stream
 * @param stream Stream instance
 * @return size_t size of the stream
 */
size_t stream_size(Stream* stream);

/**
 * Write N bytes to the stream
 * @param stream Stream instance
 * @param data data to write
 * @param size size of data to be written
 * @return size_t how many bytes was written
 */
size_t stream_write(Stream* stream, const uint8_t* data, size_t size);

/**
 * Read N bytes from stream
 * @param stream Stream instance
 * @param data data to be read
 * @param count size of data to be read
 * @return size_t how many bytes was read
 */
size_t stream_read(Stream* stream, uint8_t* data, size_t count);

/**
 * Delete N chars from the stream and write data by calling write_callback(context)
 * @param stream Stream instance
 * @param delete_size size of data to be deleted
 * @param write_callback write callback
 * @param context write callback context
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_delete_and_insert(
    Stream* stream,
    size_t delete_size,
    StreamWriteCB write_callback,
    const void* context);

/********************************** Some random helpers starts here **********************************/

/**
 * Read line from a stream (supports LF and CRLF line endings)
 * @param stream 
 * @param str_result 
 * @return true 
 * @return false 
 */
bool stream_read_line(Stream* stream, string_t str_result);

/**
 * Moves the rw pointer to the start
 * @param stream Stream instance
 */
bool stream_rewind(Stream* stream);

/**
 * Write char to the stream
 * @param stream Stream instance
 * @param c char value
 * @return size_t how many bytes was written
 */
size_t stream_write_char(Stream* stream, char c);

/**
 * Write string to the stream
 * @param stream Stream instance
 * @param string string value
 * @return size_t how many bytes was written
 */
size_t stream_write_string(Stream* stream, string_t string);

/**
 * Write const char* to the stream
 * @param stream Stream instance
 * @param string c-string value
 * @return size_t how many bytes was written
 */
size_t stream_write_cstring(Stream* stream, const char* string);

/**
 * Write formatted string to the stream
 * @param stream Stream instance
 * @param format 
 * @param ... 
 * @return size_t how many bytes was written
 */
size_t stream_write_format(Stream* stream, const char* format, ...);

/**
 * Write formatted string to the stream, va_list version
 * @param stream Stream instance
 * @param format 
 * @param args 
 * @return size_t how many bytes was written
 */
size_t stream_write_vaformat(Stream* stream, const char* format, va_list args);

/**
 * Insert N chars to the stream, starting at the current pointer.
 * Data will be inserted, not overwritteт, so the stream will be increased in size.
 * @param stream Stream instance
 * @param data data to be inserted
 * @param size size of data to be inserted
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_insert(Stream* stream, const uint8_t* data, size_t size);

/**
 * Insert char to the stream
 * @param stream Stream instance
 * @param c char value
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_insert_char(Stream* stream, char c);

/**
 * Insert string to the stream
 * @param stream Stream instance
 * @param string string value
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_insert_string(Stream* stream, string_t string);

/**
 * Insert const char* to the stream
 * @param stream Stream instance
 * @param string c-string value
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_insert_cstring(Stream* stream, const char* string);

/**
 * Insert formatted string to the stream
 * @param stream Stream instance
 * @param format 
 * @param ... 
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_insert_format(Stream* stream, const char* format, ...);

/**
 * Insert formatted string to the stream, va_list version
 * @param stream Stream instance
 * @param format 
 * @param args 
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_insert_vaformat(Stream* stream, const char* format, va_list args);

/**
 * Delete N chars from the stream and insert char to the stream
 * @param stream Stream instance
 * @param delete_size size of data to be deleted
 * @param c char value
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_delete_and_insert_char(Stream* stream, size_t delete_size, char c);

/**
 * Delete N chars from the stream and insert string to the stream
 * @param stream Stream instance
 * @param delete_size size of data to be deleted
 * @param string string value
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, string_t string);

/**
 * Delete N chars from the stream and insert const char* to the stream
 * @param stream Stream instance
 * @param delete_size size of data to be deleted
 * @param string c-string value
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_delete_and_insert_cstring(Stream* stream, size_t delete_size, const char* string);

/**
 * Delete N chars from the stream and insert formatted string to the stream
 * @param stream Stream instance
 * @param delete_size size of data to be deleted
 * @param format 
 * @param ... 
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_delete_and_insert_format(Stream* stream, size_t delete_size, const char* format, ...);

/**
 * Delete N chars from the stream and insert formatted string to the stream, va_list version
 * @param stream Stream instance
 * @param delete_size size of data to be deleted
 * @param format 
 * @param args 
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_delete_and_insert_vaformat(
    Stream* stream,
    size_t delete_size,
    const char* format,
    va_list args);

/**
 * Remove N chars from the stream, starting at the current pointer.
 * The size may be larger than stream size, the stream will be cleared from current rw pointer to the end.
 * @param stream Stream instance
 * @param size how many chars need to be deleted
 * @return true if the operation was successful
 * @return false on error
 */
bool stream_delete(Stream* stream, size_t size);

/**
 * Copy data from one stream to another. Data will be copied from current rw pointer and to current rw pointer.
 * @param stream_from 
 * @param stream_to 
 * @param size 
 * @return size_t 
 */
size_t stream_copy(Stream* stream_from, Stream* stream_to, size_t size);

/**
 * Copy data from one stream to another. Data will be copied from start of one stream and to start of other stream.
 * @param stream_from 
 * @param stream_to 
 * @return size_t 
 */
size_t stream_copy_full(Stream* stream_from, Stream* stream_to);

/**
 * Splits one stream into two others. The original stream will remain untouched.
 * @param stream 
 * @param stream_left 
 * @param stream_right 
 * @return true 
 * @return false 
 */
bool stream_split(Stream* stream, Stream* stream_left, Stream* stream_right);

/**
 * 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.
 * @param stream Stream instance 
 * @param storage 
 * @param path 
 * @return size_t 
 */
size_t stream_load_from_file(Stream* stream, Storage* storage, const char* path);

/**
 * 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.
 * @param stream Stream instance 
 * @param storage 
 * @param path 
 * @param mode 
 * @return size_t 
 */
size_t stream_save_to_file(Stream* stream, Storage* storage, const char* path, FS_OpenMode mode);

/**
 * Dump stream inner data (size, RW positiot, content)
 * @param stream Stream instance 
 */
void stream_dump_data(Stream* stream);

#ifdef __cplusplus
}
#endif