// converts a NT Path to a more familiar (and user mode friendly) path // we can pass to createfile std::wstring NTPathToDosPath(LPCWSTR ntPath) { std::wstring dosPath; static const WCHAR backSlash = L'\\'; dosPath.reserve(MAX_PATH); // if it's a relative path // add on the path to the drivers directory if(PathIsRelativeW(ntPath)) { static const WCHAR driverString[] = L"drivers/"; WCHAR buf[MAX_PATH] = {0}; GetSystemDirectory(buf, MAX_PATH - (ARRAYSIZE(driverString) + 1)); PathAppend(buf, driverString); dosPath = buf; dosPath += ntPath; } else { // otherwise check for various prefixes static const WCHAR* sysRoot = L"\\systemroot\\"; const std::wstring& windir = GetWindowsDirectoryString(); // find the first slash on the windir path const WCHAR* startOfWindirPath = PathSkipRoot(windir.c_str()) - 1; static const WCHAR* dosDevSymLink = L"\\??\\"; // lowercase the input string std::transform(ntPath, ntPath + wcslen(ntPath) + 1, std::back_inserter(dosPath), std::ptr_fun(towlower)); std::wstring::size_type pos = 0; // if the prefix is systemroot if((pos = dosPath.find(sysRoot)) != std::wstring::npos) { // replace the first and last slashes with percent signs dosPath[0] = dosPath[11] = L'%'; // add in a backslash after the last percent dosPath.insert(12, 1, backSlash); // expand the systemroot environment string std::vector<WCHAR> temp(ExpandEnvironmentStrings(dosPath.c_str(), NULL, 0)); ExpandEnvironmentStrings(dosPath.c_str(), &temp[0], temp.size()); temp.pop_back(); // remove the terminating NULL dosPath.assign(temp.begin(), temp.end()); } // if the prefix is the dos device symlink, just remove it else if((pos = dosPath.find(dosDevSymLink)) != std::wstring::npos) { dosPath.erase(0, 4); } // if the prefix is the start of the windows path // add on the drive else if((pos = dosPath.find(startOfWindirPath)) != std::wstring::npos) { // insert the drive and the colon dosPath.insert(0, windir, 0, 2); } // else leave it alone else __assume(0); } return dosPath; }
CString CFilePath::ShellGetRoot() const { LPCTSTR psPath = msPath; LPCTSTR psRootEnd = PathSkipRoot(psPath); if (psRootEnd == NULL) return CString(); return msPath.Left(psRootEnd - psPath); }
CFilePath CFilePath::GetPath(BOOL bIncludeRoot) const { LPCTSTR psPath = msPath; LPCTSTR psFileName = PathFindFileName(psPath); if (psFileName == NULL) psFileName = psPath + msPath.GetLength(); LPCTSTR psRootEnd = bIncludeRoot ? NULL : PathSkipRoot(psPath); CFilePath RetPath; if (psRootEnd == NULL) return (LPCTSTR)msPath.Left(psFileName - psPath); else return (LPCTSTR)msPath.Mid(psRootEnd - psPath, psFileName - psRootEnd); }
OEMCHAR *md_drive_parse(OEMCHAR *new_dir) { OEMCHAR *root_str; int ret = PathGetDriveNumber(new_dir); if(ret != -1) { md_drive_set(ret); } if(np2cfg.mountdir[cur_drive][0] == '\0') { return new_dir; } root_str = PathSkipRoot(new_dir); if(root_str) { // Absolute patch. Switch back to virtual root directory new_dir = root_str; SetCurrentDirectory(np2cfg.mountdir[cur_drive]); } return new_dir; }
/** * @brief Creates folder even if parent folder doesn't exist. * @param pszFolder - folder name. * @return true if operation was completed successfully. */ BOOL CreateFolder(PCTSTR pszFolder) { PCTSTR pszOldSegment = PathSkipRoot(pszFolder); _ASSERTE(pszOldSegment != NULL); if (pszOldSegment == NULL) return FALSE; TCHAR szFolderPath[MAX_PATH]; _tcsncpy_s(szFolderPath, countof(szFolderPath), pszFolder, pszOldSegment - pszFolder); while (*pszOldSegment) { PCTSTR pszSegment = PathFindNextComponent(pszOldSegment); _tcsncat_s(szFolderPath, countof(szFolderPath), pszOldSegment, pszSegment - pszOldSegment); if (! CreateDirectory(szFolderPath, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) return FALSE; pszOldSegment = pszSegment; } return TRUE; }
path without_root() const { return PathSkipRoot(this->buf); }