# HG changeset patch # User David Demelier # Date 1336687837 -7200 # Node ID 0cf53c588a83c0eb4dc126e943fdd4cf5d3fde0f # Parent d62f2f657768d930a93b204275f79c374bea769f# Parent e3cf5ac9a5aac617ed083b82df10859b30b8e990 Merge diff -r d62f2f657768 -r 0cf53c588a83 array.c --- a/array.c Fri May 11 00:09:30 2012 +0200 +++ b/array.c Fri May 11 00:10:37 2012 +0200 @@ -86,7 +86,7 @@ /* * Add to the head of array. NOTE: this may be very slow when adding a lot * of object (about 100000). If you need to add a lot of data please consider - * using linked list instead. + * using linked list instead. Returns -1 on failure or 0 on success. */ int @@ -103,7 +103,7 @@ /* * Insert the data at the specified index. The function returns -1 on - * allocation failure or when the index is outof bounds otherwise 0 is returned. + * allocation failure or the position of the added element. */ int @@ -119,11 +119,12 @@ (char *)arr->data + OFFSET(index), OFFSET(arr->length++ - index)); memcpy((char *)arr->data + OFFSET(index), data, arr->unit); - return 0; + return index; } /* - * Append the data to the end of array. + * Append the data to the end of array. Returns -1 on failure or the position + * of the added element. */ int @@ -134,7 +135,7 @@ memcpy((char *)arr->data + OFFSET(arr->length++), data, arr->unit); - return 0; + return (arr->length - 1); } /* @@ -267,7 +268,7 @@ */ void -array_map(const struct array *arr, array_map_fn fn, void *udata) +array_map(const struct array *arr, array_map_t fn, void *udata) { int i; @@ -282,7 +283,7 @@ */ int -array_find(const struct array *arr, array_cmp_fn fn, void *dst, void *u) +array_find(const struct array *arr, array_cmp_t fn, void *dst, void *u) { int st, i; diff -r d62f2f657768 -r 0cf53c588a83 array.h --- a/array.h Fri May 11 00:09:30 2012 +0200 +++ b/array.h Fri May 11 00:10:37 2012 +0200 @@ -49,8 +49,8 @@ void * (*realloc)(void *, size_t); }; -typedef void (*array_map_fn)(void *, void *); -typedef int (*array_cmp_fn)(void *, void *); +typedef void (*array_map_t)(void *, void *); +typedef int (*array_cmp_t)(void *, void *); int array_init(struct array *, size_t); @@ -86,10 +86,10 @@ array_pswap(struct array *, const void *, const void *); void -array_map(const struct array *, array_map_fn, void *); +array_map(const struct array *, array_map_t, void *); int -array_find(const struct array *, array_cmp_fn, void *, void *); +array_find(const struct array *, array_cmp_t, void *, void *); void * array_first(const struct array *); diff -r d62f2f657768 -r 0cf53c588a83 buf.c --- a/buf.c Fri May 11 00:09:30 2012 +0200 +++ b/buf.c Fri May 11 00:10:37 2012 +0200 @@ -130,7 +130,9 @@ int buf_putc(struct buf *buf, int c) { - return buf_printf(buf, "%c", c); + char str[2] = { c, '\0' }; + + return buf_ncat(buf, str, 1); } /* @@ -204,6 +206,28 @@ } /* + * Remove `n' characters from the buffer, a positive value will cut the string + * from beginning, negative value will cut from end. + */ + +void +buf_cut(struct buf *buf, int n) +{ + int pos; + + if (n < 0 || (unsigned int)n >= buf->length) + return; + + if (n < 0 && buf->length - (-n) > 0) + pos = buf->length - (-n); + else if ((unsigned int)n < buf->length) + pos = n; + + buf->text[pos] = '\0'; + buf->length -= buf->length - pos; +} + +/* * Clear the string buffer. */ diff -r d62f2f657768 -r 0cf53c588a83 buf.h --- a/buf.h Fri May 11 00:09:30 2012 +0200 +++ b/buf.h Fri May 11 00:10:37 2012 +0200 @@ -31,10 +31,6 @@ #ifdef __GNUC__ # define _buf_at_printf(i1, i2) __attribute__ ((format (printf, i1, i2))) -# if __GNUC_MINOR__ > 2 -# pragma GCC diagnostic push -# pragma GCC diagnostic warning "-Wformat" -# endif #else # define _buf_at_printf(i1, i2) #endif @@ -82,15 +78,14 @@ buf_trim(struct buf *); void +buf_cut(struct buf *, int); + +void buf_clear(struct buf *); void buf_free(struct buf *); -#if defined(__GNUC__) && __GNUC_MINOR__ > 2 -# pragma GCC diagnostic pop -#endif - #ifdef __cplusplus } #endif diff -r d62f2f657768 -r 0cf53c588a83 ini.h --- a/ini.h Fri May 11 00:09:30 2012 +0200 +++ b/ini.h Fri May 11 00:10:37 2012 +0200 @@ -124,52 +124,6 @@ void ini_free(struct ini_config *, int); -#if 0 - -/* -------------------------------------------------------- - * Convenient api to query and convert data - * -------------------------------------------------------- */ - -/* - * For the config, read all available values and store them in - * the array ini_handler. - */ - -void -ini_dispatch(INI_Section *, INI_Handler *, int); - -/* - * Convert to bool. dst must be (char *). - * It converts "yes" "true" "1" or opposites. Only lower - * case is supported right now. - */ - -void -ini_convert_bool(void *, const char *, void *); - -/* - * Convert to an int. dst must be (int *). - */ - -void -ini_convert_int(void *, const char *, void *); - -/* - * Convert to a short. dst must be (short *). - */ - -void -ini_convert_short(void *, const char *, void *); - -/* - * Convert to a char *. dst must be (char **). This - * function uses strdup(). You need to free the dst - * pointer. - */ - -void -ini_convert_string(void *, const char *, void *); - #endif #ifdef __cplusplus diff -r d62f2f657768 -r 0cf53c588a83 pack.c --- a/pack.c Fri May 11 00:09:30 2012 +0200 +++ b/pack.c Fri May 11 00:10:37 2012 +0200 @@ -394,7 +394,8 @@ continue; for (i = 0; i < nelem; ++i) { - fread(&ptr[tocopy * i], tocopy, 1, fp); + if (fread(&ptr[tocopy * i], tocopy, 1, fp) <= 0) + return -1; /* Convert if needed */ convert = pack_getconvert_by_tok(tok); diff -r d62f2f657768 -r 0cf53c588a83 parray.c --- a/parray.c Fri May 11 00:09:30 2012 +0200 +++ b/parray.c Fri May 11 00:10:37 2012 +0200 @@ -82,7 +82,7 @@ /* * Add to the head of array. NOTE: this may be very slow when adding a lot * of object (about 100000). If you need to add a lot of data please consider - * using linked list instead. + * using linked list instead. Returns -1 on failure or 0 on success. */ int @@ -102,7 +102,7 @@ /* * Insert the data at the specified index. The function returns -1 on - * allocation failure. + * allocation failure or the position of the added element. */ int @@ -121,11 +121,12 @@ if (arr->flags & PARRAY_NULLEND) arr->data[arr->length] = NULL; - return 0; + return index; } /* - * Append the data to the end of array. + * Append the data to the end of array. Returns -1 on failure or the position + * of the added element. */ int @@ -139,7 +140,7 @@ if (arr->flags & PARRAY_NULLEND) arr->data[arr->length] = NULL; - return 0; + return (arr->length - 1); } /* @@ -253,7 +254,7 @@ */ void -parray_map(const struct parray *arr, parray_map_fn fn, void *udata) +parray_map(const struct parray *arr, parray_map_t fn, void *udata) { int i; @@ -269,7 +270,7 @@ */ int -parray_find(const struct parray *arr, parray_cmp_fn fn, void *ptr, void *u) +parray_find(const struct parray *arr, parray_cmp_t fn, void *ptr, void *u) { int st, i; diff -r d62f2f657768 -r 0cf53c588a83 parray.h --- a/parray.h Fri May 11 00:09:30 2012 +0200 +++ b/parray.h Fri May 11 00:10:37 2012 +0200 @@ -48,8 +48,8 @@ void * (*realloc)(void *, size_t); }; -typedef void (*parray_map_fn)(void *, void *); -typedef int (*parray_cmp_fn)(void *, void *); +typedef void (*parray_map_t)(void *, void *); +typedef int (*parray_cmp_t)(void *, void *); int parray_init(struct parray *); @@ -85,10 +85,10 @@ parray_pswap(struct parray *, const void *, const void *); void -parray_map(const struct parray *, parray_map_fn, void *); +parray_map(const struct parray *, parray_map_t, void *); int -parray_find(const struct parray *, parray_cmp_fn, void *, void *); +parray_find(const struct parray *, parray_cmp_t, void *, void *); void * parray_first(const struct parray *);