Skip to content

Commit

Permalink
Minor fixes to the strlib interface
Browse files Browse the repository at this point in the history
- Fixed a typo in the strnlen() macro (was strnln)
- Initialized strlib->strnlen and strlib->strtoull to NULL on platforms
  that don't require them. Please note that those two functions are
  supposed to never be called directly but only through the strnlen()
  and strtoull() macros, which take care of the platform abstraction.
- Removed localized versions of yes/no from config_switch (if you really
  want to be able to use "oui", "ja", "si", "non", "nein", feel free to
  add them back yourself following the example in strlib.c)

Signed-off-by: Haru <haru@dotalux.com>
  • Loading branch information
MishimaHaruna committed Jan 23, 2014
1 parent 0b96dc2 commit 22b7f90
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
34 changes: 25 additions & 9 deletions src/common/strlib.c
Expand Up @@ -240,16 +240,19 @@ char* _strtok_r(char *s1, const char *s2, char **lasts) {
}
#endif

// 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.
#if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(HAVE_STRNLEN)
/* 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;
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;
}
#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)
{
Expand Down Expand Up @@ -331,13 +334,22 @@ int e_mail_check(char* email)

//--------------------------------------------------
// Return numerical value of a switch configuration
// on/off, english, français, deutsch, español
// on/off, yes/no, true/false, number
//--------------------------------------------------
int config_switch(const char* str)
{
if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0)
int config_switch(const char* str) {
size_t len = strlen(str);
if ((len == 2 && strcmpi(str, "on") == 0)
|| (len == 3 && strcmpi(str, "yes") == 0)
|| (len == 4 && strcmpi(str, "true") == 0)
// || (len == 3 && strcmpi(str, "oui") == 0) // Uncomment and edit to add your own localized versions
)
return 1;
if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0)

if ((len == 3 && strcmpi(str, "off") == 0)
|| (len == 2 && strcmpi(str, "no") == 0)
|| (len == 5 && strcmpi(str, "false") == 0)
// || (len == 3 && strcmpi(str, "non") == 0) // Uncomment and edit to add your own localized versions
)
return 0;

return (int)strtol(str, NULL, 0);
Expand Down Expand Up @@ -1119,10 +1131,14 @@ void strlib_defaults(void) {

#if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(HAVE_STRNLEN)
strlib->strnlen = strnlen;
#else
strlib->strnlen = NULL;
#endif

#if defined(WIN32) && defined(_MSC_VER) && _MSC_VER <= 1200
strlib->strtoull = strtoull;
#else
strlib->strtoull = NULL;
#endif
strlib->e_mail_check = e_mail_check;
strlib->config_switch = config_switch;
Expand Down
2 changes: 1 addition & 1 deletion src/common/strlib.h
Expand Up @@ -175,7 +175,7 @@ void strlib_defaults(void);
#define stristr(haystack,needle) (strlib->stristr((haystack),(needle)))

#if !(defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(HAVE_STRNLEN)
#define strnln(string,maxlen) (strlib->strnlen((string),(maxlen)))
#define strnlen(string,maxlen) (strlib->strnlen((string),(maxlen)))
#endif

#if defined(WIN32) && defined(_MSC_VER) && _MSC_VER <= 1200
Expand Down

0 comments on commit 22b7f90

Please sign in to comment.