📄 filesystem_traits.hpp
字号:
static is_int_t str_compare_no_case(char_type const* s1, char_type const* s2)
{
return ::lstrcmpiA(s1, s2);
}
static size_type str_len(char_type const* src)
{
return static_cast<size_type>(::lstrlenA(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));
}
// File-system entry names
static char_type *ensure_dir_end(char_type *dir)
{
char_type *end;
char_type const separator = (NULL == strchr(dir, '/') && NULL != strchr(dir, '\\')) ? '\\' : '/';
for(end = dir; *end != '\0'; ++end)
{}
if( dir < end &&
*(end - 1) != separator)
{
*end = separator;
*(end + 1) = '\0';
}
return dir;
}
static char_type *remove_dir_end(char_type *dir)
{
char_type *end;
for(end = dir; *end != '\0'; ++end)
{}
if( dir < end &&
*(end - 1) == path_name_separator())
{
*(end - 1) = '\0';
}
return dir;
}
static bool_type has_dir_end(char_type const* dir)
{
is_size_t len = str_len(dir);
return (0 < len) && path_name_separator() == dir[len - 1];
}
static bool_type is_dots(char_type const* dir)
{
return dir != 0 &&
dir[0] == '.' &&
( dir[1] == '\0' ||
( dir[1] == '.' &&
dir[2] == '\0'));
}
static bool_type is_path_rooted(char_type const* path)
{
INETSTL_ASSERT(NULL != path);
return '/' == *path;
}
static bool_type is_path_absolute(char_type const* path)
{
return is_path_rooted(path);
}
static char_type path_separator()
{
return ';';
}
static char_type path_name_separator()
{
return '/';
}
static char_type const* pattern_all()
{
return "*";
}
static is_dword_t get_full_path_name(HINTERNET hconn, char_type const* fileName, is_dword_t cchBuffer, char_type* buffer, char_type **ppFile)
{
INETSTL_ASSERT(0 == cchBuffer || NULL != buffer);
INETSTL_ASSERT(NULL == buffer || 0 != cchBuffer);
// Deduce the separator
char_type const separator = (NULL == strchr(fileName, '/') && NULL != strchr(fileName, '\\')) ? '\\' : '/';
char_type fullPath[1 + _MAX_PATH];
// If we're not rooted, then get the current directory and concatenate
if(separator != *fileName)
{
is_size_t cchBuffer = STLSOFT_NUM_ELEMENTS(fullPath);
int isDot = 0 == str_compare(fileName, ".");
if(!get_current_directory(hconn, cchBuffer, fullPath))
{
fullPath[0] = '\0';
}
if(!isDot)
{
ensure_dir_end(fullPath);
str_cat(fullPath, fileName);
}
fileName = fullPath;
}
size_type len = str_len(fileName);
if(NULL != buffer)
{
if(cchBuffer < len)
{
len = cchBuffer;
}
::strncpy(buffer, fileName, cchBuffer);
if(NULL != ppFile)
{
char_type const* pRSlash = strrchr(buffer, '/');
char_type const* pRBackSlash = strrchr(buffer, '\\');
if(pRSlash < pRBackSlash)
{
pRSlash = pRBackSlash;
}
if(NULL == pRSlash)
{
*ppFile = NULL;
}
else
{
*ppFile = const_cast<char_type*>(pRSlash) + 1;
}
}
}
return len;
}
static is_dword_t get_full_path_name(HINTERNET hconn, char_type const* fileName, is_dword_t cchBuffer, char_type* buffer)
{
char_type *pFile;
return get_full_path_name(hconn, fileName, cchBuffer, buffer, &pFile);
}
// Internet connectivity
static HINTERNET internet_open(char_type const* agent, is_dword_t accessType, char_type const* proxy, char_type const* proxyBypass, is_dword_t flags)
{
return ::InternetOpenA(agent, accessType, proxy, proxyBypass, flags);
}
static HINTERNET internet_connect(HINTERNET hsess, char_type const* server, INTERNET_PORT port, char_type const* userName, char_type const* password, is_dword_t service, is_dword_t flags, is_dword_t context)
{
return ::InternetConnectA(hsess, server, port, userName, password, service, flags, context);
}
static void close_connection(HINTERNET hconn)
{
INETSTL_ASSERT(NULL != hconn);
::InternetCloseHandle(hconn);
}
// FindFile() API
static HINTERNET find_first_file(HINTERNET hconn, char_type const* spec, find_data_type *findData, is_dword_t flags = 0, is_dword_t context = 0)
{
HINTERNET hfind = ::FtpFindFirstFileA(hconn, spec, stlsoft_ns_qual(any_caster)<find_data_type*, LPWIN32_FIND_DATAA, LPWIN32_FIND_DATAW>(findData), flags, context);
#if 0
if(NULL == hfind)
{
findData->cFileName[0] = '\0';
}
printf("find_first_file(0x%08x, %s => %s)\n", hfind, spec, findData->cFileName);
#endif /* 0 */
return hfind;
}
static bool_type find_next_file(HANDLE h, find_data_type *findData)
{
return FALSE != ::InternetFindNextFileA(h, findData);
}
static void find_close(HINTERNET hfind)
{
INETSTL_ASSERT(NULL != hfind);
::InternetCloseHandle(hfind);
}
// File system state
static bool_type set_current_directory(HINTERNET hconn, char_type const* dir)
{
return ::FtpSetCurrentDirectoryA(hconn, dir) != FALSE;
}
static bool_type get_current_directory(HINTERNET hconn, is_size_t &cchBuffer, char_type* buffer)
{
return FALSE != ::FtpGetCurrentDirectoryA(hconn, buffer, sap_cast<unsigned long*>(&cchBuffer));
}
static bool_type file_exists(HINTERNET hconn, char_type const* fileName)
{
find_data_type data;
HINTERNET hfind = find_first_file(hconn, fileName, &data);
return (NULL == hfind) ? false : (find_close(hfind), true);
}
#if 0
static bool_type is_file(HINTERNET hconn, char_type const* path);
static bool_type is_directory(HINTERNET hconn, char_type const* path);
static bool_type stat(HINTERNET hconn, char_type const* path, stat_data_type *stat_data);
#endif /* 0 */
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);
static bool_type is_readonly(stat_data_type const* stat_data)
{
return FILE_ATTRIBUTE_READONLY == (stat_data->dwFileAttributes & FILE_ATTRIBUTE_READONLY);
}
// File system control
static bool_type create_directory(HINTERNET hconn, char_type const* dir)
{
return FALSE != ::FtpCreateDirectoryA(hconn, dir);
}
static bool_type remove_directory(HINTERNET hconn, char_type const* dir)
{
return FALSE != ::FtpRemoveDirectoryA(hconn, dir);
}
static bool_type delete_file(HINTERNET hconn, char_type const* file)
{
return FALSE != ::FtpDeleteFileA(hconn, file);
}
static bool_type rename_file(HINTERNET hconn, char_type const* currentName, char_type const* newName)
{
return FALSE != ::FtpRenameFileA(hconn, currentName, newName);
}
};
STLSOFT_TEMPLATE_SPECIALISATION
struct filesystem_traits<is_char_w_t>
{
public:
typedef is_char_w_t char_type;
typedef is_size_t size_type;
typedef is_ptrdiff_t difference_type;
typedef WIN32_FIND_DATAW find_data_type;
typedef WIN32_FIND_DATAW stat_data_type;
typedef filesystem_traits<is_char_w_t> class_type;
typedef is_int_t int_type;
typedef is_bool_t bool_type;
typedef DWORD error_type;
public:
// General string handling
static char_type *str_copy(char_type *dest, char_type const* src)
{
return ::lstrcpyW(dest, src);
}
static char_type *str_n_copy(char_type *dest, char_type const* src, is_size_t cch)
{
return ::wcsncpy(dest, src, cch);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -