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