From f1ec18b6fce73f7346cf4265211721a30787b1ce Mon Sep 17 00:00:00 2001 From: Martin Szulecki Date: Mon, 18 Feb 2013 01:41:59 +0100 Subject: Fix compilation of strtok_r and localtime_r replacements for WIN32 builds --- nanohttp/nanohttp-common.c | 105 ++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c index 18b206b..7ec3d17 100644 --- a/nanohttp/nanohttp-common.c +++ b/nanohttp/nanohttp-common.c @@ -60,6 +60,61 @@ #ifdef WIN32 #include +#include +#endif + +#ifdef WIN32 + +#ifndef strtok_r +/* strtok_r() */ +char * +strtok_r(char *s, const char *delim, char **save_ptr) +{ + char *token; + + if (s == NULL) + s = *save_ptr; + + /* Scan leading delimiters. */ + s += strspn(s, delim); + if (*s == '\0') + return NULL; + + /* Find the end of the token. */ + token = s; + s = strpbrk(token, delim); + if (s == NULL) + /* This token finishes the string. */ + *save_ptr = strchr(token, '\0'); + else + { + /* Terminate the token and make *SAVE_PTR point past it. */ + *s = '\0'; + *save_ptr = s + 1; + } + return token; +} +#endif + +/* New mingw pthread package seems to define localtime_r as a macro */ +#ifdef localtime_r +#undef localtime_r +#endif + +/* localtime_r() */ +struct tm * +localtime_r(const time_t * timep, struct tm* p_tm) +{ + static struct tm *tmp; + tmp = localtime(timep); + if (tmp) + { + memcpy(p_tm, tmp, sizeof(struct tm)); + tmp = p_tm; + } + return tmp; +} + #endif hpair_t * @@ -108,7 +163,7 @@ hpairnode_parse(const char *str, const char *delim, hpair_t * next) pair->value = ""; pair->next = next; - key = strtok_r((char *) str, delim, &value); + key = (char*)strtok_r((char *) str, delim, &value); if (key != NULL) { @@ -484,51 +539,3 @@ attachments_free(struct attachments_t *message) return; } - - -#ifdef WIN32 - -/* strtok_r() */ -char * -strtok_r(char *s, const char *delim, char **save_ptr) -{ - char *token; - - if (s == NULL) - s = *save_ptr; - - /* Scan leading delimiters. */ - s += strspn(s, delim); - if (*s == '\0') - return NULL; - - /* Find the end of the token. */ - token = s; - s = strpbrk(token, delim); - if (s == NULL) - /* This token finishes the string. */ - *save_ptr = strchr(token, '\0'); - else - { - /* Terminate the token and make *SAVE_PTR point past it. */ - *s = '\0'; - *save_ptr = s + 1; - } - return token; -} - -/* localtime_r() */ -struct tm * -localtime_r(const time_t * const timep, struct tm *p_tm) -{ - static struct tm *tmp; - tmp = localtime(timep); - if (tmp) - { - memcpy(p_tm, tmp, sizeof(struct tm)); - tmp = p_tm; - } - return tmp; -} - -#endif -- cgit v1.1-32-gdbae