📄 filesystem_traits.hpp
字号:
return create_directory(dir, 0);
}
static bool_type create_directory(char_type const *dir, mode_type permissions)
{
#if defined(_WIN32) && \
( defined(STLSOFT_COMPILER_IS_INTEL) || \
defined(STLSOFT_COMPILER_IS_MSVC))
return 0 == ::mkdir(dir);
#else /* ? _WIN32 */
return 0 == ::mkdir(dir, permissions);
#endif /* _WIN32 */
}
static bool_type remove_directory(char_type const *dir)
{
return 0 == ::rmdir(dir);
}
static bool_type unlink_file(char_type const *file)
{
return 0 == ::remove(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 0 == ::rename(currentName, newName);
}
// File functions
static file_handle_type open_file(char_type const *fileName, int oflag, int pmode)
{
return ::open(fileName, oflag, pmode);
}
static bool_type close_file(file_handle_type fd)
{
return 0 == ::close(fd);
}
static file_handle_type open(char_type const *fileName, int oflag, int pmode)
{
return class_type::open_file(fileName, oflag, pmode);
}
static bool_type close(file_handle_type fd)
{
return class_type::close_file(fd);
}
#ifdef STLSOFT_CF_64BIT_INT_SUPPORT
static us_uint64_t get_file_size(file_handle_type fd)
{
struct stat st;
return class_type::fstat(fd, &st) ? st.st_size : 0;
}
#endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
// Error
static error_type get_last_error()
{
return errno;
}
static void set_last_error(error_type er = error_type())
{
errno = er;
}
// Environment
static size_type get_environment_variable(char_type const *name, char_type *buffer, size_type cchBuffer)
{
char const *var = ::getenv(name);
if(NULL == var)
{
return 0;
}
else
{
us_size_t var_len = ::strlen(var);
if(NULL == buffer)
{
return var_len;
}
else
{
::strncpy(buffer, var, cchBuffer);
return (var_len < cchBuffer) ? var_len : cchBuffer;
}
}
}
static size_type expand_environment_strings(char_type const *src, char_type *buffer, size_type cchBuffer);
};
#if 0
STLSOFT_TEMPLATE_SPECIALISATION
struct filesystem_traits<us_char_w_t>
{
public:
typedef us_char_w_t char_type;
typedef us_size_t size_type;
#ifdef PATH_MAX
enum
{
maxPathLength = 1 + PATH_MAX //!< The maximum length of a path for the current file system
};
#endif /* PATH_MAX */
public:
// General string handling
static char_type *str_copy(char_type *dest, char_type const *src)
{
return ::wcscpy(dest, src);
}
static char_type *str_n_copy(char_type *dest, char_type const *src, size_type cch)
{
return ::wcsncpy(dest, src, cch);
}
static char_type *str_cat(char_type *dest, char_type const *src)
{
return ::wcscat(dest, src);
}
static int_type str_compare(char_type const *s1, char_type const *s2)
{
return ::wcscmp(s1, s2);
}
static int_type str_compare_no_case(char_type const *s1, char_type const *s2);
static int_type str_n_compare(char_type const *s1, char_type const *s2, size_type cch)
{
return ::wcsncmp(s1, s2, cch);
}
static size_type str_len(char_type const *src)
{
return static_cast<size_type>(::wcslen(src));
}
static char_type *str_chr(char_type const *s, char_type ch)
{
return const_cast<char_type*>(::wcschr(s, ch));
}
static char_type *str_rchr(char_type const *s, char_type ch)
{
return const_cast<char_type*>(::wcsrchr(s, ch));
}
static char_type *str_str(char_type const *s, char_type const *sub)
{
return const_cast<char_type*>(::wcsstr(s, sub));
}
static char_type *str_pbrk(char_type const *s, char_type const *charSet)
{
return const_cast<char_type*>(::wcspbrk(s, charSet));
}
static char_type *str_end(char_type const *s)
{
UNIXSTL_ASSERT(NULL != s);
for(; *s != L'\0'; ++s)
{}
return const_cast<char_type*>(s);
}
// File-system entry names
static char_type *ensure_dir_end(char_type *dir)
{
UNIXSTL_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)
{
UNIXSTL_ASSERT(NULL != 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)
{
UNIXSTL_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)
{
UNIXSTL_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)
{
UNIXSTL_ASSERT(NULL != path);
#ifdef _WIN32
// If it's really on Windows, then we need to skip the drive, if present
if( isalpha(path[0]) &&
':' == path[1])
{
path += 2;
}
// If it's really on Windows, then we need to account for the fact that
// the slash might be backwards
if('\\' == *path)
{
return true;
}
#endif /* _WIN32 */
return '/' == *path;
}
static bool_type is_path_absolute(char_type const *path)
{
UNIXSTL_ASSERT(NULL != path);
#ifdef _WIN32
// If it's really on Windows, then it can only be absolute if ...
//
// ... it's a UNC path, or ...
if(is_path_UNC(path))
{
return true;
}
// ... it's got drive + root slash, or
if( isalpha(path[0]) &&
':' == path[1] &&
is_path_name_separator(path[2]))
{
return true;
}
// ... it's got root forward slash
if('/' == path[0])
{
return true;
}
return false;
#else /* ? _WIN32 */
return is_path_rooted(path);
#endif /* _WIN32 */
}
static bool_type is_path_UNC(char_type const *path)
{
UNIXSTL_ASSERT(NULL != path);
#ifdef _WIN32
return (L'\\' == path[0] && L'\\' == path[1]);
#else /* ? _WIN32 */
STLSOFT_SUPPRESS_UNUSED(path);
return false;
#endif /* _WIN32 */
}
static bool_type is_path_name_separator(char_type ch)
{
#ifdef _WIN32
return L'\\' == ch || L'/' == ch;
#else /* ? _WIN32 */
return L'/' == ch;
#endif /* _WIN32 */
}
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()
{
#if defined(PATH_MAX)
return 1 + PATH_MAX;
#else /* ? PATH_MAX */
return 1 + pathconf("/", _PC_PATH_MAX);
#endif /* PATH_MAX */
}
static size_type get_full_path_name(char_type const *fileName, size_type cchBuffer, char_type *buffer, char_type **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);
}
// File-system state
static bool_type set_current_directory(char_type const *dir);
static size_type get_current_directory(size_type cchBuffer, char_type *buffer);
};
#endif /* 0 */
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Unit-testing
*/
#ifdef STLSOFT_UNITTEST
# include "./unittest/filesystem_traits_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _UNIXSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace unixstl
# else
} // namespace unixstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_UNIXSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -