factorize integer parsing
[protos/libecoli.git] / include / ecoli_string.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #ifndef ECOLI_STRING_
6 #define ECOLI_STRING_
7
8 #include <stddef.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11
12 /* count the number of identical chars at the beginning of 2 strings */
13 size_t ec_strcmp_count(const char *s1, const char *s2);
14
15 /* return 1 if 's' starts with 'beginning' */
16 int ec_str_startswith(const char *s, const char *beginning);
17
18 /* like asprintf, but use libecoli allocator */
19 int ec_asprintf(char **buf, const char *fmt, ...);
20
21 /* like vasprintf, but use libecoli allocator */
22 int ec_vasprintf(char **buf, const char *fmt, va_list ap);
23
24 /* return true if string is only composed of spaces (' ', '\n', ...) */
25 bool ec_str_is_space(const char *s);
26
27 /**
28  * Parse a string for a signed integer.
29  *
30  * @param str
31  *   The string to parse.
32  * @param base
33  *   The base (0 means "guess").
34  * @param min
35  *   The minimum allowed value.
36  * @param max
37  *   The maximum allowed value.
38  * @param val
39  *   The pointer to the value to be set on success.
40  * @return
41  *   On success, return 0. Else, return -1 and set errno.
42  */
43 int ec_str_parse_llint(const char *str, unsigned int base, int64_t min,
44                 int64_t max, int64_t *val);
45
46 /**
47  * Parse a string for an unsigned integer.
48  *
49  * @param str
50  *   The string to parse.
51  * @param base
52  *   The base (0 means "guess").
53  * @param min
54  *   The minimum allowed value.
55  * @param max
56  *   The maximum allowed value.
57  * @param val
58  *   The pointer to the value to be set on success.
59  * @return
60  *   On success, return 0. Else, return -1 and set errno.
61  */
62 int ec_str_parse_ullint(const char *str, unsigned int base, uint64_t min,
63                         uint64_t max, uint64_t *val);
64 #endif