X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=misc.c;h=a44c9d2f96a7bfafe721d95120c8c64acfb2cc76;hb=b1b41dce461ab21ae9a2bd26d7a00a381a5f81f8;hp=8a4b9cfc5d2657fbb8ed69076c849d88b1efa5f6;hpb=efc9a360c599ac2317853a6ff3ca6c3d208b7316;p=pkg%2Fabook.git diff --git a/misc.c b/misc.c index 8a4b9cf..a44c9d2 100644 --- a/misc.c +++ b/misc.c @@ -19,6 +19,10 @@ #ifdef HAVE_CONFIG_H # include "config.h" #endif +#ifdef HANDLE_MULTIBYTE +# include +# include "mbswidth.h" +#endif #include "misc.h" #ifdef ABOOK_SRC # include "abook.h" @@ -85,12 +89,12 @@ char * mkstr (const char *format, ... ) { MY_VA_LOCAL_DECL; - int size = 100; + size_t size = 100; char *buffer = #ifdef ABOOK_SRC (char *) abook_malloc (size); #else - (char *) malloc (size); + (char *) xmalloc (size); #endif assert(format != NULL); @@ -114,7 +118,7 @@ mkstr (const char *format, ... ) #ifdef ABOOK_SRC (char *) abook_realloc (buffer, size); #else - (char *) realloc (buffer, size); + (char *) xrealloc (buffer, size); #endif } } @@ -142,7 +146,7 @@ strconcat (const char *str, ...) #ifdef ABOOK_SRC abook_malloc(l); #else - malloc(l); + xmalloc(l); #endif if(concat == NULL) return NULL; @@ -188,7 +192,7 @@ char * my_getcwd() { char *dir = NULL; - int size = 100; + size_t size = 100; if( (dir = (char *)malloc(size)) == NULL) return NULL; @@ -204,8 +208,8 @@ my_getcwd() #define INITIAL_SIZE 128 #ifndef ABOOK_SRC -# define abook_malloc(X) malloc(X) -# define abook_realloc(X, XX) realloc(X, XX) +# define abook_malloc(X) xmalloc(X) +# define abook_realloc(X, XX) xrealloc(X, XX) #endif char * @@ -265,6 +269,28 @@ getaline(FILE *f) return buf; } +int +strwidth(const char *s) +{ + assert(s); +#ifdef HANDLE_MULTIBYTE + return (int)mbswidth(s, 0); +#else + return strlen(s); +#endif +} + +int +bytes2width(const char *s, int width) +{ + assert(s); +#ifdef HANDLE_MULTIBYTE + return mbsnbytes(s, strlen(s), width, 0); +#else + return width; +#endif +} + /************************************************************** * Original: * Patrick Powell Tue Apr 11 09:48:21 PDT 1995