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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #if defined(unix) || defined(__APPLE__)
  13. #include <wordexp.h>
  14. #endif
  15. #include "utils/strings.h"
  16. bool stringEndsWith(const char *str, const char *suffix) {
  17. if (!str || !suffix)
  18. return false;
  19. size_t lenstr = strlen(str);
  20. size_t lensuffix = strlen(suffix);
  21. if (lensuffix > lenstr)
  22. return false;
  23. return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
  24. }
  25. char *bufferString(const char *string, ...) {
  26. int sz = 60;
  27. int n;
  28. char *text;
  29. va_list args;
  30. if (!string || !string[0])
  31. return NULL;
  32. text = new char[sz];
  33. va_start(args, string);
  34. n = vsnprintf(text, sz, string, args);
  35. if (n < 0) {
  36. delete [] text;
  37. return NULL; // encoding error
  38. } else if (n >= sz) {
  39. sz = n + 1; // buffer too small
  40. delete [] text;
  41. text = new char[sz];
  42. n = vsnprintf(text, sz, string, args);
  43. }
  44. va_end(args);
  45. return text;
  46. }
  47. char *fullPath(const char *path, char end) {
  48. unsigned int lenPath, offset;
  49. wordexp_t word;
  50. char *dir;
  51. if (!path || !path[0])
  52. return NULL;
  53. if (path[0] == '~') {
  54. #if defined(unix) || defined(__APPLE__)
  55. // Expand string into segments
  56. if (wordexp(path, &word, 0) != 0) {
  57. return NULL;
  58. }
  59. // Get length of complete string
  60. lenPath = 0;
  61. for (unsigned int i = 0; i < word.we_wordc; i++) {
  62. lenPath += strlen(word.we_wordv[i]);
  63. }
  64. // Allocate new string
  65. dir = new char[lenPath + 2]; // space for end char
  66. // Copy segments into new string
  67. offset = 0;
  68. for (unsigned int i = 0; i < word.we_wordc; i++) {
  69. unsigned int len = strlen(word.we_wordv[i]);
  70. strncpy(dir + offset, word.we_wordv[i], len);
  71. offset += len;
  72. }
  73. wordfree(&word);
  74. #else
  75. printf("WARNING: Tilde expansion not supported on this platform!\n");
  76. lenPath = strlen(path);
  77. dir = new char[lenPath + 2]; // space for end char
  78. strncpy(dir, path, lenPath);
  79. #endif
  80. } else {
  81. lenPath = strlen(path);
  82. dir = new char[lenPath + 2]; // space for end char
  83. strncpy(dir, path, lenPath);
  84. }
  85. // Make sure ends in "end" char
  86. if (end && (dir[lenPath - 1] != end)) {
  87. dir[lenPath] = end;
  88. dir[lenPath + 1] = 0;
  89. } else {
  90. dir[lenPath] = 0;
  91. }
  92. return dir;
  93. }
  94. bool rc_command(const char *symbol, char *command) {
  95. if (!symbol || !symbol[0] || !command || !command[0])
  96. return false;
  97. int lens = strlen(symbol);
  98. if (strncmp(command, symbol, lens) == 0) {
  99. int lenc = strlen(command);
  100. //! \fixme Should ignore whitespace, but only if it is really there...?
  101. // lens+1 skips '=' or ' '
  102. for (int i = 0, j = lens+1; j < lenc; ++i, ++j) {
  103. command[i] = command[j];
  104. command[i+1] = 0;
  105. }
  106. return true;
  107. }
  108. return false;
  109. }
  110. int rc_get_bool(char *buffer, bool *val) {
  111. if (!buffer || !buffer[0])
  112. return -1;
  113. if ((buffer[0] == '1') || (strncmp(buffer, "true", 4) == 0))
  114. *val = true;
  115. else if ((buffer[0] == '0') || (strncmp(buffer, "false", 5) == 0))
  116. *val = false;
  117. else
  118. return -2;
  119. return 0;
  120. }