Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

strings.cpp 4.0KB

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