|
5 | 5 | #include "filesystemfunc.h" |
6 | 6 |
|
7 | 7 | #include <direct.h> //for _getcwd() |
| 8 | +#include <algorithm> //for std::transform() |
8 | 9 |
|
9 | 10 | #include <sys/types.h> |
10 | 11 | #include <sys/stat.h> |
|
13 | 14 | #endif |
14 | 15 | #ifdef WIN32 |
15 | 16 | #define stat _stat |
| 17 | +#include <Windows.h> //for GetShortPathName() |
16 | 18 | #endif |
17 | 19 |
|
18 | 20 | namespace filesystem |
@@ -106,6 +108,74 @@ namespace filesystem |
106 | 108 | return parent; |
107 | 109 | } |
108 | 110 |
|
| 111 | + std::string getShortPathForm(const std::string & iPath) |
| 112 | + { |
| 113 | + //ASSERT_TRUE(getShortPathForm("a b c.txt") == "ABC~1.TXT"); |
| 114 | + //ASSERT_TRUE(getShortPathForm("abcdefgh.text") == "ABCDEF~1.TEX"); |
| 115 | + //ASSERT_TRUE(getShortPathForm("abcdefghijklmnopqrstuvwxyz.txt") == "ABCDEF~1.TXT"); |
| 116 | + //ASSERT_TRUE(getShortPathForm("abcde.t x t") == "ABCDE~1.TXT"); |
| 117 | + //ASSERT_TRUE(getShortPathForm("Program Files (x86)") == "PROGRA~1"); |
| 118 | + |
| 119 | + std::string shortPath; |
| 120 | + #ifdef _WIN32 |
| 121 | + // First obtain the size needed by passing NULL and 0. |
| 122 | + long length = GetShortPathName(iPath.c_str(), NULL, 0); |
| 123 | + if (length == 0) |
| 124 | + return ""; |
| 125 | + |
| 126 | + // Dynamically allocate the correct size |
| 127 | + // (terminating null char was included in length) |
| 128 | + char * buffer = new char[length]; |
| 129 | + |
| 130 | + // Now simply call again using same long path. |
| 131 | + length = GetShortPathName(iPath.c_str(), buffer, length); |
| 132 | + if (length == 0) |
| 133 | + return ""; |
| 134 | + |
| 135 | + shortPath = buffer; |
| 136 | + |
| 137 | + delete [] buffer; |
| 138 | + #else |
| 139 | + std::vector<std::string> pathElements; |
| 140 | + splitPath(iPath, pathElements); |
| 141 | + for(size_t i=0; i<pathElements.size(); i++) |
| 142 | + { |
| 143 | + const std::string & element = pathElements[i]; |
| 144 | + if (element.size() > 12 || element.find(' ') != std::string::npos) |
| 145 | + { |
| 146 | + std::string element83 = element; |
| 147 | + std::string ext = getFileExtention(element); |
| 148 | + stringfunc::strReplace(element83, (std::string(".")+ext).c_str(), ""); //remove extension from filename |
| 149 | + stringfunc::strReplace(ext, " ", ""); //remove spaces in extension |
| 150 | + ext = ext.substr(0, 3); //truncate file extension |
| 151 | + stringfunc::strReplace(element83, " ", ""); //remove spaces |
| 152 | + element83 = element83.substr(0, 6); //truncate file name |
| 153 | + element83.append("~1"); |
| 154 | + if (!ext.empty()) |
| 155 | + { |
| 156 | + element83.append("."); |
| 157 | + element83.append(ext); |
| 158 | + } |
| 159 | + |
| 160 | + //uppercase everything |
| 161 | + std::transform(element83.begin(), element83.end(), element83.begin(), ::toupper); |
| 162 | + |
| 163 | + //add to shortPath |
| 164 | + if (!shortPath.empty()) |
| 165 | + shortPath.append("\\"); |
| 166 | + shortPath.append(element83); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + if (!shortPath.empty()) |
| 171 | + shortPath.append("\\"); |
| 172 | + shortPath.append(element); |
| 173 | + } |
| 174 | + } |
| 175 | + #endif |
| 176 | + return shortPath; |
| 177 | + } |
| 178 | + |
109 | 179 | void splitPath(const std::string & iPath, std::string & oFolder, std::string & oFilename) |
110 | 180 | { |
111 | 181 | oFolder = ""; |
|
0 commit comments