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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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__)
  13. #include <wordexp.h>
  14. #endif
  15. #include "utils/strings.h"
  16. void printStringVector(std::vector<char *> *args) {
  17. printf("(");
  18. for (std::vector<char *>::size_type i = 0; i < args->size(); i++) {
  19. printf("%s", args->at(i));
  20. if (i < (args->size() - 1))
  21. printf(" ");
  22. }
  23. printf(")");
  24. }
  25. bool stringEndsWith(const char *str, const char *suffix) {
  26. assert(str != NULL);
  27. assert(suffix != NULL);
  28. size_t lenstr = strlen(str);
  29. size_t lensuffix = strlen(suffix);
  30. if (lensuffix > lenstr)
  31. return false;
  32. return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
  33. }
  34. char *bufferString(const char *string, va_list args) {
  35. int sz = 60;
  36. int n;
  37. char *text;
  38. assert(string != NULL);
  39. assert(string[0] != '\0');
  40. text = new char[sz];
  41. n = vsnprintf(text, sz, string, args);
  42. if (n < 0) {
  43. delete [] text;
  44. return NULL; // encoding error
  45. } else if (n >= sz) {
  46. sz = n + 1; // buffer too small
  47. delete [] text;
  48. text = new char[sz];
  49. n = vsnprintf(text, sz, string, args);
  50. }
  51. return text;
  52. }
  53. char *bufferString(const char *string, ...) {
  54. va_list args;
  55. va_start(args, string);
  56. char *text = bufferString(string, args);
  57. va_end(args);
  58. return text;
  59. }
  60. char *fullPath(const char *path, char end) {
  61. unsigned int lenPath, offset;
  62. wordexp_t word;
  63. char *dir;
  64. assert(path != NULL);
  65. assert(path[0] != '\0');
  66. if (path[0] == '~') {
  67. #if defined(unix) || defined(__APPLE__)
  68. #ifdef __APPLE__
  69. // Workaround for Mac OS X. See:
  70. // http://stackoverflow.com/questions/20534788/why-does-wordexp-fail-with-wrde-syntax-on-os-x
  71. signal(SIGCHLD, SIG_DFL);
  72. #endif
  73. // Expand string into segments
  74. int res = wordexp(path, &word, 0);
  75. #ifdef __APPLE__
  76. signal(SIGCHLD, SIG_IGN);
  77. #endif
  78. if (res != 0) {
  79. printf("fullPath> wordexp() failed: %d\n", res);
  80. return NULL;
  81. }
  82. // Get length of complete string
  83. lenPath = 0;
  84. for (unsigned int i = 0; i < word.we_wordc; i++) {
  85. lenPath += strlen(word.we_wordv[i]);
  86. }
  87. // Allocate new string
  88. dir = new char[lenPath + 2]; // space for end char
  89. // Copy segments into new string
  90. offset = 0;
  91. for (unsigned int i = 0; i < word.we_wordc; i++) {
  92. unsigned int len = strlen(word.we_wordv[i]);
  93. strncpy(dir + offset, word.we_wordv[i], len);
  94. offset += len;
  95. }
  96. wordfree(&word);
  97. #else
  98. printf("WARNING: Tilde expansion not supported on this platform:\n\t%s\n", path);
  99. lenPath = strlen(path);
  100. dir = new char[lenPath + 2]; // space for end char
  101. strncpy(dir, path, lenPath);
  102. #endif
  103. } else {
  104. lenPath = strlen(path);
  105. dir = new char[lenPath + 2]; // space for end char
  106. strncpy(dir, path, lenPath);
  107. }
  108. // Make sure ends in "end" char
  109. if ((lenPath > 0) && (end != 0) && (dir[lenPath - 1] != end)) {
  110. dir[lenPath] = end;
  111. dir[lenPath + 1] = 0;
  112. } else {
  113. dir[lenPath] = 0;
  114. }
  115. return dir;
  116. }