src/server/ucx/string.c

Sat, 15 Dec 2012 16:05:03 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 15 Dec 2012 16:05:03 +0100
changeset 37
360b9aabe17e
parent 36
450d2d5f4735
child 51
b28cf69f42e8
permissions
-rw-r--r--

added support for asynchronous safs

15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * File: sstring.c
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 * Author: olaf
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 *
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 * Created on 17. Juni 2010, 13:27
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 */
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 #include <stdlib.h>
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
9 #include <strings.h>
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 #include <stdarg.h>
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 #include "string.h"
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 sstr_t sstr (char *s) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 sstr_t string;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 string.ptr = s;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 string.length = strlen(s);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 return string;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 sstr_t sstrn (char *s, size_t n) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 sstr_t string;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 string.ptr = s;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 string.length = n;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 return string;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 }
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 size_t sstrnlen (size_t n, sstr_t s, ...) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 va_list ap;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 size_t size = s.length;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 va_start(ap, s);
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 for (int i=0;i<n-1;i++) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 sstr_t str = va_arg(ap, sstr_t);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 size += str.length;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
37 va_end(ap);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 return size;
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
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 sstr_t sstrcat (sstr_t s, ...) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 va_list ap;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 va_start(ap, s);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 s.ptr[0] = 0;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 sstr_t str = va_arg (ap, sstr_t);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 while (str.ptr != NULL) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 s.ptr = strncat (s.ptr, str.ptr, s.length);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 str = va_arg (ap, sstr_t);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
52 va_end(ap);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 return s;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 sstr_t sstrncat (size_t n, sstr_t s, sstr_t c1, ...) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 va_list ap;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 va_start(ap, c1);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 s.ptr[0] = 0;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
61
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
62 size_t len = s.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
63 size_t cplen = c1.length > len ? len : c1.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
64 char *ptr = s.ptr;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
65
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
66 memcpy(ptr, c1.ptr, cplen);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
67 len -= cplen;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
68 ptr += cplen;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 for (int i=0;i<n-1;i++) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 sstr_t str = va_arg (ap, sstr_t);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
71 cplen = str.length > len ? len : str.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
72 if(cplen <= 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
73 va_end(ap);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
74 return s;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
75 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
76 memcpy(ptr, str.ptr, cplen);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
77 len -= cplen;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
78 ptr += cplen;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
80 va_end(ap);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 return s;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 sstr_t sstrsubs (sstr_t s, size_t start) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 return sstrsubsl (s, start, s.length-start);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 }
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 sstr_t sstrsubsl (sstr_t s, size_t start, size_t length) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 sstr_t new_sstr;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 if (start < 0 || start >= s.length || length < 0) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 return s;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 if (length > s.length-start) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 length = s.length-start;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 new_sstr.ptr = &s.ptr[start];
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 new_sstr.length = length;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 return new_sstr;
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
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
102 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
103 if (d.length == 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
104 return NULL;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
105 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
106
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
107 sstr_t* result;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
108 size_t nmax = *n;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
109 *n = 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
110
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
111 /* special case: exact match - no processing needed */
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
112 if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
113 result = malloc(sizeof(sstr_t));
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
114 result[0] = sstrn("", 0);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
115 return result;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
116 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
117 sstr_t sv = sstrdup(s);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
118
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
119 for (int i = 0 ; i < s.length ; i++) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
120 if (sv.ptr[i] == d.ptr[0]) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
121 _Bool match = 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
122 for (int j = 1 ; j < d.length ; j++) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
123 if (j+i < s.length) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
124 match &= (sv.ptr[i+j] == d.ptr[j]);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
125 } else {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
126 match = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
127 break;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
128 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
129 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
130 if (match) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
131 (*n)++;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
132 for (int j = 0 ; j < d.length ; j++) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
133 sv.ptr[i+j] = 0;
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 i += d.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
136 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
137 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
138 if ((*n) == nmax) break;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
139 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
140 result = malloc(sizeof(sstr_t) * (*n));
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
141
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
142 char *pptr = sv.ptr;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
143 for (int i = 0 ; i < *n ; i++) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
144 size_t l = strlen(pptr);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
145 char* ptr = malloc(l + 1);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
146 memcpy(ptr, pptr, l);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
147 ptr[l] = 0;
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 result[i] = sstrn(ptr, l);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
150 pptr += l + d.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
151 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
152
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
153 free(sv.ptr);
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 return result;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
158 int sstrcmp(sstr_t s1, sstr_t s2) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
159 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
160 }
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
161
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
162 sstr_t sstrdup(sstr_t s) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
163 sstr_t newstring;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
164 newstring.ptr = (char*) malloc(s.length + 1);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
165 if (newstring.ptr != NULL) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
166 newstring.length = s.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
167 newstring.ptr[newstring.length] = 0;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
168
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
169 memcpy(newstring.ptr, s.ptr, s.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
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
172 return newstring;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
173 }
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
174
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
175 // webserver extension
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
176 sstr_t sstrtrim(sstr_t string) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
177 sstr_t newstr = string;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
178 int nsoff = 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
179 int l = 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
180 for(int i=0;i<string.length;i++) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
181 char c = string.ptr[i];
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
182 if(l) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
183 /* leading whitespace */
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
184 if(c > 32) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
185 l = 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
186 nsoff = i;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
187 newstr.ptr = &string.ptr[i];
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
188 newstr.length = string.length - nsoff;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
189 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
190 } else {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
191 /* trailing whitespace */
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
192 if(c > 32) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
193 newstr.length = (i - nsoff) + 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
194 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
195 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
196 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
197 return newstr;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
198 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
199
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
200 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
201 sstr_t newstring;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
202 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
203 if (newstring.ptr != NULL) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
204 newstring.length = s.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
205 newstring.ptr[newstring.length] = 0;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
206
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
207 memcpy(newstring.ptr, s.ptr, s.length);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
208 }
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
209
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
210 return newstring;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
211 }
30
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
212
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
213 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
214 sstr_t newstring;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
215 newstring.ptr = (char*)pool_malloc(pool, s.length + 1);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
216 if (newstring.ptr != NULL) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
217 newstring.length = s.length;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
218 newstring.ptr[newstring.length] = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
219
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
220 memcpy(newstring.ptr, s.ptr, s.length);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
221 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
222
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
223 return newstring;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
224 }

mercurial