forked from zorxx/microhttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.c
More file actions
116 lines (94 loc) · 2.47 KB
/
helpers.c
File metadata and controls
116 lines (94 loc) · 2.47 KB
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
/*! \copyright 2018 Zorxx Software. All rights reserved.
* \license This file is released under the MIT License. See the LICENSE file for details.
* \file helpers.c
* \brief microhttpd helpers
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "debug.h"
#include "helpers.h"
char *lower(char* s)
{
char *tmp;
for(tmp = s; *tmp != '\0'; ++tmp)
*tmp = tolower((unsigned char) *tmp);
return s;
}
char *string_find(char *string, uint32_t string_length, char *delimiter,
uint32_t delimiter_length)
{
char *cur = string, *end = string + string_length;
uint32_t match = 0;
while(cur < end)
{
if(*cur == delimiter[match])
++match;
if(match >= delimiter_length)
return cur - (match - 1);
++cur;
}
return NULL;
}
void string_shift(char *string, uint32_t shift, uint32_t length)
{
uint32_t offset;
ASSERT(length >= shift);
for(offset = 0; offset < (length - shift); ++offset)
{
if((offset + shift) >= length)
string[offset] = '\0';
else
string[offset] = string[offset + shift];
}
}
char *string_chop(char **string, uint32_t *string_length, char *delimiter,
uint32_t delimiter_length)
{
char *start = *string;
uint32_t offset = 0;
ASSERT(string_length > 0);
ASSERT(delimiter_length > 0);
ASSERT(*string != NULL);
ASSERT(delimiter != NULL);
while(offset < delimiter_length && *string_length > 0)
{
if (**string == delimiter[offset])
++offset;
(*string)++;
--(*string_length);
}
if(*string_length > 0)
{
*((*string) - delimiter_length) = '\0';
return start;
}
*string = start;
return NULL;
}
bool string_list_add(char *string, uint32_t string_length, char ***string_list, uint32_t *list_size)
{
uint32_t idx = *list_size;
*string_list = realloc(*string_list, (idx + 1) * sizeof(char *));
if(NULL == *string_list)
return false;
(*string_list)[idx] = malloc(string_length + 1);
if((*string_list)[idx] == NULL)
return false;
memcpy((*string_list)[idx], string, string_length);
((*string_list)[idx])[string_length] = '\0';
++(*list_size);
return true;
}
void string_list_clear(char ***string_list, uint32_t *list_size)
{
uint32_t idx;
if(*string_list == NULL)
return;
for(idx = 0; idx < *list_size; ++idx)
free((*string_list)[idx]);
free(*string_list);
*string_list = NULL;
*list_size = 0;
}