]> mj.ucw.cz Git - libucw.git/blob - lib/object.c
Minor optimizations.
[libucw.git] / lib / object.c
1 /*
2  *      Sherlock Library -- Object Functions
3  *
4  *      (c) 1997--2004 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   return a ? a->val : NULL;
173 }
174
175 struct oattr *
176 obj_set_attr(struct odes *o, uns x, byte *v)
177 {
178   struct oattr *a, **z;
179
180   z = &o->attrs;
181   while (a = *z)
182     {
183       if (a->attr == x)
184         {
185           *z = a->next;
186           goto set;
187         }
188       z = &a->next;
189     }
190
191  set:
192   if (v)
193     {
194       a = oa_new(o, x, v);
195       a->next = o->attrs;
196       o->attrs = a;
197     }
198   else
199     a = NULL;
200   o->cached_attr = a;
201   return a;
202 }
203
204 struct oattr *
205 obj_set_attr_num(struct odes *o, uns a, uns v)
206 {
207   byte x[32];
208
209   sprintf(x, "%d", v);
210   return obj_set_attr(o, a, x);
211 }
212
213 static inline struct oattr *
214 obj_add_attr_internal(struct odes *o, struct oattr *b)
215 {
216   struct oattr *a;
217
218   if (!(a = o->cached_attr) || a->attr != b->attr)
219     {
220       if (!(a = obj_find_attr(o, b->attr)))
221         {
222           b->next = o->attrs;
223           o->attrs = b;
224           goto done;
225         }
226     }
227   while (a->same)
228     a = a->same;
229   a->same = b;
230  done:
231   o->cached_attr = b;
232   return b;
233 }
234
235 struct oattr *
236 obj_add_attr(struct odes *o, uns x, byte *v)
237 {
238   return obj_add_attr_internal(o, oa_new(o, x, v));
239 }
240
241 struct oattr *
242 obj_add_attr_ref(struct odes *o, uns x, byte *v)
243 {
244   return obj_add_attr_internal(o, oa_new_ref(o, x, v));
245 }
246
247 struct oattr *
248 obj_prepend_attr(struct odes *o, uns x, byte *v)
249 {
250   struct oattr *a, *b, **z;
251
252   b = oa_new(o, x, v);
253   z = &o->attrs;
254   while (a = *z)
255     {
256       if (a->attr == x)
257         {
258           b->same = a;
259           b->next = a->next;
260           a->next = NULL;
261           *z = b;
262           return b;
263         }
264       z = &a->next;
265     }
266   b->next = o->attrs;
267   o->attrs = b;
268   return b;
269 }
270
271 struct oattr *
272 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
273 {
274   struct oattr *b = oa_new(o, first->attr, v);
275   b->same = after->same;
276   after->same = b;
277   return b;
278 }
279
280 void
281 obj_move_attr_to_head(struct odes *o, uns x)
282 {
283   struct oattr *a, **z;
284
285   z = &o->attrs;
286   while (a = *z)
287     {
288       if (a->attr == x)
289         {
290           *z = a->next;
291           a->next = o->attrs;
292           o->attrs = a;
293           break;
294         }
295       z = &a->next;
296     }
297 }
298
299 void
300 obj_move_attr_to_tail(struct odes *o, uns x)
301 {
302   struct oattr *a, **z;
303
304   z = &o->attrs;
305   while (a = *z)
306     {
307       if (a->attr == x)
308         {
309           *z = a->next;
310           while (*z)
311             z = &(*z)->next;
312           *z = a;
313           a->next = NULL;
314           break;
315         }
316       z = &a->next;
317     }
318 }