From 2ce7edfe772c319756a9e4feb46f33b201ccfae5 Mon Sep 17 00:00:00 2001 From: Haru Date: Thu, 18 Jun 2015 02:06:30 +0200 Subject: Cleaned up strlib interface - Replaced some macro calls with the proper interface syntax - Removed useless macros and workarounds - Removed no longer needed library function re-definitions API changes summary: - The macros remove_control_chars(), trim(), normalize_name(), stristr(), e_mail_check(), config_switch(), safestrncpy(), safestrnlen(), safesnprintf(), strline(), bin2hex() can now be safely used both inside and outside strlib.c - The macros strnlen() and strtok_r() can now be safely used both inside and outside strlib.c, on the systems where they are necessary. The systems that provide those natively, aren't affected by this change. - jstrescape() is now strlib->jstrescape() - jstrescapecpy() is now strlib->jstrescapecpy() - jmemescapecpy() is now strlib->jmemescapecpy() - a custom strtoull() implementation is no longer provided, since all supported systems and compilers provide a library implementation. Signed-off-by: Haru --- src/common/strlib.h | 77 +++++++++++++++++++++++------------------------------ 1 file changed, 34 insertions(+), 43 deletions(-) (limited to 'src/common/strlib.h') 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 #include +/// 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 */ -- cgit v1.2.3-60-g2f50