Home | History | Annotate | Download | only in dae
      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 #include <cstdarg>
      9 #include <algorithm>
     10 #include <iterator>
     11 #include <dae/daeUtils.h>
     12 #include <dae/daeURI.h>
     13 
     14 #ifdef _WIN32
     15 #include <direct.h>  // for getcwd (windows)
     16 #else
     17 #include <unistd.h>  // for getcwd (linux)
     18 #endif
     19 
     20 using namespace std;
     21 
     22 cdom::systemType cdom::getSystemType() {
     23 #ifdef WIN32
     24 	return Windows;
     25 #else
     26 	return Posix;
     27 #endif
     28 }
     29 
     30 string cdom::replace(const string& s, const string& replace, const string& replaceWith) {
     31 	if (replace.empty())
     32 		return s;
     33 
     34 	string result;
     35 	size_t pos1 = 0, pos2 = s.find(replace);
     36 	while (pos2 != string::npos) {
     37 		result += s.substr(pos1, pos2-pos1);
     38 		result += replaceWith;
     39 		pos1 = pos2 + replace.length();
     40 		pos2 = s.find(replace, pos1);
     41 	}
     42 
     43 	result += s.substr(pos1, s.length()-pos1);
     44 	return result;
     45 }
     46 
     47 void cdom::trimWhitespaces(string& str) {
     48     string whitespaces ( " \t\f\v\n\r" );
     49 
     50     size_t found = str.find_last_not_of( whitespaces );
     51     if ( found != std::string::npos )
     52     {
     53         str.erase( found + 1 );
     54         found = str.find_first_not_of( whitespaces );
     55         if ( found != std::string::npos )
     56             str.erase( 0, found );
     57     }
     58     else
     59     {
     60         // whitespaces only
     61         str.clear();
     62     }
     63 }
     64 
     65 void cdom::tokenize(const string& s,
     66                     const string& separators,
     67                     /* out */ list<string>& tokens,
     68                     bool separatorsInResult) {
     69 	size_t currentIndex = 0, nextTokenIndex = 0;
     70 	while (currentIndex < s.length() &&
     71 	       (nextTokenIndex = s.find_first_of(separators, currentIndex)) != string::npos) {
     72 		if ((nextTokenIndex - currentIndex) > 0)
     73 			tokens.push_back(s.substr(currentIndex, nextTokenIndex-currentIndex));
     74 		if (separatorsInResult)
     75 			tokens.push_back(string(1, s[nextTokenIndex]));
     76 		currentIndex = nextTokenIndex+1;
     77 	}
     78 
     79 	if (currentIndex < s.length())
     80 		tokens.push_back(s.substr(currentIndex, s.length()-currentIndex));
     81 }
     82 
     83 list<string> cdom::tokenize(const string& s,
     84                             const string& separators,
     85                             bool separatorsInResult) {
     86 	list<string> result;
     87 	tokenize(s, separators, result, separatorsInResult);
     88 	return result;
     89 }
     90 
     91 vector<string> cdom::makeStringArray(const char* s, ...) {
     92 	va_list args;
     93 	va_start(args, s);
     94 	vector<string> result;
     95 	while (s) {
     96 		result.push_back(s);
     97 		s = va_arg(args, const char*);
     98 	}
     99 	va_end(args);
    100 	return result;
    101 }
    102 
    103 list<string> cdom::makeStringList(const char* s, ...) {
    104 	va_list args;
    105 	va_start(args, s);
    106 	list<string> result;
    107 	while (s) {
    108 		result.push_back(s);
    109 		s = va_arg(args, const char*);
    110 	}
    111 	va_end(args);
    112 	return result;
    113 }
    114 
    115 string cdom::getCurrentDir() {
    116 #ifdef __CELLOS_LV2__
    117 	// The PS3 has no getcwd call.
    118 	// !!!steveT Should we return app_home instead?
    119 	return "/";
    120 #else
    121 	char buffer[1024];
    122 #ifdef _WIN32
    123 	_getcwd(buffer, 1024);
    124 #else
    125 	getcwd(buffer, 1024);
    126 #endif
    127 	return buffer;
    128 #endif
    129 }
    130 
    131 string cdom::getCurrentDirAsUri() {
    132 	string result = string("file://") + cdom::nativePathToUri(getCurrentDir());
    133 	// Make sure the last char is a /
    134 	if (!result.empty()  &&  result[result.length()-1] != '/')
    135 		result += "/";
    136 	return result;
    137 }
    138 
    139 char cdom::getFileSeparator() {
    140     if (getSystemType() == Windows) {
    141         return '\\';
    142     }
    143     return '/';
    144 }
    145 #ifndef NO_BOOST
    146 const string& cdom::getSystemTmpDir() {
    147 #ifdef WIN32
    148     static string tmpDir = string(getenv("TMP")) + getFileSeparator();
    149 #elif defined(__linux__) || defined(__linux)
    150     static string tmpDir = "/tmp/";
    151 #elif defined __APPLE_CC__
    152 static string tmpDir = string(getenv("TMPDIR"));
    153 #elif defined __CELLOS_LV2__
    154 #error tmp dir for your system unknown
    155 #else
    156 #error tmp dir for your system unknown
    157 #endif
    158     return tmpDir;
    159 }
    160 
    161 string cdom::getRandomFileName() {
    162     std::string randomSegment;
    163 #ifdef WIN32
    164     std::string tmp(tmpnam(0));
    165     randomSegment = tmp.substr(tmp.find_last_of('\\')+1);
    166 #elif defined(__linux__) || defined(__linux)
    167     std::string tmp(tmpnam(0));
    168     randomSegment = tmp.substr(tmp.find_last_of('/')+1);
    169 #elif defined __APPLE_CC__
    170 	std::string tmp(tmpnam(0));
    171 	randomSegment = tmp.substr(tmp.find_last_of('/')+1);
    172 #elif defined __CELLOS_LV2__
    173 #error  usage of tmpnam() for your system unknown
    174 #else
    175 #error  usage of tmpnam() for your system unknown
    176 #endif
    177     return randomSegment;
    178 }
    179 
    180 const string& cdom::getSafeTmpDir() {
    181     static string tmpDir = getSystemTmpDir() + getRandomFileName() + getFileSeparator();
    182     return tmpDir;
    183 }
    184 #endif //NO_BOOST
    185 
    186 int cdom::strcasecmp(const char* str1, const char* str2) {
    187 #ifdef _MSC_VER
    188 	return _stricmp(str1, str2);
    189 #else
    190 	return ::strcasecmp(str1, str2);
    191 #endif
    192 }
    193 
    194 string cdom::tolower(const string& s) {
    195 	string result;
    196 	transform(s.begin(), s.end(), back_inserter(result), ::tolower);
    197 	return result;
    198 }
    199