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 14 #include <string> 15 #include <sstream> 16 #include <list> 17 #include <vector> 18 #include <dae/daePlatform.h> 19 20 namespace cdom { 21 // System type info. We only need to distinguish between Posix and Winodws for now. 22 enum systemType { 23 Posix, 24 Windows 25 }; 26 27 // Get the system type at runtime. 28 DLLSPEC systemType getSystemType(); 29 30 // String replace function. Usage: replace("abcdef", "cd", "12") --> "ab12ef". 31 DLLSPEC std::string replace(const std::string& s, 32 const std::string& replace, 33 const std::string& replaceWith); 34 35 // Removes whitespaces (" \t\f\v\n\r") at the beginning and the end of str. 36 // If str consists of whitespaces only it will be erased. 37 // Usage: 38 // trimWhitespaces(" a b") --> "a b" 39 // trimWhitespaces("a b ") --> "a b" 40 // trimWhitespaces(" a b ") --> "a b" 41 // trimWhitespaces(" ") --> "" 42 DLLSPEC void trimWhitespaces(std::string& str); 43 44 // Usage: 45 // tokenize("this/is some#text", "/#", true) --> ("this" "/" "is some" "#" "text") 46 // tokenize("this is some text", " ", false) --> ("this" "is" "some" "text") 47 DLLSPEC std::list<std::string> tokenize(const std::string& s, 48 const std::string& separators, 49 bool separatorsInResult = false); 50 // Same as the previous function, but returns the result via a parameter to avoid an object copy. 51 DLLSPEC void tokenize(const std::string& s, 52 const std::string& separators, 53 /* out */ std::list<std::string>& tokens, 54 bool separatorsInResult = false); 55 56 typedef std::list<std::string>::iterator tokenIter; 57 58 DLLSPEC std::vector<std::string> makeStringArray(const char* s, ...); 59 DLLSPEC std::list<std::string> makeStringList(const char* s, ...); 60 61 DLLSPEC std::string getCurrentDir(); 62 DLLSPEC std::string getCurrentDirAsUri(); 63 64 // Returns platform specific file separator. 65 // \ on windows 66 // / on other platforms 67 DLLSPEC char getFileSeparator(); 68 69 #ifndef NO_BOOST 70 // Returns system wide temporary directory. 71 // Reads environment variable TMP. 72 DLLSPEC const std::string& getSystemTmpDir(); 73 74 // Returns a filename obtained via tmpnam(). 75 // On systems where tmpnam()'s result is preceded 76 // with a directory, that directory is cutoff. 77 DLLSPEC std::string getRandomFileName(); 78 79 // Returns getSystemTmpDir() appended with a randomly 80 // generated directory name. 81 // This directory will be deleted when DAE gets destroyed. 82 DLLSPEC const std::string& getSafeTmpDir(); 83 #endif //NO_BOOST 84 85 DLLSPEC int strcasecmp(const char* str1, const char* str2); 86 DLLSPEC std::string tolower(const std::string& s); 87 88 // Disable VS warning 89 #ifdef _MSC_VER 90 #pragma warning(push) 91 #pragma warning(disable : 4267) 92 #endif 93 template<typename T> 94 std::string toString(const T& val) { 95 std::ostringstream stream; 96 stream << val; 97 return stream.str(); 98 } 99 #ifdef _MSC_VER 100 #pragma warning(pop) 101 #endif 102 } 103 104 #endif 105