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.6KB

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