daeUtils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2006 Sony Computer Entertainment Inc.
  3. *
  4. * Licensed under the MIT Open Source License, for details please see license.txt or the website
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. */
  8. // A home for commonly used utility functions. These are mostly for internal DOM
  9. // use, but the automated tests use some of these functions, so we'll export
  10. // them.
  11. #ifndef daeUtils_h
  12. #define daeUtils_h
  13. #include <string>
  14. #include <sstream>
  15. #include <list>
  16. #include <vector>
  17. #include <dae/daePlatform.h>
  18. namespace cdom {
  19. // System type info. We only need to distinguish between Posix and Winodws for now.
  20. enum systemType {
  21. Posix,
  22. Windows
  23. };
  24. // Get the system type at runtime.
  25. DLLSPEC systemType getSystemType();
  26. // String replace function. Usage: replace("abcdef", "cd", "12") --> "ab12ef".
  27. DLLSPEC std::string replace(const std::string& s,
  28. const std::string& replace,
  29. const std::string& replaceWith);
  30. // Removes whitespaces (" \t\f\v\n\r") at the beginning and the end of str.
  31. // If str consists of whitespaces only it will be erased.
  32. // Usage:
  33. // trimWhitespaces(" a b") --> "a b"
  34. // trimWhitespaces("a b ") --> "a b"
  35. // trimWhitespaces(" a b ") --> "a b"
  36. // trimWhitespaces(" ") --> ""
  37. DLLSPEC void trimWhitespaces(std::string& str);
  38. // Usage:
  39. // tokenize("this/is some#text", "/#", true) --> ("this" "/" "is some" "#" "text")
  40. // tokenize("this is some text", " ", false) --> ("this" "is" "some" "text")
  41. DLLSPEC std::list<std::string> tokenize(const std::string& s,
  42. const std::string& separators,
  43. bool separatorsInResult = false);
  44. // Same as the previous function, but returns the result via a parameter to avoid an object copy.
  45. DLLSPEC void tokenize(const std::string& s,
  46. const std::string& separators,
  47. /* out */ std::list<std::string>& tokens,
  48. bool separatorsInResult = false);
  49. typedef std::list<std::string>::iterator tokenIter;
  50. #if 1
  51. DLLSPEC std::vector<std::string> makeStringArray(const char* s, ...);
  52. DLLSPEC std::list<std::string> makeStringList(const char* s, ...);
  53. #else
  54. // degenerate makeString<T>(Container&, 0): end of recursion
  55. template < class Container, typename Type0 >
  56. void makeString(Container&, Type0 string0)
  57. {
  58. // All existing calls end with 0
  59. assert(! string0);
  60. }
  61. // makeString(Container&, at least one string, ..., 0)
  62. template < class Container,
  63. typename Type0, typename Type1, typename ... Types >
  64. void makeString(Container& partial,
  65. Type0 string0, Type1 string1, Types... strings)
  66. {
  67. partial.push_back(string0);
  68. makeString(partial, string1, strings...);
  69. }
  70. template < typename ... Types >
  71. std::vector<std::string> makeStringArray(Types... strings)
  72. {
  73. std::vector<std::string> result;
  74. makeString(result, strings...);
  75. return result;
  76. }
  77. template < typename ... Types >
  78. std::list<std::string> makeStringList(Types... strings)
  79. {
  80. std::list<std::string> result;
  81. makeString(result, strings...);
  82. return result;
  83. }
  84. #endif
  85. DLLSPEC std::string getCurrentDir();
  86. DLLSPEC std::string getCurrentDirAsUri();
  87. // Returns platform specific file separator.
  88. // \ on windows
  89. // / on other platforms
  90. DLLSPEC char getFileSeparator();
  91. #ifndef NO_BOOST
  92. // Returns system wide temporary directory.
  93. // Reads environment variable TMP.
  94. DLLSPEC const std::string& getSystemTmpDir();
  95. // Returns a filename obtained via tmpnam().
  96. // On systems where tmpnam()'s result is preceded
  97. // with a directory, that directory is cutoff.
  98. DLLSPEC std::string getRandomFileName();
  99. // Returns getSystemTmpDir() appended with a randomly
  100. // generated directory name.
  101. // This directory will be deleted when DAE gets destroyed.
  102. DLLSPEC const std::string& getSafeTmpDir();
  103. #endif //NO_BOOST
  104. DLLSPEC int strcasecmp(const char* str1, const char* str2);
  105. DLLSPEC std::string tolower(const std::string& s);
  106. // Disable VS warning
  107. #ifdef _MSC_VER
  108. #pragma warning(push)
  109. #pragma warning(disable : 4267)
  110. #endif
  111. template<typename T>
  112. std::string toString(const T& val) {
  113. std::ostringstream stream;
  114. stream << val;
  115. return stream.str();
  116. }
  117. #ifdef _MSC_VER
  118. #pragma warning(pop)
  119. #endif
  120. }
  121. #endif