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 5.4KB

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