#include "FileSystemUtilities.h" #include #include #include #ifdef __MINGW32__ int wcsncpy_s(wchar_t* dest, size_t nof, const wchar_t *src, size_t count ) { if (!dest || !src) return EINVAL; if (!nof) return ERANGE; dest[0] = 0; size_t size = 0; for (; ( (size+1) < count) && src[size]; ++size) ; if (size >= nof) return ERANGE; memcpy( dest, src, size*sizeof(WCHAR) ); dest[size] = '\0'; return 0; } #endif bool createDirectoryComponent(const std::string& Name, std::string::size_type& idx) { std::string::size_type next = Name.find('\\',idx); std::string Path(Name,0,next==std::string::npos?Name.length():next); idx = next; TCHAR TPath[10241]; int x = MultiByteToWideChar(CP_ACP,0,Path.c_str(),Path.length(),TPath,10240); TPath[x] = 0; if (CreateDirectory(TPath,NULL)) return true; if (GetLastError() == ERROR_ALREADY_EXISTS) return true; return false; } bool createDirectory(const std::string& Name) { std::string::size_type idx = 0; while (idx != std::string::npos) { ++idx; if (!createDirectoryComponent(Name,idx)) return false; } return true; } bool createDirectoryForFile(const std::string& Name) { std::string::size_type idx = Name.rfind('\\'); if (idx == std::string::npos) return true; return createDirectory(Name.substr(0,idx)); } bool fileExists(const std::string& Name) { WIN32_FILE_ATTRIBUTE_DATA Info; TCHAR TPath[10241]; int x = MultiByteToWideChar(CP_ACP,0,Name.c_str(),Name.length(),TPath,10240); TPath[x] = 0; if (!GetFileAttributesEx(TPath,GetFileExInfoStandard, &Info)) return false; if (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return false; return true; } bool fileIsNewer(const std::string& Name1, const std::string& Name2) { WIN32_FILE_ATTRIBUTE_DATA Info1,Info2; TCHAR TPath[10241]; int x = MultiByteToWideChar(CP_ACP,0,Name1.c_str(),Name1.length(),TPath,10240); TPath[x] = 0; if (!GetFileAttributesEx(TPath,GetFileExInfoStandard, &Info1)) return false; if (Info1.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return false; x = MultiByteToWideChar(CP_ACP,0,Name2.c_str(),Name2.length(),TPath,10240); TPath[x] = 0; if (!GetFileAttributesEx(TPath,GetFileExInfoStandard, &Info2)) return true; if (Info2.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return true; if (Info1.ftLastWriteTime.dwHighDateTime == Info2.ftLastWriteTime.dwHighDateTime) return Info1.ftLastWriteTime.dwLowDateTime > Info2.ftLastWriteTime.dwLowDateTime; return Info1.ftLastWriteTime.dwHighDateTime > Info2.ftLastWriteTime.dwHighDateTime; } std::string pathAppend(const std::string& A, const std::string& B) { if (!A.length()) return B; if (!B.length()) return A; return A+"\\"+B; } std::string pathOf(const std::string& A) { std::string::size_type idx = A.find_last_of('\\'); if (idx == std::string::npos) return "."; return A.substr(0,idx); } std::string testExecutableInDirectory(const std::string& ProcessName, TCHAR* Dir) { TCHAR TPath[10241]; wcsncpy_s(TPath, 10240, Dir, 10241); unsigned int Idx; for (Idx = 0; TPath[Idx]; ++Idx) ; TPath[Idx] = '\\'; int x = MultiByteToWideChar(CP_ACP,0,ProcessName.c_str(),ProcessName.length(),TPath+Idx+1,10239-Idx); TPath[Idx+1+x] = 0; WIN32_FILE_ATTRIBUTE_DATA Info; if (!GetFileAttributesEx(TPath,GetFileExInfoStandard, &Info)) return ""; if (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return ""; char buffer[10240]; x = WideCharToMultiByte(CP_ACP,0,TPath,-1,buffer,10240,NULL,NULL); buffer[x] = 0; return std::string(buffer,x-1); } std::string findExecutableInPath(const std::string& ProcessName) { TCHAR Path[10241]; if (!GetEnvironmentVariable(_T("Path"),Path,10240)) return ""; int StartIdx = 0; for (unsigned int i=0; Path[i]; ++i) { if (Path[i] == ';') { Path[i] = 0; std::string Res = testExecutableInDirectory(ProcessName, Path+StartIdx); if (Res.length()) return Res; StartIdx = i+1; } } return testExecutableInDirectory(ProcessName,Path+StartIdx); } std::string eolTextSequence() { return std::string("\r\n"); } std::string toNativePath(const std::string& A) { std::string Result; for (unsigned int i=0; i