str_replace.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // This file is part of BOINC.
  2. // http://boinc.berkeley.edu
  3. // Copyright (C) 2008 University of California
  4. //
  5. // BOINC is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation,
  8. // either version 3 of the License, or (at your option) any later version.
  9. //
  10. // BOINC is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. // See the GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
  17. // declare replacement string functions for platforms that lack themn
  18. #ifndef BOINC_STR_REPLACE_H
  19. #define BOINC_STR_REPLACE_H
  20. #ifndef _WIN32
  21. #include <sys/types.h>
  22. #include <ctype.h>
  23. #endif
  24. #include "config.h"
  25. // strlcpy and strlcat guarantee NULL-terminated result
  26. // (unlike strncpy and strncat)
  27. //
  28. #if !HAVE_STRLCPY
  29. extern size_t strlcpy(char*, const char*, size_t);
  30. #endif
  31. #if !HAVE_STRLCAT
  32. extern size_t strlcat(char *dst, const char *src, size_t size);
  33. #endif
  34. #if !HAVE_STRCASESTR
  35. extern const char *strcasestr(const char *s1, const char *s2);
  36. #endif
  37. #if !HAVE_STRCASECMP
  38. inline int strcasecmp(const char* s1, const char* s2) {
  39. while (*s1 && *s2) {
  40. char c1 = tolower(*s1++);
  41. char c2 = tolower(*s2++);
  42. if (c1 < c2) return -1;
  43. if (c1 > c2) return 1;
  44. }
  45. if (*s1) return 1;
  46. if (*s2) return -1;
  47. return 0;
  48. }
  49. #endif
  50. #ifdef _WIN32
  51. #define snprintf _snprintf
  52. // Yucky! _snprintf() is not the same as snprintf();
  53. // it doesn't null-terminate if buffer is too small.
  54. // This is a workaround until we switch to VS2015, which has sprintf()
  55. #endif
  56. #endif