From 889ce1c0e880478f7269c0751675e8e69ba6ffc0 Mon Sep 17 00:00:00 2001 From: kpfleming Date: Fri, 24 Jun 2005 22:45:15 +0000 Subject: split up string/time functions into separate header files make more LOW_MEMORY optimizations, and ensure that a non-inline version of each inlinable function is always available (for external modules) move compiler-specific stuff into a separate header file git-svn-id: http://svn.digium.com/svn/asterisk/trunk@6008 f38db490-d61c-443f-a65b-d21fe96a405b --- config.c | 30 ------ include/asterisk/astobj.h | 6 +- include/asterisk/config.h | 18 ---- include/asterisk/unaligned.h | 2 +- include/asterisk/utils.h | 220 +------------------------------------------ utils.c | 74 +++++++++++---- 6 files changed, 62 insertions(+), 288 deletions(-) diff --git a/config.c b/config.c index 9c30eac61..7800b71e6 100755 --- a/config.c +++ b/config.c @@ -82,36 +82,6 @@ struct ast_config { int max_include_level; }; -int ast_true(const char *s) -{ - if (!s) - return 0; - /* Determine if this is a true value */ - if (!strcasecmp(s, "yes") || - !strcasecmp(s, "true") || - !strcasecmp(s, "y") || - !strcasecmp(s, "t") || - !strcasecmp(s, "1") || - !strcasecmp(s, "on")) - return -1; - return 0; -} - -int ast_false(const char *s) -{ - if (!s) - return 0; - /* Determine if this is a false value */ - if (!strcasecmp(s, "no") || - !strcasecmp(s, "false") || - !strcasecmp(s, "n") || - !strcasecmp(s, "f") || - !strcasecmp(s, "0") || - !strcasecmp(s, "off")) - return -1; - return 0; -} - struct ast_variable *ast_variable_new(const char *name, const char *value) { struct ast_variable *variable; diff --git a/include/asterisk/astobj.h b/include/asterisk/astobj.h index ac1b280d7..96876ba18 100755 --- a/include/asterisk/astobj.h +++ b/include/asterisk/astobj.h @@ -15,7 +15,9 @@ #define _ASTERISK_ASTOBJ_H #include + #include "asterisk/lock.h" +#include "asterisk/compiler.h" /*! \file * \brief A set of macros implementing objects and containers. @@ -84,10 +86,6 @@ extern "C" { #define ASTOBJ_FLAG_MARKED (1 << 0) /* Object has been marked for future operation */ -#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) -#define __builtin_expect(exp, c) (exp) -#endif - /* C++ is simply a syntactic crutch for those who cannot think for themselves in an object oriented way. */ diff --git a/include/asterisk/config.h b/include/asterisk/config.h index 17fe05ec4..4b9a3c529 100755 --- a/include/asterisk/config.h +++ b/include/asterisk/config.h @@ -99,24 +99,6 @@ struct ast_variable *ast_variable_browse(const struct ast_config *config, const */ char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable); -/*! Make sure something is true */ -/*! - * Determine if a string containing a boolean value is "true". - * This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1". - * - * Returns 0 if val is a NULL pointer, -1 if "true", and 0 otherwise. - */ -int ast_true(const char *val); - -/*! Make sure something is false */ -/*! - * Determine if a string containing a boolean value is "false". - * This function checks to see whether a string passed to it is an indication of an "false" value. It checks to see if the string is "no", "false", "n", "f", "off" or "0". - * - * Returns 0 if val is a NULL pointer, -1 if "false", and 0 otherwise. - */ -int ast_false(const char *val); - /*! Retrieve a category if it exists */ /*! * \param config which config to use diff --git a/include/asterisk/unaligned.h b/include/asterisk/unaligned.h index fc012049f..80d38af32 100755 --- a/include/asterisk/unaligned.h +++ b/include/asterisk/unaligned.h @@ -1,7 +1,7 @@ /* * Asterisk -- A telephony toolkit for Linux. * - * Asterisk internal frame definitions. + * Handle unaligned data access * * Copyright (C) 1999 - 2005, Digium, Inc. * diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h index 854e73379..7449c1d6e 100755 --- a/include/asterisk/utils.h +++ b/include/asterisk/utils.h @@ -20,9 +20,10 @@ #include /* we want to override inet_ntoa */ #include #include -#include #include "asterisk/lock.h" +#include "asterisk/time.h" +#include "asterisk/strings.h" /* Note: It is very important to use only unsigned variables to hold @@ -121,134 +122,11 @@ struct ast_flags { unsigned int flags; }; -static inline int ast_strlen_zero(const char *s) -{ - return (*s == '\0'); -} - struct ast_hostent { struct hostent hp; char buf[1024]; }; -/*! - \brief Gets a pointer to the first non-whitespace character in a string. - \param str the input string - \return a pointer to the first non-whitespace character - */ -#if defined(LOW_MEMORY) -char *ast_skip_blanks(char *str); -#else -static inline -#endif -#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) -char *ast_skip_blanks(char *str) -{ - while (*str && *str < 33) - str++; - return str; -} -#endif - -/*! - \brief Trims trailing whitespace characters from a string. - \param str the input string - \return a pointer to the NULL following the string - */ -#if defined(LOW_MEMORY) -char *ast_trim_blanks(char *str); -#else -static inline -#endif -#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) -char *ast_trim_blanks(char *str) -{ - char *work = str; - - if (work) { - work += strlen(work) - 1; - /* It's tempting to only want to erase after we exit this loop, - but since ast_trim_blanks *could* receive a constant string - (which we presumably wouldn't have to touch), we shouldn't - actually set anything unless we must, and it's easier just - to set each position to \0 than to keep track of a variable - for it */ - while ((work >= str) && *work < 33) - *(work--) = '\0'; - } - return str; -} -#endif - -/*! - \brief Gets a pointer to first whitespace character in a string. - \param str the input string - \return a pointer to the first whitespace character - */ -#if defined(LOW_MEMORY) -char *ast_skip_nonblanks(char *str); -#else -static inline -#endif -#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) -char *ast_skip_nonblanks(char *str) -{ - while (*str && *str > 32) - str++; - return str; -} -#endif - -/*! - \brief Strip leading/trailing whitespace from a string. - \param s The string to be stripped (will be modified). - \return The stripped string. - - This functions strips all leading and trailing whitespace - characters from the input string, and returns a pointer to - the resulting string. The string is modified in place. -*/ -#if defined(LOW_MEMORY) -char *ast_strip(char *s); -#else -static inline -#endif -#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) -char *ast_strip(char *s) -{ - s = ast_skip_blanks(s); - if (s) - ast_trim_blanks(s); - return s; -} -#endif - -/*! - \brief Strip leading/trailing whitespace and quotes from a string. - \param s The string to be stripped (will be modified). - \param beg_quotes The list of possible beginning quote characters. - \param end_quotes The list of matching ending quote characters. - \return The stripped string. - - This functions strips all leading and trailing whitespace - characters from the input string, and returns a pointer to - the resulting string. The string is modified in place. - - It can also remove beginning and ending quote (or quote-like) - characters, in matching pairs. If the first character of the - string matches any character in beg_quotes, and the last - character of the string is the matching character in - end_quotes, then they are removed from the string. - - Examples: - \code - ast_strip_quoted(buf, "\"", "\""); - ast_strip_quoted(buf, "'", "'"); - ast_strip_quoted(buf, "[{(", "]})"); - \endcode - */ -char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes); - extern struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp); /* ast_md5_hash: Produces MD5 hash based on input string */ extern void ast_md5_hash(char *output, char *input); @@ -261,24 +139,6 @@ extern const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia); extern int ast_utils_init(void); extern int ast_wait_for_input(int fd, int ms); -/* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */ - -struct ast_realloca { - char *ptr; - int alloclen; -}; - -#define ast_restrdupa(ra, s) \ - ({ \ - if ((ra)->ptr && strlen(s) + 1 < (ra)->alloclen) { \ - strcpy((ra)->ptr, s); \ - } else { \ - (ra)->ptr = alloca(strlen(s) + 1 - (ra)->alloclen); \ - if ((ra)->ptr) (ra)->alloclen = strlen(s) + 1; \ - } \ - (ra)->ptr; \ - }) - #ifdef inet_ntoa #undef inet_ntoa #endif @@ -288,81 +148,5 @@ struct ast_realloca { #define ast_pthread_create(a,b,c,d) ast_pthread_create_stack(a,b,c,d,0) extern int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize); -#ifdef __linux__ -#define ast_strcasestr strcasestr -#else -extern char *ast_strcasestr(const char *, const char *); -#endif /* __linux__ */ - -#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) -#define __builtin_expect(exp, c) (exp) -#endif - -/*! - \brief Size-limited null-terminating string copy. - \param dst The destination buffer. - \param src The source string - \param size The size of the destination buffer - \return Nothing. - - This is similar to \a strncpy, with two important differences: - - the destination buffer will \b always be null-terminated - - the destination buffer is not filled with zeros past the copied string length - These differences make it slightly more efficient, and safer to use since it will - not leave the destination buffer unterminated. There is no need to pass an artificially - reduced buffer size to this function (unlike \a strncpy), and the buffer does not need - to be initialized to zeroes prior to calling this function. -*/ -#if defined(LOW_MEMORY) -void ast_copy_string(char *dst, const char *src, size_t size); -#else -static inline -#endif -#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) -void ast_copy_string(char *dst, const char *src, size_t size) -{ - while (*src && size) { - *dst++ = *src++; - size--; - } - if (__builtin_expect(!size, 0)) - dst--; - *dst = '\0'; -} -#endif - -/*! - \brief Build a string in a buffer, designed to be called repeatedly - - This is a wrapper for snprintf, that properly handles the buffer pointer - and buffer space available. - - \return 0 on success, non-zero on failure. - \param buffer current position in buffer to place string into (will be updated on return) - \param space remaining space in buffer (will be updated on return) - \param fmt printf-style format string -*/ -int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__ ((format (printf, 3, 4))); - -/* functions for working with 'struct timeval' instances */ - -/*! - * \brief Computes the difference (in milliseconds) between two \c struct \c timeval instances. - * \param start the beginning of the time period - * \param end the end of the time period - * \return the difference in milliseconds - */ -#if defined(LOW_MEMORY) -int ast_tvdiff_ms(const struct timeval *start, const struct timeval *end); -#else -static inline -#endif -#if !defined(LOW_MEMORY) || defined(AST_API_MODULE) -int ast_tvdiff_ms(const struct timeval *start, const struct timeval *end) -{ - return ((end->tv_sec - start->tv_sec) * 1000) + ((end->tv_usec - start->tv_usec) / 1000); -} -#endif - #undef AST_API_MODULE #endif /* _ASTERISK_UTILS_H */ diff --git a/utils.c b/utils.c index d0ee57d82..1319f71b9 100755 --- a/utils.c +++ b/utils.c @@ -33,29 +33,18 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/logger.h" #include "asterisk/md5.h" +#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */ +#include "asterisk/strings.h" + +#define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */ +#include "asterisk/time.h" + #define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */ #include "asterisk/utils.h" static char base64[64]; static char b2a[256]; -char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes) -{ - char *e; - char *q; - - s = ast_strip(s); - if ((q = strchr(beg_quotes, *s))) { - e = s + strlen(s) - 1; - if (*e == *(end_quotes + (q - beg_quotes))) { - s++; - *e = '\0'; - } - } - - return s; -} - #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) /* duh? ERANGE value copied from web... */ @@ -433,6 +422,23 @@ int ast_wait_for_input(int fd, int ms) return poll(pfd, 1, ms); } +char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes) +{ + char *e; + char *q; + + s = ast_strip(s); + if ((q = strchr(beg_quotes, *s))) { + e = s + strlen(s) - 1; + if (*e == *(end_quotes + (q - beg_quotes))) { + s++; + *e = '\0'; + } + } + + return s; +} + int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) { va_list ap; @@ -455,6 +461,40 @@ int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) return 0; } +int ast_true(const char *s) +{ + if (!s || ast_strlen_zero(s)) + return 0; + + /* Determine if this is a true value */ + if (!strcasecmp(s, "yes") || + !strcasecmp(s, "true") || + !strcasecmp(s, "y") || + !strcasecmp(s, "t") || + !strcasecmp(s, "1") || + !strcasecmp(s, "on")) + return -1; + + return 0; +} + +int ast_false(const char *s) +{ + if (!s || ast_strlen_zero(s)) + return 0; + + /* Determine if this is a false value */ + if (!strcasecmp(s, "no") || + !strcasecmp(s, "false") || + !strcasecmp(s, "n") || + !strcasecmp(s, "f") || + !strcasecmp(s, "0") || + !strcasecmp(s, "off")) + return -1; + + return 0; +} + /* Case-insensitive substring matching */ #ifndef LINUX static char *upper(const char *orig, char *buf, int bufsize) -- cgit v1.2.3