📄 filesystem_traits.hpp
字号:
WINSTL_ASSERT(NULL != path);
return is_path_name_separator(*path) || is_path_absolute(path);
}
static bool_type is_path_absolute(char_type const* path)
{
WINSTL_ASSERT(NULL != path);
size_type len = str_len(path);
return is_path_UNC(path) ||
( (2 < len) &&
(L':' == path[1]) &&
is_path_name_separator(path[2]));
}
static bool_type is_path_UNC(char_type const* path)
{
WINSTL_ASSERT(NULL != path);
return (L'\\' == path[0] && L'\\' == path[1]);
}
private:
static bool_type is_root_drive_(char_type const* path)
{
if( isalpha(path[0]) &&
L':' == path[1] &&
is_path_name_separator(path[2]) &&
L'\0' == path[3])
{
return true;
}
return false;
}
static bool_type is_root_UNC_(char_type const* path)
{
if(is_path_UNC(path))
{
char_type const* sep = str_pbrk(path + 2, L"\\/");
if( NULL == sep ||
L'\0' == sep[1])
{
return true;
}
}
return false;
}
static bool_type is_root_directory_(char_type const* path)
{
if( is_path_name_separator(path[0]) &&
L'\0' == path[1])
{
return true;
}
return false;
}
public:
static bool_type is_root_designator(char_type const* path)
{
WINSTL_ASSERT(NULL != path);
return is_root_directory_(path) || is_root_drive_(path) || is_root_UNC_(path);
}
static bool_type is_path_name_separator(char_type ch)
{
return L'\\' == ch || L'/' == ch;
}
static char_type path_separator()
{
return L';';
}
static char_type path_name_separator()
{
return L'\\';
}
static char_type const* pattern_all()
{
return L"*.*";
}
static size_type path_max()
{
return (::GetVersion() & 0x80000000) ? (1 + _MAX_PATH) : (1 + CONST_NT_MAX_PATH);
}
static size_type get_full_path_name(char_type const* fileName, size_type cchBuffer, char_type* buffer, char_type **ppFile)
{
WINSTL_MESSAGE_ASSERT("GetFullPathNameW() will crash when the file-name and buffer parameters are the same", fileName != buffer);
return ::GetFullPathNameW(fileName, cchBuffer, buffer, ppFile);
}
static size_type get_full_path_name(char_type const* fileName, char_type* buffer, size_type cchBuffer)
{
char_type *pFile;
return get_full_path_name(fileName, cchBuffer, buffer, &pFile);
}
static size_type get_full_path_name(char_type const* fileName, size_type cchBuffer, char_type* buffer)
{
return get_full_path_name(fileName, buffer, cchBuffer);
}
static size_type get_short_path_name(char_type const* fileName, size_type cchBuffer, char_type* buffer)
{
return ::GetShortPathNameW(fileName, buffer, cchBuffer);
}
static size_type get_short_path_name(char_type const* fileName, char_type* buffer, size_type cchBuffer)
{
return ::GetShortPathNameW(fileName, buffer, cchBuffer);
}
// FindFile() API
static HANDLE find_first_file(char_type const* spec, find_data_type *findData)
{
return ::FindFirstFileW(spec, findData);
}
#if defined(_WIN32_WINNT) && \
_WIN32_WINNT >= 0x0400
static HANDLE find_first_file_ex(char_type const* spec, FINDEX_SEARCH_OPS flags, find_data_type *findData)
{
return ::FindFirstFileExW(spec, FindExInfoStandard, findData, flags, NULL, 0);
}
#endif /* _WIN32_WINNT >= 0x0400 */
static bool_type find_next_file(HANDLE h, find_data_type *findData)
{
return ::FindNextFileW(h, findData) != FALSE;
}
static void find_file_close(HANDLE h)
{
WINSTL_ASSERT(INVALID_HANDLE_VALUE != h);
::FindClose(h);
}
// FindVolume() API
#ifndef _WINSTL_NO_FINDVOLUME_API
static HANDLE find_first_volume(char_type *volume_name, size_type cch_volume_name)
{
return ::FindFirstVolumeW(volume_name, cch_volume_name);
}
static bool_type find_next_volume(HANDLE h, char_type *volume_name, size_type cch_volume_name)
{
return ::FindNextVolumeW(h, volume_name, cch_volume_name) != FALSE;
}
static void find_volume_close(HANDLE h)
{
WINSTL_ASSERT(INVALID_HANDLE_VALUE != h);
::FindVolumeClose(h);
}
#endif // !_WINSTL_NO_FINDVOLUME_API
// File-system state
static bool_type set_current_directory(char_type const* dir)
{
return ::SetCurrentDirectoryW(dir) != FALSE;
}
static size_type get_current_directory(char_type *buffer, size_type cchBuffer)
{
return ::GetCurrentDirectoryW(cchBuffer, buffer);
}
static size_type get_current_directory(size_type cchBuffer, char_type* buffer)
{
return class_type::get_current_directory(buffer, cchBuffer);
}
static bool_type file_exists(char_type const* fileName)
{
return 0xFFFFFFFF != ::GetFileAttributesW(fileName);
}
static bool_type is_file(char_type const* path)
{
DWORD attr = ::GetFileAttributesW(path);
return 0xFFFFFFFF != attr && 0 == (attr & FILE_ATTRIBUTE_DIRECTORY);
}
static bool_type is_directory(char_type const* path)
{
DWORD attr = ::GetFileAttributesW(path);
return 0xFFFFFFFF != attr && 0 != (attr & FILE_ATTRIBUTE_DIRECTORY);
}
private:
static bool_type stat_direct_(char_type const* path, stat_data_type *stat_data)
{
WINSTL_ASSERT(NULL != path);
WINSTL_ASSERT(NULL != stat_data);
if(is_root_drive_(path))
{
stat_data->dwFileAttributes = ::GetFileAttributesW(path);
stat_data->ftCreationTime.dwLowDateTime = 0;
stat_data->ftCreationTime.dwHighDateTime = 0;
stat_data->ftLastAccessTime.dwLowDateTime = 0;
stat_data->ftLastAccessTime.dwHighDateTime = 0;
stat_data->ftLastWriteTime.dwLowDateTime = 0;
stat_data->ftLastWriteTime.dwHighDateTime = 0;
stat_data->nFileSizeHigh = 0;
stat_data->nFileSizeLow = 0;
{ for(ws_size_t i = 0; i < 4; ++i)
{
stat_data->cFileName[i] = path[i];
stat_data->cAlternateFileName[i] = path[i];
}}
return 0xFFFFFFFF != stat_data->dwFileAttributes;
}
HANDLE h = find_first_file(path, stat_data);
return (INVALID_HANDLE_VALUE == h) ? false : (find_file_close(h), true);
}
public:
static bool_type stat(char_type const* path, stat_data_type *stat_data)
{
WINSTL_ASSERT(NULL != path);
WINSTL_ASSERT(NULL != stat_data);
#if !defined(STLSOFT_COMPILER_IS_MSVC) || \
_MSC_VER >= 1200
size_type len = class_type::str_len(path);
bool_type bTryEndTest = false;
if(is_path_absolute(path))
{
if(len > 4)
{
bTryEndTest = true;
}
}
else if(is_path_rooted(path))
{
if(len > 3)
{
bTryEndTest = true;
}
}
else if(len > 2)
{
bTryEndTest = true;
}
if( bTryEndTest &&
class_type::has_dir_end(path + len - 2))
{
typedef stlsoft_ns_qual(auto_buffer)<char_type> buffer_t;
WINSTL_ASSERT(len > 2);
buffer_t buffer(1 + len);
if(0 == buffer.size())
{
return false;
}
else
{
WINSTL_ASSERT(len > 0);
class_type::str_copy(&buffer[0], path);
buffer[len - 1] = L'\0';
return class_type::stat_direct_(buffer.data(), stat_data);
}
}
else
#endif /* compiler */
{
return stat_direct_(path, stat_data);
}
}
static bool_type fstat(file_handle_type fd, fstat_data_type *fstat_data)
{
return filesystem_traits_::fstat(fd, fstat_data);
}
static bool_type is_file(stat_data_type const* stat_data)
{
return FILE_ATTRIBUTE_DIRECTORY != (stat_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
}
static bool_type is_directory(stat_data_type const* stat_data)
{
return FILE_ATTRIBUTE_DIRECTORY == (stat_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
}
static bool_type is_link(stat_data_type const* /* stat_data */)
{
return false;
}
static bool_type is_readonly(stat_data_type const* stat_data)
{
return FILE_ATTRIBUTE_READONLY == (stat_data->dwFileAttributes & FILE_ATTRIBUTE_READONLY);
}
static bool_type is_file(fstat_data_type const* stat_data)
{
return filesystem_traits_::is_file(stat_data);
}
static bool_type is_directory(fstat_data_type const* stat_data)
{
return filesystem_traits_::is_directory(stat_data);
}
static bool_type is_link(fstat_data_type const* stat_data)
{
return filesystem_traits_::is_link(stat_data);
}
static bool_type is_readonly(fstat_data_type const* stat_data)
{
return filesystem_traits_::is_readonly(stat_data);
}
static bool_type drive_exists(char_type driveLetter)
{
WINSTL_ASSERT(IsCharAlphaW(driveLetter));
const DWORD drivesBitmap = ::GetLogicalDrives();
const int driveOrdinal = (driveLetter - (IsCharUpperW(driveLetter) ? L'A' : L'a'));
return 0 != ((0x01 << driveOrdinal) & drivesBitmap);
}
static DWORD get_drive_type(char_type driveLetter)
{
WINSTL_ASSERT(IsCharAlphaW(driveLetter));
char_type drive[] = { L'?', L':', L'\\', L'\0' };
return (drive[0] = driveLetter, ::GetDriveTypeW(drive));
}
// File-system control
static bool_type create_directory(char_type const* dir)
{
return FALSE != ::CreateDirectoryW(dir, NULL);
}
static bool_type create_directory(char_type const* dir, LPSECURITY_ATTRIBUTES lpsa)
{
return FALSE != ::CreateDirectoryW(dir, lpsa);
}
static bool_type remove_directory(char_type const* dir)
{
return FALSE != ::RemoveDirectoryW(dir);
}
static bool_type unlink_file(char_type const* file)
{
return FALSE != ::DeleteFileW(file);
}
static bool_type delete_file(char_type const* file)
{
return unlink_file(file);
}
static bool_type rename_file(char_type const* currentName, char_type const* newName)
{
return FALSE != ::MoveFileW(currentName, newName);
}
static bool_type copy_file(char_type const* sourceName, char_type const* newName, bool_type bFailIfExists = false)
{
return FALSE != ::CopyFileW(sourceName, newName, bFailIfExists);
}
static file_handle_type create_file(char_type const* fileName, size_type desiredAccess, size_type shareMode, LPSECURITY_ATTRIBUTES sa, size_type creationDisposition, size_type flagAndAttributes, file_handle_type hTemplateFile)
{
return ::CreateFileW(fileName, desiredAccess, shareMode, sa, creationDisposition, flagAndAttributes, hTemplateFile);
}
static bool_type close_file(file_handle_type h)
{
return filesystem_traits_::close_handle(h);
}
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
static ws_uint64_t get_file_size(file_handle_type h)
{
return filesystem_traits_::get_file_size(h);
}
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
};
#endif /* STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Unit-testing
*/
#ifdef STLSOFT_UNITTEST
# include "./unittest/filesystem_traits_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -