| 1137 | #endif |
| 1138 | |
| 1139 | string GetAbsolutePath(const string &file) |
| 1140 | { |
| 1141 | string str = file; |
| 1142 | |
| 1143 | // If this is a relative path, complement it with the current path |
| 1144 | if( !((str.length() > 0 && (str[0] == '/' || str[0] == '\\')) || |
| 1145 | str.find(":") != string::npos) ) |
| 1146 | { |
| 1147 | str = GetCurrentDir() + "/" + str; |
| 1148 | } |
| 1149 | |
| 1150 | // Replace backslashes for forward slashes |
| 1151 | size_t pos = 0; |
| 1152 | while( (pos = str.find("\\", pos)) != string::npos ) |
| 1153 | str[pos] = '/'; |
| 1154 | |
| 1155 | // Replace /./ with / |
| 1156 | pos = 0; |
| 1157 | while( (pos = str.find("/./", pos)) != string::npos ) |
| 1158 | str.erase(pos+1, 2); |
| 1159 | |
| 1160 | // For each /../ remove the parent dir and the /../ |
| 1161 | pos = 0; |
| 1162 | while( (pos = str.find("/../")) != string::npos ) |
| 1163 | { |
| 1164 | size_t pos2 = str.rfind("/", pos-1); |
| 1165 | if( pos2 != string::npos ) |
| 1166 | str.erase(pos2, pos+3-pos2); |
| 1167 | else |
| 1168 | { |
| 1169 | // The path is invalid |
| 1170 | break; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return str; |
| 1175 | } |
| 1176 | |
| 1177 | string GetCurrentDir() |
| 1178 | { |
no test coverage detected