123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- #include <string>
- #include <type_traits>
- #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 <typename T>
- bool _CreateDir (const T & DirName, const T & strStopDirName, std::function <void (const T &)> 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 <T> m_arr; // DString Array to hold Directory Structures
- T tem; // Temporary DString Object
- for (int Index = 0; Index<dd.GetLength (); Index++) // Parse the supplied DString Directory String
- {
- if (dd.GetAt (Index) == '\\') // if the Charachter is a \
- {
- m_arr.push_back (tem); // if the Character is a \ Add the Temp String to the DString Array
- tem += _ToSV ("\\"); // Now add the \ to the temp string
- }
- else
- if (dd.GetAt (Index) == '/') // if the Charachter is a /
- {
- m_arr.push_back (tem); // if the Character is a / Add the Temp String to the DString Array
- tem += _ToSV ("/"); // Now add the \ to the temp string
- }
- else
- tem += dd.GetAt (Index); // else add character to Temp String
- if (Index == dd.GetLength () - 1) // If we reached the end of the file add the remaining string
- if ((dd [Index] != '\\') && (dd [Index] != '/'))
- m_arr.push_back (tem);
- }
- for (auto & Dir : m_arr)
- {
- if (StopDirName.GetLength ())
- if (Dir.CompareNoCase (StopDirName) < 0)
- continue;
- // CreateDirectory (Dir, nullptr);
- fun (Dir);
- }
- return true;
- }
- inline bool CreateDir (const eSTR::DString & DirName, const eSTR::DString & StopDirName)
- {
- return _CreateDir <eSTR::DString> (DirName, StopDirName, [ ] (const eSTR::DString & Dir)
- {
- CreateDirectoryA (Dir, nullptr);
- });
- }
- inline bool CreateDir (const eSTR::WString & DirName, const eSTR::WString & StopDirName)
- {
- return _CreateDir <eSTR::WString> (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;
- }
- }}}
|