111 |
122 |
112 // |
123 // |
113 // CdlTkUtil |
124 // CdlTkUtil |
114 // |
125 // |
115 |
126 |
|
127 #ifdef CDL_W32 |
116 string CdlTkUtil::CurrentDrive() |
128 string CdlTkUtil::CurrentDrive() |
117 { |
129 { |
118 static string drive = "?:"; |
130 static string drive = "?:"; |
119 if (drive == "?:") |
131 if (drive == "?:") |
120 drive[0] = 'A' + _getdrive() - 1; |
132 drive[0] = 'A' + _getdrive() - 1; |
121 return drive; |
133 return drive; |
122 } |
134 } |
|
135 #else // Linux |
|
136 string CdlTkUtil::CurrentDrive() |
|
137 { |
|
138 char *epocroot = getenv("EPOCROOT"); |
|
139 assert(epocroot != NULL); |
|
140 |
|
141 return std::string(epocroot); |
|
142 } |
|
143 #endif |
123 |
144 |
124 string CdlTkUtil::CurrentDir() |
145 string CdlTkUtil::CurrentDir() |
125 { |
146 { |
126 static string dir = ""; |
147 static string dir = ""; |
127 if (dir == "") |
148 if (dir == "") |
128 { |
149 { |
129 char buf[256]; |
150 char buf[256]; |
|
151 #ifdef CDL_W32 |
130 dir = _getcwd(buf, 255) + 2; // +2 removes drive |
152 dir = _getcwd(buf, 255) + 2; // +2 removes drive |
131 dir += "\\"; |
153 #else |
|
154 //for linux |
|
155 dir = _getcwd(buf, 255); |
|
156 #endif |
|
157 dir += PATHSEP; |
132 } |
158 } |
133 return dir; |
159 return dir; |
134 } |
160 } |
135 |
161 |
136 static string gOutputPath = ""; |
162 static string gOutputPath = ""; |
144 |
170 |
145 void CdlTkUtil::SetOutputPath(const string& aPath) |
171 void CdlTkUtil::SetOutputPath(const string& aPath) |
146 { |
172 { |
147 gOutputPath = aPath; |
173 gOutputPath = aPath; |
148 if (gOutputPath.size() == 0) |
174 if (gOutputPath.size() == 0) |
149 gOutputPath += ".\\"; |
175 gOutputPath += PATHSEP; |
150 else if (gOutputPath[gOutputPath.size()-1] != '\\') |
176 else if (! IsPathSeparator( gOutputPath[gOutputPath.size()-1] ) ) |
151 gOutputPath += "\\"; // CDL Tk convention is that paths always end in \ |
177 gOutputPath += PATHSEP; // CDL Tk convention is that paths always end in backslash. |
152 } |
178 } |
153 |
179 |
154 string CdlTkUtil::ToLower(const string& aString) |
180 string CdlTkUtil::ToLower(const string& aString) |
155 { |
181 { |
156 string r; |
182 string r; |
168 } |
194 } |
169 |
195 |
170 string CdlTkUtil::ToCpp(const string& aString) |
196 string CdlTkUtil::ToCpp(const string& aString) |
171 { |
197 { |
172 string r = aString; |
198 string r = aString; |
|
199 r = Replace("\r", "", r); |
|
200 r = Replace("\n", "", r); |
|
201 |
173 for (string::iterator pC = r.begin(); pC != r.end(); ++pC) |
202 for (string::iterator pC = r.begin(); pC != r.end(); ++pC) |
174 { |
203 { |
175 if (!CdlTkUtil::IsCpp(*pC)) |
204 if (!CdlTkUtil::IsCpp(*pC)) |
176 *pC = '_'; |
205 *pC = '_'; |
177 } |
206 } |
180 return r; |
209 return r; |
181 } |
210 } |
182 |
211 |
183 string CdlTkUtil::StripPath(const string& aPath) |
212 string CdlTkUtil::StripPath(const string& aPath) |
184 { |
213 { |
185 return aPath.substr(aPath.rfind('\\')+1); |
214 return aPath.substr( FindLastPathSeparator(aPath) + 1 ); |
186 } |
215 } |
187 |
216 |
188 string CdlTkUtil::ResolvePath(const string& aPath, const string& aFileName) |
217 string CdlTkUtil::ResolvePath(const string& aPath, const string& aFileName) |
189 { |
218 { |
190 int size = aFileName.size(); |
219 int size = aFileName.size(); |
191 // if aFileName is absolute, return it |
220 // if aFileName is absolute, return it |
192 if (size > 0 && aFileName[0] == '\\' || size > 1 && aFileName[1] == ':') |
221 if (size > 0 && IsPathSeparator( aFileName[0] ) || size > 1 && aFileName[1] == ':') |
193 return aFileName; |
222 return aFileName; |
194 |
223 |
195 string path = aPath; |
224 string path = aPath; |
196 string file = aFileName; |
225 string file = aFileName; |
197 |
226 |
200 { |
229 { |
201 // if file starts with a "..", chop the tail directory off the path |
230 // if file starts with a "..", chop the tail directory off the path |
202 if (file.size() > 1 && file[1]=='.' && !path.empty()) |
231 if (file.size() > 1 && file[1]=='.' && !path.empty()) |
203 { |
232 { |
204 path.resize(path.size()-1); // remove the last slash |
233 path.resize(path.size()-1); // remove the last slash |
205 path.resize(path.rfind('\\')+1); // remove everything after the next last slash |
234 path.resize( FindLastPathSeparator(path) + 1 ); // remove everything after the next last slash |
206 } |
235 } |
207 |
236 |
208 // chop the head directory off the file - it has to have a '\' if it has a '.' |
237 // chop the head directory off the file - it has to have a '\' if it has a '.' |
209 int fileSlashPos = file.find('\\'); |
238 string::size_type fileSlashPos = FindFirstPathSeparator( file ); |
210 if (fileSlashPos == string::npos) |
239 if (fileSlashPos == string::npos) |
211 throw CdlTkAssert("Illegal filename"); |
240 throw CdlTkAssert("Illegal filename"); |
212 file = file.substr(fileSlashPos + 1); |
241 file = file.substr(fileSlashPos + 1); |
213 } |
242 } |
214 |
243 |
219 string CdlTkUtil::CapitalizeFilename(const string& aString) |
248 string CdlTkUtil::CapitalizeFilename(const string& aString) |
220 { |
249 { |
221 // convert the whole thing to lower case |
250 // convert the whole thing to lower case |
222 string res = ToLower(aString); |
251 string res = ToLower(aString); |
223 // find the first character after the last \ - will be 0 if no \ is present. |
252 // find the first character after the last \ - will be 0 if no \ is present. |
224 int filenamePos = res.find_last_of('\\') + 1; |
253 string::size_type filenamePos = FindLastPathSeparator(res) + 1; |
225 if (filenamePos >= res.size()) |
254 if (filenamePos >= res.size()) |
226 throw CdlTkAssert(aString + " has no filename"); |
255 throw CdlTkAssert(aString + " has no filename"); |
227 // uppercase the first character |
256 // uppercase the first character |
228 res[filenamePos] = toupper(res[filenamePos]); |
257 res[filenamePos] = toupper(res[filenamePos]); |
229 cerr << "Warning, filename capitalized: " << res << endl; |
258 cerr << "Warning, filename capitalized: " << res << endl; |
347 } |
376 } |
348 |
377 |
349 void CdlTkUtil::OpenTempOutput(ofstream& aStream, CCdlTkFileCleanup& aFile, ios_base::openmode aOpenMode) |
378 void CdlTkUtil::OpenTempOutput(ofstream& aStream, CCdlTkFileCleanup& aFile, ios_base::openmode aOpenMode) |
350 { |
379 { |
351 char tmpName[256]; |
380 char tmpName[256]; |
|
381 #ifdef CDL_W32 |
352 if (!tmpnam(tmpName)) |
382 if (!tmpnam(tmpName)) |
353 { |
383 { |
354 throw CdlTkAssert("Can't create temporary file name"); |
384 throw CdlTkAssert("Can't create temporary file name"); |
355 } |
385 } |
|
386 #else |
|
387 strcpy(tmpName, "cdltkutilXXXXXX"); |
|
388 |
|
389 if (-1 == mkstemp(tmpName)) |
|
390 { |
|
391 throw CdlTkAssert("Can't create temporary file name"); |
|
392 } |
|
393 #endif |
356 |
394 |
357 OpenOutput(aStream, tmpName, aOpenMode); |
395 OpenOutput(aStream, tmpName, aOpenMode); |
358 aFile.Set(tmpName); |
396 aFile.Set(tmpName); |
359 } |
397 } |
360 |
398 |
361 void CdlTkUtil::OpenOutput(ofstream& aStream, const string& aFileName, ios_base::openmode aOpenMode) |
399 void CdlTkUtil::OpenOutput(ofstream& aStream, const string& aFileName, ios_base::openmode aOpenMode) |
362 { |
400 { |
363 aStream.open(aFileName.c_str(), aOpenMode); |
401 aStream.open(aFileName.c_str(), aOpenMode); |
364 if (!aStream.is_open()) |
402 if (!aStream.is_open()) |
365 { |
403 { |
366 throw CdlTkFileOpenErr(aFileName); |
404 //throw CdlTkFileOpenErr(aFileName); |
367 } |
405 } |
368 } |
406 } |
369 |
407 |
370 void CdlTkUtil::OpenInput(ifstream& aStream, const string& aFileName, ios_base::openmode aOpenMode) |
408 void CdlTkUtil::OpenInput(ifstream& aStream, const string& aFileName, ios_base::openmode aOpenMode) |
371 { |
409 { |
449 bool CdlTkUtil::IsCpp(char aChar) |
487 bool CdlTkUtil::IsCpp(char aChar) |
450 { |
488 { |
451 return IsAlpha(aChar) || IsNumeric(aChar) || aChar == '_'; |
489 return IsAlpha(aChar) || IsNumeric(aChar) || aChar == '_'; |
452 } |
490 } |
453 |
491 |
|
492 bool CdlTkUtil::IsPathSeparator(char aChar) |
|
493 { |
|
494 return aChar == '/' || aChar == '\\'; |
|
495 } |
|
496 |
|
497 std::string::size_type CdlTkUtil::FindFirstPathSeparator(const std::string& s) |
|
498 { |
|
499 std::string::size_type f = s.find(FORWARDSLASH); |
|
500 std::string::size_type b = s.find(BACKSLASH); |
|
501 |
|
502 if(f == std::string::npos) |
|
503 return b; |
|
504 |
|
505 if(b == std::string::npos) |
|
506 return f; |
|
507 |
|
508 return f < b ? f : b; |
|
509 } |
|
510 |
|
511 std::string::size_type CdlTkUtil::FindLastPathSeparator(const std::string& s) |
|
512 { |
|
513 std::string::size_type f = s.rfind(FORWARDSLASH); |
|
514 std::string::size_type b = s.rfind(BACKSLASH); |
|
515 |
|
516 if(f == std::string::npos) |
|
517 return b; |
|
518 |
|
519 if(b == std::string::npos) |
|
520 return f; |
|
521 |
|
522 return f > b ? f : b; |
|
523 } |
454 void ZeroInts(int* aInts, int aCount) |
524 void ZeroInts(int* aInts, int aCount) |
455 { |
525 { |
456 for (int ii=0; ii<aCount; ii++) |
526 for (int ii=0; ii<aCount; ii++) |
457 aInts[ii] = 0; |
527 aInts[ii] = 0; |
458 } |
528 } |
477 const string& target = aSet[ii].first; |
547 const string& target = aSet[ii].first; |
478 int& targetMatch = match[ii]; |
548 int& targetMatch = match[ii]; |
479 if (target[targetMatch] == ch) |
549 if (target[targetMatch] == ch) |
480 { |
550 { |
481 ++targetMatch; |
551 ++targetMatch; |
482 if (targetMatch == target.size()) |
552 if ( static_cast<string::size_type>( targetMatch ) == target.size()) |
483 { |
553 { |
484 AppendString(ret, aIn.substr(lastMatch, pos - targetMatch - lastMatch)); |
554 AppendString(ret, aIn.substr(lastMatch, pos - targetMatch - lastMatch)); |
485 AppendString(ret, aSet[ii].second); |
555 AppendString(ret, aSet[ii].second); |
486 lastMatch = pos; |
556 lastMatch = pos; |
487 ZeroInts(match, setCount); |
557 ZeroInts(match, setCount); |
499 return ret; |
569 return ret; |
500 } |
570 } |
501 |
571 |
502 void CdlTkUtil::AppendString(string& aTarget, const string& aAppend) |
572 void CdlTkUtil::AppendString(string& aTarget, const string& aAppend) |
503 { |
573 { |
504 int resSize = aTarget.size() + aAppend.size(); |
574 string::size_type resSize = aTarget.size() + aAppend.size(); |
505 if (aTarget.capacity() < resSize) |
575 if (aTarget.capacity() < resSize) |
506 aTarget.reserve(resSize*2); |
576 aTarget.reserve(resSize*2); |
507 aTarget.append(aAppend); |
577 aTarget.append(aAppend); |
508 } |
578 } |
509 |
579 |
515 } |
585 } |
516 |
586 |
517 void CdlTkUtil::SetCommandLine(int argc, char* argv[]) |
587 void CdlTkUtil::SetCommandLine(int argc, char* argv[]) |
518 { |
588 { |
519 string tool(argv[0]); |
589 string tool(argv[0]); |
520 tool = tool.substr(tool.find_last_of('\\') + 1); |
590 |
|
591 tool = StripPath( tool ); |
521 gCommandLine = tool.substr(0, tool.find_last_of('.')); |
592 gCommandLine = tool.substr(0, tool.find_last_of('.')); |
522 for (int ii=1; ii<argc; ii++) |
593 for (int ii=1; ii<argc; ii++) |
523 { |
594 { |
524 AppendString(gCommandLine, " "); |
595 AppendString(gCommandLine, " "); |
525 AppendString(gCommandLine, argv[ii]); |
596 AppendString(gCommandLine, argv[ii]); |