diff --git a/Makefile.common b/Makefile.common index 0dcdbe9f23..696caae392 100644 --- a/Makefile.common +++ b/Makefile.common @@ -173,8 +173,8 @@ OBJ += frontend/frontend.o \ patch.o \ libretro-common/queues/fifo_buffer.o \ core_options.o \ - libretro-common/compat/compat.o \ libretro-common/compat/compat_fnmatch.o \ + libretro-common/compat/compat_posix_string.o \ cheats.o \ core_info.o \ libretro-common/file/config_file.o \ @@ -209,6 +209,17 @@ OBJ += frontend/frontend.o \ record/drivers/record_null.o \ performance.o +ifneq ($(HAVE_GETOPT_LONG), 1) +OBJ += libretro-common/compat/compat_getopt.o +endif + +ifneq ($(HAVE_STRCASESTR), 1) +OBJ += libretro-common/compat/compat_strcasestr.o +endif + +ifneq ($(HAVE_STRL), 1) +OBJ += libretro-common/compat/compat_strl.o +endif OBJ += gfx/image/image.o diff --git a/griffin/griffin.c b/griffin/griffin.c index 39cd5768cc..56eecd9f79 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -22,9 +22,7 @@ #define HAVE_COMPRESSION #endif -#if defined(_MSC_VER) #include -#endif #if defined(HAVE_LOGGER) && !defined(ANDROID) #include "../netlogger.c" @@ -53,7 +51,18 @@ PERFORMANCE /*============================================================ COMPATIBILITY ============================================================ */ -#include "../compat/compat.c" +#ifndef HAVE_GETOPT_LONG +#include "../compat/compat_getopt.c" +#endif + +#ifndef HAVE_STRCASESTR +#include "../compat/compat_strcasestr.c" +#endif + +#ifndef HAVE_STRL +#include "../compat/compat_strl.c" +#endif + #include "../libretro-common/compat/compat_fnmatch.c" #include "../libretro-common/memmap/memalign.c" diff --git a/libretro-common/compat/compat.c b/libretro-common/compat/compat_getopt.c similarity index 65% rename from libretro-common/compat/compat.c rename to libretro-common/compat/compat_getopt.c index f2a3c2241a..2d4258e4cc 100644 --- a/libretro-common/compat/compat.c +++ b/libretro-common/compat/compat_getopt.c @@ -1,7 +1,7 @@ /* Copyright (C) 2010-2015 The RetroArch team * * --------------------------------------------------------------------------------------- - * The following license statement only applies to this file (compat.c). + * The following license statement only applies to this file (compat_getopt.c). * --------------------------------------------------------------------------------------- * * Permission is hereby granted, free of charge, @@ -22,14 +22,12 @@ #include -#ifndef HAVE_GETOPT_LONG #include #include #include #include #include -#endif #include #include @@ -38,7 +36,6 @@ #include -#ifndef HAVE_GETOPT_LONG char *optarg; int optind, opterr, optopt; @@ -111,7 +108,8 @@ static int parse_short(const char *optstring, char * const *argv) return optarg ? opt[0] : '?'; } - else if (embedded_arg) + + if (embedded_arg) { /* If we see additional characters, * and they don't take arguments, this @@ -119,11 +117,9 @@ static int parse_short(const char *optstring, char * const *argv) memmove(&argv[0][1], &argv[0][2], strlen(&argv[0][2]) + 1); return opt[0]; } - else - { - optind++; - return opt[0]; - } + + optind++; + return opt[0]; } static int parse_long(const struct option *longopts, char * const *argv) @@ -215,161 +211,8 @@ int getopt_long(int argc, char *argv[], if (short_index == 0) return parse_short(optstring, &argv[optind]); - else if (long_index == 0) + if (long_index == 0) return parse_long(longopts, &argv[optind]); - else - return '?'; + + return '?'; } - -#endif - -#ifndef HAVE_STRCASESTR -/* Pretty much strncasecmp. */ -static int casencmp(const char *a, const char *b, size_t n) -{ - size_t i; - - for (i = 0; i < n; i++) - { - int a_lower = tolower(a[i]); - int b_lower = tolower(b[i]); - if (a_lower != b_lower) - return a_lower - b_lower; - } - - return 0; -} - -char *strcasestr_rarch__(const char *haystack, const char *needle) -{ - size_t i, hay_len, needle_len, search_off; - - hay_len = strlen(haystack); - needle_len = strlen(needle); - if (needle_len > hay_len) - return NULL; - - search_off = hay_len - needle_len; - for (i = 0; i <= search_off; i++) - if (!casencmp(haystack + i, needle, needle_len)) - return (char*)haystack + i; - - return NULL; -} -#endif - -#ifndef HAVE_STRL - -/* Implementation of strlcpy()/strlcat() based on OpenBSD. */ - -size_t strlcpy(char *dest, const char *source, size_t size) -{ - size_t src_size = 0; - size_t n = size; - - if (n) - while (--n && (*dest++ = *source++)) src_size++; - - if (!n) - { - if (size) *dest = '\0'; - while (*source++) src_size++; - } - - return src_size; -} - -size_t strlcat(char *dest, const char *source, size_t size) -{ - size_t len = strlen(dest); - - dest += len; - - if (len > size) - size = 0; - else - size -= len; - - return len + strlcpy(dest, source, size); -} - -#endif - -#ifdef _WIN32 - -#undef strcasecmp -#undef strdup -#undef isblank -#undef strtok_r -#include -#include -#include -#include - -#include - -int rarch_strcasecmp__(const char *a, const char *b) -{ - while (*a && *b) - { - int a_ = tolower(*a); - int b_ = tolower(*b); - - if (a_ != b_) - return a_ - b_; - - a++; - b++; - } - - return tolower(*a) - tolower(*b); -} - -char *rarch_strdup__(const char *orig) -{ - size_t len = strlen(orig) + 1; - char *ret = (char*)malloc(len); - if (!ret) - return NULL; - - strlcpy(ret, orig, len); - return ret; -} - -int rarch_isblank__(int c) -{ - return (c == ' ') || (c == '\t'); -} - -char *rarch_strtok_r__(char *str, const char *delim, char **saveptr) -{ - char *first = NULL; - if (!saveptr || !delim) - return NULL; - - if (str) - *saveptr = str; - - do - { - char *ptr = NULL; - first = *saveptr; - while (*first && strchr(delim, *first)) - *first++ = '\0'; - - if (*first == '\0') - return NULL; - - ptr = first + 1; - - while (*ptr && !strchr(delim, *ptr)) - ptr++; - - *saveptr = ptr + (*ptr ? 1 : 0); - *ptr = '\0'; - } while (strlen(first) == 0); - - return first; -} - -#endif diff --git a/libretro-common/compat/compat_posix_string.c b/libretro-common/compat/compat_posix_string.c new file mode 100644 index 0000000000..0854d19eb9 --- /dev/null +++ b/libretro-common/compat/compat_posix_string.c @@ -0,0 +1,106 @@ +/* Copyright (C) 2010-2015 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (compat.c). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +#include + +#include + +#ifdef _WIN32 + +#undef strcasecmp +#undef strdup +#undef isblank +#undef strtok_r +#include +#include +#include +#include + +#include + +int rarch_strcasecmp__(const char *a, const char *b) +{ + while (*a && *b) + { + int a_ = tolower(*a); + int b_ = tolower(*b); + + if (a_ != b_) + return a_ - b_; + + a++; + b++; + } + + return tolower(*a) - tolower(*b); +} + +char *rarch_strdup__(const char *orig) +{ + size_t len = strlen(orig) + 1; + char *ret = (char*)malloc(len); + if (!ret) + return NULL; + + strlcpy(ret, orig, len); + return ret; +} + +int rarch_isblank__(int c) +{ + return (c == ' ') || (c == '\t'); +} + +char *rarch_strtok_r__(char *str, const char *delim, char **saveptr) +{ + char *first = NULL; + if (!saveptr || !delim) + return NULL; + + if (str) + *saveptr = str; + + do + { + char *ptr = NULL; + first = *saveptr; + while (*first && strchr(delim, *first)) + *first++ = '\0'; + + if (*first == '\0') + return NULL; + + ptr = first + 1; + + while (*ptr && !strchr(delim, *ptr)) + ptr++; + + *saveptr = ptr + (*ptr ? 1 : 0); + *ptr = '\0'; + } while (strlen(first) == 0); + + return first; +} + +#endif diff --git a/libretro-common/compat/compat_strcasestr.c b/libretro-common/compat/compat_strcasestr.c new file mode 100644 index 0000000000..c9cbb34246 --- /dev/null +++ b/libretro-common/compat/compat_strcasestr.c @@ -0,0 +1,59 @@ +/* Copyright (C) 2010-2015 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (compat_strcasestr.c). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +#include +#include + +/* Pretty much strncasecmp. */ +static int casencmp(const char *a, const char *b, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) + { + int a_lower = tolower(a[i]); + int b_lower = tolower(b[i]); + if (a_lower != b_lower) + return a_lower - b_lower; + } + + return 0; +} + +char *strcasestr_rarch__(const char *haystack, const char *needle) +{ + size_t i, hay_len, needle_len, search_off; + + hay_len = strlen(haystack); + needle_len = strlen(needle); + if (needle_len > hay_len) + return NULL; + + search_off = hay_len - needle_len; + for (i = 0; i <= search_off; i++) + if (!casencmp(haystack + i, needle, needle_len)) + return (char*)haystack + i; + + return NULL; +} diff --git a/libretro-common/compat/compat_strl.c b/libretro-common/compat/compat_strl.c new file mode 100644 index 0000000000..d8d89d2f79 --- /dev/null +++ b/libretro-common/compat/compat_strl.c @@ -0,0 +1,61 @@ +/* Copyright (C) 2010-2015 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (compat_strl.c). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +#include +#include + +#include + +/* Implementation of strlcpy()/strlcat() based on OpenBSD. */ + +size_t strlcpy(char *dest, const char *source, size_t size) +{ + size_t src_size = 0; + size_t n = size; + + if (n) + while (--n && (*dest++ = *source++)) src_size++; + + if (!n) + { + if (size) *dest = '\0'; + while (*source++) src_size++; + } + + return src_size; +} + +size_t strlcat(char *dest, const char *source, size_t size) +{ + size_t len = strlen(dest); + + dest += len; + + if (len > size) + size = 0; + else + size -= len; + + return len + strlcpy(dest, source, size); +} diff --git a/libretro-common/include/compat/strl.h b/libretro-common/include/compat/strl.h index 14a0c03204..b48eaf6d11 100644 --- a/libretro-common/include/compat/strl.h +++ b/libretro-common/include/compat/strl.h @@ -34,6 +34,10 @@ extern "C" { #endif +#ifdef __MACH__ +#define HAVE_STRL +#endif + #ifndef HAVE_STRL /* Avoid possible naming collisions during link since * we prefer to use the actual name. */