diff options
-rw-r--r-- | src/common/strlib.c | 116 | ||||
-rw-r--r-- | src/common/strlib.h | 77 |
2 files changed, 73 insertions, 120 deletions
diff --git a/src/common/strlib.c b/src/common/strlib.c index 555f591e6..abef22d48 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -4,9 +4,7 @@ #define HERCULES_CORE -#define H_STRLIB_C #include "strlib.h" -#undef H_STRLIB_C #include "common/cbasetypes.h" #include "common/malloc.h" @@ -117,7 +115,7 @@ int jmemescapecpy (char* pt, const char* spt, int size) } // Function to suppress control characters in a string. -int remove_control_chars(char* str) +int strlib_remove_control_chars(char *str) { int i; int change = 0; @@ -134,7 +132,7 @@ int remove_control_chars(char* str) // Removes characters identified by ISSPACE from the start and end of the string // NOTE: make sure the string is not const!! -char* trim(char* str) +char *strlib_trim(char *str) { size_t start; size_t end; @@ -162,7 +160,7 @@ char* trim(char* str) // Converts one or more consecutive occurrences of the delimiters into a single space // and removes such occurrences from the beginning and end of string // NOTE: make sure the string is not const!! -char* normalize_name(char* str,const char* delims) +char *strlib_normalize_name(char *str, const char *delims) { char* in = str; char* out = str; @@ -200,7 +198,7 @@ char* normalize_name(char* str,const char* delims) //stristr: Case insensitive version of strstr, code taken from //http://www.daniweb.com/code/snippet313.html, Dave Sinkula // -const char* stristr(const char* haystack, const char* needle) +const char *strlib_stristr(const char *haystack, const char *needle) { if ( !*needle ) { @@ -228,8 +226,9 @@ const char* stristr(const char* haystack, const char* needle) return 0; } +char* strlib_strtok_r(char *s1, const char *s2, char **lasts) +{ #ifdef __WIN32 -char* strtok_r_(char *s1, const char *s2, char **lasts) { char *ret; if (s1 == NULL) @@ -245,9 +244,13 @@ char* strtok_r_(char *s1, const char *s2, char **lasts) { *s1++ = '\0'; *lasts = s1; return ret; -} +#else + return strtok_r(s1, s2, lasts); #endif +} +size_t strlib_strnlen(const char *string, size_t maxlen) +{ // TODO: The _MSC_VER check can probably be removed (we no longer support VS // versions <= 2003, do we?), but this implementation might be still necessary // for NetBSD 5.x and possibly some Solaris versions. @@ -255,60 +258,17 @@ char* strtok_r_(char *s1, const char *s2, char **lasts) { /* Find the length of STRING, but scan at most MAXLEN characters. * If no '\0' terminator is found in that many characters, return MAXLEN. */ -size_t strnlen(const char* string, size_t maxlen) { const char* end = (const char*)memchr(string, '\0', maxlen); return end ? (size_t) (end - string) : maxlen; -} +#else + return strnlen(string, maxlen); #endif - -// TODO: This should probably be removed, I don't think we support MSVC++ 6.0 anymore. -#if defined(WIN32) && defined(_MSC_VER) && _MSC_VER <= 1200 -uint64 strtoull(const char* str, char** endptr, int base) -{ - uint64 result; - int count; - int n; - - if( base == 0 ) - { - if( str[0] == '0' && (str[1] == 'x' || str[1] == 'X') ) - base = 16; - else - if( str[0] == '0' ) - base = 8; - else - base = 10; - } - - if( base == 8 ) - count = sscanf(str, "%I64o%n", &result, &n); - else - if( base == 10 ) - count = sscanf(str, "%I64u%n", &result, &n); - else - if( base == 16 ) - count = sscanf(str, "%I64x%n", &result, &n); - else - count = 0; // fail - - if( count < 1 ) - { - errno = EINVAL; - result = 0; - n = 0; - } - - if( endptr ) - *endptr = (char*)str + n; - - return result; } -#endif //---------------------------------------------------- // E-mail check: return 0 (not correct) or 1 (valid). //---------------------------------------------------- -int e_mail_check(char* email) +int strlib_e_mail_check(char *email) { char ch; char* last_arobas; @@ -345,7 +305,7 @@ int e_mail_check(char* email) // Return numerical value of a switch configuration // on/off, yes/no, true/false, number //-------------------------------------------------- -int config_switch(const char* str) { +int strlib_config_switch(const char *str) { size_t len = strlen(str); if ((len == 2 && strcmpi(str, "on") == 0) || (len == 3 && strcmpi(str, "yes") == 0) @@ -365,7 +325,7 @@ int config_switch(const char* str) { } /// strncpy that always null-terminates the string -char* safestrncpy(char* dst, const char* src, size_t n) +char *strlib_safestrncpy(char *dst, const char *src, size_t n) { if( n > 0 ) { @@ -386,7 +346,7 @@ char* safestrncpy(char* dst, const char* src, size_t n) } /// doesn't crash on null pointer -size_t safestrnlen(const char* string, size_t maxlen) +size_t strlib_safestrnlen(const char *string, size_t maxlen) { return ( string != NULL ) ? strnlen(string, maxlen) : 0; } @@ -400,8 +360,9 @@ size_t safestrnlen(const char* string, size_t maxlen) /// @param fmt Format string /// @param ... Format arguments /// @return The size of the string or -1 if the buffer is too small -int safesnprintf(char *buf, size_t sz, const char *fmt, ...) __attribute__((format(printf, 3, 4))); -int safesnprintf(char *buf, size_t sz, const char *fmt, ...) { +int strlib_safesnprintf(char *buf, size_t sz, const char *fmt, ...) __attribute__((format(printf, 3, 4))); +int strlib_safesnprintf(char *buf, size_t sz, const char *fmt, ...) +{ va_list ap; int ret; @@ -417,7 +378,7 @@ int safesnprintf(char *buf, size_t sz, const char *fmt, ...) { /// Returns the line of the target position in the string. /// Lines start at 1. -int strline(const char* str, size_t pos) +int strlib_strline(const char *str, size_t pos) { const char* target; int line; @@ -443,7 +404,7 @@ int strline(const char* str, size_t pos) /// @param output Output string /// @param input Binary input buffer /// @param count Number of bytes to convert -bool bin2hex(char* output, unsigned char* input, size_t count) +bool strlib_bin2hex(char *output, unsigned char *input, size_t count) { char toHex[] = "0123456789abcdef"; size_t i; @@ -1133,29 +1094,30 @@ void strlib_defaults(void) { strlib->jstrescape = jstrescape; strlib->jstrescapecpy = jstrescapecpy; strlib->jmemescapecpy = jmemescapecpy; - strlib->remove_control_chars = remove_control_chars; - strlib->trim = trim; - strlib->normalize_name = normalize_name; - strlib->stristr = stristr; + strlib->remove_control_chars_ = strlib_remove_control_chars; + strlib->trim_ = strlib_trim; + strlib->normalize_name_ = strlib_normalize_name; + strlib->stristr_ = strlib_stristr; #if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(HAVE_STRNLEN) - strlib->strnlen = strnlen; + strlib->strnlen_ = strlib_strnlen; #else - strlib->strnlen = NULL; + strlib->strnlen_ = NULL; #endif -#if defined(WIN32) && defined(_MSC_VER) && _MSC_VER <= 1200 - strlib->strtoull = strtoull; +#ifdef WIN32 + strlib->strtok_r_ = strlib_strtok_r; #else - strlib->strtoull = NULL; + strlib->strtok_r_ = NULL; #endif - strlib->e_mail_check = e_mail_check; - strlib->config_switch = config_switch; - strlib->safestrncpy = safestrncpy; - strlib->safestrnlen = safestrnlen; - strlib->safesnprintf = safesnprintf; - strlib->strline = strline; - strlib->bin2hex = bin2hex; + + strlib->e_mail_check_ = strlib_e_mail_check; + strlib->config_switch_ = strlib_config_switch; + strlib->safestrncpy_ = strlib_safestrncpy; + strlib->safestrnlen_ = strlib_safestrnlen; + strlib->safesnprintf_ = strlib_safesnprintf; + strlib->strline_ = strlib_strline; + strlib->bin2hex_ = strlib_bin2hex; StrBuf->Malloc = StringBuf_Malloc; StrBuf->Init = StringBuf_Init; diff --git a/src/common/strlib.h b/src/common/strlib.h index a768ebff5..cd7b90b08 100644 --- a/src/common/strlib.h +++ b/src/common/strlib.h @@ -10,12 +10,30 @@ #include <stdarg.h> #include <string.h> +/// Convenience macros + +#define remove_control_chars(str) (strlib->remove_control_chars_(str)) +#define trim(str) (strlib->trim_(str)) +#define normalize_name(str,delims) (strlib->normalize_name_((str),(delims))) +#define stristr(haystack,needle) (strlib->stristr_((haystack),(needle))) + +#if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(HAVE_STRNLEN) + #define strnlen(string,maxlen) (strlib->strnlen_((string),(maxlen))) +#endif + #ifdef WIN32 #define HAVE_STRTOK_R - #define strtok_r(s,delim,save_ptr) strtok_r_((s),(delim),(save_ptr)) - char *strtok_r_(char* s1, const char* s2, char** lasts); + #define strtok_r(s,delim,save_ptr) strlib->strtok_r_((s),(delim),(save_ptr)) #endif +#define e_mail_check(email) (strlib->e_mail_check_(email)) +#define config_switch(str) (strlib->config_switch_(str)) +#define safestrncpy(dst,src,n) (strlib->safestrncpy_((dst),(src),(n))) +#define safestrnlen(string,maxlen) (strlib->safestrnlen_((string),(maxlen))) +#define safesnprintf(buf,sz,fmt,...) (strlib->safesnprintf_((buf),(sz),(fmt),##__VA_ARGS__)) +#define strline(str,pos) (strlib->strline_((str),(pos))) +#define bin2hex(output,input,count) (strlib->bin2hex_((output),(input),(count))) + /// Bitfield determining the behavior of sv_parse and sv_split. typedef enum e_svopt { // default: no escapes and no line terminator @@ -59,39 +77,39 @@ struct strlib_interface { char *(*jstrescape) (char* pt); char *(*jstrescapecpy) (char* pt, const char* spt); int (*jmemescapecpy) (char* pt, const char* spt, int size); - int (*remove_control_chars) (char* str); - char *(*trim) (char* str); - char *(*normalize_name) (char* str,const char* delims); - const char *(*stristr) (const char *haystack, const char *needle); + int (*remove_control_chars_) (char* str); + char *(*trim_) (char* str); + char *(*normalize_name_) (char* str,const char* delims); + const char *(*stristr_) (const char *haystack, const char *needle); /* only used when '!(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(HAVE_STRNLEN)', needs to be defined at all times however */ - size_t (*strnlen) (const char* string, size_t maxlen); + size_t (*strnlen_) (const char* string, size_t maxlen); - /* only used when 'defined(WIN32) && defined(_MSC_VER) && _MSC_VER <= 1200', needs to be defined at all times however */ - uint64 (*strtoull) (const char* str, char** endptr, int base); + /* only used when 'WIN32' */ + char * (*strtok_r_) (char *s1, const char *s2, char **lasts); - int (*e_mail_check) (char* email); - int (*config_switch) (const char* str); + int (*e_mail_check_) (char* email); + int (*config_switch_) (const char* str); /// strncpy that always null-terminates the string - char *(*safestrncpy) (char* dst, const char* src, size_t n); + char *(*safestrncpy_) (char* dst, const char* src, size_t n); /// doesn't crash on null pointer - size_t (*safestrnlen) (const char* string, size_t maxlen); + size_t (*safestrnlen_) (const char* string, size_t maxlen); /// Works like snprintf, but always null-terminates the buffer. /// Returns the size of the string (without null-terminator) /// or -1 if the buffer is too small. - int (*safesnprintf) (char *buf, size_t sz, const char *fmt, ...) __attribute__((format(printf, 3, 4))); + int (*safesnprintf_) (char *buf, size_t sz, const char *fmt, ...) __attribute__((format(printf, 3, 4))); /// Returns the line of the target position in the string. /// Lines start at 1. - int (*strline) (const char* str, size_t pos); + int (*strline_) (const char* str, size_t pos); /// Produces the hexadecimal representation of the given input. /// The output buffer must be at least count*2+1 in size. /// Returns true on success, false on failure. - bool (*bin2hex) (char* output, unsigned char* input, size_t count); + bool (*bin2hex_) (char* output, unsigned char* input, size_t count); }; struct strlib_interface *strlib; @@ -160,31 +178,4 @@ struct sv_interface *sv; void strlib_defaults(void); #endif // HERCULES_CORE -/* the purpose of these macros is simply to not make calling them be an annoyance */ -#ifndef H_STRLIB_C - #define jstrescape(pt) (strlib->jstrescape(pt)) - #define jstrescapecpy(pt,spt) (strlib->jstrescapecpy((pt),(spt))) - #define jmemescapecpy(pt,spt,size) (strlib->jmemescapecpy((pt),(spt),(size))) - #define remove_control_chars(str) (strlib->remove_control_chars(str)) - #define trim(str) (strlib->trim(str)) - #define normalize_name(str,delims) (strlib->normalize_name((str),(delims))) - #define stristr(haystack,needle) (strlib->stristr((haystack),(needle))) - - #if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(HAVE_STRNLEN) - #define strnlen(string,maxlen) (strlib->strnlen((string),(maxlen))) - #endif - - #if defined(WIN32) && defined(_MSC_VER) && _MSC_VER <= 1200 - #define strtoull(str,endptr,base) (strlib->strtoull((str),(endptr),(base))) - #endif - - #define e_mail_check(email) (strlib->e_mail_check(email)) - #define config_switch(str) (strlib->config_switch(str)) - #define safestrncpy(dst,src,n) (strlib->safestrncpy((dst),(src),(n))) - #define safestrnlen(string,maxlen) (strlib->safestrnlen((string),(maxlen))) - #define safesnprintf(buf,sz,fmt,...) (strlib->safesnprintf((buf),(sz),(fmt),##__VA_ARGS__)) - #define strline(str,pos) (strlib->strline((str),(pos))) - #define bin2hex(output,input,count) (strlib->bin2hex((output),(input),(count))) -#endif /* H_STRLIB_C */ - #endif /* COMMON_STRLIB_H */ |