#include #include #include "String.WString.hpp" #include "WStringArray.hpp" #include "String.StringView.hpp" #pragma once //namespace eFile = ECOM::Utility::File; namespace ECOM { namespace Utility { namespace File { // using PCXSTR = const char *; // using PCWSTR = const wchar_t *; using PCXSTR = CV_String &; using PCWSTR = CV_WString &; bool IsExist (PCXSTR FileName); bool IsExist (PCWSTR FileName); size_t Length (PCXSTR FileName); size_t Length (PCWSTR FileName); bool Delete (PCXSTR FileName); bool Delete (PCWSTR FileName); bool ForceDelete (PCXSTR FileName); bool ForceDelete (PCWSTR FileName); bool Rename (PCXSTR fromFileName, PCXSTR toFileName); bool Rename (PCWSTR fromFileName, PCWSTR toFileName); bool CreateDir (PCXSTR DirName); bool CreateDir (PCWSTR DirName); // 返回目录名 eSTR::DString GetParent (PCXSTR FileName); eSTR::WString GetParent (PCWSTR FileName); } } } namespace ECOM { namespace Utility { namespace File { inline bool IsExist (PCXSTR FileName) { WIN32_FIND_DATAA FindData; lstrcpyA (FindData.cFileName, FileName); auto hContext = ::FindFirstFileA (FileName, &FindData); if (hContext == INVALID_HANDLE_VALUE) return false; ::FindClose (hContext); return true; } inline bool IsExist (PCWSTR FileName) { WIN32_FIND_DATAW FindData; wcsncpy_s (FindData.cFileName, FileName, sizeof (FindData.cFileName) / sizeof (wchar_t)); HANDLE hContext = ::FindFirstFileW (FileName, &FindData); if (hContext == INVALID_HANDLE_VALUE) return false; ::FindClose (hContext); return true; } inline bool Delete (PCXSTR FileName) { auto rc = ::DeleteFileA (FileName); return (rc); } inline bool Delete (PCWSTR FileName) { auto rc = ::DeleteFileW (FileName); return (rc); } inline bool ForceDelete (PCXSTR FileName) { ::SetFileAttributesA (FileName, FILE_ATTRIBUTE_NORMAL); auto rc = ::DeleteFileA (FileName); return (rc); } inline bool ForceDelete (PCWSTR FileName) { ::SetFileAttributesW (FileName, FILE_ATTRIBUTE_NORMAL); auto rc = ::DeleteFileW (FileName); return (rc); } // 如果 toFileName 有子目录, 应该让调用者来创建子目录 inline bool Rename (PCXSTR fromFileName, PCXSTR toFileName) { // auto DirName = GetParent (toFileName); // CreateDir (DirName); auto rc = ::MoveFileA (fromFileName, toFileName); return (rc); } inline bool Rename (PCWSTR fromFileName, PCWSTR toFileName) { // auto DirName = GetParent (toFileName); // CreateDir (DirName); auto rc = ::MoveFileW (fromFileName, toFileName); return (rc); } inline size_t Length (PCXSTR FileName) { auto H = CreateFileA (FileName, READ_CONTROL, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); if (H == INVALID_HANDLE_VALUE) return 0; LARGE_INTEGER large; auto rc = GetFileSizeEx (H, &large); CloseHandle (H); return large.QuadPart; } inline size_t Length (PCWSTR FileName) { auto H = CreateFileW (FileName, READ_CONTROL, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); if (H == INVALID_HANDLE_VALUE) return 0; LARGE_INTEGER large; auto rc = GetFileSizeEx (H, &large); CloseHandle (H); return large.QuadPart; } inline constexpr eSTR::DStringView _ToSV (const char * S) { return eSTR::DStringView (S); } inline constexpr eSTR::WStringView _ToSV (const wchar_t * S) { return eSTR::WStringView (S); } template bool _CreateDir (const T & DirName, const T & strStopDirName, std::function fun) { /// 删除 StopDirName 尾部的反斜杠 T StopDirName (strStopDirName); int len = StopDirName.GetLength (); bool bFound = (StopDirName.GetAt (len - 1) == '\\'); if (bFound) { int At = len - 1; for (;; At--) if (StopDirName.GetAt (At) != '\\') break; StopDirName = StopDirName.Left (At+1); } /// T dd (DirName); std::vector m_arr; // DString Array to hold Directory Structures T tem; // Temporary DString Object for (int Index = 0; Index (DirName, StopDirName, [ ] (const eSTR::DString & Dir) { CreateDirectoryA (Dir, nullptr); }); } inline bool CreateDir (const eSTR::WString & DirName, const eSTR::WString & StopDirName) { return _CreateDir (DirName, StopDirName, [ ] (const eSTR::WString & Dir) { CreateDirectoryW (Dir, nullptr); }); } inline bool CreateDir (const eSTR::DString & DirName) { return CreateDir (DirName, eSTR::DString ()); } inline bool CreateDir (const eSTR::WString & DirName) { return CreateDir (DirName, eSTR::WString ()); } inline bool CreateDir (PCXSTR DirName) { return CreateDir (eSTR::DString{ DirName }, eSTR::DString ()); } inline bool CreateDir (PCWSTR DirName) { return CreateDir (eSTR::WString{ DirName }, eSTR::WString ()); } inline eSTR::DString GetParent (PCXSTR FileName) { eSTR::DString path (FileName); auto pos = path.ReverseFind ('\\'); eSTR::DString parent; if (pos > 0) parent = path.Mid (0, pos); return parent; } inline eSTR::WString GetParent (PCWSTR FileName) { eSTR::WString path (FileName); auto pos = path.ReverseFind ('\\'); eSTR::WString parent; if (pos > 0) parent = path.Mid (0, pos); return parent; } }}}