Sat, 22 Jun 2013 13:08:36 +0200
some fixes
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
71 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 | * | |
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
71 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include <stdlib.h> |
71 | 30 | #include <string.h> |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include <stdarg.h> |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | #include "string.h" |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | |
71 | 35 | sstr_t sstr(char *s) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | sstr_t string; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | string.ptr = s; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | string.length = strlen(s); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | return string; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | |
71 | 42 | sstr_t sstrn(char *s, size_t n) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | sstr_t string; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | string.ptr = s; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | string.length = n; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | return string; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | |
71 | 49 | size_t sstrnlen(size_t n, sstr_t s, ...) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | va_list ap; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | size_t size = s.length; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | va_start(ap, s); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | |
71 | 54 | for (size_t i = 0 ; i < n-1 ; i++) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | sstr_t str = va_arg(ap, sstr_t); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | size += str.length; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
58 | va_end(ap); |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | return size; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | |
71 | 63 | sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | va_list ap; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | va_start(ap, c1); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | s.ptr[0] = 0; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
67 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
68 | size_t len = s.length; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
69 | size_t cplen = c1.length > len ? len : c1.length; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
70 | char *ptr = s.ptr; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
71 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
72 | memcpy(ptr, c1.ptr, cplen); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
73 | len -= cplen; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
74 | ptr += cplen; |
71 | 75 | for (size_t i = 0 ; i < n-1 ; i++) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | sstr_t str = va_arg (ap, sstr_t); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
77 | cplen = str.length > len ? len : str.length; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
78 | if(cplen <= 0) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
79 | va_end(ap); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
80 | return s; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
81 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
82 | memcpy(ptr, str.ptr, cplen); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
83 | len -= cplen; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
84 | ptr += cplen; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
86 | va_end(ap); |
71 | 87 | s.length = ptr - s.ptr; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | return s; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | |
71 | 92 | sstr_t sstrsubs(sstr_t s, size_t start) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | return sstrsubsl (s, start, s.length-start); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | |
71 | 96 | sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | sstr_t new_sstr; |
70
4e6e812c1d97
fixed some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
98 | if (start >= s.length) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | return s; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | if (length > s.length-start) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | length = s.length-start; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | new_sstr.ptr = &s.ptr[start]; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | new_sstr.length = length; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | return new_sstr; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
109 | sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
110 | if (d.length == 0) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
111 | return NULL; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
112 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
113 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
114 | sstr_t* result; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
115 | size_t nmax = *n; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
116 | *n = 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
117 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
118 | /* special case: exact match - no processing needed */ |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
119 | if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) { |
71 | 120 | *n = 0; |
121 | return NULL; | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
122 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
123 | sstr_t sv = sstrdup(s); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
124 | |
71 | 125 | for (size_t i = 0 ; i < s.length ; i++) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
126 | if (sv.ptr[i] == d.ptr[0]) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
127 | _Bool match = 1; |
71 | 128 | for (size_t j = 1 ; j < d.length ; j++) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
129 | if (j+i < s.length) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
130 | match &= (sv.ptr[i+j] == d.ptr[j]); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
131 | } else { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
132 | match = 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
133 | break; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
134 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
135 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
136 | if (match) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
137 | (*n)++; |
71 | 138 | for (size_t j = 0 ; j < d.length ; j++) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
139 | sv.ptr[i+j] = 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
140 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
141 | i += d.length; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
142 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
143 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
144 | if ((*n) == nmax) break; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
145 | } |
71 | 146 | result = (sstr_t*) malloc(sizeof(sstr_t) * (*n)); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
147 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
148 | char *pptr = sv.ptr; |
71 | 149 | for (size_t i = 0 ; i < *n ; i++) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
150 | size_t l = strlen(pptr); |
71 | 151 | char* ptr = (char*) malloc(l + 1); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
152 | memcpy(ptr, pptr, l); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
153 | ptr[l] = 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
154 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
155 | result[i] = sstrn(ptr, l); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
156 | pptr += l + d.length; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
157 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
158 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
159 | free(sv.ptr); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
160 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
161 | return result; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
164 | int sstrcmp(sstr_t s1, sstr_t s2) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
165 | return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length); |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | } |
16
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
167 | |
28 | 168 | sstr_t sstrdup(sstr_t s) { |
169 | sstr_t newstring; | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
170 | newstring.ptr = (char*) malloc(s.length + 1); |
71 | 171 | newstring.length = 0; |
172 | if (newstring.ptr) { | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
173 | newstring.length = s.length; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
174 | newstring.ptr[newstring.length] = 0; |
28 | 175 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
176 | memcpy(newstring.ptr, s.ptr, s.length); |
71 | 177 | } else { |
178 | newstring.length = 0; | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
179 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
180 | |
28 | 181 | return newstring; |
182 | } | |
16
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
183 | |
71 | 184 | sstr_t sstrtrim(sstr_t string) { |
185 | sstr_t newstr = string; | |
186 | if (string.length == 0) { | |
187 | return newstr; | |
188 | } | |
189 | ||
190 | size_t i; | |
191 | for(i=0;i<string.length;i++) { | |
192 | char c = string.ptr[i]; | |
193 | if(c > 32) { | |
194 | break; | |
195 | } | |
196 | } | |
197 | newstr.ptr = &string.ptr[i]; | |
198 | newstr.length = string.length - i; | |
199 | ||
200 | if(newstr.length == 0) { | |
201 | return newstr; | |
202 | } | |
203 | ||
204 | i = newstr.length - 1; | |
205 | for(;;) { | |
206 | char c = newstr.ptr[i]; | |
207 | if(c > 32) { | |
208 | break; | |
209 | } | |
210 | if(i > 0) { | |
211 | i--; | |
212 | } else { | |
213 | break; | |
214 | } | |
215 | } | |
216 | newstr.length = i + 1; | |
217 | ||
218 | return newstr; | |
219 | } | |
220 | ||
221 | ||
16
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
222 | // webserver extension |
51 | 223 | |
224 | int sstr_startswith(sstr_t string, sstr_t cmp) { | |
225 | sstr_t sub = sstrsubsl(string, 0, cmp.length); | |
226 | if(!sstrcmp(sub, cmp)) { | |
227 | return 1; | |
228 | } else { | |
229 | return 0; | |
230 | } | |
231 | } | |
232 | ||
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
233 | sstr_t sstrdup_mp(UcxMempool *pool, sstr_t s) { |
16
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
234 | sstr_t newstring; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
235 | newstring.ptr = (char*)ucx_mempool_malloc(pool, s.length + 1); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
236 | if (newstring.ptr != NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
237 | newstring.length = s.length; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
238 | newstring.ptr[newstring.length] = 0; |
16
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
239 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
240 | memcpy(newstring.ptr, s.ptr, s.length); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
241 | } |
16
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
242 | |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
243 | return newstring; |
a9bbd82d2dce
New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
244 | } |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
28
diff
changeset
|
245 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
246 | sstr_t sstrdup_pool(pool_handle_t *pool, sstr_t s) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
247 | sstr_t newstring; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
248 | newstring.ptr = (char*)pool_malloc(pool, s.length + 1); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
249 | if (newstring.ptr != NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
250 | newstring.length = s.length; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
251 | newstring.ptr[newstring.length] = 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
252 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
253 | memcpy(newstring.ptr, s.ptr, s.length); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
254 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
255 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
256 | return newstring; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
257 | } |