1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
15 | * |
---|
16 | * Jabber |
---|
17 | * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ |
---|
18 | */ |
---|
19 | |
---|
20 | #include "libxode.h" |
---|
21 | |
---|
22 | xode_pool xode_spool_getpool(const xode_spool s) |
---|
23 | { |
---|
24 | if(s == NULL) |
---|
25 | return NULL; |
---|
26 | |
---|
27 | return s->p; |
---|
28 | } |
---|
29 | |
---|
30 | int xode_spool_getlen(const xode_spool s) |
---|
31 | { |
---|
32 | if(s == NULL) |
---|
33 | return 0; |
---|
34 | |
---|
35 | return s->len; |
---|
36 | } |
---|
37 | |
---|
38 | void xode_spool_free(xode_spool s) |
---|
39 | { |
---|
40 | xode_pool_free(xode_spool_getpool(s)); |
---|
41 | } |
---|
42 | |
---|
43 | xode_spool xode_spool_newfrompool(xode_pool p) |
---|
44 | { |
---|
45 | xode_spool s; |
---|
46 | |
---|
47 | s = xode_pool_malloc(p, sizeof(struct xode_spool_struct)); |
---|
48 | s->p = p; |
---|
49 | s->len = 0; |
---|
50 | s->last = NULL; |
---|
51 | s->first = NULL; |
---|
52 | return s; |
---|
53 | } |
---|
54 | |
---|
55 | xode_spool xode_spool_new(void) |
---|
56 | { |
---|
57 | return xode_spool_newfrompool(xode_pool_heap(512)); |
---|
58 | } |
---|
59 | |
---|
60 | void xode_spool_add(xode_spool s, char *str) |
---|
61 | { |
---|
62 | struct xode_spool_node *sn; |
---|
63 | int len; |
---|
64 | |
---|
65 | if(str == NULL) |
---|
66 | return; |
---|
67 | |
---|
68 | len = strlen(str); |
---|
69 | if(len == 0) |
---|
70 | return; |
---|
71 | |
---|
72 | sn = xode_pool_malloc(s->p, sizeof(struct xode_spool_node)); |
---|
73 | sn->c = xode_pool_strdup(s->p, str); |
---|
74 | sn->next = NULL; |
---|
75 | |
---|
76 | s->len += len; |
---|
77 | if(s->last != NULL) |
---|
78 | s->last->next = sn; |
---|
79 | s->last = sn; |
---|
80 | if(s->first == NULL) |
---|
81 | s->first = sn; |
---|
82 | } |
---|
83 | |
---|
84 | void xode_spooler(xode_spool s, ...) |
---|
85 | { |
---|
86 | va_list ap; |
---|
87 | char *arg = NULL; |
---|
88 | |
---|
89 | if(s == NULL) |
---|
90 | return; |
---|
91 | |
---|
92 | va_start(ap, s); |
---|
93 | |
---|
94 | /* loop till we hit our end flag, the first arg */ |
---|
95 | while(1) |
---|
96 | { |
---|
97 | arg = va_arg(ap,char *); |
---|
98 | if((int)arg == (int)s || arg == NULL) |
---|
99 | break; |
---|
100 | else |
---|
101 | xode_spool_add(s, arg); |
---|
102 | } |
---|
103 | |
---|
104 | va_end(ap); |
---|
105 | } |
---|
106 | |
---|
107 | char *xode_spool_tostr(xode_spool s) |
---|
108 | { |
---|
109 | char *ret,*tmp; |
---|
110 | struct xode_spool_node *next; |
---|
111 | |
---|
112 | if(s == NULL || s->len == 0 || s->first == NULL) |
---|
113 | return NULL; |
---|
114 | |
---|
115 | ret = xode_pool_malloc(s->p, s->len + 1); |
---|
116 | *ret = '\0'; |
---|
117 | |
---|
118 | next = s->first; |
---|
119 | tmp = ret; |
---|
120 | while(next != NULL) |
---|
121 | { |
---|
122 | tmp = strcat(tmp,next->c); |
---|
123 | next = next->next; |
---|
124 | } |
---|
125 | |
---|
126 | return ret; |
---|
127 | } |
---|
128 | |
---|
129 | /* convenience :) */ |
---|
130 | char *xode_spool_str(xode_pool p, ...) |
---|
131 | { |
---|
132 | va_list ap; |
---|
133 | xode_spool s; |
---|
134 | char *arg = NULL; |
---|
135 | |
---|
136 | if(p == NULL) |
---|
137 | return NULL; |
---|
138 | |
---|
139 | s = xode_spool_newfrompool(p); |
---|
140 | |
---|
141 | va_start(ap, p); |
---|
142 | |
---|
143 | /* loop till we hit our end flag, the first arg */ |
---|
144 | while(1) |
---|
145 | { |
---|
146 | arg = va_arg(ap,char *); |
---|
147 | if((int)arg == (int)p) |
---|
148 | break; |
---|
149 | else |
---|
150 | xode_spool_add(s, arg); |
---|
151 | } |
---|
152 | |
---|
153 | va_end(ap); |
---|
154 | |
---|
155 | return xode_spool_tostr(s); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | char *xode_strunescape(xode_pool p, char *buf) |
---|
160 | { |
---|
161 | int i,j=0; |
---|
162 | char *temp; |
---|
163 | |
---|
164 | if (p == NULL || buf == NULL) return(NULL); |
---|
165 | |
---|
166 | if (strchr(buf,'&') == NULL) return(buf); |
---|
167 | |
---|
168 | temp = xode_pool_malloc(p,strlen(buf)+1); |
---|
169 | |
---|
170 | if (temp == NULL) return(NULL); |
---|
171 | |
---|
172 | for(i=0;i<strlen(buf);i++) |
---|
173 | { |
---|
174 | if (buf[i]=='&') |
---|
175 | { |
---|
176 | if (strncmp(&buf[i],"&",5)==0) |
---|
177 | { |
---|
178 | temp[j] = '&'; |
---|
179 | i += 4; |
---|
180 | } else if (strncmp(&buf[i],""",6)==0) { |
---|
181 | temp[j] = '\"'; |
---|
182 | i += 5; |
---|
183 | } else if (strncmp(&buf[i],"'",6)==0) { |
---|
184 | temp[j] = '\''; |
---|
185 | i += 5; |
---|
186 | } else if (strncmp(&buf[i],"<",4)==0) { |
---|
187 | temp[j] = '<'; |
---|
188 | i += 3; |
---|
189 | } else if (strncmp(&buf[i],">",4)==0) { |
---|
190 | temp[j] = '>'; |
---|
191 | i += 3; |
---|
192 | } |
---|
193 | } else { |
---|
194 | temp[j]=buf[i]; |
---|
195 | } |
---|
196 | j++; |
---|
197 | } |
---|
198 | temp[j]='\0'; |
---|
199 | return(temp); |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | char *xode_strescape(xode_pool p, char *buf) |
---|
204 | { |
---|
205 | int i,j,oldlen,newlen; |
---|
206 | char *temp; |
---|
207 | |
---|
208 | if (p == NULL || buf == NULL) return(NULL); |
---|
209 | |
---|
210 | oldlen = newlen = strlen(buf); |
---|
211 | for(i=0;i<oldlen;i++) |
---|
212 | { |
---|
213 | switch(buf[i]) |
---|
214 | { |
---|
215 | case '&': |
---|
216 | newlen+=5; |
---|
217 | break; |
---|
218 | case '\'': |
---|
219 | newlen+=6; |
---|
220 | break; |
---|
221 | case '\"': |
---|
222 | newlen+=6; |
---|
223 | break; |
---|
224 | case '<': |
---|
225 | newlen+=4; |
---|
226 | break; |
---|
227 | case '>': |
---|
228 | newlen+=4; |
---|
229 | break; |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | if(oldlen == newlen) return buf; |
---|
234 | |
---|
235 | temp = xode_pool_malloc(p,newlen+1); |
---|
236 | |
---|
237 | if (temp==NULL) return(NULL); |
---|
238 | |
---|
239 | for(i=j=0;i<oldlen;i++) |
---|
240 | { |
---|
241 | switch(buf[i]) |
---|
242 | { |
---|
243 | case '&': |
---|
244 | memcpy(&temp[j],"&",5); |
---|
245 | j += 5; |
---|
246 | break; |
---|
247 | case '\'': |
---|
248 | memcpy(&temp[j],"'",6); |
---|
249 | j += 6; |
---|
250 | break; |
---|
251 | case '\"': |
---|
252 | memcpy(&temp[j],""",6); |
---|
253 | j += 6; |
---|
254 | break; |
---|
255 | case '<': |
---|
256 | memcpy(&temp[j],"<",4); |
---|
257 | j += 4; |
---|
258 | break; |
---|
259 | case '>': |
---|
260 | memcpy(&temp[j],">",4); |
---|
261 | j += 4; |
---|
262 | break; |
---|
263 | default: |
---|
264 | temp[j++] = buf[i]; |
---|
265 | } |
---|
266 | } |
---|
267 | temp[j] = '\0'; |
---|
268 | return temp; |
---|
269 | } |
---|