📄 filesystem_traits.hpp
字号:
/// \name Environment
/// @{
public:
/// \brief Gets an environment variable into the given buffer
///
/// \param name The name of the variable to find
/// \param buffer The buffer in which to write the variable. If this is NULL, then the required length is returned
/// \param cchBuffer The size of the buffer, in characters
static size_type get_environment_variable(char_type const *name, char_type *buffer, size_type cchBuffer);
/// \brief Expands environment strings in \c src into \c buffer, up to a maximum \c cchDest characters
static size_type expand_environment_strings(char_type const *src, char_type *buffer, size_type cchBuffer);
/// @}
};
#else /* ? STLSOFT_DOCUMENTATION_SKIP_SECTION */
template <ss_typename_param_k C>
struct filesystem_traits;
STLSOFT_TEMPLATE_SPECIALISATION
struct filesystem_traits<us_char_a_t>
{
public:
typedef us_char_a_t char_type;
typedef us_size_t size_type;
typedef us_ptrdiff_t difference_type;
typedef struct stat stat_data_type;
typedef struct stat fstat_data_type;
typedef filesystem_traits<us_char_a_t> class_type;
typedef us_int_t int_type;
typedef us_bool_t bool_type;
typedef int file_handle_type;
typedef void *module_type;
typedef int error_type;
#ifdef _WIN32
typedef unsigned short mode_type;
#else /* ? _WIN32 */
typedef mode_t mode_type;
#endif /* _WIN32 */
private:
typedef stlsoft_ns_qual(auto_buffer_old)<char_type> buffer_type_;
public:
#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 ::strcpy(dest, src);
}
static char_type *str_n_copy(char_type *dest, char_type const *src, size_type cch)
{
return ::strncpy(dest, src, cch);
}
static char_type *str_cat(char_type *dest, char_type const *src)
{
return ::strcat(dest, src);
}
static char_type *str_n_cat(char_type *dest, char_type const *src, size_type cch)
{
return ::strncat(dest, src, cch);
}
static int_type str_compare(char_type const *s1, char_type const *s2)
{
return ::strcmp(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 ::strncmp(s1, s2, cch);
}
static size_type str_len(char_type const *src)
{
return static_cast<size_type>(::strlen(src));
}
static char_type *str_chr(char_type const *s, char_type ch)
{
return const_cast<char_type*>(::strchr(s, ch));
}
static char_type *str_rchr(char_type const *s, char_type ch)
{
return const_cast<char_type*>(::strrchr(s, ch));
}
static char_type *str_str(char_type const *s, char_type const *sub)
{
return const_cast<char_type*>(::strstr(s, sub));
}
static char_type *str_pbrk(char_type const *s, char_type const *charSet)
{
return const_cast<char_type*>(::strpbrk(s, charSet));
}
static char_type *str_end(char_type const *s)
{
UNIXSTL_ASSERT(NULL != s);
for(; *s != '\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) = '\0';
}
return dir;
}
static char_type *remove_dir_end(char_type *dir)
{
UNIXSTL_ASSERT(NULL != dir);
#ifdef _WIN32
if( isalpha(dir[0]) &&
':' == dir[1] &&
is_path_name_separator(dir[2]) &&
'\0' == dir[3])
{
return dir;
}
if( '\\' == dir[0] &&
'\\' == dir[1] &&
'\0' == dir[3])
{
return dir;
}
#endif /* _WIN32 */
char_type *end = str_end(dir);
if( dir < end &&
is_path_name_separator(*(end - 1)))
{
*(end - 1) = '\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] == '\0' ||
( dir[1] == '.' &&
dir[2] == '\0'));
}
static bool_type is_path_rooted(char_type const *path)
{
UNIXSTL_ASSERT(NULL != path);
#ifdef _WIN32
// It might be a UNC path. This is handled by the second test below, but
// this is a bit clearer, and since this is a debug kind of thing, we're
// not worried about the cost
if( '\\' == path[0] &&
'\\' == path[1])
{
return true;
}
// 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, but that's taken care of for us by
// is_path_name_separator()
#endif /* _WIN32 */
return is_path_name_separator(*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 ('\\' == path[0] && '\\' == path[1]);
#else /* ? _WIN32 */
STLSOFT_SUPPRESS_UNUSED(path);
return false;
#endif /* _WIN32 */
}
private:
static bool_type is_root_drive_(char_type const *path)
{
#ifdef _WIN32
if( isalpha(path[0]) &&
':' == path[1] &&
is_path_name_separator(path[2]) &&
'\0' == path[3])
{
return true;
}
#else /* ? _WIN32 */
STLSOFT_SUPPRESS_UNUSED(path);
#endif /* _WIN32 */
return false;
}
static bool_type is_root_UNC_(char_type const *path)
{
#ifdef _WIN32
if(is_path_UNC(path))
{
char_type const *sep = str_pbrk(path + 2, "\\/");
if( NULL == sep ||
'\0' == sep[1])
{
return true;
}
}
#else /* ? _WIN32 */
STLSOFT_SUPPRESS_UNUSED(path);
#endif /* _WIN32 */
return false;
}
static bool_type is_root_directory_(char_type const *path)
{
if( is_path_name_separator(path[0]) &&
'\0' == path[1])
{
return true;
}
return false;
}
public:
static bool_type is_root_designator(char_type const *path)
{
UNIXSTL_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)
{
#ifdef _WIN32
return '\\' == ch || '/' == ch;
#else /* ? _WIN32 */
return '/' == ch;
#endif /* _WIN32 */
}
static char_type path_separator()
{
return ':';
}
static char_type path_name_separator()
{
return '/';
}
static char_type const *pattern_all()
{
return "*";
}
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 */
}
private:
static size_type get_full_path_name_impl2(char_type const *fileName, us_size_t len, char_type *buffer, size_type cchBuffer)
{
// The next thing to so is determine whether the path is absolute, in
// which case we'll just copy it into the buffer
if(is_path_rooted(fileName))
{
// Given path is absolute, so simply copy into buffer
if(NULL == buffer)
{
len = len;
}
else if(cchBuffer < len)
{
str_n_copy(&buffer[0], fileName, cchBuffer);
len = cchBuffer;
}
else
{
// Given buffer is large enough, so copy
str_copy(buffer, fileName);
}
}
else
{
// Given path is relative, so get the current directory, and then concatenate
buffer_type_ directory(1 + path_max());
if(0 == directory.size())
{
set_last_error(ENOMEM);
return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -