1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/*
* Copyright 2006 Sony Computer Entertainment Inc.
*
* Licensed under the MIT Open Source License, for details please see license.txt or the website
* http://www.opensource.org/licenses/mit-license.php
*
*/
#include <cstdarg>
#include <algorithm>
#include <iterator>
#include <dae/daeUtils.h>
#include <dae/daeURI.h>
#ifdef _WIN32
#include <direct.h> // for getcwd (windows)
#else
#include <unistd.h> // for getcwd (linux)
#endif
#ifndef NO_BOOST
#include <boost/filesystem/convenience.hpp> // THIS WAS NOT COMMENTED.
#endif
#include <cstdio> // for tmpnam
using namespace std;
cdom::systemType cdom::getSystemType() {
#ifdef WIN32
return Windows;
#else
return Posix;
#endif
}
string cdom::replace(const string& s, const string& replace, const string& replaceWith) {
if (replace.empty())
return s;
string result;
size_t pos1 = 0, pos2 = s.find(replace);
while (pos2 != string::npos) {
result += s.substr(pos1, pos2-pos1);
result += replaceWith;
pos1 = pos2 + replace.length();
pos2 = s.find(replace, pos1);
}
result += s.substr(pos1, s.length()-pos1);
return result;
}
void cdom::trimWhitespaces(string& str) {
string whitespaces ( " \t\f\v\n\r" );
size_t found = str.find_last_not_of( whitespaces );
if ( found != std::string::npos )
{
str.erase( found + 1 );
found = str.find_first_not_of( whitespaces );
if ( found != std::string::npos )
str.erase( 0, found );
}
else
{
// whitespaces only
str.clear();
}
}
void cdom::tokenize(const string& s,
const string& separators,
/* out */ list<string>& tokens,
bool separatorsInResult) {
size_t currentIndex = 0, nextTokenIndex = 0;
while (currentIndex < s.length() &&
(nextTokenIndex = s.find_first_of(separators, currentIndex)) != string::npos) {
if ((nextTokenIndex - currentIndex) > 0)
tokens.push_back(s.substr(currentIndex, nextTokenIndex-currentIndex));
if (separatorsInResult)
tokens.push_back(string(1, s[nextTokenIndex]));
currentIndex = nextTokenIndex+1;
}
if (currentIndex < s.length())
tokens.push_back(s.substr(currentIndex, s.length()-currentIndex));
}
list<string> cdom::tokenize(const string& s,
const string& separators,
bool separatorsInResult) {
list<string> result;
tokenize(s, separators, result, separatorsInResult);
return result;
}
vector<string> cdom::makeStringArray(const char* s, ...) {
va_list args;
va_start(args, s);
vector<string> result;
while (s) {
result.push_back(s);
s = va_arg(args, const char*);
}
va_end(args);
return result;
}
list<string> cdom::makeStringList(const char* s, ...) {
va_list args;
va_start(args, s);
list<string> result;
while (s) {
result.push_back(s);
s = va_arg(args, const char*);
}
va_end(args);
return result;
}
string cdom::getCurrentDir() {
#ifdef __CELLOS_LV2__
// The PS3 has no getcwd call.
// !!!steveT Should we return app_home instead?
return "/";
#else
char buffer[1024];
#ifdef _WIN32
_getcwd(buffer, 1024);
#else
getcwd(buffer, 1024);
#endif
return buffer;
#endif
}
string cdom::getCurrentDirAsUri() {
string result = string("file://") + cdom::nativePathToUri(getCurrentDir());
// Make sure the last char is a /
if (!result.empty() && result[result.length()-1] != '/')
result += "/";
return result;
}
char cdom::getFileSeparator() {
if (getSystemType() == Windows) {
return '\\';
}
return '/';
}
#ifndef NO_BOOST
const string& cdom::getSystemTmpDir() {
#ifdef WIN32
static string tmpDir = string(getenv("TMP")) + getFileSeparator();
#elif defined(__linux__) || defined(__linux)
static string tmpDir = "/tmp/";
#elif defined __APPLE_CC__
static string tmpDir = string(getenv("TMPDIR"));
#elif defined __CELLOS_LV2__
#error tmp dir for your system unknown
#else
#error tmp dir for your system unknown
#endif
return tmpDir;
}
string cdom::getRandomFileName() {
std::string randomSegment;
// have to createa a buffer in order to make it multi-thread safe
std::string tmpbuffer; tmpbuffer.resize(L_tmpnam*2+1);
std::string tmp(tmpnam(&tmpbuffer[0]));
#ifdef WIN32
randomSegment = tmp.substr(tmp.find_last_of('\\')+1);
#elif defined(__linux__) || defined(__linux)
randomSegment = tmp.substr(tmp.find_last_of('/')+1);
#elif defined __APPLE_CC__
randomSegment = tmp.substr(tmp.find_last_of('/')+1);
#elif defined __CELLOS_LV2__
#error usage of tmpnam() for your system unknown
#else
#error usage of tmpnam() for your system unknown
#endif
return randomSegment;
}
const string& cdom::getSafeTmpDir() {
// there is a race condition here is multiple collada-dom -enabled processes call getSafeTmpDir at the same time.
// Therefore, have to check if directory already exists before using it. This still leaves the race
// condition, but makes it more difficult to reproduce. A better alternative would be to stop relying on tmpnam!
static string tmpDir;
do {
tmpDir = getSystemTmpDir() + getRandomFileName() + getFileSeparator();
} while(boost::filesystem::is_directory(tmpDir));
return tmpDir;
}
#endif //NO_BOOST
#ifndef __MINGW64_VERSION_MAJOR
int cdom::strcasecmp(const char* str1, const char* str2) {
#ifdef _MSC_VER
return _stricmp(str1, str2);
#else
return ::strcasecmp(str1, str2);
#endif
}
#endif
string cdom::tolower(const string& s) {
string result;
transform(s.begin(), s.end(), back_inserter(result), ::tolower);
return result;
}
|