src/server/ucx/string.c

Fri, 21 Jun 2013 12:10:44 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 21 Jun 2013 12:10:44 +0200
changeset 71
069c152f6272
parent 70
4e6e812c1d97
child 88
73b3485e96f1
permissions
-rw-r--r--

ucx update

15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
3 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
5 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
6 * Redistribution and use in source and binary forms, with or without
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
7 * modification, are permitted provided that the following conditions are met:
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
8 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
14 * documentation and/or other materials provided with the distribution.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
15 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
120 *n = 0;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
168 sstr_t sstrdup(sstr_t s) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
171 newstring.length = 0;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
177 } else {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
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
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
181 return newstring;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
182 }
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
183
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
184 sstr_t sstrtrim(sstr_t string) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
185 sstr_t newstr = string;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
186 if (string.length == 0) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
187 return newstr;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
188 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
189
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
190 size_t i;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
191 for(i=0;i<string.length;i++) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
192 char c = string.ptr[i];
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
193 if(c > 32) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
194 break;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
195 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
196 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
197 newstr.ptr = &string.ptr[i];
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
198 newstr.length = string.length - i;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
199
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
200 if(newstr.length == 0) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
201 return newstr;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
202 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
203
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
204 i = newstr.length - 1;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
205 for(;;) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
206 char c = newstr.ptr[i];
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
207 if(c > 32) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
208 break;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
209 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
210 if(i > 0) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
211 i--;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
212 } else {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
213 break;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
214 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
215 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
216 newstr.length = i + 1;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
217
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
218 return newstr;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
219 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
220
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
221
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
222 // webserver extension
51
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
223
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
224 int sstr_startswith(sstr_t string, sstr_t cmp) {
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
225 sstr_t sub = sstrsubsl(string, 0, cmp.length);
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
226 if(!sstrcmp(sub, cmp)) {
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
227 return 1;
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
228 } else {
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
229 return 0;
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
230 }
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
231 }
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
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 }

mercurial