]> mj.ucw.cz Git - libucw.git/blob - lib/obj2buck.c
Adapted to new unicode includes.
[libucw.git] / lib / obj2buck.c
1 /*
2  *      Generating Buckets from Objects
3  *
4  *      (c) 2004, Robert Spalek <robert@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/fastbuf.h"
12 #include "lib/ff-utf8.h"
13 #include "lib/bucket.h"
14 #include "lib/object.h"
15
16 #include <string.h>
17 #include <stdarg.h>
18
19 static uns use_v33;
20
21 void
22 attr_set_type(uns type)
23 {
24   switch (type)
25     {
26     case BUCKET_TYPE_PLAIN:
27     case BUCKET_TYPE_V30:
28       use_v33 = 0;
29       break;
30     case BUCKET_TYPE_V33:
31     case BUCKET_TYPE_V33_LIZARD:
32       use_v33 = 1;
33       break;
34     default:
35       die("Don't know how to generate buckets of type %08x", type);
36     }
37 }
38
39 inline byte *
40 put_attr(byte *ptr, uns type, byte *val, uns len)
41 {
42   if (use_v33)
43   {
44     PUT_UTF8(ptr, len+1);
45     memcpy(ptr, val, len);
46     ptr += len;
47     *ptr++ = type;
48   }
49   else
50   {
51     *ptr++ = type;
52     memcpy(ptr, val, len);
53     ptr += len;
54     *ptr++ = '\n';
55   }
56   return ptr;
57 }
58
59 byte *
60 put_attr_str(byte *ptr, uns type, byte *val)
61 {
62   return put_attr(ptr, type, val, strlen(val));
63 }
64
65 inline byte *
66 put_attr_vformat(byte *ptr, uns type, byte *mask, va_list va)
67 {
68   if (use_v33)
69   {
70     uns len = vsprintf(ptr+1, mask, va);
71     if (len >= 127)
72     {
73       byte tmp[6], *tmp_end = tmp;
74       PUT_UTF8(tmp_end, len+1);
75       uns l = tmp_end - tmp;
76       memmove(ptr+l, ptr+1, len);
77       memcpy(ptr, tmp, l);
78       ptr += l + len;
79     }
80     else
81     {
82       *ptr = len+1;
83       ptr += len+1;
84     }
85     *ptr++ = type;
86   }
87   else
88   {
89     *ptr++ = type;
90     ptr += vsprintf(ptr, mask, va);
91     *ptr++ = '\n';
92   }
93   return ptr;
94 }
95
96 byte *
97 put_attr_format(byte *ptr, uns type, char *mask, ...)
98 {
99   va_list va;
100   va_start(va, mask);
101   byte *ret = put_attr_vformat(ptr, type, mask, va);
102   va_end(va);
103   return ret;
104 }
105
106 byte *
107 put_attr_num(byte *ptr, uns type, uns val)
108 {
109   if (use_v33)
110   {
111     uns len = sprintf(ptr+1, "%d", val) + 1;
112     *ptr = len;
113     ptr += len;
114     *ptr++ = type;
115   }
116   else
117     ptr += sprintf(ptr, "%c%d\n", type, val);
118   return ptr;
119 }
120
121 inline void
122 bput_attr(struct fastbuf *b, uns type, byte *val, uns len)
123 {
124   if (use_v33)
125   {
126     bput_utf8(b, len+1);
127     bwrite(b, val, len);
128     bputc(b, type);
129   }
130   else
131   {
132     bputc(b, type);
133     bwrite(b, val, len);
134     bputc(b, '\n');
135   }
136 }
137
138 void
139 bput_attr_str(struct fastbuf *b, uns type, byte *val)
140 {
141   bput_attr(b, type, val, strlen(val));
142 }
143
144 inline void
145 bput_attr_vformat(struct fastbuf *b, uns type, byte *mask, va_list va)
146 {
147   if (use_v33)
148   {
149     int len = vsnprintf(NULL, 0, mask, va);
150     if (len < 0)
151       die("vsnprintf() does not support size=0");
152     bput_utf8(b, len+1);
153     vbprintf(b, mask, va);
154     bputc(b, type);
155   }
156   else
157   {
158     bputc(b, type);
159     vbprintf(b, mask, va);
160     bputc(b, '\n');
161   }
162 }
163
164 void
165 bput_attr_format(struct fastbuf *b, uns type, char *mask, ...)
166 {
167   va_list va;
168   va_start(va, mask);
169   bput_attr_vformat(b, type, mask, va);
170   va_end(va);
171 }
172
173 void
174 bput_attr_num(struct fastbuf *b, uns type, uns val)
175 {
176   if (use_v33)
177   {
178     byte tmp[12];
179     uns len = sprintf(tmp, "%d", val);
180     bputc(b, len+1);
181     bwrite(b, tmp, len);
182     bputc(b, type);
183   }
184   else
185     bprintf(b, "%c%d\n", type, val);
186 }