src/server/ucx/string.c

Mon, 09 Sep 2013 12:15:54 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 09 Sep 2013 12:15:54 +0200
changeset 93
95b77e842db3
parent 88
73b3485e96f1
permissions
-rw-r--r--

fixed solaris build

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"
88
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
34 #include "allocator.h"
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
36 sstr_t sstr(char *s) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 sstr_t string;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 string.ptr = s;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 string.length = strlen(s);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 return string;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
43 sstr_t sstrn(char *s, size_t n) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 sstr_t string;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 string.ptr = s;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 string.length = n;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 return string;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
50 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
51 va_list ap;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 size_t size = s.length;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 va_start(ap, s);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
55 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
56 sstr_t str = va_arg(ap, sstr_t);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 size += str.length;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
59 va_end(ap);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 return size;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
64 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
65 va_list ap;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 va_start(ap, c1);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 s.ptr[0] = 0;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
68
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
69 size_t len = s.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
70 size_t cplen = c1.length > len ? len : c1.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
71 char *ptr = s.ptr;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
72
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
73 memcpy(ptr, c1.ptr, cplen);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
74 len -= cplen;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
75 ptr += cplen;
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
76 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
77 sstr_t str = va_arg (ap, sstr_t);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
78 cplen = str.length > len ? len : str.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
79 if(cplen <= 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
80 va_end(ap);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
81 return s;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
82 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
83 memcpy(ptr, str.ptr, cplen);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
84 len -= cplen;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
85 ptr += cplen;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
87 va_end(ap);
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
88 s.length = ptr - s.ptr;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 return s;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
93 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
94 return sstrsubsl (s, start, s.length-start);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
97 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
98 sstr_t new_sstr;
70
4e6e812c1d97 fixed some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
99 if (start >= s.length) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 return s;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 if (length > s.length-start) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 length = s.length-start;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 new_sstr.ptr = &s.ptr[start];
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 new_sstr.length = length;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 return new_sstr;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109
88
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
110 sstr_t sstrchr(sstr_t s, int c) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
111 for(size_t i=0;i<s.length;i++) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
112 if(s.ptr[i] == c) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
113 return sstrsubs(s, i);
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
114 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
115 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
116 sstr_t n;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
117 n.ptr = NULL;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
118 n.length = 0;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
119 return n;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
120 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
121
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
122 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
123 if (d.length == 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
124 return NULL;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
125 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
126
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
127 sstr_t* result;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
128 size_t nmax = *n;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
129 *n = 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
130
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
131 /* special case: exact match - no processing needed */
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
132 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
133 *n = 0;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
134 return NULL;
36
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 sstr_t sv = sstrdup(s);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
137
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
138 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
139 if (sv.ptr[i] == d.ptr[0]) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
140 _Bool match = 1;
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
141 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
142 if (j+i < s.length) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
143 match &= (sv.ptr[i+j] == d.ptr[j]);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
144 } else {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
145 match = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
146 break;
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 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
149 if (match) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
150 (*n)++;
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
151 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
152 sv.ptr[i+j] = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
153 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
154 i += d.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
155 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
156 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
157 if ((*n) == nmax) break;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
158 }
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
159 result = (sstr_t*) malloc(sizeof(sstr_t) * (*n));
36
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 char *pptr = sv.ptr;
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
162 for (size_t i = 0 ; i < *n ; i++) {
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
163 size_t l = strlen(pptr);
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
164 char* ptr = (char*) malloc(l + 1);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
165 memcpy(ptr, pptr, l);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
166 ptr[l] = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
167
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
168 result[i] = sstrn(ptr, l);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
169 pptr += l + d.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
170 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
171
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
172 free(sv.ptr);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
173
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
174 return result;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
177 int sstrcmp(sstr_t s1, sstr_t s2) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
178 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
179 }
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
180
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
181 sstr_t sstrdup(sstr_t s) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
182 sstr_t newstring;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
183 newstring.ptr = (char*) malloc(s.length + 1);
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
184 if (newstring.ptr) {
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
185 newstring.length = s.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
186 newstring.ptr[newstring.length] = 0;
88
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
187
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
188 memcpy(newstring.ptr, s.ptr, s.length);
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
189 } else {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
190 newstring.length = 0;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
191 }
88
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
192
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
193 return newstring;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
194 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
195
88
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
196 sstr_t sstrdup_alloc(UcxAllocator *allocator, sstr_t s) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
197 sstr_t newstring;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
198 newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1);
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
199 if (newstring.ptr) {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
200 newstring.length = s.length;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
201 newstring.ptr[newstring.length] = 0;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
202
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
203 memcpy(newstring.ptr, s.ptr, s.length);
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
204 } else {
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
205 newstring.length = 0;
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
206 }
73b3485e96f1 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
207
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
208 return newstring;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
209 }
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
210
71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
211 sstr_t sstrtrim(sstr_t string) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
212 sstr_t newstr = string;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
213 if (string.length == 0) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
214 return newstr;
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
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
217 size_t i;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
218 for(i=0;i<string.length;i++) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
219 char c = string.ptr[i];
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
220 if(c > 32) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
221 break;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
222 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
223 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
224 newstr.ptr = &string.ptr[i];
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
225 newstr.length = string.length - i;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
226
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
227 if(newstr.length == 0) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
228 return newstr;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
229 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
230
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
231 i = newstr.length - 1;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
232 for(;;) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
233 char c = newstr.ptr[i];
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
234 if(c > 32) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
235 break;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
236 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
237 if(i > 0) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
238 i--;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
239 } else {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
240 break;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
241 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
242 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
243 newstr.length = i + 1;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
244
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
245 return newstr;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
246 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 70
diff changeset
247
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
248 // webserver extension
51
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
249
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
250 int sstr_startswith(sstr_t string, sstr_t cmp) {
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
251 sstr_t sub = sstrsubsl(string, 0, cmp.length);
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
252 if(!sstrcmp(sub, cmp)) {
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
253 return 1;
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
254 } else {
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
255 return 0;
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
256 }
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
257 }
b28cf69f42e8 added acls
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
258
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
259 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
260 sstr_t newstring;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
261 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
262 if (newstring.ptr != NULL) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
263 newstring.length = s.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
264 newstring.ptr[newstring.length] = 0;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
265
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
266 memcpy(newstring.ptr, s.ptr, s.length);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
267 }
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
268
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
269 return newstring;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
270 }
30
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
271
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
272 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
273 sstr_t newstring;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
274 newstring.ptr = (char*)pool_malloc(pool, s.length + 1);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
275 if (newstring.ptr != NULL) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
276 newstring.length = s.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
277 newstring.ptr[newstring.length] = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
278
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
279 memcpy(newstring.ptr, s.ptr, s.length);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
280 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
281
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
282 return newstring;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
283 }

mercurial