From: Robert Spalek Date: Sun, 4 Jul 2004 11:29:54 +0000 (+0000) Subject: writing attributes to buckets totally rewritten according to MJ's remarks X-Git-Tag: holmes-import~977 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=4aa712644886cd4ebb82803a19aba6d39eaec8c2;p=libucw.git writing attributes to buckets totally rewritten according to MJ's remarks --- diff --git a/lib/obj2buck.c b/lib/obj2buck.c new file mode 100644 index 00000000..afd4c1df --- /dev/null +++ b/lib/obj2buck.c @@ -0,0 +1,171 @@ +/* + * Generating V33 buckets + * + * (c) 2004, Robert Spalek + */ + +#include "lib/lib.h" +#include "lib/fastbuf.h" +#include "lib/obj2buck.h" +#include "lib/bucket.h" +#include "charset/unistream.h" + +#include +#include + +static uns use_v33; + +void +attr_set_type(uns type) +{ + use_v33 = (type == BUCKET_TYPE_V33 || type == BUCKET_TYPE_V33_LIZARD); +} + +inline byte * +put_attr(byte *ptr, uns type, byte *val, uns len) +{ + if (use_v33) + { + PUT_UTF8(ptr, len+1); + memcpy(ptr, val, len); + ptr += len; + *ptr++ = type; + } + else + { + *ptr++ = type; + memcpy(ptr, val, len); + ptr += len; + *ptr++ = '\n'; + } + return ptr; +} + +byte * +put_attr_str(byte *ptr, uns type, byte *val) +{ + return put_attr(ptr, type, val, strlen(val)); +} + +inline byte * +put_attr_vformat(byte *ptr, uns type, byte *mask, va_list va) +{ + if (use_v33) + { + uns len = vsprintf(ptr+1, mask, va); + if (len >= 127) + { + byte tmp[6], *tmp_end = tmp; + PUT_UTF8(tmp_end, len+1); + uns l = tmp_end - tmp; + memmove(ptr+l, ptr+1, len); + memcpy(ptr, tmp, l); + ptr += l + len; + } + else + { + *ptr = len+1; + ptr += len+1; + } + *ptr++ = type; + } + else + { + *ptr++ = type; + ptr += vsprintf(ptr, mask, va); + *ptr++ = '\n'; + } + return ptr; +} + +byte * +put_attr_format(byte *ptr, uns type, char *mask, ...) +{ + va_list va; + va_start(va, mask); + byte *ret = put_attr_vformat(ptr, type, mask, va); + va_end(va); + return ret; +} + +byte * +put_attr_num(byte *ptr, uns type, uns val) +{ + if (use_v33) + { + uns len = sprintf(ptr+1, "%d", val) + 1; + *ptr = len; + ptr += len; + *ptr++ = type; + } + else + ptr += sprintf(ptr, "%c%d\n", type, val); + return ptr; +} + +inline void +bput_attr(struct fastbuf *b, uns type, byte *val, uns len) +{ + if (use_v33) + { + bput_utf8(b, len+1); + bwrite(b, val, len); + bputc(b, type); + } + else + { + bputc(b, type); + bwrite(b, val, len); + bputc(b, '\n'); + } +} + +void +bput_attr_str(struct fastbuf *b, uns type, byte *val) +{ + bput_attr(b, type, val, strlen(val)); +} + +inline void +bput_attr_vformat(struct fastbuf *b, uns type, byte *mask, va_list va) +{ + if (use_v33) + { + int len = vsnprintf(NULL, 0, mask, va); + if (len < 0) + die("vsnprintf() does not support size=0"); + bput_utf8(b, len+1); + vbprintf(b, mask, va); + bputc(b, type); + } + else + { + bputc(b, type); + vbprintf(b, mask, va); + bputc(b, '\n'); + } +} + +void +bput_attr_format(struct fastbuf *b, uns type, char *mask, ...) +{ + va_list va; + va_start(va, mask); + bput_attr_vformat(b, type, mask, va); + va_end(va); +} + +void +bput_attr_num(struct fastbuf *b, uns type, uns val) +{ + if (use_v33) + { + byte tmp[12]; + uns len = sprintf(tmp, "%d", val); + bputc(b, len+1); + bwrite(b, tmp, len); + bputc(b, type); + } + else + bprintf(b, "%c%d\n", type, val); +} diff --git a/lib/obj2buck.h b/lib/obj2buck.h index 6f7b335a..0b6f72ee 100644 --- a/lib/obj2buck.h +++ b/lib/obj2buck.h @@ -4,75 +4,17 @@ * (c) 2004, Robert Spalek */ -#define WRITE_V33(ptr, type, text, len) ({\ - uns _len = len; \ - PUT_UTF8(ptr, _len+1); \ - memcpy(ptr, text, _len); \ - ptr += _len; \ - *ptr++ = type; \ -}) - -#define PUTS_V33(ptr, type, text) WRITE_V33(ptr, type, text, strlen(text)) - -#define VPRINTF_V33(ptr, type, mask, va) ({\ - uns _len = vsprintf(ptr+1, mask, va) + 1; \ - *ptr = _len; \ - ptr += _len; \ - *ptr++ = type; \ -}) // requires _len < 127 ! - -#define PRINTF_V33(ptr, type, mask...) ({\ - uns _len = sprintf(ptr+1, mask) + 1; \ - *ptr = _len; \ - ptr += _len; \ - *ptr++ = type; \ -}) // requires _len < 127 ! - -#define PUTL_V33(ptr, type, num) PRINTF_V33(ptr, type, "%d", num) - -#include "charset/unistream.h" - -static inline void -bwrite_v33(struct fastbuf *b, uns type, byte *text, uns len) -{ - bput_utf8(b, len+1); - bwrite(b, text, len); - bputc(b, type); -} - -static inline void -bputs_v33(struct fastbuf *b, uns type, byte *text) -{ - bwrite_v33(b, type, text, strlen(text)); -} - -#include - -static void UNUSED -bprintf_v33(struct fastbuf *b, uns type, byte *mask, ...) - /* requires _len < 127 ! */ -{ - byte *ptr; - if (bdirect_write_prepare(b, &ptr) < 130) - { - bflush(b); - bdirect_write_prepare(b, &ptr); - } - va_list va; - va_start(va, mask); - VPRINTF_V33(ptr, type, mask, va); - bdirect_write_commit(b, ptr); -} - -static inline void -bputl_v33(struct fastbuf *b, uns type, uns num) -{ - byte *ptr; - if (bdirect_write_prepare(b, &ptr) < 20) - { - bflush(b); - bdirect_write_prepare(b, &ptr); - } - PUTL_V33(ptr, type, num); - bdirect_write_commit(b, ptr); -} +void attr_set_type(uns type); + +byte *put_attr(byte *ptr, uns type, byte *val, uns len); +byte *put_attr_str(byte *ptr, uns type, byte *val); +byte *put_attr_vformat(byte *ptr, uns type, byte *mask, va_list va); +byte *put_attr_format(byte *ptr, uns type, char *mask, ...) __attribute__((format(printf,3,4))); +byte *put_attr_num(byte *ptr, uns type, uns val); + +struct fastbuf; +void bput_attr(struct fastbuf *b, uns type, byte *val, uns len); +void bput_attr_str(struct fastbuf *b, uns type, byte *val); +void bput_attr_vformat(struct fastbuf *b, uns type, byte *mask, va_list va); +void bput_attr_format(struct fastbuf *b, uns type, char *mask, ...) __attribute__((format(printf,3,4))); +void bput_attr_num(struct fastbuf *b, uns type, uns val);