1 /* |
1 /* |
2 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). |
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
3 * All rights reserved. |
3 * All rights reserved. |
4 * This component and the accompanying materials are made available |
4 * This component and the accompanying materials are made available |
5 * under the terms of the License "Eclipse Public License v1.0" |
5 * under the terms of the License "Eclipse Public License v1.0" |
6 * which accompanies this distribution, and is available |
6 * which accompanies this distribution, and is available |
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
50 { |
49 { |
51 // If path semantics is correct (as needed by sisx library) |
50 // If path semantics is correct (as needed by sisx library) |
52 // then the function will return 0 |
51 // then the function will return 0 |
53 int ret = 0; |
52 int ret = 0; |
54 int pos = 0; |
53 int pos = 0; |
55 #ifndef __TOOLS2_LINUX__ |
54 if((pos = aPath.find(L"/", aIndex)) != std::wstring::npos) |
56 if((pos = aPath.find(L"//", aIndex)) != std::wstring::npos) |
55 { |
|
56 ret = 1; |
|
57 } |
|
58 else if((pos = aPath.find(L"\\\\", aIndex)) != std::wstring::npos) |
57 { |
59 { |
58 ret = 2; |
60 ret = 2; |
59 } |
61 } |
60 #else |
|
61 if((pos = aPath.find(L"\\\\", aIndex)) != std::wstring::npos) |
|
62 { |
|
63 ret = 2; |
|
64 } |
|
65 #endif |
|
66 |
|
67 #ifndef __TOOLS2_LINUX__ |
|
68 else if((pos = aPath.find(L"/", aIndex)) != std::wstring::npos) |
|
69 { |
|
70 ret = 1; |
|
71 } |
|
72 #else |
|
73 else if((pos = aPath.find(L"\\", aIndex)) != std::wstring::npos) |
|
74 { |
|
75 ret = 1; |
|
76 } |
|
77 #endif |
|
78 aIndex = pos; |
62 aIndex = pos; |
79 return ret; |
63 return ret; |
80 } |
64 } |
81 |
65 |
82 std::wstring StringUtils::FixPathDelimiters( const std::string& aString ) |
66 std::wstring StringUtils::FixPathDelimiters( const std::string& aString ) |
87 // |
71 // |
88 wchar_t* buf = NULL ; |
72 wchar_t* buf = NULL ; |
89 int len = ConvertMultiByteToWideChar(src, -1, NULL, 0); |
73 int len = ConvertMultiByteToWideChar(src, -1, NULL, 0); |
90 buf = new wchar_t[len+1]; |
74 buf = new wchar_t[len+1]; |
91 len = ConvertMultiByteToWideChar(src, -1, buf, len); |
75 len = ConvertMultiByteToWideChar(src, -1, buf, len); |
92 |
76 ret = std::wstring( buf ); |
93 ret = std::wstring( buf ,len); |
|
94 |
|
95 delete[] buf; |
77 delete[] buf; |
96 // |
78 // |
97 std::wstring::size_type idx = 0; |
79 std::wstring::size_type idx = 0; |
98 while(len = FirstInvalidDirSeparatorSize(ret, idx)) |
80 while(len = FirstInvalidDirSeparatorSize(ret, idx)) |
99 { |
81 { |
100 ret.replace(idx, len, KDirectorySeparator); |
82 ret.replace(idx, len, KDirectorySeparator); |
101 } |
83 } |
102 // |
84 // |
103 return ret.c_str(); |
85 return ret; |
104 } |
86 } |
105 |
87 |
106 |
88 |
107 std::string StringUtils::TrimWhiteSpace( const std::string& aString ) |
89 std::string StringUtils::TrimWhiteSpace( const std::string& aString ) |
108 { |
90 { |
160 std::transform( ret.begin(), ret.end(), ret.begin(), tolower ); |
142 std::transform( ret.begin(), ret.end(), ret.begin(), tolower ); |
161 // |
143 // |
162 return ret; |
144 return ret; |
163 } |
145 } |
164 |
146 |
165 std::wstring StringUtils::ToLower( const std::wstring& aString ) |
|
166 { |
|
167 std::wstring ret( aString ); |
|
168 std::transform( ret.begin(), ret.end(), ret.begin(), tolower ); |
|
169 |
|
170 return ret; |
|
171 } |
|
172 |
|
173 bool StringUtils::IsLastCharacter( const std::wstring& aString, wchar_t aChar ) |
147 bool StringUtils::IsLastCharacter( const std::wstring& aString, wchar_t aChar ) |
174 { |
148 { |
175 bool isLast = false; |
149 bool isLast = false; |
176 // |
150 // |
177 if ( aString.length() ) |
151 if ( aString.length() ) |
188 { |
162 { |
189 bool startsWithDrive = false; |
163 bool startsWithDrive = false; |
190 // |
164 // |
191 if ( aText.length() >= 3 ) |
165 if ( aText.length() >= 3 ) |
192 { |
166 { |
193 const std::string prefix = ToUpper( wstring2string( aText.substr( 0, 3 ) ) ); |
167 const std::string prefix = ToUpper( Ucs2ToUtf8( aText.substr( 0, 3 ) ) ); |
194 // |
168 // |
195 const char drive = prefix[ 0 ]; |
169 const char drive = prefix[ 0 ]; |
196 const char colon = prefix[ 1 ]; |
170 const char colon = prefix[ 1 ]; |
197 const char backslash = prefix[ 2 ]; |
171 const char backslash = prefix[ 2 ]; |
198 // |
172 // |
199 #ifndef __TOOLS2_LINUX__ |
|
200 if ( colon == ':' && backslash == '\\' ) |
173 if ( colon == ':' && backslash == '\\' ) |
201 #else |
|
202 if ( colon == ':' && (backslash == '/' || backslash == '\\')) |
|
203 #endif |
|
204 { |
174 { |
205 startsWithDrive = ( drive >= 'A' && drive <= 'Z' ) || (drive == '!') || (drive == '$') ; |
175 startsWithDrive = ( drive >= 'A' && drive <= 'Z' ) || (drive == '!') ; |
206 } |
176 } |
207 } |
177 } |
208 // |
178 // |
209 return startsWithDrive; |
179 return startsWithDrive; |
210 } |
180 } |
298 |
268 |
299 The function returns TRUE if a match is found, or FALSE. |
269 The function returns TRUE if a match is found, or FALSE. |
300 */ |
270 */ |
301 bool StringUtils::WildcardCompare(const std::wstring& aWildCardFileName, const std::wstring& aFileName) |
271 bool StringUtils::WildcardCompare(const std::wstring& aWildCardFileName, const std::wstring& aFileName) |
302 { |
272 { |
303 #ifdef __TOOLS2_LINUX__ |
|
304 // In case the incoming data is a DB entry then it will be having windows |
|
305 // specific paths due to the consistency of DB contents across WINDOWS |
|
306 // and LINUX. So, we need to convert them to LINUX paths and then compare. |
|
307 |
|
308 std::wstring::size_type idx = 0; |
|
309 |
|
310 while( (idx = aWildCardFileName.find(L"\\", idx)) != std::wstring::npos) |
|
311 { |
|
312 aWildCardFileName.replace( idx, 1, L"/" ); |
|
313 } |
|
314 |
|
315 idx = 0; |
|
316 |
|
317 while( (idx = aFileName.find(L"\\", idx)) != std::wstring::npos) |
|
318 { |
|
319 aFileName.replace( idx, 1, L"/" ); |
|
320 } |
|
321 #endif |
|
322 |
|
323 aWildCardFileName = StringUtils::ToLower(aWildCardFileName); |
|
324 aFileName = StringUtils::ToLower(aFileName); |
|
325 |
|
326 std::wstring::const_iterator wildCurr = aWildCardFileName.begin(); |
273 std::wstring::const_iterator wildCurr = aWildCardFileName.begin(); |
327 std::wstring::const_iterator wildEnd = aWildCardFileName.end(); |
274 std::wstring::const_iterator wildEnd = aWildCardFileName.end(); |
328 |
275 |
329 std::wstring::const_iterator fileCurr = aFileName.begin(); |
276 std::wstring::const_iterator fileCurr = aFileName.begin(); |
330 std::wstring::const_iterator fileEnd = aFileName.end(); |
277 std::wstring::const_iterator fileEnd = aFileName.end(); |
408 |
355 |
409 @return The filename and extension. |
356 @return The filename and extension. |
410 */ |
357 */ |
411 std::wstring StringUtils::NameAndExt( const std::wstring& aFile ) |
358 std::wstring StringUtils::NameAndExt( const std::wstring& aFile ) |
412 { |
359 { |
413 #ifndef __TOOLS2_LINUX__ |
|
414 int pos = aFile.find_last_of(L"\\"); |
360 int pos = aFile.find_last_of(L"\\"); |
415 #else |
|
416 // We should also check for backward slash since the caller could be |
|
417 // passing a string containing a windows-specific paths within LINUX. |
|
418 // |
|
419 // One instance being - an SCR database entry under LINUX, which has |
|
420 // the windows specific paths of the installed files to maintain |
|
421 // consistency across WINDOWS and LINUX platforms. |
|
422 |
|
423 int pos = aFile.find_last_of(L"/"); |
|
424 pos = pos == std::wstring::npos ? aFile.find_last_of(L"\\") : pos; |
|
425 #endif |
|
426 if (pos == std::wstring::npos) |
361 if (pos == std::wstring::npos) |
427 { |
362 { |
428 return L""; |
363 return L""; |
429 } |
364 } |
430 else |
365 else |
445 |
380 |
446 @return The drive and path. |
381 @return The drive and path. |
447 */ |
382 */ |
448 std::wstring StringUtils::DriveAndPath( const std::wstring& aFile ) |
383 std::wstring StringUtils::DriveAndPath( const std::wstring& aFile ) |
449 { |
384 { |
450 #ifndef __TOOLS2_LINUX__ |
385 int pos = aFile.find_last_of(L"\\"); |
451 int pos = aFile.find_last_of(L"\\"); |
|
452 #else |
|
453 int pos = aFile.find_last_of(L"/"); |
|
454 #endif |
|
455 if (pos == std::wstring::npos) |
386 if (pos == std::wstring::npos) |
456 { |
387 { |
457 return L""; |
388 return L""; |
458 } |
389 } |
459 else |
390 else |
472 |
403 |
473 @return The path. It always begins and ends in a backslash. |
404 @return The path. It always begins and ends in a backslash. |
474 */ |
405 */ |
475 std::wstring StringUtils::Path( const std::wstring& aFile ) |
406 std::wstring StringUtils::Path( const std::wstring& aFile ) |
476 { |
407 { |
477 #ifndef __TOOLS2_LINUX__ |
408 int firstPos = aFile.find_first_of(L"\\"); |
478 int firstPos = aFile.find_first_of(L"\\"); |
409 int lastPos = aFile.find_last_of(L"\\"); |
479 int lastPos = aFile.find_last_of(L"\\"); |
|
480 #else |
|
481 int firstPos = aFile.find_first_of(L"/"); |
|
482 int lastPos = aFile.find_last_of(L"/"); |
|
483 #endif |
|
484 |
410 |
485 if (lastPos >= firstPos) |
411 if (lastPos >= firstPos) |
486 { |
412 { |
487 std::wstring path(aFile.substr(firstPos,lastPos-1)); |
413 std::wstring path(aFile.substr(firstPos,lastPos-1)); |
488 return path; |
414 return path; |
500 |
426 |
501 @return The filename. |
427 @return The filename. |
502 */ |
428 */ |
503 std::wstring StringUtils::Name( const std::wstring& aFile ) |
429 std::wstring StringUtils::Name( const std::wstring& aFile ) |
504 { |
430 { |
505 #ifndef __TOOLS2_LINUX__ |
|
506 int startPos = aFile.find_last_of(L"\\"); |
431 int startPos = aFile.find_last_of(L"\\"); |
507 #else |
|
508 int startPos = aFile.find_last_of(L"/"); |
|
509 #endif |
|
510 |
|
511 int endPos = aFile.find_last_of(L"."); |
432 int endPos = aFile.find_last_of(L"."); |
512 |
433 |
513 if (endPos > startPos) |
434 if (endPos > startPos) |
514 { |
435 { |
515 std::wstring extension(aFile.substr(startPos+1,endPos)); |
436 std::wstring extension(aFile.substr(startPos+1,endPos)); |