Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strings.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*!
  2. * \file include/utils/strings.h
  3. * \brief String handling utilities
  4. *
  5. * \author xythobuz
  6. * \author Mongoose
  7. */
  8. #include <cstdlib>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #if defined(unix) || defined(__APPLE__) || defined(__linux__)
  13. #include <wordexp.h>
  14. #endif
  15. #include "utils/strings.h"
  16. char *stringRemoveQuotes(const char *s) {
  17. size_t length = strlen(s);
  18. if ((s[0] == '"') && (s[length - 1] == '"')) {
  19. char *buf = new char[length - 1];
  20. for (size_t i = 1; i < (length - 1); i++)
  21. buf[i - 1] = s[i];
  22. buf[length - 2] = '\0';
  23. return buf;
  24. } else {
  25. return bufferString("%s", s);
  26. }
  27. }
  28. char *stringReplace(const char *s, const char *search, const char *replace) {
  29. char *tmp = strstr((char *)s, search);
  30. if (tmp == NULL)
  31. return NULL;
  32. size_t offset = tmp - s;
  33. size_t length = strlen(s) - strlen(search) + strlen(replace);
  34. char *buf = new char[length + 1];
  35. buf[length] = '\0';
  36. for (size_t i = 0; i < offset; i++)
  37. buf[i] = s[i];
  38. for (size_t i = 0; i < strlen(replace); i++)
  39. buf[offset + i] = replace[i];
  40. for (size_t i = (offset + strlen(search)); i < strlen(s); i++)
  41. buf[i + strlen(replace) - strlen(search)] = s[i];
  42. tmp = stringReplace(buf, search, replace);
  43. if (tmp == NULL)
  44. return buf;
  45. else {
  46. delete [] buf;
  47. return tmp;
  48. }
  49. }
  50. int readBool(const char *value, bool *var) {
  51. if ((strcmp(value, "1") == 0) || (strcmp(value, "true") == 0) || (strcmp(value, "on") == 0)) {
  52. *var = true;
  53. } else if ((strcmp(value, "0") == 0) || (strcmp(value, "false") == 0) || (strcmp(value, "off") == 0)) {
  54. *var = false;
  55. } else {
  56. return -1;
  57. }
  58. return 0;
  59. }
  60. bool stringEndsWith(const char *str, const char *suffix) {
  61. assert(str != NULL);
  62. assert(suffix != NULL);
  63. size_t lenstr = strlen(str);
  64. size_t lensuffix = strlen(suffix);
  65. if (lensuffix > lenstr)
  66. return false;
  67. return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
  68. }
  69. char *bufferString(const char *string, va_list args) {
  70. int sz = 60;
  71. int n;
  72. char *text;
  73. va_list tmp;
  74. assert(string != NULL);
  75. assert(string[0] != '\0');
  76. text = new char[sz];
  77. va_copy(tmp, args);
  78. n = vsnprintf(text, sz, string, tmp);
  79. va_end(tmp);
  80. if (n < 0) {
  81. delete [] text;
  82. return NULL; // encoding error
  83. } else if (n >= sz) {
  84. sz = n + 1; // buffer too small
  85. delete [] text;
  86. text = new char[sz + 1];
  87. va_copy(tmp, args);
  88. vsnprintf(text, sz, string, tmp);
  89. va_end(tmp);
  90. }
  91. return text;
  92. }
  93. char *bufferString(const char *string, ...) {
  94. va_list args;
  95. va_start(args, string);
  96. char *text = bufferString(string, args);
  97. va_end(args);
  98. return text;
  99. }
  100. char *fullPath(const char *path, char end) {
  101. unsigned int lenPath;
  102. wordexp_t word;
  103. char *dir;
  104. assert(path != NULL);
  105. assert(path[0] != '\0');
  106. if (path[0] == '~') {
  107. #if defined(unix) || defined(__APPLE__) || defined(__linux__)
  108. #ifdef __APPLE__
  109. // Workaround for Mac OS X. See:
  110. // http://stackoverflow.com/questions/20534788/why-does-wordexp-fail-with-wrde-syntax-on-os-x
  111. signal(SIGCHLD, SIG_DFL);
  112. #endif
  113. // Expand string into segments
  114. int res = wordexp(path, &word, 0);
  115. #ifdef __APPLE__
  116. signal(SIGCHLD, SIG_IGN);
  117. #endif
  118. if (res != 0) {
  119. printf("fullPath> wordexp() failed: %d\n", res);
  120. return NULL;
  121. }
  122. // Get length of complete string
  123. lenPath = 0;
  124. for (unsigned int i = 0; i < word.we_wordc; i++) {
  125. lenPath += strlen(word.we_wordv[i]);
  126. }
  127. // Allocate new string
  128. dir = new char[lenPath + 2]; // space for end char
  129. // Copy segments into new string
  130. unsigned int offset = 0;
  131. for (unsigned int i = 0; i < word.we_wordc; i++) {
  132. unsigned int len = strlen(word.we_wordv[i]);
  133. strncpy(dir + offset, word.we_wordv[i], len);
  134. offset += len;
  135. }
  136. wordfree(&word);
  137. #else
  138. printf("WARNING: Tilde expansion not supported on this platform:\n\t%s\n", path);
  139. lenPath = strlen(path);
  140. dir = new char[lenPath + 2]; // space for end char
  141. strncpy(dir, path, lenPath);
  142. #endif
  143. } else {
  144. lenPath = strlen(path);
  145. dir = new char[lenPath + 2]; // space for end char
  146. strncpy(dir, path, lenPath);
  147. }
  148. // Make sure ends in "end" char
  149. if ((lenPath > 0) && (end != 0) && (dir[lenPath - 1] != end)) {
  150. dir[lenPath] = end;
  151. dir[lenPath + 1] = '\0';
  152. } else {
  153. dir[lenPath] = '\0';
  154. }
  155. return dir;
  156. }