compat_posix_string.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (C) 2010-2018 The RetroArch team
  2. *
  3. * ---------------------------------------------------------------------------------------
  4. * The following license statement only applies to this file (compat_posix_string.c).
  5. * ---------------------------------------------------------------------------------------
  6. *
  7. * Permission is hereby granted, free of charge,
  8. * to any person obtaining a copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <ctype.h>
  23. #include <compat/posix_string.h>
  24. #ifdef _WIN32
  25. #undef strcasecmp
  26. #undef strdup
  27. #undef isblank
  28. #undef strtok_r
  29. #include <ctype.h>
  30. #include <stdlib.h>
  31. #include <stddef.h>
  32. #include <compat/strl.h>
  33. #include <string.h>
  34. int retro_strcasecmp__(const char *a, const char *b)
  35. {
  36. while (*a && *b)
  37. {
  38. int a_ = tolower(*a);
  39. int b_ = tolower(*b);
  40. if (a_ != b_)
  41. return a_ - b_;
  42. a++;
  43. b++;
  44. }
  45. return tolower(*a) - tolower(*b);
  46. }
  47. char *retro_strdup__(const char *orig)
  48. {
  49. size_t len = strlen(orig) + 1;
  50. char *ret = (char*)malloc(len);
  51. if (!ret)
  52. return NULL;
  53. strlcpy(ret, orig, len);
  54. return ret;
  55. }
  56. int retro_isblank__(int c)
  57. {
  58. return (c == ' ') || (c == '\t');
  59. }
  60. char *retro_strtok_r__(char *str, const char *delim, char **saveptr)
  61. {
  62. char *first = NULL;
  63. if (!saveptr || !delim)
  64. return NULL;
  65. if (str)
  66. *saveptr = str;
  67. do
  68. {
  69. char *ptr = NULL;
  70. first = *saveptr;
  71. while (*first && strchr(delim, *first))
  72. *first++ = '\0';
  73. if (*first == '\0')
  74. return NULL;
  75. ptr = first + 1;
  76. while (*ptr && !strchr(delim, *ptr))
  77. ptr++;
  78. *saveptr = ptr + (*ptr ? 1 : 0);
  79. *ptr = '\0';
  80. } while (strlen(first) == 0);
  81. return first;
  82. }
  83. #endif