]> git.deb.at Git - pkg/abook.git/blobdiff - misc.c
Imported Upstream version 0.5.3
[pkg/abook.git] / misc.c
diff --git a/misc.c b/misc.c
index 919e5db3223b51638d0763d8c32fb631d349c2f6..24123a63fa0e7a4ab54bbf00350a8ed92b538a47 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -1,6 +1,6 @@
 
 /*
- * $Id: misc.c,v 1.7 2001/12/19 20:20:55 jheinonen Exp $
+ * $Id: misc.c,v 1.14 2004/02/18 19:40:57 jheinonen Exp $
  *
  * by JH <jheinonen@users.sourceforge.net>
  *
@@ -19,6 +19,9 @@
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
+#ifdef HANDLE_MULTIBYTE
+#      include <mbswidth.h>
+#endif
 #include "misc.h"
 #ifdef ABOOK_SRC
 #      include "abook.h"
 
 #include <assert.h>
 
-char *
-revstr(char *str)
-{
-       char *s, *s2;
-
-       assert(str != NULL);
-
-       s = s2 = strdup(str);
-
-       while( *str )
-               str++;
-
-       while( *s )
-               *--str = *s++;
-
-       free(s2);
-       return str;
-}
-
-char *
-strupper(char *str)
-{
-       char *tmp = str;
-
-       assert(str != NULL);
-
-       while( ( *str = toupper( *str ) ) )
-               str++;
-       
-       return tmp;
-}
-
 char *
 strlower(char *str)
 {
@@ -117,12 +88,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);
@@ -146,7 +117,7 @@ mkstr (const char *format, ... )
 #ifdef ABOOK_SRC
                        (char *) abook_realloc (buffer, size);
 #else
-                       (char *) realloc (buffer, size);
+                       (char *) xrealloc (buffer, size);
 #endif
        }
 }
@@ -155,7 +126,7 @@ mkstr (const char *format, ... )
 char*
 strconcat (const char *str, ...)
 {
-       int   l;
+       unsigned long l;
        MY_VA_LOCAL_DECL;
        char *s, *concat;
 
@@ -174,8 +145,10 @@ strconcat (const char *str, ...)
 #ifdef ABOOK_SRC
        abook_malloc(l);
 #else
-       malloc(l);
+       xmalloc(l);
 #endif
+       if(concat == NULL)
+               return NULL;
 
        strcpy (concat, str);
        MY_VA_START(str);
@@ -218,13 +191,15 @@ char *
 my_getcwd()
 {
        char *dir = NULL;
-       int size = 100;
+       size_t size = 100;
 
-       if( (dir = malloc(size)) == NULL)
+       if( (dir = (char *)malloc(size)) == NULL)
                return NULL;
+
+       *dir = 0;
        
        while( getcwd(dir, size) == NULL && errno == ERANGE )
-               if( (dir = realloc(dir, size *=2)) == NULL)
+               if( (dir = (char *)realloc(dir, size *=2)) == NULL)
                        return NULL;
 
        return dir;
@@ -232,8 +207,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 *
@@ -266,7 +241,8 @@ getaline(FILE *f)
                        break;          /* the whole line has been read */
 
                for (inc = size, p = NULL; inc > mininc; inc /= 2)
-                       if ((p = abook_realloc(buf, size + inc)) != NULL)
+                       if ((p = (char *)abook_realloc(buf, size + inc)) !=
+                                       NULL)
                                break;
 
                size += inc;
@@ -282,7 +258,7 @@ getaline(FILE *f)
                buf[--len] = '\0';
 
        if (size - len > mucho) { /* a plenitude of unused memory? */
-               p = abook_realloc(buf, len+1);
+               p = (char *)abook_realloc(buf, len+1);
                if (p != NULL) {
                        buf = p;
                        size = len+1;
@@ -292,6 +268,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