]> mj.ucw.cz Git - libucw.git/blob - lib/object.c
OBJ_POOL_SIZE no longer needed.
[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
14 #include <string.h>
15 #include <stdio.h>
16
17 void
18 obj_dump(struct odes *o)
19 {
20   struct oattr *a, *b;
21
22   for(a=o->attrs; a; a=a->next)
23     for(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));
31
32   a->next = a->same = NULL;
33   a->attr = x;
34   strcpy(a->val, v);
35   return a;
36 }
37
38 struct odes *
39 obj_new(struct mempool *pool)
40 {
41   struct odes *o;
42
43   o = mp_alloc(pool, sizeof(struct odes));
44   o->pool = pool;
45   o->attrs = NULL;
46   o->cached_attr = NULL;
47   return o;
48 }
49
50 int
51 obj_read(struct fastbuf *f, struct odes *o)
52 {
53   byte buf[1024];
54
55   while (bgets(f, buf, sizeof(buf)))
56     {
57       if (!buf[0])
58         return 1;
59       obj_add_attr(o, buf[0], buf+1);
60     }
61   return 0;
62 }
63
64 void
65 obj_write(struct fastbuf *f, struct odes *d)
66 {
67   struct oattr *a, *b;
68   byte *z;
69
70   for(a=d->attrs; a; a=a->next)
71     for(b=a; b; b=b->same)
72       {
73         bputc(f, a->attr);
74         for(z = b->val; *z; z++)
75           if (*z >= ' ' || *z == '\t')
76             bputc(f, *z);
77           else
78             {
79               bputc(f, '?');
80               log(L_ERROR, "obj_dump: Found non-ASCII characters (URL might be %s)", obj_find_aval(d, 'U'));
81             }
82         bputc(f, '\n');
83       }
84 }
85
86 struct oattr *
87 obj_find_attr(struct odes *o, uns x)
88 {
89   struct oattr *a;
90
91   for(a=o->attrs; a && a->attr != x; a=a->next)
92     ;
93   return a;
94 }
95
96 struct oattr *
97 obj_find_attr_last(struct odes *o, uns x)
98 {
99   struct oattr *a = obj_find_attr(o, x);
100
101   if (a)
102     {
103       while (a->same)
104         a = a->same;
105     }
106   return a;
107 }
108
109 uns
110 obj_del_attr(struct odes *o, struct oattr *a)
111 {
112   struct oattr *x, **p, *y, *l;
113   byte aa = a->attr;
114
115   o->cached_attr = NULL;
116   p = &o->attrs;
117   while (x = *p)
118     {
119       if (x->attr == aa)
120         {
121           y = x;
122           l = NULL;
123           while (x = *p)
124             {
125               if (x == a)
126                 {
127                   *p = x->same;
128                   return 1;
129                 }
130               p = &x->same;
131               l = x;
132             }
133           return 0;
134         }
135       p = &x->next;
136     }
137   return 0;
138 }
139
140 byte *
141 obj_find_aval(struct odes *o, uns x)
142 {
143   struct oattr *a = obj_find_attr(o, x);
144
145   return a ? a->val : NULL;
146 }
147
148 struct oattr *
149 obj_set_attr(struct odes *o, uns x, byte *v)
150 {
151   struct oattr *a, **z;
152
153   z = &o->attrs;
154   while (a = *z)
155     {
156       if (a->attr == x)
157         {
158           *z = a->next;
159           goto set;
160         }
161       z = &a->next;
162     }
163
164  set:
165   if (v)
166     {
167       a = oa_new(o, x, v);
168       a->next = o->attrs;
169       o->attrs = a;
170     }
171   else
172     a = NULL;
173   o->cached_attr = a;
174   return a;
175 }
176
177 struct oattr *
178 obj_set_attr_num(struct odes *o, uns a, uns v)
179 {
180   byte x[32];
181
182   sprintf(x, "%d", v);
183   return obj_set_attr(o, a, x);
184 }
185
186 struct oattr *
187 obj_add_attr(struct odes *o, uns x, byte *v)
188 {
189   struct oattr *a, *b;
190
191   b = oa_new(o, x, v);
192   if (!(a = o->cached_attr) || a->attr != x)
193     {
194       if (!(a = obj_find_attr(o, x)))
195         {
196           b->next = o->attrs;
197           o->attrs = b;
198           goto done;
199         }
200     }
201   while (a->same)
202     a = a->same;
203   a->same = b;
204  done:
205   o->cached_attr = b;
206   return b;
207 }
208
209 struct oattr *
210 obj_prepend_attr(struct odes *o, uns x, byte *v)
211 {
212   struct oattr *a, *b, **z;
213
214   b = oa_new(o, x, v);
215   z = &o->attrs;
216   while (a = *z)
217     {
218       if (a->attr == x)
219         {
220           b->same = a;
221           b->next = a->next;
222           a->next = NULL;
223           *z = b;
224           return b;
225         }
226       z = &a->next;
227     }
228   b->next = o->attrs;
229   o->attrs = b;
230   return b;
231 }
232
233 struct oattr *
234 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
235 {
236   struct oattr *b;
237
238   b = oa_new(o, first->attr, v);
239   b->same = after->same;
240   after->same = b;
241   return b;
242 }