]> mj.ucw.cz Git - libucw.git/blob - lib/obj2buck.h
macros and inline functions for generating V33 buckets
[libucw.git] / lib / obj2buck.h
1 /*
2  *      Generating V33 buckets
3  *
4  *      (c) 2004, Robert Spalek <robert@ucw.cz>
5  */
6
7 #define WRITE_V33(ptr, type, text, len) ({\
8   uns _len = len;               \
9   PUT_UTF8(ptr, _len+1);        \
10   memcpy(ptr, text, _len);      \
11   ptr += _len;                  \
12   *ptr++ = type;                \
13 })
14
15 #define PUTS_V33(ptr, type, text)       WRITE_V33(ptr, type, text, strlen(text))
16
17 #define VPRINTF_V33(ptr, type, mask, va)        ({\
18   uns _len = vsprintf(ptr+1, mask, va) + 1;     \
19   *ptr = _len;                  \
20   ptr += _len;                  \
21   *ptr++ = type;                \
22 })      // requires _len < 127 !
23
24 #define PRINTF_V33(ptr, type, mask...)  ({\
25   uns _len = sprintf(ptr+1, mask) + 1;  \
26   *ptr = _len;                  \
27   ptr += _len;                  \
28   *ptr++ = type;                \
29 })      // requires _len < 127 !
30
31 #define PUTL_V33(ptr, type, num)        PRINTF_V33(ptr, type, "%d", num)
32
33 #include "charset/unistream.h"
34
35 static inline void
36 bwrite_v33(struct fastbuf *b, uns type, byte *text, uns len)
37 {
38   bput_utf8(b, len+1);
39   bwrite(b, text, len);
40   bputc(b, type);
41 }
42
43 static inline void
44 bputs_v33(struct fastbuf *b, uns type, byte *text)
45 {
46   bwrite_v33(b, type, text, strlen(text));
47 }
48
49 #include <stdarg.h>
50
51 static void UNUSED
52 bprintf_v33(struct fastbuf *b, uns type, byte *mask, ...)
53   /* requires _len < 127 ! */
54 {
55   byte *ptr;
56   if (bdirect_write_prepare(b, &ptr) < 130)
57   {
58     bflush(b);
59     bdirect_write_prepare(b, &ptr);
60   }
61   va_list va;
62   va_start(va, mask);
63   VPRINTF_V33(ptr, type, mask, va);
64   bdirect_write_commit(b, ptr);
65 }
66
67 static inline void
68 bputl_v33(struct fastbuf *b, uns type, uns num)
69 {
70   byte *ptr;
71   if (bdirect_write_prepare(b, &ptr) < 20)
72   {
73     bflush(b);
74     bdirect_write_prepare(b, &ptr);
75   }
76   PUTL_V33(ptr, type, num);
77   bdirect_write_commit(b, ptr);
78 }