#include <ctype.h>
#include <unistd.h>
#include <errno.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+#ifdef HANDLE_MULTIBYTE
+# include <wchar.h>
+# include "mbswidth.h"
+#endif
#include "misc.h"
#ifdef ABOOK_SRC
# include "abook.h"
#include <assert.h>
-char *
-revstr(char *str)
-{
- char *s, *s2;
-
- s = s2 = strdup(str);
-
- while( *str )
- str++;
-
- while( *s )
- *--str = *s++;
-
- free(s2);
- return str;
-}
-
-char *
-strupper(char *str)
-{
- char *tmp = str;
-
- while( ( *str = toupper( *str ) ) )
- str++;
-
- return tmp;
-}
-
char *
strlower(char *str)
{
char *tmp = str;
+ assert(str != NULL);
+
while( ( *str = tolower ( *str ) ) )
str++;
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);
+
for(;;) {
int n;
MY_VA_START(format);
#ifdef ABOOK_SRC
(char *) abook_realloc (buffer, size);
#else
- (char *) realloc (buffer, size);
+ (char *) xrealloc (buffer, size);
#endif
}
}
char*
strconcat (const char *str, ...)
{
- int l;
+ unsigned long l;
MY_VA_LOCAL_DECL;
char *s, *concat;
- if(str == NULL)
- return NULL;
+ assert(str != NULL);
l = 1 + strlen (str);
MY_VA_START(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);
int
-safe_strcmp(const char *s1, const char * s2)
+safe_strcmp(const char *s1, const char *s2)
{
if (s1 == NULL && s2 == NULL) return 0;
if (s1 == NULL) return -1;
return strcmp(s1, s2);
}
+int
+safe_strcoll(const char *s1, const char *s2)
+{
+#ifdef HAVE_STRCOLL
+ if (s1 == NULL && s2 == NULL) return 0;
+ if (s1 == NULL) return -1;
+ if (s2 == NULL) return 1;
+
+ return strcoll(s1, s2);
+#else /* fall back to strcmp */
+ return safe_strcmp(s1, s2);
+#endif
+}
+
char *
my_getcwd()
{
char *dir = NULL;
- int size = 100;
+ size_t size = 100;
+
+ if( (dir = (char *)malloc(size)) == NULL)
+ return NULL;
- dir = malloc(size);
+ *dir = 0;
while( getcwd(dir, size) == NULL && errno == ERANGE )
- dir = realloc(dir, size *=2);
+ if( (dir = (char *)realloc(dir, size *=2)) == NULL)
+ return NULL;
return dir;
}
#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 *
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;
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;
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