From: Pavel Charvat Date: Thu, 19 Oct 2006 08:34:16 +0000 (+0200) Subject: Completed merge with dev-img branch. X-Git-Tag: holmes-import~507^2~41 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=e30a6049a172b94b9e54d502f7b8d96746cd2187;p=libucw.git Completed merge with dev-img branch. - libimages library - gatherer uses libimages instead of GraphicsMagick - added indexation of image signatures and support for similar images to search server (IMAGESIM) - CZ indexation split to text and images - ... and more small changes --- diff --git a/lib/Makefile b/lib/Makefile index 775c7de6..15dc0a24 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -28,7 +28,8 @@ LIBUCW_MODS= \ base64 base224 \ sync \ qache \ - string + string \ + bbuf LIBUCW_INCLUDES= \ lib.h config.h math.h \ @@ -80,7 +81,7 @@ $(o)/lib/lizard-test: $(o)/lib/lizard-test.o $(LIBUCW) $(o)/lib/kmp-test: $(o)/lib/kmp-test.o $(LIBUCW) $(LIBCHARSET) $(o)/lib/ipaccess-test: $(o)/lib/ipaccess-test.o $(LIBUCW) -TESTS+=$(addprefix $(o)/lib/,regex.test unicode-utf8.test hash-test.test mempool.test stkstring.test slists.test kmp-test.test) +TESTS+=$(addprefix $(o)/lib/,regex.test unicode-utf8.test hash-test.test mempool.test stkstring.test slists.test kmp-test.test bbuf.test) $(o)/lib/regex.test: $(o)/lib/regex-t $(o)/lib/unicode-utf8.test: $(o)/lib/unicode-utf8-t $(o)/lib/hash-test.test: $(o)/lib/hash-test @@ -89,6 +90,7 @@ $(o)/lib/stkstring.test: $(o)/lib/stkstring-t $(o)/lib/bitops.test: $(o)/lib/bit-ffs-t $(o)/lib/bit-fls-t $(o)/lib/slists.test: $(o)/lib/slists-t $(o)/lib/kmp-test.test: $(o)/lib/kmp-test +$(o)/lib/bbuf.test: $(o)/lib/bbuf-t INCLUDES+=$(o)/lib/.include-stamp $(o)/lib/.include-stamp: $(addprefix $(s)/lib/,$(LIBUCW_INCLUDES)) diff --git a/lib/bbuf.c b/lib/bbuf.c new file mode 100644 index 00000000..fae58700 --- /dev/null +++ b/lib/bbuf.c @@ -0,0 +1,86 @@ +/* + * UCW Library -- A simple growing buffers for byte-sized items + * + * (c) 2006 Pavel Charvat + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" +#include "lib/bbuf.h" + +#include + +char * +bb_vprintf_at(bb_t *bb, uns ofs, char *fmt, va_list args) +{ + bb_grow(bb, ofs + 1); + va_list args2; + va_copy(args2, args); + int cnt = vsnprintf(bb->ptr + ofs, bb->len - ofs, fmt, args2); + va_end(args2); + if (cnt < 0) + { + /* Our C library doesn't support C99 return value of vsnprintf, so we need to iterate */ + do + { + bb_do_grow(bb, bb->len + 1); + va_copy(args2, args); + cnt = vsnprintf(bb->ptr + ofs, bb->len - ofs, fmt, args2); + va_end(args2); + } + while (cnt < 0); + } + else if ((uns)cnt >= bb->len - ofs) + { + bb_do_grow(bb, ofs + cnt + 1); + va_copy(args2, args); + int cnt2 = vsnprintf(bb->ptr + ofs, bb->len - ofs, fmt, args2); + va_end(args2); + ASSERT(cnt2 == cnt); + } + return bb->ptr + ofs; +} + +char * +bb_printf_at(bb_t *bb, uns ofs, char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + char *res = bb_vprintf_at(bb, ofs, fmt, args); + va_end(args); + return res; +} + +char * +bb_vprintf(bb_t *bb, char *fmt, va_list args) +{ + return bb_vprintf_at(bb, 0, fmt, args); +} + +char * +bb_printf(bb_t *bb, char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + char *res = bb_vprintf_at(bb, 0, fmt, args); + va_end(args); + return res; +} + +#ifdef TEST + +int main(void) +{ + bb_t bb; + bb_init(&bb); + char *x = bb_printf(&bb, "", "World"); + fputs(x, stdout); + x = bb_printf_at(&bb, 5, "\n", "World"); + fputs(x, stdout); + bb_done(&bb); + return 0; +} + +#endif diff --git a/lib/bbuf.h b/lib/bbuf.h index 0d17f8cc..2585bec3 100644 --- a/lib/bbuf.h +++ b/lib/bbuf.h @@ -14,4 +14,9 @@ #define GBUF_PREFIX(x) bb_##x #include "lib/gbuf.h" +char *bb_vprintf(bb_t *bb, char *fmt, va_list args); +char *bb_printf(bb_t *bb, char *fmt, ...); +char *bb_vprintf_at(bb_t *bb, uns ofs, char *fmt, va_list args); +char *bb_printf_at(bb_t *bb, uns ofs, char *fmt, ...); + #endif diff --git a/lib/bbuf.t b/lib/bbuf.t new file mode 100644 index 00000000..cfc70b31 --- /dev/null +++ b/lib/bbuf.t @@ -0,0 +1,4 @@ +# Tests for growing buffers + +Run: obj/lib/bbuf-t +Out: diff --git a/lib/clists.h b/lib/clists.h index 9c0f816a..4f1848d8 100644 --- a/lib/clists.h +++ b/lib/clists.h @@ -48,6 +48,8 @@ static inline int clist_empty(clist *l) #define CLIST_FOR_EACH(type,n,list) for(type n=(void*)(list).head.next; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->next) #define CLIST_FOR_EACH_DELSAFE(type,n,list,tmp) for(type n=(void*)(list).head.next; tmp=(void*)((cnode*)(n))->next, (cnode*)(n) != &(list).head; n=(void*)tmp) +#define CLIST_FOR_EACH_BACKWARDS(type,n,list) for(type n=(void*)(list).head.prev; (cnode*)(n) != &(list).head; n=(void*)((cnode*)(n))->prev) + static inline void clist_insert_after(cnode *what, cnode *after) { cnode *before = after->next; diff --git a/lib/sorter.h b/lib/sorter.h index 64092d6c..11daf95a 100644 --- a/lib/sorter.h +++ b/lib/sorter.h @@ -50,7 +50,7 @@ * int PREFIX_compare(SORT_KEY *a, *b) * compare two keys, result like strcmp * int PREFIX_fetch_key(struct fastbuf *f, SORT_KEY *k) - * fetch next key, returns 1=ok, 0=eof + * fetch next key, returns nonzero=ok, 0=eof * void PREFIX_copy_data(struct fastbuf *src, *dest, SORT_KEY *k) * write just fetched key k to dest and copy all data * belonging to this key from src to dest.