From 8b7093aff990803877b20e86826f0693817cbcaf Mon Sep 17 00:00:00 2001 From: pdmef Date: Tue, 22 Mar 2005 00:44:54 +0000 Subject: [PATCH] Rocco Rutte: move string functions from lib to ./lib git-svn-id: svn://svn.berlios.de/mutt-ng/trunk@223 e385b8ad-14ed-0310-8656-cc95a2468c6d --- Makefile.am | 4 +- globals.h | 6 ++ lib.c | 174 ------------------------------------------- lib.h | 45 +----------- lib/Makefile.am | 4 +- lib/str.c | 191 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/str.h | 50 ++++++++++++- 7 files changed, 250 insertions(+), 224 deletions(-) create mode 100644 lib/str.c diff --git a/Makefile.am b/Makefile.am index db773aa..45bfa78 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,8 +43,8 @@ muttng_SOURCES = $(BUILT_SOURCES) \ muttng_LDADD = @MUTT_LIB_OBJECTS@ @LIBOBJS@ $(LIBIMAP) $(LIBPOP) $(LIBNNTP) \ -Llib -lsane $(MUTTLIBS) $(INTLLIBS) $(LIBICONV) -muttng_DEPENDENCIES = @MUTT_LIB_OBJECTS@ @LIBOBJS@ $(LIBIMAPDEPS) $(LIBPOPDEPS) $(LIBNNTPDEPS) \ - $(INTLDEPS) +muttng_DEPENDENCIES = @MUTT_LIB_OBJECTS@ @LIBOBJS@ $(top_srcdir)/lib/libsane.a \ + $(LIBIMAPDEPS) $(LIBPOPDEPS) $(LIBNNTPDEPS) $(INTLDEPS) makedoc_SOURCES = makedoc.c makedoc_LDADD = diff --git a/globals.h b/globals.h index 88782e7..b7ba1b8 100644 --- a/globals.h +++ b/globals.h @@ -6,6 +6,10 @@ * It's licensed under the GNU General Public License, * please see the file GPL in the top level source directory. */ +#ifndef _GLOBALS_H +#define _GLOBALS_H + +#include "lib/str.h" WHERE void (*mutt_error) (const char *, ...); WHERE void (*mutt_message) (const char *, ...); @@ -300,3 +304,5 @@ extern const char *Months[]; #include "sort.h" #include "mutt_crypt.h" #endif /* MAIN_C */ + +#endif /* !_GLOBALS_H */ diff --git a/lib.c b/lib.c index 937ffcc..c5adcc3 100644 --- a/lib.c +++ b/lib.c @@ -56,84 +56,6 @@ int safe_fclose (FILE ** f) return r; } -char *safe_strdup (const char *s) -{ - char *p; - size_t l; - - if (!s || !*s) - return 0; - l = mutt_strlen (s) + 1; - p = (char *) safe_malloc (l); - memcpy (p, s, l); - return (p); -} - -char *safe_strcat (char *d, size_t l, const char *s) -{ - char *p = d; - - if (!l) - return d; - - l--; /* Space for the trailing '\0'. */ - - for (; *d && l; l--) - d++; - for (; *s && l; l--) - *d++ = *s++; - - *d = '\0'; - - return p; -} - -char *safe_strncat (char *d, size_t l, const char *s, size_t sl) -{ - char *p = d; - - if (!l) - return d; - - l--; /* Space for the trailing '\0'. */ - - for (; *d && l; l--) - d++; - for (; *s && l && sl; l--, sl--) - *d++ = *s++; - - *d = '\0'; - - return p; -} - - -void mutt_str_replace (char **p, const char *s) -{ - FREE (p); - *p = safe_strdup (s); -} - -void mutt_str_adjust (char **p) -{ - if (!p || !*p) - return; - safe_realloc (p, mutt_strlen (*p) + 1); -} - -/* convert all characters in the string to lowercase */ -char *mutt_strlower (char *s) -{ - char *p = s; - - while (*p) { - *p = tolower ((unsigned char) *p); - p++; - } - - return (s); -} - void mutt_unlink (const char *s) { int fd; @@ -454,35 +376,6 @@ char *mutt_read_line (char *s, size_t * size, FILE * fp, int *line) } } -char *mutt_substrcpy (char *dest, const char *beg, const char *end, - size_t destlen) -{ - size_t len; - - len = end - beg; - if (len > destlen - 1) - len = destlen - 1; - memcpy (dest, beg, len); - dest[len] = 0; - return dest; -} - -char *mutt_substrdup (const char *begin, const char *end) -{ - size_t len; - char *p; - - if (end) - len = end - begin; - else - len = mutt_strlen (begin); - - p = safe_malloc (len + 1); - memcpy (p, begin, len); - p[len] = 0; - return p; -} - /* prepare a file name to survive the shell's quoting rules. * From the Unix programming FAQ by way of Liviu. */ @@ -518,73 +411,6 @@ size_t mutt_quote_filename (char *d, size_t l, const char *f) return j; } -/* NULL-pointer aware string comparison functions */ - -int mutt_strcmp (const char *a, const char *b) -{ - return strcmp (NONULL (a), NONULL (b)); -} - -int mutt_strcasecmp (const char *a, const char *b) -{ - return strcasecmp (NONULL (a), NONULL (b)); -} - -int mutt_strncmp (const char *a, const char *b, size_t l) -{ - return strncmp (NONULL (a), NONULL (b), l); -} - -int mutt_strncasecmp (const char *a, const char *b, size_t l) -{ - return strncasecmp (NONULL (a), NONULL (b), l); -} - -size_t mutt_strlen (const char *a) -{ - return a ? strlen (a) : 0; -} - -int mutt_strcoll (const char *a, const char *b) -{ - return strcoll (NONULL (a), NONULL (b)); -} - -const char *mutt_stristr (const char *haystack, const char *needle) -{ - const char *p, *q; - - if (!haystack) - return NULL; - if (!needle) - return (haystack); - - while (*(p = haystack)) { - for (q = needle; - *p && *q && - tolower ((unsigned char) *p) == tolower ((unsigned char) *q); - p++, q++); - if (!*q) - return (haystack); - haystack++; - } - return NULL; -} - -char *mutt_skip_whitespace (char *p) -{ - SKIPWS (p); - return p; -} - -void mutt_remove_trailing_ws (char *s) -{ - char *p; - - for (p = s + mutt_strlen (s) - 1; p >= s && ISSPACE (*p); p--) - *p = 0; -} - char *mutt_concat_path (char *d, const char *dir, const char *fname, size_t l) { const char *fmt = "%s/%s"; diff --git a/lib.h b/lib.h index a6eb67b..e07a410 100644 --- a/lib.h +++ b/lib.h @@ -32,25 +32,6 @@ # define TRUE 1 # define FALSE 0 -# define HUGE_STRING 5120 -# define LONG_STRING 1024 -# define STRING 256 -# define SHORT_STRING 128 - -/* - * Create a format string to be used with scanf. - * To use it, write, for instance, MUTT_FORMAT(HUGE_STRING). - * - * See K&R 2nd ed, p. 231 for an explanation. - */ -# define _MUTT_FORMAT_2(a,b) "%" a b -# define _MUTT_FORMAT_1(a, b) _MUTT_FORMAT_2(#a, b) -# define MUTT_FORMAT(a) _MUTT_FORMAT_1(a, "s") -# define MUTT_FORMAT2(a,b) _MUTT_FORMAT_1(a, b) - -# define ISSPACE(c) isspace((unsigned char)c) -# define strfcpy(A,B,C) strncpy(A,B,C), *(A+(C)-1)=0 - # undef MAX # undef MIN # define MAX(a,b) ((a) < (b) ? (b) : (a)) @@ -58,18 +39,6 @@ #define FOREVER while (1) -/* this macro must check for *c == 0 since isspace(0) has unreliable behavior - on some systems */ -# define SKIPWS(c) while (*(c) && isspace ((unsigned char) *(c))) c++; - -#define ISBLANK(c) (c == ' ' || c == '\t') -/* - * These functions aren't defined in lib.c, but - * they are used there. - * - * A non-mutt "implementation" (ahem) can be found in extlib.c. - */ - # ifndef _EXTLIB_C extern void (*mutt_error) (const char *, ...); # endif @@ -82,12 +51,6 @@ FILE *safe_fopen (const char *, const char *); char *mutt_concat_path (char *, const char *, const char *, size_t); char *mutt_read_line (char *, size_t *, FILE *, int *); char *mutt_skip_whitespace (char *); -char *mutt_strlower (char *); -char *mutt_substrcpy (char *, const char *, const char *, size_t); -char *mutt_substrdup (const char *, const char *); -char *safe_strcat (char *, size_t, const char *); -char *safe_strncat (char *, size_t, const char *, size_t); -char *safe_strdup (const char *); const char *mutt_stristr (const char *, const char *); const char *mutt_basename (const char *); @@ -95,11 +58,7 @@ const char *mutt_basename (const char *); int mutt_copy_stream (FILE *, FILE *); int mutt_copy_bytes (FILE *, FILE *, size_t); int mutt_rx_sanitize_string (char *, size_t, const char *); -int mutt_strcasecmp (const char *, const char *); -int mutt_strcmp (const char *, const char *); -int mutt_strncasecmp (const char *, const char *, size_t); -int mutt_strncmp (const char *, const char *, size_t); -int mutt_strcoll (const char *, const char *); + int safe_open (const char *, int); int safe_symlink (const char *, const char *); int safe_rename (const char *, const char *); @@ -111,8 +70,6 @@ size_t mutt_strlen (const char *); void mutt_nocurses_error (const char *, ...); void mutt_remove_trailing_ws (char *); void mutt_sanitize_filename (char *, short); -void mutt_str_replace (char **p, const char *s); -void mutt_str_adjust (char **p); void mutt_unlink (const char *); #endif diff --git a/lib/Makefile.am b/lib/Makefile.am index f26cca8..7d2b12e 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -9,5 +9,5 @@ INCLUDES = -I$(top_srcdir) -I../intl noinst_LIBRARIES = libsane.a noinst_HEADERS = mem.h str.h exit.h intl.h list.h -libsane_a_SOURCES = mem.c exit.c list.c \ - mem.h exit.h str.h intl.h list.h +libsane_a_SOURCES = mem.c exit.c str.c list.c \ + mem.h exit.h str.h list.h intl.h diff --git a/lib/str.c b/lib/str.c new file mode 100644 index 0000000..79bb469 --- /dev/null +++ b/lib/str.c @@ -0,0 +1,191 @@ +/* + * Copyright notice from original mutt: + * Copyright (C) 1996-2000 Michael R. Elkins + * Copyright (C) 1999-2000 Thomas Roessler + * + * This file is part of mutt-ng, see http://www.muttng.org/. + * It's licensed under the GNU General Public License, + * please see the file GPL in the top level source directory. + */ + +#include +#include +#include + +#include "str.h" + +#include "mem.h" + +char *safe_strdup (const char *s) +{ + char *p; + size_t l; + + if (!s || !*s) + return 0; + l = mutt_strlen (s) + 1; + p = (char *) safe_malloc (l); + memcpy (p, s, l); + return (p); +} + +char *safe_strcat (char *d, size_t l, const char *s) +{ + char *p = d; + + if (!l) + return d; + + l--; /* Space for the trailing '\0'. */ + + for (; *d && l; l--) + d++; + for (; *s && l; l--) + *d++ = *s++; + + *d = '\0'; + + return p; +} + +char *safe_strncat (char *d, size_t l, const char *s, size_t sl) +{ + char *p = d; + + if (!l) + return d; + + l--; /* Space for the trailing '\0'. */ + + for (; *d && l; l--) + d++; + for (; *s && l && sl; l--, sl--) + *d++ = *s++; + + *d = '\0'; + + return p; +} + +void mutt_str_replace (char **p, const char *s) +{ + FREE (p); + *p = safe_strdup (s); +} + +void mutt_str_adjust (char **p) +{ + if (!p || !*p) + return; + safe_realloc (p, mutt_strlen (*p) + 1); +} + +/* convert all characters in the string to lowercase */ +char *mutt_strlower (char *s) +{ + char *p = s; + + while (*p) { + *p = tolower ((unsigned char) *p); + p++; + } + + return (s); +} + +/* NULL-pointer aware string comparison functions */ + +char *mutt_substrcpy (char *dest, const char *beg, const char *end, + size_t destlen) +{ + size_t len; + + len = end - beg; + if (len > destlen - 1) + len = destlen - 1; + memcpy (dest, beg, len); + dest[len] = 0; + return dest; +} + +char *mutt_substrdup (const char *begin, const char *end) +{ + size_t len; + char *p; + + if (end) + len = end - begin; + else + len = mutt_strlen (begin); + + p = safe_malloc (len + 1); + memcpy (p, begin, len); + p[len] = 0; + return p; +} + +int mutt_strcmp (const char *a, const char *b) +{ + return strcmp (NONULL (a), NONULL (b)); +} + +int mutt_strcasecmp (const char *a, const char *b) +{ + return strcasecmp (NONULL (a), NONULL (b)); +} + +int mutt_strncmp (const char *a, const char *b, size_t l) +{ + return strncmp (NONULL (a), NONULL (b), l); +} + +int mutt_strncasecmp (const char *a, const char *b, size_t l) +{ + return strncasecmp (NONULL (a), NONULL (b), l); +} + +size_t mutt_strlen (const char *a) +{ + return a ? strlen (a) : 0; +} + +int mutt_strcoll (const char *a, const char *b) +{ + return strcoll (NONULL (a), NONULL (b)); +} + +const char *mutt_stristr (const char *haystack, const char *needle) +{ + const char *p, *q; + + if (!haystack) + return NULL; + if (!needle) + return (haystack); + + while (*(p = haystack)) { + for (q = needle; + *p && *q && + tolower ((unsigned char) *p) == tolower ((unsigned char) *q); + p++, q++); + if (!*q) + return (haystack); + haystack++; + } + return NULL; +} + +char *mutt_skip_whitespace (char *p) +{ + SKIPWS (p); + return p; +} + +void mutt_remove_trailing_ws (char *s) +{ + char *p; + + for (p = s + mutt_strlen (s) - 1; p >= s && ISSPACE (*p); p--) + *p = 0; +} + diff --git a/lib/str.h b/lib/str.h index b9adfec..cc2e3ac 100644 --- a/lib/str.h +++ b/lib/str.h @@ -8,11 +8,57 @@ * please see the file GPL in the top level source directory. */ -/* mutt functions which are generally useful. */ - #ifndef _LIB_STR_H #define _LIB_STR_H +#include + #define NONULL(x) x?x:"" +# define HUGE_STRING 5120 +# define LONG_STRING 1024 +# define STRING 256 +# define SHORT_STRING 128 + +/* + * Create a format string to be used with scanf. + * To use it, write, for instance, MUTT_FORMAT(HUGE_STRING). + * + * See K&R 2nd ed, p. 231 for an explanation. + */ +# define _MUTT_FORMAT_2(a,b) "%" a b +# define _MUTT_FORMAT_1(a, b) _MUTT_FORMAT_2(#a, b) +# define MUTT_FORMAT(a) _MUTT_FORMAT_1(a, "s") +# define MUTT_FORMAT2(a,b) _MUTT_FORMAT_1(a, b) + +# define ISSPACE(c) isspace((unsigned char)c) +# define strfcpy(A,B,C) strncpy(A,B,C), *(A+(C)-1)=0 + +/* this macro must check for *c == 0 since isspace(0) has unreliable behavior + on some systems */ +# define SKIPWS(c) while (*(c) && isspace ((unsigned char) *(c))) c++; + +#define ISBLANK(c) (c == ' ' || c == '\t') +/* + * These functions aren't defined in lib.c, but + * they are used there. + * + * A non-mutt "implementation" (ahem) can be found in extlib.c. + */ + +char *mutt_strlower (char *); +char *mutt_substrcpy (char *, const char *, const char *, size_t); +char *mutt_substrdup (const char *, const char *); +char *safe_strcat (char *, size_t, const char *); +char *safe_strncat (char *, size_t, const char *, size_t); +char *safe_strdup (const char *); +int mutt_strcasecmp (const char *, const char *); +int mutt_strcmp (const char *, const char *); +int mutt_strncasecmp (const char *, const char *, size_t); +int mutt_strncmp (const char *, const char *, size_t); +int mutt_strcoll (const char *, const char *); +void mutt_str_replace (char **p, const char *s); +void mutt_str_adjust (char **p); +size_t mutt_strlen (const char *a); + #endif /* !_LIB_STR_H */ -- 2.20.1