| #include "Path.h" | |
| #include "String.h" | |
| void Replace(STRING str, char oldValue, char newValue) | |
| { | |
| int len = String::StrLen(str); | |
| for ( int i = 0; i < len; i++) | |
| if ( oldValue == *(i + str) ) *(i + str) = newValue; | |
| } | |
| STRING Path::Combine(STRING path1, STRING path2) | |
| { | |
| //check that path1 ends with PATH_SEPARATOR | |
| //if not - append it | |
| UINT path1Length = String::StrLen(path1); | |
| UINT path2Length = String::StrLen(path2); | |
| bool appendPathSeparator = ( *PATH_SEPARATOR != *(path1 + path1Length - 1)); | |
| STRING result = (STRING)System::SafeAllocMemory(path1Length + path2Length + 2); | |
| if ( 0 == result ) | |
| return 0; | |
| String::StrCpy(result, path1); | |
| if ( appendPathSeparator ) | |
| { | |
| String::StrCat(result,PATH_SEPARATOR); | |
| } | |
| STRING tmpPath2 = String::StrDup(path2); | |
| //replace other system's path separators with current | |
| #ifdef WINDOWS | |
| Replace(tmpPath2,*PATH_SEPARATOR_NIX_A, *PATH_SEPARATOR); | |
| #else | |
| Replace(tmpPath2, *PATH_SEPARATOR_WIN_A, *PATH_SEPARATOR); | |
| #endif | |
| String::StrCat(result, tmpPath2); | |
| System::SafeFreeMemory(tmpPath2); | |
| return result; | |
| } | |
| STRING Path::GetFileFolder(STRING filePath) | |
| { | |
| UINT len = String::StrLen(filePath); | |
| while( len-- > 1) | |
| if ( *PATH_SEPARATOR == *(filePath + len - 1) ) | |
| break; | |
| if ( 0 != len ) | |
| { | |
| STRING result = (STRING)System::SafeAllocMemory(len + 1); | |
| String::StrNCpy(result, filePath, len); | |
| return result; | |
| } | |
| else | |
| return 0; | |
| } | |
| bool Path::IsDirectory(STRING path) | |
| { | |
| if ( 0 == path) | |
| return 0; | |
| UINT len = String::StrLen(path); | |
| return (*PATH_SEPARATOR == *(path + len - 1) ); | |
| } | |
| STRING Path::GetFileDirectory(STRING filePath){ | |
| if ( 0 == filePath ) | |
| return 0; | |
| if ( IsDirectory(filePath) ) | |
| return String::StrDup(filePath); | |
| else { | |
| UINT lastDelimiterIndx = String::LastIndexOf(PATH_SEPARATOR, filePath); | |
| if ( lastDelimiterIndx == -1) | |
| return 0; | |
| STRING directory = (STRING)System::SafeAllocMemory(lastDelimiterIndx + 2 ); //+1 for ending 0, +1 for path_separator | |
| String::StrNCpy(directory,filePath, lastDelimiterIndx + 1); | |
| return directory; | |
| } | |
| } |