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