📄 filesystem_traits.hpp
字号:
L'\0' == dir[3])
{
return dir;
}
if( L'\\' == dir[0] &&
L'\\' == dir[1] &&
L'\0' == dir[3])
{
return dir;
}
if( dir < end &&
is_path_name_separator(*(end - 1)))
{
*(end - 1) = L'\0';
}
return dir;
}
static bool_type has_dir_end(char_type const *dir)
{
WINSTL_ASSERT(NULL != dir);
size_type len = str_len(dir);
return (0 < len) && is_path_name_separator(dir[len - 1]);
}
static bool_type is_dots(char_type const *dir)
{
WINSTL_ASSERT(NULL != dir);
return dir[0] == '.' &&
( dir[1] == L'\0' ||
( dir[1] == L'.' &&
dir[2] == L'\0'));
}
static bool_type is_path_rooted(char_type const *path)
{
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, size_type cchBuffer, char_type *buffer)
{
char_type *pFile;
return get_full_path_name(fileName, cchBuffer, buffer, &pFile);
}
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
// Modules
static size_type get_module_filename(HINSTANCE hModule, char_type *buffer, size_type cchBuffer)
{
return ::GetModuleFileNameW(hModule, buffer, cchBuffer);
}
static size_type get_system_directory(char_type *buffer, size_type cchBuffer)
{
return ::GetSystemDirectoryW(buffer, cchBuffer);
}
static size_type get_windows_directory(char_type *buffer, size_type cchBuffer)
{
return ::GetWindowsDirectoryW(buffer, cchBuffer);
}
// Dynamic Loading
static module_type load_library(char_type const *name)
{
return ::LoadLibraryW(name);
}
static bool_type free_library(module_type hModule)
{
return filesystem_traits_::free_library(hModule);
}
static FARPROC find_symbol(module_type hModule, char const *symbolName)
{
return filesystem_traits_::find_symbol(hModule, symbolName);
}
// File-system state
static bool_type set_current_directory(char_type const *dir)
{
return ::SetCurrentDirectoryW(dir) != FALSE;
}
static size_type get_current_directory(size_type cchBuffer, char_type *buffer)
{
return ::GetCurrentDirectoryW(cchBuffer, buffer);
}
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_li
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -