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
360
361
362
363
|
/* See LICENSE file for copyright and license details. */
/**
* Calculates the sum of two `struct timespec`s
*
* @param sum Output parameter for the result
* @param augend One of the terms
* @param addend The other term
* @return 0 on success, -1 on failure
* @throws ERANGE The result is too large or too small to be store; the result
* will be `{.tv_sec = TIME_MAX, .tv_nsec = 999999999L}` if the
* result too large and `{.tv_sec = TIME_MIN, .tv_nsec = 0L}`
* if the result too small
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_sumtimespec(struct timespec *, const struct timespec *, const struct timespec *);
#ifndef sumtimespec
# define sumtimespec libsimple_sumtimespec
#endif
/**
* Calculates the difference of two `struct timespec`s
*
* @param diff Output parameter for the result
* @param minuend The left-hand term
* @param subtrahend The right-hand term
* @return 0 on success, -1 on failure
* @throws ERANGE The result is too large or too small to be store; the result
* will be `{.tv_sec = TIME_MAX, .tv_nsec = 999999999L}` if the
* result too large and `{.tv_sec = TIME_MIN, .tv_nsec = 0L}`
* if the result too small
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_difftimespec(struct timespec *, const struct timespec *, const struct timespec *);
#ifndef difftimespec
# define difftimespec libsimple_difftimespec
#endif
/**
* Calculates the product of a `struct timespec` and an integer
*
* @param prod Output parameter for the result
* @param multiplicand One of the factors
* @param multiplier The other factor
* @return 0 on success, -1 on failure
* @throws ERANGE The result is too large or too small to be store; the result
* will be `{.tv_sec = TIME_MAX, .tv_nsec = 999999999L}` if the
* result too large and `{.tv_sec = TIME_MIN, .tv_nsec = 0L}`
* if the result too small
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_multimespec(struct timespec *, const struct timespec *, int);
#ifndef multimespec
# define multimespec libsimple_multimespec
#endif
/**
* Compares two `struct timespec`s
*
* @param a One of the `struct timespec`s
* @param b The other `struct timespec`
* @return -1 if `a` < `b`, 0 if `a` = `b`, and +1 if `a` > `b`
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __warn_unused_result__)))
static inline int
libsimple_cmptimespec(const struct timespec *__a, const struct timespec *__b)
{
if (__a->tv_sec != __b->tv_sec)
return __a->tv_sec < __b->tv_sec ? -1 : +1;
return __a->tv_nsec < __b->tv_nsec ? -1 : __a->tv_nsec > __b->tv_nsec;
}
#ifndef cmptimespec
# define cmptimespec libsimple_cmptimespec
#endif
/**
* Calculates the sum of two `struct timeval`s
*
* @param sum Output parameter for the result
* @param augend One of the terms
* @param addend The other term
* @return 0 on success, -1 on failure
* @throws ERANGE The result is too large or too small to be store; the result
* will be `{.tv_sec = TIME_MAX, .tv_usec = 999999L}` if the
* result too large and `{.tv_sec = TIME_MIN, .tv_usec = 0L}`
* if the result too small
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_sumtimeval(struct timeval *, const struct timeval *, const struct timeval *);
#ifndef sumtimeval
# define sumtimeval libsimple_sumtimeval
#endif
/**
* Calculates the difference of two `struct timeval`s
*
* @param diff Output parameter for the result
* @param minuend The left-hand term
* @param subtrahend The right-hand term
* @return 0 on success, -1 on failure
* @throws ERANGE The result is too large or too small to be store; the result
* will be `{.tv_sec = TIME_MAX, .tv_usec = 999999L}` if the
* result too large and `{.tv_sec = TIME_MIN, .tv_usec = 0L}`
* if the result too small
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_difftimeval(struct timeval *, const struct timeval *, const struct timeval *);
#ifndef difftimeval
# define difftimeval libsimple_difftimeval
#endif
/**
* Calculates the product of a `struct timeval` and an integer
*
* @param prod Output parameter for the result
* @param multiplicand One of the factors
* @param multiplier The other factor
* @return 0 on success, -1 on failure
* @throws ERANGE The result is too large or too small to be store; the result
* will be `{.tv_sec = TIME_MAX, .tv_usec = 999999L}` if the
* result too large and `{.tv_sec = TIME_MIN, .tv_usec = 0L}`
* if the result too small
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_multimeval(struct timeval *, const struct timeval *, int);
#ifndef multimeval
# define multimeval libsimple_multimeval
#endif
/**
* Compares two `struct timeval`s
*
* @param a One of the `struct timeval`s
* @param b The other `struct timeval`
* @return -1 if `a` < `b`, 0 if `a` = `b`, and +1 if `a` > `b`
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __warn_unused_result__)))
static inline int
libsimple_cmptimeval(const struct timeval *__a, const struct timeval *__b)
{
if (__a->tv_sec != __b->tv_sec)
return __a->tv_sec < __b->tv_sec ? -1 : +1;
return __a->tv_usec < __b->tv_usec ? -1 : __a->tv_usec > __b->tv_usec;
}
#ifndef cmptimeval
# define cmptimeval libsimple_cmptimeval
#endif
/**
* Converts a `struct timeval` to a `struct timespec`
*
* @param ts Output parameter for the result
* @param tv The value to convert
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
static inline void
libsimple_timeval2timespec(struct timespec *restrict __ts, const struct timeval *restrict __tv)
{
__ts->tv_sec = __tv->tv_sec;
__ts->tv_nsec = __tv->tv_usec;
__ts->tv_nsec *= 1000L;
}
#ifndef timeval2timespec
# define timeval2timespec libsimple_timeval2timespec
#endif
/**
* Converts a `struct timespec` to a `struct timeval`
*
* @param tv The value to convert
* @param ts Output parameter for the result
* @return 0 on success, -1 on error
* @throws EOVERFLOW The value is too large to be convert, the value will be
* truncated to `{.tv_sec = TIME_MAX, .tv_usec = 999999L}`
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
int libsimple_timespec2timeval(struct timeval *restrict, const struct timespec *restrict);
#ifndef timespec2timeval
# define timespec2timeval libsimple_timespec2timeval
#endif
/**
* Converts a string to `struct timespec`
*
* Repeating decimal are supported ith the repetend inside
* round brackets or after an additional point, for example
* "0.1(6)" or "0.1.6" for one sixth
*
* @param ts Output parameter for the result
* @param s The string to parse
* @param end Output parameter for where the parsing stopped;
* will only be set on success or on ERANGE error
* @return 0 on success, -1 on error
* @throws EINVAL The input string is invalid (`*end` is not set)
* @throws ERANGE The result is too large or too small to be store; the result
* will be `{.tv_sec = TIME_MAX, .tv_nsec = 999999999L}` if the
* result too large and `{.tv_sec = TIME_MIN, .tv_nsec = 0L}`
* if the result too small (`*end` is set)
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1, 2))))
int libsimple_strtotimespec(struct timespec *restrict, const char *restrict, char **restrict);
#ifndef strtotimespec
# define strtotimespec libsimple_strtotimespec
#endif
/**
* Converts a string to `struct timeval`
*
* Repeating decimal are supported ith the repetend inside
* round brackets or after an additional point, for example
* "0.1(6)" or "0.1.6" for one sixth
*
* @param tv Output parameter for the result
* @param s The string to parse
* @param end Output parameter for where the parsing stopped;
* will only be set on success or on ERANGE error
* @return 0 on success, -1 on error
* @throws EINVAL The input string is invalid (`*end` is not set)
* @throws ERANGE The result is too large or too small to be store; the result
* will be `{.tv_sec = TIME_MAX, .tv_usec = 999999L}` if the
* result too large and `{.tv_sec = TIME_MIN, .tv_usec = 0L}`
* if the result too small (`*end` is set)
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1, 2))))
int libsimple_strtotimeval(struct timeval *restrict, const char *restrict, char **restrict);
#ifndef strtotimeval
# define strtotimeval libsimple_strtotimeval
#endif
/**
* Converts a `struct timespec` to string
*
* The output will always have a sign ("+" or "-") and 9 decimals.
*
* @param buf Output buffer to use, if `NULL` a new buffer will be allocated
* @param ts The value to convert
* @return The result (same pointer as `buf` unless `buf == NULL`),
* `NULL` on failure
* @throws ENOMEM Output buffer could not be allocated (only if `buf == NULL`)
* @throws EINVAL `ts->tv_nsec` is negative or greater than 999999999
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2))))
char *libsimple_timespectostr(char *restrict, const struct timespec *restrict);
#ifndef timespectostr
# define timespectostr libsimple_timespectostr
#endif
/**
* Converts a `struct timeval` to string
*
* The output will always have a sign ("+" or "-") and 6 decimals.
*
* @param buf Output buffer to use, if `NULL` a new buffer will be allocated
* @param tv The value to convert
* @return The result (same pointer as `buf` unless `buf == NULL`),
* `NULL` on failure
* @throws ENOMEM Output buffer could not be allocated (only if `buf == NULL`)
* @throws EINVAL `ts->tv_usec` is negative or greater than 999999
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2))))
char *libsimple_timevaltostr(char *restrict, const struct timeval *restrict);
#ifndef timevaltostr
# define timevaltostr libsimple_timevaltostr
#endif
/**
* Converts a `struct timespec` to `double`
*
* @param ts The value to convert
* @return The result
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
static inline double
libsimple_timespectodouble(const struct timespec *__ts)
{
double __ret = (double)(__ts->tv_nsec);
__ret /= (double)1000000000L;
__ret += (double)(__ts->tv_sec);
return __ret;
}
#ifndef timespectodouble
# define timespectodouble libsimple_timespectodouble
#endif
/**
* Converts a `struct timeval` to `double`
*
* @param tv The value to convert
* @return The result
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
static inline double
libsimple_timevaltodouble(const struct timeval *__tv)
{
double __ret = (double)(__tv->tv_usec);
__ret /= (double)1000000L;
__ret += (double)(__tv->tv_sec);
return __ret;
}
#ifndef timevaltodouble
# define timevaltodouble libsimple_timevaltodouble
#endif
/**
* Converts a double to `struct timespec`
*
* No overflow check is made
*
* @param ts Output parameter for the result
* @param d The value to convert
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
void libsimple_doubletotimespec(struct timespec *, double);
#ifndef doubletotimespec
# define doubletotimespec libsimple_doubletotimespec
#endif
/**
* Converts a double to `struct timeval`
*
* No overflow check is made
*
* @param tv Output parameter for the result
* @param d The value to convert
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__)))
void libsimple_doubletotimeval(struct timeval *, double);
#ifndef doubletotimeval
# define doubletotimeval libsimple_doubletotimeval
#endif
/**
* Minimises a numerical string
*
* This function does not support repeating decimals
*
* @param s The string to minimise
* @return `s`
*/
_LIBSIMPLE_GCC_ONLY(__attribute__((__returns_nonnull__, __nonnull__)))
char *libsimple_minimise_number_string(char *);
#ifndef minimise_number_string
# define minimise_number_string libsimple_minimise_number_string
#endif
|