From 69ba4854152415ab3e2ec11cdb6f9c9ce5af9ac8 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Wed, 23 Jan 2013 02:21:26 -0500 Subject: [PATCH 1/2] Fix compile under MingW --- src/porting.h | 18 +++++++++++++----- src/util/string.cpp | 30 ++++++++++++++++++++++++++++++ src/util/string.h | 1 + 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/porting.h b/src/porting.h index 184e1ab5..c8d19154 100644 --- a/src/porting.h +++ b/src/porting.h @@ -42,18 +42,26 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifdef _WIN32 #include - #define ALIGNOF(x) __alignof(x) #define sleep_ms(x) Sleep(x) +#else + #include + #include //for uintptr_t + + #define sleep_ms(x) usleep(x*1000) +#endif + +#ifdef _MSC_VER + #define ALIGNOF(x) __alignof(x) #define strtok_r(x, y, z) strtok_s(x, y, z) #define strtof(x, y) (float)strtod(x, y) #define strtoll(x, y, z) _strtoi64(x, y, z) #define strtoull(x, y, z) _strtoui64(x, y, z) #else - #include - #include //for uintptr_t - #define ALIGNOF(x) __alignof__(x) - #define sleep_ms(x) usleep(x*1000) +#endif + +#ifdef __MINGW32__ + #define strtok_r(x, y, z) mystrtok_r(x, y, z) #endif #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1)) diff --git a/src/util/string.cpp b/src/util/string.cpp index 215ac299..481e74da 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -47,3 +47,33 @@ size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata) { stream->write(ptr, count); return count; } + +char *mystrtok_r(char *s, const char *sep, char **lasts) { + char *t; + int delim_reached; + + if (!s) + s = *lasts; + + while (*s && strchr(sep, *s)) + s++; + + if (!*s) + return NULL; + + delim_reached = 0; + t = s; + while (*t) { + if (strchr(sep, *t)) { + *t = '\0'; + delim_reached = 1; + } else if (delim_reached) { + *lasts = t; + return s; + } + t++; + } + + *lasts = t; + return s; +} diff --git a/src/util/string.h b/src/util/string.h index 58274c67..d081b365 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -283,6 +283,7 @@ inline std::string wrap_rows(const std::string &from, u32 rowlen) std::string translatePassword(std::string playername, std::wstring password); size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata); +char *mystrtok_r(char *s, const char *sep, char **lasts); #endif From 9d69b3858385e657a3d1a8b64d302c0bd5a23166 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Wed, 23 Jan 2013 17:37:53 -0500 Subject: [PATCH 2/2] Slightly improved version of mystrtok_r --- src/util/string.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/util/string.cpp b/src/util/string.cpp index 481e74da..c10755ae 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -50,7 +50,6 @@ size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata) { char *mystrtok_r(char *s, const char *sep, char **lasts) { char *t; - int delim_reached; if (!s) s = *lasts; @@ -61,15 +60,11 @@ char *mystrtok_r(char *s, const char *sep, char **lasts) { if (!*s) return NULL; - delim_reached = 0; t = s; while (*t) { if (strchr(sep, *t)) { - *t = '\0'; - delim_reached = 1; - } else if (delim_reached) { - *lasts = t; - return s; + *t++ = '\0'; + break; } t++; }