* Copyright (C) Jaakko Heinonen
*/
-#define ABOOK_SRC 1
-/*#undef ABOOK_SRC*/
-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
-#include "misc.h"
-#ifdef ABOOK_SRC
-# include "abook.h"
+#ifdef HANDLE_MULTIBYTE
+# include <mbswidth.h>
#endif
+#include "misc.h"
+#include "xmalloc.h"
#ifndef DEBUG
# define NDEBUG 1
#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)
{
assert(s != NULL);
- for(t = s; ISSPACE(*t); t++);
+ for(t = s; isspace(*t); t++);
memmove(s, t, strlen(t)+1);
for (tt = t = s; *t != '\0'; t++)
- if(!ISSPACE(*t))
+ if(!isspace(*t))
tt = t+1;
*tt = '\0';
mkstr (const char *format, ... )
{
MY_VA_LOCAL_DECL;
- int size = 100;
- char *buffer =
-#ifdef ABOOK_SRC
- (char *) abook_malloc (size);
-#else
- (char *) malloc (size);
-#endif
+ size_t size = 100;
+ char *buffer = xmalloc (size);
assert(format != NULL);
else
size *= 2;
- buffer =
-#ifdef ABOOK_SRC
- (char *) abook_realloc (buffer, size);
-#else
- (char *) realloc (buffer, size);
-#endif
+ buffer = xrealloc(buffer, size);
}
}
char*
strconcat (const char *str, ...)
{
- int l;
+ unsigned long l;
MY_VA_LOCAL_DECL;
char *s, *concat;
MY_VA_END;
concat = (char *)
-#ifdef ABOOK_SRC
- abook_malloc(l);
-#else
- malloc(l);
-#endif
+ xmalloc(l);
+ if(concat == NULL)
+ return NULL;
strcpy (concat, str);
MY_VA_START(str);
my_getcwd()
{
char *dir = NULL;
- int size = 100;
+ size_t size = 100;
- if( (dir = (char *)malloc(size)) == NULL)
+ if( (dir = xmalloc(size)) == NULL)
return NULL;
+
+ *dir = 0;
while( getcwd(dir, size) == NULL && errno == ERANGE )
- if( (dir = (char *)realloc(dir, size *=2)) == NULL)
+ if( (dir = xrealloc(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)
-#endif
char *
getaline(FILE *f)
len = 0;
size = thres;
- buf = (char *)abook_malloc(size);
+ buf = xmalloc(size);
while (fgets(buf+len, size-len, f) != NULL) {
len += strlen(buf+len);
break; /* the whole line has been read */
for (inc = size, p = NULL; inc > mininc; inc /= 2)
- if ((p = (char *)abook_realloc(buf, size + inc)) !=
+ if ((p = xrealloc_inc(buf, size, inc)) !=
NULL)
break;
}
if (len == 0) {
- free(buf);
+ xfree(buf);
return NULL; /* nothing read (eof or error) */
}
buf[--len] = '\0';
if (size - len > mucho) { /* a plenitude of unused memory? */
- p = (char *)abook_realloc(buf, len+1);
+ p = xrealloc_inc(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