]> mj.ucw.cz Git - libucw.git/blob - lib/object.c
32beab2e895e0474825fba895afeeae97f5dc52c
[libucw.git] / lib / object.c
1 /*
2  *      Sherlock Library -- Object Functions
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "lib/lib.h"
12 #include "lib/pools.h"
13 #include "lib/fastbuf.h"
14 #include "lib/object.h"
15
16 #include <string.h>
17 #include <stdio.h>
18
19 void
20 obj_dump(struct odes *o)
21 {
22   for(struct oattr *a=o->attrs; a; a=a->next)
23     for(struct oattr *b=a; b; b=b->same)
24       printf("%c%s\n", (a==b ? a->attr : ' '), b->val);
25 }
26
27 static struct oattr *
28 oa_new(struct odes *o, uns x, byte *v)
29 {
30   struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v)+1);
31
32   a->next = a->same = NULL;
33   a->attr = x;
34   a->val = (byte*) (a+1);
35   strcpy(a->val, v);
36   return a;
37 }
38
39 static struct oattr *
40 oa_new_ref(struct odes *o, uns x, byte *v)
41 {
42   struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr));
43
44   a->next = a->same = NULL;
45   a->attr = x;
46   a->val = v;
47   return a;
48 }
49
50 struct odes *
51 obj_new(struct mempool *pool)
52 {
53   struct odes *o = mp_alloc(pool, sizeof(struct odes));
54   o->pool = pool;
55   o->attrs = NULL;
56   o->cached_attr = NULL;
57   return o;
58 }
59
60 int
61 obj_read(struct fastbuf *f, struct odes *o)
62 {
63   byte buf[MAX_ATTR_SIZE];
64
65   while (bgets(f, buf, sizeof(buf)))
66     {
67       if (!buf[0])
68         return 1;
69       obj_add_attr(o, buf[0], buf+1);
70     }
71   return 0;
72 }
73
74 void
75 obj_read_multi(struct fastbuf *f, struct odes *o)
76 {
77   /* Read a multi-part object ending with either EOF or a NUL character */
78   byte buf[MAX_ATTR_SIZE];
79   while (bpeekc(f) > 0 && bgets(f, buf, sizeof(buf)))
80     if (buf[0])
81       obj_add_attr(o, buf[0], buf+1);
82 }
83
84 void
85 obj_write(struct fastbuf *f, struct odes *d)
86 {
87   for(struct oattr *a=d->attrs; a; a=a->next)
88     for(struct oattr *b=a; b; b=b->same)
89       {
90         byte *z;
91         for (z = b->val; *z; z++)
92           if (*z < ' ' && *z != '\t')
93             {
94               log(L_ERROR, "obj_dump: Found non-ASCII characters (URL might be %s)", obj_find_aval(d, 'U'));
95               *z = '?';
96             }
97         ASSERT(z - b->val <= MAX_ATTR_SIZE-2);
98         bput_attr_str(f, a->attr, b->val);
99       }
100 }
101
102 void
103 obj_write_nocheck(struct fastbuf *f, struct odes *d)
104 {
105   for(struct oattr *a=d->attrs; a; a=a->next)
106     for(struct oattr *b=a; b; b=b->same)
107       bput_attr_str(f, a->attr, b->val);
108 }
109
110 struct oattr *
111 obj_find_attr(struct odes *o, uns x)
112 {
113   struct oattr *a;
114   for(a=o->attrs; a && a->attr != x; a=a->next)
115     ;
116   return a;
117 }
118
119 struct oattr *
120 obj_find_attr_last(struct odes *o, uns x)
121 {
122   struct oattr *a = obj_find_attr(o, x);
123
124   if (a)
125     {
126       while (a->same)
127         a = a->same;
128     }
129   return a;
130 }
131
132 uns
133 obj_del_attr(struct odes *o, struct oattr *a)
134 {
135   struct oattr *x, **p, *y, *l;
136   byte aa = a->attr;
137
138   o->cached_attr = NULL;
139   p = &o->attrs;
140   while (x = *p)
141     {
142       if (x->attr == aa)
143         {
144           y = x;
145           l = NULL;
146           while (x = *p)
147             {
148               if (x == a)
149                 {
150                   *p = x->same;
151                   return 1;
152                 }
153               p = &x->same;
154               l = x;
155             }
156           return 0;
157         }
158       p = &x->next;
159     }
160   return 0;
161 }
162
163 byte *
164 obj_find_aval(struct odes *o, uns x)
165 {
166   struct oattr *a = obj_find_attr(o, x);
167   return a ? a->val : NULL;
168 }
169
170 struct oattr *
171 obj_set_attr(struct odes *o, uns x, byte *v)
172 {
173   struct oattr *a, **z;
174
175   z = &o->attrs;
176   while (a = *z)
177     {
178       if (a->attr == x)
179         {
180           *z = a->next;
181           goto set;
182         }
183       z = &a->next;
184     }
185
186  set:
187   if (v)
188     {
189       a = oa_new(o, x, v);
190       a->next = o->attrs;
191       o->attrs = a;
192     }
193   else
194     a = NULL;
195   o->cached_attr = a;
196   return a;
197 }
198
199 struct oattr *
200 obj_set_attr_num(struct odes *o, uns a, uns v)
201 {
202   byte x[32];
203
204   sprintf(x, "%d", v);
205   return obj_set_attr(o, a, x);
206 }
207
208 static inline struct oattr *
209 obj_add_attr_internal(struct odes *o, struct oattr *b)
210 {
211   struct oattr *a;
212
213   if (!(a = o->cached_attr) || a->attr != b->attr)
214     {
215       if (!(a = obj_find_attr(o, b->attr)))
216         {
217           b->next = o->attrs;
218           o->attrs = b;
219           goto done;
220         }
221     }
222   while (a->same)
223     a = a->same;
224   a->same = b;
225  done:
226   o->cached_attr = b;
227   return b;
228 }
229
230 struct oattr *
231 obj_add_attr(struct odes *o, uns x, byte *v)
232 {
233   return obj_add_attr_internal(o, oa_new(o, x, v));
234 }
235
236 struct oattr *
237 obj_add_attr_ref(struct odes *o, uns x, byte *v)
238 {
239   return obj_add_attr_internal(o, oa_new_ref(o, x, v));
240 }
241
242 struct oattr *
243 obj_prepend_attr(struct odes *o, uns x, byte *v)
244 {
245   struct oattr *a, *b, **z;
246
247   b = oa_new(o, x, v);
248   z = &o->attrs;
249   while (a = *z)
250     {
251       if (a->attr == x)
252         {
253           b->same = a;
254           b->next = a->next;
255           a->next = NULL;
256           *z = b;
257           return b;
258         }
259       z = &a->next;
260     }
261   b->next = o->attrs;
262   o->attrs = b;
263   return b;
264 }
265
266 struct oattr *
267 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
268 {
269   struct oattr *b = oa_new(o, first->attr, v);
270   b->same = after->same;
271   after->same = b;
272   return b;
273 }
274
275 void
276 obj_move_attr_to_head(struct odes *o, uns x)
277 {
278   struct oattr *a, **z;
279
280   z = &o->attrs;
281   while (a = *z)
282     {
283       if (a->attr == x)
284         {
285           *z = a->next;
286           a->next = o->attrs;
287           o->attrs = a;
288           break;
289         }
290       z = &a->next;
291     }
292 }
293
294 void
295 obj_move_attr_to_tail(struct odes *o, uns x)
296 {
297   struct oattr *a, **z;
298
299   z = &o->attrs;
300   while (a = *z)
301     {
302       if (a->attr == x)
303         {
304           *z = a->next;
305           while (*z)
306             z = &(*z)->next;
307           *z = a;
308           a->next = NULL;
309           break;
310         }
311       z = &a->next;
312     }
313 }