📄 filesystem_traits.hpp
字号:
n = r + 1;
}
}
}
}
}
else
{
n = get_full_path_name_impl(fileName, len, buffer, cchBuffer, ppFile);
}
// Paths that exceed _MAX_PATH cause GetFullPathNameA() to fail, but it
// does not (appear to) call SetLastError()
if( 0 == n &&
0 == ::GetLastError() &&
str_len(fileName) > _MAX_PATH)
{
::SetLastError(ERROR_FILENAME_EXCED_RANGE);
}
return n;
}
#endif /* compiler */
static size_type get_full_path_name(char_type const* fileName, char_type* buffer, size_type cchBuffer)
{
WINSTL_ASSERT(NULL != fileName);
char_type *pFile;
if('\0' == *fileName)
{
static const char s_dot[2] = { '.', '\0' };
fileName = s_dot;
}
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 ::GetShortPathNameA(fileName, buffer, cchBuffer);
}
static size_type get_short_path_name(char_type const* fileName, char_type* buffer, size_type cchBuffer)
{
return ::GetShortPathNameA(fileName, buffer, cchBuffer);
}
// File-system enumeration
static HANDLE find_first_file(char_type const* spec, find_data_type *findData)
{
return ::FindFirstFileA(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 ::FindFirstFileExA(spec, FindExInfoStandard, findData, flags, NULL, 0);
}
#endif /* _WIN32_WINNT >= 0x0400 */
static bool_type find_next_file(HANDLE h, find_data_type *findData)
{
return ::FindNextFileA(h, findData) != FALSE;
}
static void find_file_close(HANDLE h)
{
WINSTL_ASSERT(INVALID_HANDLE_VALUE != h);
::FindClose(h);
}
#ifndef _WINSTL_NO_FINDVOLUME_API
static HANDLE find_first_volume(char_type *volume_name, size_type cch_volume_name)
{
return ::FindFirstVolumeA(volume_name, cch_volume_name);
}
static bool_type find_next_volume(HANDLE h, char_type *volume_name, size_type cch_volume_name)
{
return ::FindNextVolumeA(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 ::SetCurrentDirectoryA(dir) != FALSE;
}
static size_type get_current_directory(char_type *buffer, size_type cchBuffer)
{
return ::GetCurrentDirectoryA(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 != ::GetFileAttributesA(fileName);
}
static bool_type is_file(char_type const* path)
{
DWORD attr = ::GetFileAttributesA(path);
return 0xFFFFFFFF != attr && 0 == (attr & FILE_ATTRIBUTE_DIRECTORY);
}
static bool_type is_directory(char_type const* path)
{
DWORD attr = ::GetFileAttributesA(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 = ::GetFileAttributesA(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] = '\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(::IsCharAlphaA(driveLetter));
const DWORD drivesBitmap = ::GetLogicalDrives();
const int driveOrdinal = (driveLetter - (::IsCharUpperA(driveLetter) ? 'A' : 'a'));
return 0 != ((0x01 << driveOrdinal) & drivesBitmap);
}
static DWORD get_drive_type(char_type driveLetter)
{
WINSTL_ASSERT(::IsCharAlphaA(driveLetter));
char_type drive[] = { '?', ':', '\\', '\0' };
return (drive[0] = driveLetter, ::GetDriveTypeA(drive));
}
static bool_type create_directory(char_type const* dir)
{
return FALSE != ::CreateDirectoryA(dir, NULL);
}
static bool_type create_directory(char_type const* dir, LPSECURITY_ATTRIBUTES lpsa)
{
return FALSE != ::CreateDirectoryA(dir, lpsa);
}
static bool_type remove_directory(char_type const* dir)
{
return FALSE != ::RemoveDirectoryA(dir);
}
static bool_type unlink_file(char_type const* file)
{
return FALSE != ::DeleteFileA(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 != ::MoveFileA(currentName, newName);
}
static bool_type copy_file(char_type const* sourceName, char_type const* newName, bool_type bFailIfExists = false)
{
return FALSE != ::CopyFileA(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 ::CreateFileA(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 */
};
STLSOFT_TEMPLATE_SPECIALISATION
struct filesystem_traits<ws_char_w_t>
: public system_traits<ws_char_w_t>
{
public:
typedef ws_char_w_t char_type;
typedef ws_size_t size_type;
typedef ws_ptrdiff_t difference_type;
typedef WIN32_FIND_DATAW find_data_type;
typedef WIN32_FIND_DATAW stat_data_type;
typedef BY_HANDLE_FILE_INFORMATION fstat_data_type;
typedef filesystem_traits<char_type> class_type;
typedef ws_int_t int_type;
typedef ws_bool_t bool_type;
typedef HANDLE file_handle_type;
typedef HINSTANCE module_type;
typedef DWORD error_type;
private:
#if !defined(STLSOFT_COMPILER_IS_MSVC) || \
_MSC_VER >= 1200
typedef stlsoft_ns_qual(auto_buffer)<char_type> buffer_type_;
#endif /* compiler */
public:
enum
{
maxPathLength = 1 + CONST_NT_MAX_PATH //!< The maximum length of a path for the current file system
};
public:
// File-system entry names
static char_type *ensure_dir_end(char_type *dir)
{
WINSTL_ASSERT(NULL != dir);
char_type *end = str_end(dir);
if( dir < end &&
!is_path_name_separator(*(end - 1)))
{
*end = path_name_separator();
*(end + 1) = L'\0';
}
return dir;
}
static char_type *remove_dir_end(char_type *dir)
{
WINSTL_ASSERT(NULL != dir);
// Don't trim drive roots ...
if( isalpha(dir[0]) &&
L':' == dir[1] &&
is_path_name_separator(dir[2]) &&
L'\0' == dir[3])
{
return dir;
}
// ... or UNC roots
if( L'\\' == dir[0] &&
L'\\' == dir[1] &&
L'\0' == dir[3])
{
return dir;
}
char_type *end = str_end(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)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -