char *ensure_absolute_ascii_path(char *out, const char *in) { char tmpout[MAX_PATH]; char nonexistent[MAX_PATH]; char *pathcomponent; unsigned int nonexistentidx; unsigned int pathcomponentlen; unsigned int lenchars; lasterror_t lasterror; get_lasterrors(&lasterror); if (!GetFullPathNameA(in, MAX_PATH, tmpout, NULL)) goto normal_copy; lenchars = 0; nonexistentidx = MAX_PATH - 1; nonexistent[nonexistentidx] = '\0'; while (lenchars == 0) { lenchars = GetLongPathNameA(tmpout, out, MAX_PATH); if (lenchars) break; if (GetLastError() != ERROR_FILE_NOT_FOUND && GetLastError() != ERROR_PATH_NOT_FOUND && GetLastError() != ERROR_INVALID_NAME) goto normal_copy; pathcomponent = strrchr(tmpout, '\\'); if (pathcomponent == NULL) goto normal_copy; pathcomponentlen = (unsigned int)strlen(pathcomponent); nonexistentidx -= pathcomponentlen; memcpy(nonexistent + nonexistentidx, pathcomponent, pathcomponentlen * sizeof(char)); *pathcomponent = '\0'; } strncat(out, nonexistent + nonexistentidx, MAX_PATH - strlen(out)); goto out; normal_copy: strncpy(out, in, MAX_PATH); out: if (is_wow64_fs_redirection_disabled() && !strnicmp(out, system32dir_a, system32dir_len)) { memmove(out + system32dir_len + 1, out + system32dir_len, strlen(out + system32dir_len) + 1); memcpy(out, sysnativedir_a, sysnativedir_len); } out[MAX_PATH - 1] = '\0'; if (out[1] == ':' && out[2] == '\\') out[0] = toupper(out[0]); set_lasterrors(&lasterror); return out; }
std::string PathImpl::tempImpl() { char buffer[MAX_PATH]; DWORD n = GetTempPathA(sizeof(buffer), buffer); if (n > 0 && n < sizeof(buffer)) { n = GetLongPathNameA(buffer, buffer, static_cast<DWORD>(sizeof buffer)); if (n <= 0) throw SystemException("Cannot get temporary directory long path name"); std::string result(buffer, n); if (result[n - 1] != '\\') result.append("\\"); return result; } else throw SystemException("Cannot get temporary directory"); }
VDStringW VDGetLongPathA(const wchar_t *s) { const VDStringA& pathA = VDTextWToA(s); CHAR buf[MAX_PATH]; DWORD len = GetLongPathNameA(pathA.c_str(), buf, MAX_PATH); VDStringW longPath; if (!len) longPath = s; else if (len <= MAX_PATH) longPath = VDTextAToW(buf); else if (len > MAX_PATH) { vdfastvector<CHAR> extbuf(len, 0); DWORD len2 = GetLongPathNameA(pathA.c_str(), extbuf.data(), len); if (len2 && len2 <= len) longPath = VDTextAToW(extbuf.data()); else longPath = s; } return longPath; }
void goToConfigDirectory(const char* argv0) { #ifdef WIN32 char fileName[_MAX_PATH]; char longFileName[_MAX_PATH]; GetModuleFileNameA(GetModuleHandleA(0), fileName, _MAX_PATH); GetLongPathNameA(fileName, longFileName, _MAX_PATH); QString applicationPath = QString(longFileName); applicationPath = applicationPath.replace(QRegExp("Build\\\\bush\\\\\\w+\\\\\\w+\\\\bush.exe"), ""); #elif defined(MACOSX) QString applicationPath = QDir::cleanPath(*argv0 == '/' ? QString(argv0) : QDir::root().current().path() + "/" + argv0); applicationPath = applicationPath.replace(QRegExp("Build/bush/\\w+/\\w+/bush.app/Contents/MacOS/bush"), ""); #else QString applicationPath = QDir::cleanPath(*argv0 == '/' ? QString(argv0) : QDir::root().current().path() + "/" + argv0); applicationPath = applicationPath.replace(QRegExp("Build/bush/\\w+/\\w+/bush"), ""); #endif applicationPath += QString("Config"); QDir::setCurrent(applicationPath); }
int WinShortToLongPath(char *shortPath, char* longPath, int maxPath) { int result = GetLongPathNameA(shortPath, longPath, maxPath); // use ANSI version if ((result >= maxPath) || (result == 0)) return -1; // failed return 0; }