-
Notifications
You must be signed in to change notification settings - Fork 6
/
strutils.c
349 lines (279 loc) · 8.3 KB
/
strutils.c
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
/*
strutils.c - a collection of useful string utilities
Copyright (c) 2019-2022 Terje Io
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "strutils.h"
#define CAPS(c) ((c >= 'a' && c <= 'z') ? c & 0x5F : c)
/*! \brief Case insensitive search for first occurrence of a string inside another.
\param s1 pointer to string to search.
\param s2 pointer to string to search for.
\returns pointer to found string or NULL if not found.
*/
char *stristr (const char *s1, const char *s2)
{
const char *s = s1, *p = s2, *r = NULL;
if (!s2 || strlen(s2) == 0)
return (char *)s1;
while(*s && *p) {
if(CAPS(*p) == CAPS(*s)) {
if(!r)
r = s;
p++;
} else {
p = s2;
if(r)
s = r + 1;
if(CAPS(*p) == CAPS(*s)) {
r = s;
p++;
} else
r = NULL;
}
s++;
}
return *p ? NULL : (char *)r;
}
/*! \brief Case insensitive search for first occurrence of a string inside another.
\param s1 pointer to string to search.
\param s2 pointer to string to search for.
\param len max. number of characters to compare.
\returns pointer to found string or NULL if not found.
*/
char *strnistr (const char *s1, const char *s2, size_t len)
{
size_t slen = len;
const char *s = s1, *p = s2, *r = NULL;
if (!s2 || strlen(s2) == 0 || len == 0)
return (char *)s1;
while(slen && *s && *p) {
if(CAPS(*p) == CAPS(*s)) {
if(!r)
r = s;
p++;
} else {
p = s2;
slen = len;
if(r)
s = r + 1;
if(CAPS(*p) == CAPS(*s)) {
r = s;
p++;
} else
r = NULL;
}
s++;
if(r)
slen--;
}
return slen == 0 || *p == '\0' ? (char *)r : NULL;
}
// NOTE: ensure buf is large enough to hold concatenated strings!
char *strappend (char *buf, int argc, ...)
{
char c, *s = buf, *arg;
va_list list;
va_start(list, argc);
while(argc--) {
arg = va_arg(list, char *);
do {
c = *s++ = *arg++;
} while(c);
s--;
}
va_end(list);
return buf;
}
uint32_t strnumentries (const char *s, const char delimiter)
{
char *p = (char *)s;
uint32_t entries = *s ? 1 : 0;
while(entries && (p = strchr(p, delimiter))) {
p++;
entries++;
}
return entries;
}
char *strgetentry (char *res, const char *s, uint32_t entry, const char delimiter)
{
char *e, *p = (char *)s;
*res = '\0';
e = strchr(p, delimiter);
if(entry == 0 && e == NULL)
strcpy(res, s);
else do {
if(entry) {
p = e;
e = strchr(++p, delimiter);
entry--;
} else {
if(e) {
strncpy(res, p, e - p);
res[e - p] = '\0';
} else
strcpy(res, p);
break;
}
} while(true);
return res;
}
int32_t strlookup (const char *s1, const char *s2, const char delimiter)
{
bool found = false;
char *e, *p = (char *)s2;
uint32_t idx = strnumentries(s2, delimiter), len = strlen(s1);
int32_t entry = 0;
while(idx--) {
if((e = strchr(p, delimiter)))
found = (e - p) == len && !strncmp(p, s1, e - p);
else
found = strlen(p) == len && !strcmp(p, s1);
if(found || e == NULL)
break;
else {
p = e + 1;
entry++;
}
}
return found ? entry : -1;
}
bool strtotime (char *s, struct tm *time)
{
char c, *s1 = s;
uint_fast16_t idx = 0;
if(s == NULL || time == NULL)
return false;
while((c = *s1)) {
if(c == ':')
*s1 = ' ';
s1++;
}
if(strchr(s, ',')) { // "text format"
s1 = strtok(s + 5, " ");
while(s1) {
switch(idx) {
case 0:
time->tm_mday = atoi(s1);
break;
case 1:
time->tm_mon = strlookup(s1, "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", ',');
break;
case 2:
time->tm_year = atoi(s1) - 1900;
break;
case 3:
time->tm_hour = atoi(s1);
break;
case 4:
time->tm_min = atoi(s1);
break;
case 5:
time->tm_sec = atoi(s1);
break;
}
idx++;
s1 = strtok(NULL, " ");
}
} else { // ISO8601 format
s1 = s;
while((c = *s1)) {
if(c == '-' || c == 'T')
*s1 = ' ';
else if(c == 'Z')
*s1 = '\0';
s1++;
}
s1 = strtok(s, " ");
while(s1) {
switch(idx) {
case 0:
time->tm_year = atoi(s1) - 1900;
break;
case 1:
time->tm_mon = atoi(s1) - 1;
break;
case 2:
time->tm_mday = atoi(s1);
break;
case 3:
time->tm_hour = atoi(s1);
break;
case 4:
time->tm_min = atoi(s1);
break;
case 5:
time->tm_sec = atoi(s1);
break;
}
idx++;
s1 = strtok(NULL, " ");
}
}
return idx >= 5;
}
char *strtoisodt (struct tm *dt)
{
static char buf[21];
if(dt == NULL || snprintf(buf, sizeof(buf), "%04i-%02i-%02iT%02i:%02i:%02i", dt->tm_year < 1000 ? dt->tm_year + 1900 : dt->tm_year, dt->tm_mon + 1, dt->tm_mday, dt->tm_hour, dt->tm_min, dt->tm_sec) > sizeof(buf))
*buf = '\0';
return buf;
}
char *strtointernetdt (struct tm *dt)
{
static const char *month_table[12] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
static const char *day_table[7] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
};
static char buf[31];
#ifdef __MSP432E401Y__
return "Thu, 01 Jan 1970 00:00:00 GMT"; // snprintf below crashes the MCU!
#else
if(dt == NULL || snprintf(buf, sizeof(buf), "%s, %02d %s %04d %02d:%02d:%02d GMT", day_table[dt->tm_wday], dt->tm_mday, month_table[dt->tm_mon], dt->tm_year < 1000 ? dt->tm_year + 1900 : dt->tm_year, dt->tm_hour, dt->tm_min, dt->tm_sec) > sizeof(buf))
return "Thu, 01 Jan 1970 00:00:00 GMT";
return buf;
#endif
}