├── Filename.h └── Filename.cpp /Filename.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _FILENAME_ 3 | #define _FILENAME_ 4 | 5 | #include 6 | 7 | namespace AVN 8 | { 9 | std::string getFilenameOnly(const std::string &strFullFilePath); 10 | std::string getFileDirectory(const std::string &strFullFilePath); 11 | std::string createFullFilePath(const std::string &strFileDirectory, const std::string &strFilenameOnly); 12 | } 13 | 14 | #endif //_FILENAME_ 15 | -------------------------------------------------------------------------------- /Filename.cpp: -------------------------------------------------------------------------------- 1 | #include "Filename.h" 2 | 3 | std::string AVN::getFilenameOnly(const std::string &strFullFilePath) 4 | { 5 | //try find forward slash 6 | size_t lastSlashPos = strFullFilePath.find_last_of("/"); 7 | 8 | //if it doesn't exist try to find backlash 9 | if(lastSlashPos == std::string::npos) 10 | { 11 | lastSlashPos = strFullFilePath.find_last_of("\\"); 12 | } 13 | 14 | //if there is still nothing found then filename is just input string 15 | if(lastSlashPos == std::string::npos) 16 | { 17 | return strFullFilePath; 18 | } 19 | 20 | //Otherwise take the substring and return 21 | return strFullFilePath.substr(lastSlashPos + 1); 22 | } 23 | 24 | std::string AVN::getFileDirectory(const std::string &strFullFilePath) 25 | { 26 | //try find forward slash 27 | size_t lastSlashPos = strFullFilePath.find_last_of("/"); 28 | 29 | //if it doesn't exist try to find backlash 30 | if(lastSlashPos == std::string::npos) 31 | { 32 | lastSlashPos = strFullFilePath.find_last_of("\\"); 33 | } 34 | 35 | //if there is still nothing found then filename is just input string 36 | if(lastSlashPos == std::string::npos) 37 | { 38 | return strFullFilePath; 39 | } 40 | 41 | //Otherwise take the substring and return 42 | return strFullFilePath.substr(0, lastSlashPos); 43 | } 44 | 45 | std::string AVN::createFullFilePath(const std::string &strFileDirectory, const std::string &strFilenameOnly) 46 | { 47 | //This function only checks whether the directory has a trailing slash and adds it as necessary 48 | 49 | std::string strFullFilePath = strFileDirectory; 50 | bool bForwardSlash = false; 51 | 52 | //try find forward slash 53 | size_t lastSlashPos = strFullFilePath.find_last_of("/"); 54 | 55 | //if it doesn't exist try to find backlash 56 | if(lastSlashPos == std::string::npos) 57 | { 58 | lastSlashPos = strFullFilePath.find_last_of("\\"); 59 | if(lastSlashPos == std::string::npos) 60 | { 61 | //If not slashes are found determine by OS 62 | #ifdef __unix__ 63 | bForwardSlash = true; 64 | #endif 65 | 66 | //Otherwise it is windows so leave as false as set above 67 | } 68 | //Found forward slash leave as false as set above 69 | } 70 | else 71 | { 72 | //Otherwise we found a forward slash so mark to use it 73 | bForwardSlash = true; 74 | } 75 | 76 | if(bForwardSlash) 77 | { 78 | if(strFullFilePath[strFullFilePath.length()-1] != '/') 79 | strFullFilePath.append("/"); 80 | } 81 | else 82 | { 83 | if(strFullFilePath[strFullFilePath.length()-1] != '\\') 84 | strFullFilePath.append("\\"); 85 | } 86 | strFullFilePath.append(strFilenameOnly); 87 | 88 | return strFullFilePath; 89 | } 90 | --------------------------------------------------------------------------------