-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
std.h
359 lines (333 loc) · 10.9 KB
/
std.h
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#ifndef __TEXT_STD_H__
#define __TEXT_STD_H__
#include "simba.h"
#include <stdarg.h>
/**
* Initialize the std module. This function must be called before
* calling any other function in this module.
*
* The module will only be initialized once even if this function is
* called multiple times.
*
* @return zero(0) or negative error code.
*/
int std_module_init(void);
/**
* Format and write data to destination buffer. The buffer must be big
* enough to fit the formatted string. The output is null terminated.
*
* A format specifier has this format:
*
* %[flags][width][length]specifier
*
* where
*
* * flags: ``0`` or ``-``
* * width: ``0``..``127``
* * length: ``l`` for long or nothing
* * specifier: ``c``, ``s``, ``S``, ``d``, ``i``, ``u``, ``x`` or ``f``
*
* The ``S`` specifier expects a far string (``far_string_t``)
* argument. Other specifiers have their usual definition.
*
* @param[out] dst_p Destination buffer. The formatted string is
* written to this buffer.
* @param[in] fmt_p Format string.
* @param[in] ... Variable arguments list.
*
* @return Length of the string written to the destination buffer, not
* including the null termination, or negative error code.
*/
ssize_t std_sprintf(char *dst_p, far_string_t fmt_p, ...);
/**
* Format and write data to given buffer. The output is null
* terminated.
*
* @param[out] dst_p Destination buffer. The formatted string is
* written to this buffer.
* @param[in] size Size of the destination buffer.
* @param[in] fmt_p Format string.
* @param[in] ... Variable arguments list.
*
* @return Length of the string written to the destination buffer, not
* including the null termination, -ENOMEM if the string does
* not fit in the destination buffer, or other negative error
* code.
*/
ssize_t std_snprintf(char *dst_p,
size_t size,
far_string_t fmt_p,
...);
/**
* Format and write data to given buffer. The output is null
* terminated.
*
* @param[out] dst_p Destination buffer. The formatted string is
* written to this buffer.
* @param[in] fmt_p Format string.
* @param[in] ap_p Variable arguments list.
*
* @return Length of the string written to the destination buffer, not
* including the null termination, or negative error code.
*/
ssize_t std_vsprintf(char *dst_p, far_string_t fmt_p, va_list *ap_p);
/**
* Format and write data to given buffer. The output is null
* terminated.
*
* @param[out] dst_p Destination buffer. The formatted string is
* written to this buffer.
* @param[in] size Size of the destination buffer.
* @param[in] fmt_p Format string.
* @param[in] ap_p Variable arguments list.
*
* @return Length of the string written to the destination buffer, not
* including the null termination, -ENOMEM if the string does
* not fit in the destination buffer, or other negative error
* code.
*/
ssize_t std_vsnprintf(char *dst_p, size_t size, far_string_t fmt_p, va_list *ap_p);
/**
* Format and print data to standard output. The output is not null
* terminated.
*
* See `std_sprintf()` for the the format string specification.
*
* @param[in] fmt_p Format string.
* @param[in] ... Variable arguments list.
*
* @return Number of characters written to standard output, or
* negative error code.
*/
ssize_t std_printf(far_string_t fmt_p, ...);
/**
* Format and print data to standard output. The output is not null
* terminated.
*
* See `std_sprintf()` for the the format string specification.
*
* @param[in] fmt_p Format string.
* @param[in] ap_p Variable arguments list.
*
* @return Number of characters written to standard output, or
* negative error code.
*/
ssize_t std_vprintf(far_string_t fmt_p, va_list *ap_p);
/**
* Format and print data to given channel. The output is not null
* terminated.
*
* See `std_sprintf()` for the the format string specification.
*
* @param[in] chan_p Output channel.
* @param[in] fmt_p Format string.
* @param[in] ... Variable arguments list.
*
* @return Number of characters written to given channel, or negative
* error code.
*/
ssize_t std_fprintf(void *chan_p, far_string_t fmt_p, ...);
/**
* Format and print data to given channel. The output is not null
* terminated.
*
* See `std_sprintf()` for the the format string specification.
*
* @param[in] chan_p Output channel.
* @param[in] fmt_p Format string.
* @param[in] ... Variable arguments list.
*
* @return Number of characters written to given channel, or negative
* error code.
*/
ssize_t std_vfprintf(void *chan_p, far_string_t fmt_p, va_list *ap_p);
/**
* Format and print data to standard output from interrupt context or
* with the system lock taken. The output is not null terminated.
*
* See `std_sprintf()` for the the format string specification.
*
* @param[in] fmt_p Format string.
* @param[in] ... Variable arguments list.
*
* @return Number of characters written to standard output, or
* negative error code.
*/
ssize_t std_printf_isr(far_string_t fmt_p, ...);
/**
* Format and print data to given channel from interrupt context or
* with the system lock taken. The output is not null terminated.
*
* See `std_sprintf()` for the the format string specification.
*
* @param[in] chan_p Output channel.
* @param[in] fmt_p Format string.
* @param[in] ... Variable arguments list.
*
* @return Number of characters written to given channel, or negative
* error code.
*/
ssize_t std_fprintf_isr(void *chan_p, far_string_t fmt_p, ...);
/**
* Convert given string to an integer in given base.
*
* @param[in] str_p Integer string.
* @param[out] value_p Parsed integer.
* @param[in] base The base of the value to parse. One of 16, 10, 8 or
* 2, or 0 to select base based on the string
* prefix. Supported string prefixes are "0x" for
* hexadecimal numbers, "0" for octal numbers and "0b"
* for binary numbers.
*
* @return Pointer to the next byte, or NULL if no value was found.
*/
const char *std_strtolb(const char *str_p,
long *value_p,
int base);
/**
* Convert given string to an integer with base selection based on the
* string prefix.
*
* @param[in] str_p Integer string.
* @param[out] value_p Parsed integer.
*
* @return Pointer to the next byte, or NULL if no value was found.
*/
const char *std_strtol(const char *str_p, long *value_p);
/**
* Convert given string to a double.
*
* @param[in] str_p Double string.
* @param[out] value_p Parsed value.
*
* @return Pointer to the next byte, or NULL if no value vas found.
*/
const char *std_strtod(const char *str_p, double *value_p);
/**
* Convert string to decimal fixed point number with given precision.
*
* @param[in] str_p Double string.
* @param[out] value_p Decimal fixed point number of given precision.
* @param[in] precision Number precision, or decimal places.
*
* @return Pointer to the next byte, or NULL on failure.
*/
const char *std_strtodfp(const char *str_p,
long *value_p,
int precision);
/**
* Copy string from far memory to memory.
*
* @param[in] dst_p Normal memory string.
* @param[in] src_p Far memory string.
*
* @return String length or negative error code.
*/
int std_strcpy(char *dst_p, far_string_t src_p);
/**
* Compare a string with a far string.
*
* @param[in] str_p Normal memory string.
* @param[in] fstr_p Far memory string.
*
* @return zero(0) if match, otherwise the difference of
* the mismatched characters
*/
int std_strcmp(const char *str_p, far_string_t fstr_p);
/**
* Compare two far strings.
*
* @param[in] fstr0_p Far memory string.
* @param[in] fstr1_p Far memory string.
*
* @return zero(0) if match, otherwise the difference of the
* mismatched characters.
*/
int std_strcmp_f(far_string_t fstr0_p,
far_string_t fstr1_p);
/**
* Compare at most `size` bytes of one far string and one string.
*
* @param[in] fstr_p Far memory string.
* @param[in] str_p String.
* @param[in] size Compare at most size number of bytes.
*
* @return zero(0) if match, otherwise the difference of the
* mismatched characters.
*/
int std_strncmp(far_string_t fstr_p,
const char *str_p,
size_t size);
/**
* Compare at most `size` bytes of two far strings.
*
* @param[in] fstr0_p Far memory string.
* @param[in] fstr1_p Far memory string.
* @param[in] size Compare at most size number of bytes.
*
* @return zero(0) if match, otherwise the difference of the
* mismatched characters.
*/
int std_strncmp_f(far_string_t fstr0_p,
far_string_t fstr1_p,
size_t size);
/**
* Get the length in bytes of given far string, not including null
* termination.
*
* @param[in] fstr_p Far memory string.
*
* @return String length in number of bytes (not including the null
* termination).
*/
int std_strlen(far_string_t fstr_p);
/**
* Strip leading and trailing characters from a string. The characters
* to strip are given by `strip_p`.
*
* @param[in] str_p String to strip characters from.
* @param[in] strip_p Characters to strip or NULL for whitespace
* characters. Must be null-terminated.
*
* @return Pointer to the stripped string.
*/
char *std_strip(char *str_p, const char *strip_p);
/**
* Write a hex dump of given data to given channel.
*
* @param[in] chan_p Channel to write the hexdump to.
* @param[in] buf_p Buffer to dump.
* @param[in] size Size of buffer.
*
* @return Number of characters written to given channel, or negative
* error code.
*/
ssize_t std_hexdump(void *chan_p, const void *buf_p, size_t size);
#endif