libite
strlite.h
Go to the documentation of this file.
1 /* Collection of frog DNA
2  *
3  * Copyright (c) 2008-2021 Joachim Wiberg <troglobit@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  */
23 
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #endif
35 
36 #ifndef LIBITE_STRING_H_
37 #define LIBITE_STRING_H_
38 
39 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, INT32_MAX, etc. */
40 #include <string.h>
41 #include <sys/param.h> /* MAX(), isset(), setbit(), TRUE, FALSE, et consortes. :-) */
42 
43 #include "strdupa.h"
44 #include "strndupa.h"
45 #include "strnlen.h"
46 
47 #ifndef min
48 
49 #define min(a,b) \
50  ({ \
51  __typeof__ (a) _a = (a); \
52  __typeof__ (b) _b = (b); \
53  _a < _b ? _a : _b; \
54  })
55 #endif
56 #ifndef max
57 
58 #define max(a,b) \
59  ({ \
60  __typeof__ (a) _a = (a); \
61  __typeof__ (b) _b = (b); \
62  _a > _b ? _a : _b; \
63  })
64 #endif
65 
66 int strnmatch (const char *str, const char **list, size_t num);
67 int strmatch (const char *str, const char **list);
68 
69 #ifndef strlcpy
70 size_t strlcpy (char *dst, const char *src, size_t siz);
71 #endif
72 #ifndef strlcat
73 size_t strlcat (char *dst, const char *src, size_t siz);
74 #endif
75 #ifndef strtonum
76 long long strtonum (const char *numstr, long long minval, long long maxval, const char **errstrp);
77 #endif
78 
79 char *strtrim (char *str);
80 
86 static inline int atonum(const char *str)
87 {
88  int val = -1;
89  const char *errstr;
90 
91  if (str) {
92  val = strtonum(str, 0, INT32_MAX, &errstr);
93  if (errstr)
94  return -1;
95  }
96 
97  return val;
98 }
99 
105 static inline int string_valid(const char *str)
106 {
107  return str && strlen(str);
108 }
109 
116 static inline int string_match(const char *a, const char *b)
117 {
118  size_t min = MIN(strlen(a), strlen(b));
119 
120  return !strncasecmp(a, b, min);
121 }
122 
129 static inline int string_compare(const char *a, const char *b)
130 {
131  return strlen(a) == strlen(b) && !strcmp(a, b);
132 }
133 
141 static inline int string_case_compare(const char *a, const char *b)
142 {
143  return strlen(a) == strlen(b) && !strcasecmp(a, b);
144 }
145 
146 #endif /* LIBITE_STRING_H_ */
147 
148 #ifdef __cplusplus
149 }
150 #endif
int strmatch(const char *str, const char **list)
Finds matching strings from a list.
Definition: strmatch.c:74
char * strtrim(char *str)
Strip leading and trailing whitespace from a string.
Definition: strtrim.c:44
#define min(a, b)
Geneirc min() macro, if a < b => a, else b.
Definition: strlite.h:49
long long strtonum(const char *numstr, long long minval, long long maxval, const char **errstrp)
Reliably convert string value to an integer.
Definition: strtonum.c:75
size_t strlcpy(char *dst, const char *src, size_t siz)
Safe version of strncpy() from OpenBSD.
Definition: strlcpy.c:44
size_t strlcat(char *dst, const char *src, size_t siz)
Safe version of strncat() from OpenBSD.
Definition: strlcat.c:45
int strnmatch(const char *str, const char **list, size_t num)
Finds matching strings from a finite list.
Definition: strmatch.c:43