open source
This commit is contained in:
@@ -0,0 +1,812 @@
|
||||
/**
|
||||
@file JCFileSystem.cpp
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_5_11
|
||||
*/
|
||||
|
||||
#include "JCFileSystem.h"
|
||||
#include <stdio.h>
|
||||
#include "../util/JCCommonMethod.h"
|
||||
#define ERROR_FILE_C_R_W (-6)
|
||||
#pragma warning(disable:4996)
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#else
|
||||
#include <cstring>
|
||||
#include <sys/stat.h>
|
||||
#include <cstdlib>
|
||||
#include <cerrno>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
namespace laya
|
||||
{
|
||||
GLOBAL_ONCREATEFILEERROR global_onCreateFileError = NULL;
|
||||
bool readFileSync( const char* p_pszFile , JCBuffer& p_buf, int p_nEncode )
|
||||
{
|
||||
if(!p_pszFile)
|
||||
return false;
|
||||
FILE* pf = NULL;
|
||||
pf=fopen(p_pszFile,"rb");
|
||||
if( pf==NULL )
|
||||
return false;
|
||||
fseek(pf,0,SEEK_END);
|
||||
int len = ftell(pf);
|
||||
bool bAsText = p_nEncode!=JCBuffer::raw ;
|
||||
fseek(pf,0,SEEK_SET);
|
||||
p_buf.create(len+(bAsText?1:0));
|
||||
int readlen = fread(p_buf.m_pPtr, 1, len, pf);
|
||||
if( readlen!=len){
|
||||
fclose(pf);
|
||||
p_buf.free();
|
||||
return false;
|
||||
}
|
||||
fclose(pf);
|
||||
if( bAsText )
|
||||
p_buf.m_pPtr[len]=0;
|
||||
return true;
|
||||
}
|
||||
std::string readFileSync1(const char* p_pszFile, const char* p_pszEncode)
|
||||
{
|
||||
JCBuffer buf;
|
||||
if (readFileSync(p_pszFile, buf, JCBuffer::utf8))
|
||||
{
|
||||
return buf.m_pPtr;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
bool writeFileSync1( const char* p_pszFile, char* p_pBuff, int p_nLen, int p_nEncode)
|
||||
{
|
||||
if (!p_pszFile || strlen(p_pszFile) <= 1)
|
||||
return false;
|
||||
FILE* pFile = NULL;
|
||||
try{
|
||||
pFile = fopen( p_pszFile, "wb");
|
||||
if( pFile==NULL){
|
||||
throw ERROR_FILE_C_R_W;
|
||||
return false;
|
||||
}
|
||||
int ret = fwrite( p_pBuff, 1, p_nLen, pFile );
|
||||
if(ret<p_nLen){
|
||||
fclose(pFile);
|
||||
throw ERROR_FILE_C_R_W;
|
||||
}
|
||||
fflush(pFile);
|
||||
fclose(pFile);
|
||||
}
|
||||
catch(int e)
|
||||
{
|
||||
if( e==ERROR_FILE_C_R_W && global_onCreateFileError){
|
||||
global_onCreateFileError();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool writeFileSync( const char* p_pszFile, JCBuffer& p_buf, int p_nEncode )
|
||||
{
|
||||
return writeFileSync1(p_pszFile, p_buf.m_pPtr, p_buf.m_nLen, p_nEncode);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
namespace fs
|
||||
{
|
||||
# define FILESYSTEM_REMOVE_DIRECTORY(P)(::rmdir(P)== 0)
|
||||
# define FILESYSTEM_DELETE_FILE(P)(::unlink(P)== 0)
|
||||
# define FILESYSTEM_ERRNO errno
|
||||
# define FILESYSTEM_SET_CURRENT_DIRECTORY(P)(::chdir(P)== 0)
|
||||
# define FILESYSTEM_CREATE_DIRECTORY(P)(::mkdir(P, S_IRWXU|S_IRWXG|S_IRWXO)== 0)
|
||||
typedef int err_t;
|
||||
typedef path::value_type value_type;
|
||||
typedef path::string_type string_type;
|
||||
typedef string_type::size_type size_type;
|
||||
directory_iterator end_dir_itr;
|
||||
const std::error_code ok;
|
||||
const char* const separators = "/";
|
||||
const char* separator_string = "/";
|
||||
const char* preferred_separator_string = "/";
|
||||
const char dot = '.';
|
||||
const std::error_code not_found_error_code (ENOENT, std::system_category());
|
||||
|
||||
path& path::remove_filename()
|
||||
{
|
||||
m_pathname.erase(m_parent_path_end());
|
||||
return *this;
|
||||
}
|
||||
size_type root_directory_start(const string_type & path, size_type size)
|
||||
// return npos if no root_directory found
|
||||
{
|
||||
|
||||
|
||||
// case "//"
|
||||
if (size == 2
|
||||
&& is_directory_separator(path[0])
|
||||
&& is_directory_separator(path[1])) return string_type::npos;
|
||||
|
||||
|
||||
// case "//net {/}"
|
||||
if (size > 3
|
||||
&& is_directory_separator(path[0])
|
||||
&& is_directory_separator(path[1])
|
||||
&& !is_directory_separator(path[2]))
|
||||
{
|
||||
string_type::size_type pos(path.find_first_of(separators, 2));
|
||||
return pos < size ? pos : string_type::npos;
|
||||
}
|
||||
|
||||
// case "/"
|
||||
if (size > 0 && is_directory_separator(path[0])) return 0;
|
||||
|
||||
return string_type::npos;
|
||||
}
|
||||
size_type filename_pos(const string_type & str,size_type end_pos) // end_pos is past-the-end position
|
||||
// return 0 if str itself is filename (or empty)
|
||||
{
|
||||
// case: "//"
|
||||
if (end_pos == 2
|
||||
&& is_directory_separator(str[0])
|
||||
&& is_directory_separator(str[1])) return 0;
|
||||
|
||||
// case: ends in "/"
|
||||
if (end_pos && is_directory_separator(str[end_pos-1]))
|
||||
return end_pos-1;
|
||||
|
||||
// set pos to start of last element
|
||||
size_type pos(str.find_last_of(separators, end_pos-1));
|
||||
|
||||
|
||||
return (pos == string_type::npos // path itself must be a filename (or empty)
|
||||
|| (pos == 1 && is_directory_separator(str[0]))) // or net
|
||||
? 0 // so filename is entire string
|
||||
: pos + 1; // or starts after delimiter
|
||||
}
|
||||
const path& dot_path()
|
||||
{
|
||||
static const path dot_pth(".");
|
||||
return dot_pth;
|
||||
}
|
||||
|
||||
const path& dot_dot_path()
|
||||
{
|
||||
static const path dot_dot("..");
|
||||
return dot_dot;
|
||||
}
|
||||
bool is_root_separator(const string_type & str, size_type pos)
|
||||
// pos is position of the separator
|
||||
{
|
||||
assert(!str.empty() && is_directory_separator(str[pos]) && "precondition violation");
|
||||
|
||||
// subsequent logic expects pos to be for leftmost slash of a set
|
||||
while (pos > 0 && is_directory_separator(str[pos-1]))
|
||||
--pos;
|
||||
|
||||
// "/" [...]
|
||||
if (pos == 0)
|
||||
return true;
|
||||
|
||||
|
||||
// "//" name "/"
|
||||
if (pos < 3 || !is_directory_separator(str[0])
|
||||
|| !is_directory_separator(str[1]))
|
||||
return false;
|
||||
|
||||
return str.find_first_of(separators, 2) == pos;
|
||||
}
|
||||
|
||||
string_type::size_type path::m_parent_path_end() const
|
||||
{
|
||||
size_type end_pos(filename_pos(m_pathname, m_pathname.size()));
|
||||
|
||||
bool filename_was_separator(m_pathname.size()
|
||||
&& is_directory_separator(m_pathname[end_pos]));
|
||||
|
||||
// skip separators unless root directory
|
||||
size_type root_dir_pos(root_directory_start(m_pathname, end_pos));
|
||||
for (;
|
||||
end_pos > 0
|
||||
&& (end_pos-1) != root_dir_pos
|
||||
&& is_directory_separator(m_pathname[end_pos-1])
|
||||
;
|
||||
--end_pos) {}
|
||||
|
||||
return (end_pos == 1 && root_dir_pos == 0 && filename_was_separator)
|
||||
? string_type::npos
|
||||
: end_pos;
|
||||
}
|
||||
path path::filename() const
|
||||
{
|
||||
size_type pos(filename_pos(m_pathname, m_pathname.size()));
|
||||
return (m_pathname.size()
|
||||
&& pos
|
||||
&& is_directory_separator(m_pathname[pos])
|
||||
&& !is_root_separator(m_pathname, pos))
|
||||
? dot_path()
|
||||
: path(m_pathname.c_str() + pos);
|
||||
}
|
||||
path& path::operator/=(const path& p)
|
||||
{
|
||||
if (p.empty())
|
||||
return *this;
|
||||
if (this == &p) // self-append
|
||||
{
|
||||
path rhs(p);
|
||||
if (!is_directory_separator(rhs.m_pathname[0]))
|
||||
m_append_separator_if_needed();
|
||||
m_pathname += rhs.m_pathname;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!is_directory_separator(*p.m_pathname.begin()))
|
||||
m_append_separator_if_needed();
|
||||
m_pathname += p.m_pathname;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
path& path::operator/=(const value_type* ptr)
|
||||
{
|
||||
if (!*ptr)
|
||||
return *this;
|
||||
if (ptr >= m_pathname.data()
|
||||
&& ptr < m_pathname.data() + m_pathname.size()) // overlapping source
|
||||
{
|
||||
path rhs(ptr);
|
||||
if (!is_directory_separator(rhs.m_pathname[0]))
|
||||
m_append_separator_if_needed();
|
||||
m_pathname += rhs.m_pathname;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!is_directory_separator(*ptr))
|
||||
m_append_separator_if_needed();
|
||||
m_pathname += ptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
path::string_type::size_type path::m_append_separator_if_needed()
|
||||
{
|
||||
if (!m_pathname.empty() &&
|
||||
!is_directory_separator(*(m_pathname.end()-1)))
|
||||
{
|
||||
string_type::size_type tmp(m_pathname.size());
|
||||
m_pathname += preferred_separator;
|
||||
return tmp;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
path path::parent_path() const
|
||||
{
|
||||
size_type end_pos(m_parent_path_end());
|
||||
return end_pos == string_type::npos
|
||||
? path()
|
||||
//: path(m_pathname.c_str(), m_pathname.c_str() + end_pos);
|
||||
:path(m_pathname.substr(0,end_pos));
|
||||
}
|
||||
bool not_found_error(int errval)
|
||||
{
|
||||
return errno == ENOENT || errno == ENOTDIR;
|
||||
}
|
||||
file_status status(const path& p, std::error_code* ec)
|
||||
{
|
||||
struct stat path_stat;
|
||||
if (::stat(p.c_str(), &path_stat)!= 0)
|
||||
{
|
||||
if (ec != 0) // always report errno, even though some
|
||||
ec->assign(errno, std::system_category()); // errno values are not status_errors
|
||||
|
||||
if (not_found_error(errno))
|
||||
{
|
||||
return file_status(file_not_found, no_perms);
|
||||
}
|
||||
//if (ec == 0)
|
||||
// BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::status",
|
||||
// p, error_code(errno, system_category())));
|
||||
if (ec == 0)
|
||||
throw filesystem_error("boost::filesystem::status");
|
||||
return file_status(status_error);
|
||||
}
|
||||
if (ec != 0) ec->clear();;
|
||||
if (S_ISDIR(path_stat.st_mode))
|
||||
return file_status(directory_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISREG(path_stat.st_mode))
|
||||
return file_status(regular_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISBLK(path_stat.st_mode))
|
||||
return file_status(block_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISCHR(path_stat.st_mode))
|
||||
return file_status(character_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISFIFO(path_stat.st_mode))
|
||||
return file_status(fifo_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISSOCK(path_stat.st_mode))
|
||||
return file_status(socket_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
return file_status(type_unknown);
|
||||
|
||||
}
|
||||
bool create_directory(const path& p, std::error_code* ec)
|
||||
{
|
||||
if (FILESYSTEM_CREATE_DIRECTORY(p.c_str()))
|
||||
{
|
||||
if (ec != 0)
|
||||
ec->clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
// attempt to create directory failed
|
||||
int errval(FILESYSTEM_ERRNO); // save reason for failure
|
||||
//error_code dummy;
|
||||
|
||||
if (is_directory(p/*, dummy*/))
|
||||
{
|
||||
if (ec != 0)
|
||||
ec->clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
// attempt to create directory failed && it doesn't already exist
|
||||
if (ec == 0)
|
||||
//BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::create_directory",
|
||||
// p, error_code(errval, system_category())));
|
||||
throw filesystem_error("boost::filesystem::create_directory");
|
||||
else
|
||||
ec->assign(errval, std::system_category());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool create_directories(const path& p, std::error_code* ec)
|
||||
{
|
||||
if (p.empty())
|
||||
{
|
||||
if (ec == 0)
|
||||
//BOOST_FILESYSTEM_THROW(filesystem_error(
|
||||
// "boost::filesystem::create_directories", p,
|
||||
// system::errc::make_error_code(system::errc::invalid_argument)));
|
||||
throw filesystem_error("boost::filesystem::create_directories");
|
||||
else
|
||||
ec->assign((int)std::errc::invalid_argument, std::generic_category());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (p.filename_is_dot() || p.filename_is_dot_dot())
|
||||
return create_directories(p.parent_path(), ec);
|
||||
|
||||
std::error_code local_ec;
|
||||
file_status p_status = status(p, &local_ec);
|
||||
|
||||
if (p_status.type() == directory_file)
|
||||
{
|
||||
if (ec != 0)
|
||||
ec->clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
path parent = p.parent_path();
|
||||
//////////assert(parent != p &&"internal error: p == p.parent_path()");
|
||||
if (!parent.empty())
|
||||
{
|
||||
// determine if the parent exists
|
||||
file_status parent_status = status(parent, &local_ec);
|
||||
|
||||
// if the parent does not exist, create the parent
|
||||
if (parent_status.type() == file_not_found)
|
||||
{
|
||||
create_directories(parent, &local_ec);
|
||||
if (local_ec)
|
||||
{
|
||||
if (ec == 0)
|
||||
//BOOST_FILESYSTEM_THROW(filesystem_error(
|
||||
// "boost::filesystem::create_directories", parent, local_ec));
|
||||
throw filesystem_error("boost::filesystem::create_directories");
|
||||
else
|
||||
*ec = local_ec;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create the directory
|
||||
return create_directory(p, ec);
|
||||
}
|
||||
bool error(err_t error_num, const path& p, std::error_code* ec, const char* message)
|
||||
{
|
||||
if (!error_num)
|
||||
{
|
||||
if (ec != 0) ec->clear();
|
||||
}
|
||||
else
|
||||
{ // error
|
||||
if (ec == 0)
|
||||
//BOOST_FILESYSTEM_THROW(filesystem_error(message,
|
||||
// p, error_code(error_num, system_category())));
|
||||
throw filesystem_error(message);
|
||||
else
|
||||
ec->assign(error_num, std::system_category());
|
||||
}
|
||||
return error_num != 0;
|
||||
}
|
||||
std::time_t last_write_time(const path& p, std::error_code* ec)
|
||||
{
|
||||
struct stat path_stat;
|
||||
if (error(::stat(p.c_str(), &path_stat)!= 0 ? FILESYSTEM_ERRNO : 0,
|
||||
p, ec, "boost::filesystem::last_write_time"))
|
||||
return std::time_t(-1);
|
||||
return path_stat.st_mtime;
|
||||
}
|
||||
|
||||
std::uintmax_t file_size(const path& p, std::error_code* ec)
|
||||
{
|
||||
struct stat path_stat;
|
||||
if (error(::stat(p.c_str(), &path_stat)!= 0 ? FILESYSTEM_ERRNO : 0,
|
||||
p, ec, "boost::filesystem::file_size"))
|
||||
return static_cast<std::uintmax_t>(-1);
|
||||
if (error(!S_ISREG(path_stat.st_mode) ? EPERM : 0,
|
||||
p, ec, "boost::filesystem::file_size"))
|
||||
return static_cast<std::uintmax_t>(-1);
|
||||
|
||||
return static_cast<std::uintmax_t>(path_stat.st_size);
|
||||
}
|
||||
// only called if directory exists
|
||||
bool remove_directory(const path& p) // true if succeeds or not found
|
||||
{
|
||||
return FILESYSTEM_REMOVE_DIRECTORY(p.c_str())
|
||||
|| not_found_error(FILESYSTEM_ERRNO); // mitigate possible file system race. See #11166
|
||||
}
|
||||
|
||||
// only called if file exists
|
||||
bool remove_file(const path& p) // true if succeeds or not found
|
||||
{
|
||||
return FILESYSTEM_DELETE_FILE(p.c_str())
|
||||
|| not_found_error(FILESYSTEM_ERRNO); // mitigate possible file system race. See #11166
|
||||
}
|
||||
|
||||
// called by remove and remove_all_aux
|
||||
bool remove_file_or_directory(const path& p, file_type type, std::error_code* ec)
|
||||
// return true if file removed, false if not removed
|
||||
{
|
||||
if (type == file_not_found)
|
||||
{
|
||||
if (ec != 0) ec->clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type == directory_file)
|
||||
{
|
||||
if (error(!remove_directory(p) ? FILESYSTEM_ERRNO : 0, p, ec,
|
||||
"boost::filesystem::remove"))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (error(!remove_file(p) ? FILESYSTEM_ERRNO : 0, p, ec,
|
||||
"boost::filesystem::remove"))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
namespace detail
|
||||
{
|
||||
file_status symlink_status(const path& p, std::error_code* ec)
|
||||
{
|
||||
|
||||
struct stat path_stat;
|
||||
if (::lstat(p.c_str(), &path_stat)!= 0)
|
||||
{
|
||||
if (ec != 0) // always report errno, even though some
|
||||
ec->assign(errno, std::system_category()); // errno values are not status_errors
|
||||
|
||||
if (errno == ENOENT || errno == ENOTDIR) // these are not errors
|
||||
{
|
||||
return file_status(file_not_found, no_perms);
|
||||
}
|
||||
if (ec == 0)
|
||||
//BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::status",
|
||||
// p, error_code(errno, system_category())));
|
||||
throw filesystem_error("boost::filesystem::status");
|
||||
return file_status(status_error);
|
||||
}
|
||||
if (ec != 0) ec->clear();
|
||||
if (S_ISREG(path_stat.st_mode))
|
||||
return file_status(regular_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISDIR(path_stat.st_mode))
|
||||
return file_status(directory_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISLNK(path_stat.st_mode))
|
||||
return file_status(symlink_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISBLK(path_stat.st_mode))
|
||||
return file_status(block_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISCHR(path_stat.st_mode))
|
||||
return file_status(character_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISFIFO(path_stat.st_mode))
|
||||
return file_status(fifo_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
if (S_ISSOCK(path_stat.st_mode))
|
||||
return file_status(socket_file,
|
||||
static_cast<perms>(path_stat.st_mode) & perms_mask);
|
||||
return file_status(type_unknown);
|
||||
|
||||
}
|
||||
}
|
||||
inline file_type query_file_type(const path& p, std::error_code* ec)
|
||||
{
|
||||
return detail::symlink_status(p, ec).type();
|
||||
}
|
||||
bool remove(const path& p, std::error_code* ec)
|
||||
{
|
||||
std::error_code tmp_ec;
|
||||
file_type type = query_file_type(p, &tmp_ec);
|
||||
if (error(type == status_error ? tmp_ec.value() : 0, p, ec,
|
||||
"boost::filesystem::remove"))
|
||||
return false;
|
||||
|
||||
// Since POSIX remove() is specified to work with either files or directories, in a
|
||||
// perfect world it could just be called. But some important real-world operating
|
||||
// systems (Windows, Mac OS X, for example) don't implement the POSIX spec. So
|
||||
// remove_file_or_directory() is always called to keep it simple.
|
||||
return remove_file_or_directory(p, type, ec);
|
||||
}
|
||||
std::uintmax_t remove_all_aux(const path& p, file_type type,std::error_code* ec)
|
||||
{
|
||||
std::uintmax_t count = 1;
|
||||
if (type == directory_file) // but not a directory symlink
|
||||
{
|
||||
directory_iterator itr;
|
||||
if (ec != 0)
|
||||
{
|
||||
itr = directory_iterator(p, *ec);
|
||||
if (*ec)
|
||||
return count;
|
||||
}
|
||||
else
|
||||
itr = directory_iterator(p);
|
||||
for (; itr != end_dir_itr; ++itr)
|
||||
{
|
||||
file_type tmp_type = query_file_type(itr->path(), ec);
|
||||
if (ec != 0 && *ec)
|
||||
return count;
|
||||
count += remove_all_aux(itr->path(), tmp_type, ec);
|
||||
if (ec != 0 && *ec)
|
||||
return count;
|
||||
}
|
||||
}
|
||||
remove_file_or_directory(p, type, ec);
|
||||
return count;
|
||||
}
|
||||
std::uintmax_t remove_all(const path& p, std::error_code* ec)
|
||||
{
|
||||
std::error_code tmp_ec;
|
||||
file_type type = query_file_type(p, &tmp_ec);
|
||||
if (error(type == status_error ? tmp_ec.value() : 0, p, ec,
|
||||
"boost::filesystem::remove_all"))
|
||||
return 0;
|
||||
|
||||
return (type != status_error && type != file_not_found) // exists
|
||||
? remove_all_aux(p, type, ec)
|
||||
: 0;
|
||||
}
|
||||
namespace detail
|
||||
{
|
||||
std::error_code path_max(std::size_t & result)
|
||||
// this code is based on Stevens and Rago, Advanced Programming in the
|
||||
// UNIX envirnment, 2nd Ed., ISBN 0-201-43307-9, page 49
|
||||
{
|
||||
# ifdef PATH_MAX
|
||||
static std::size_t max = PATH_MAX;
|
||||
# else
|
||||
static std::size_t max = 0;
|
||||
# endif
|
||||
if (max == 0)
|
||||
{
|
||||
errno = 0;
|
||||
long tmp = ::pathconf("/", _PC_NAME_MAX);
|
||||
if (tmp < 0)
|
||||
{
|
||||
if (errno == 0)// indeterminate
|
||||
max = 4096; // guess
|
||||
else return std::error_code(errno, std::system_category());
|
||||
}
|
||||
else max = static_cast<std::size_t>(tmp + 1); // relative root
|
||||
}
|
||||
result = max;
|
||||
return ok;
|
||||
}
|
||||
|
||||
#if defined(__PGI) && defined(__USE_FILE_OFFSET64)
|
||||
#define dirent dirent64
|
||||
#endif
|
||||
|
||||
std::error_code dir_itr_first(void *& handle, void *& buffer,const char* dir, std::string& target,file_status &, file_status &)
|
||||
{
|
||||
if ((handle = ::opendir(dir))== 0)
|
||||
return std::error_code(errno, std::system_category());
|
||||
target = std::string("."); // string was static but caused trouble
|
||||
// when iteration called from dtor, after
|
||||
// static had already been destroyed
|
||||
std::size_t path_size (0); // initialization quiets gcc warning (ticket #3509)
|
||||
std::error_code ec = path_max(path_size);
|
||||
if (ec)return ec;
|
||||
dirent de;
|
||||
buffer = std::malloc((sizeof(dirent) - sizeof(de.d_name))
|
||||
+ path_size + 1); // + 1 for "/0"
|
||||
return ok;
|
||||
}
|
||||
|
||||
// warning: the only dirent member updated is d_name
|
||||
inline int readdir_r_simulator(DIR * dirp, struct dirent * entry,struct dirent ** result)// *result set to 0 on end of directory
|
||||
{
|
||||
errno = 0;
|
||||
|
||||
# if !defined(__CYGWIN__)\
|
||||
&& defined(_POSIX_THREAD_SAFE_FUNCTIONS)\
|
||||
&& defined(_SC_THREAD_SAFE_FUNCTIONS)\
|
||||
&& (_POSIX_THREAD_SAFE_FUNCTIONS+0 >= 0)\
|
||||
&& (!defined(__hpux) || defined(_REENTRANT)) \
|
||||
&& (!defined(_AIX) || defined(__THREAD_SAFE))
|
||||
if (::sysconf(_SC_THREAD_SAFE_FUNCTIONS)>= 0)
|
||||
{ return ::readdir_r(dirp, entry, result); }
|
||||
# endif
|
||||
|
||||
struct dirent * p;
|
||||
*result = 0;
|
||||
if ((p = ::readdir(dirp))== 0)
|
||||
return errno;
|
||||
std::strcpy(entry->d_name, p->d_name);
|
||||
*result = entry;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::error_code dir_itr_increment(void *& handle, void *& buffer,std::string& target, file_status & sf, file_status & symlink_sf)
|
||||
{
|
||||
assert(buffer != 0);
|
||||
dirent * entry(static_cast<dirent *>(buffer));
|
||||
dirent * result;
|
||||
int return_code;
|
||||
if ((return_code = readdir_r_simulator(static_cast<DIR*>(handle), entry, &result))!= 0)
|
||||
return std::error_code(errno, std::system_category());
|
||||
if (result == 0)
|
||||
return detail::dir_itr_close(handle, buffer);
|
||||
target = entry->d_name;
|
||||
# ifdef _DIRENT_HAVE_D_TYPE //BOOST_FILESYSTEM_STATUS_CACHE
|
||||
if (entry->d_type == DT_UNKNOWN) // filesystem does not supply d_type value
|
||||
{
|
||||
sf = symlink_sf = file_status(status_error);
|
||||
}
|
||||
else // filesystem supplies d_type value
|
||||
{
|
||||
if (entry->d_type == DT_DIR)
|
||||
sf = symlink_sf = file_status(directory_file);
|
||||
else if (entry->d_type == DT_REG)
|
||||
sf = symlink_sf = file_status(regular_file);
|
||||
else if (entry->d_type == DT_LNK)
|
||||
{
|
||||
sf = file_status(status_error);
|
||||
symlink_sf = file_status(symlink_file);
|
||||
}
|
||||
else sf = symlink_sf = file_status(status_error);
|
||||
}
|
||||
# else
|
||||
sf = symlink_sf = file_status(status_error);
|
||||
# endif
|
||||
return ok;
|
||||
}
|
||||
|
||||
// dir_itr_close is called both from the ~dir_itr_imp()destructor
|
||||
// and dir_itr_increment()
|
||||
std::error_code dir_itr_close( void *& handle, void *& buffer)
|
||||
{
|
||||
|
||||
std::free(buffer);
|
||||
buffer = 0;
|
||||
if (handle == 0)return ok;
|
||||
DIR * h(static_cast<DIR*>(handle));
|
||||
handle = 0;
|
||||
return std::error_code(::closedir(h)== 0 ? 0 : errno, std::system_category());
|
||||
|
||||
}
|
||||
|
||||
void directory_iterator_construct(directory_iterator& it,const path& p, std::error_code* ec)
|
||||
{
|
||||
if (error(p.empty() ? not_found_error_code.value() : 0, p, ec,
|
||||
"boost::filesystem::directory_iterator::construct"))
|
||||
return;
|
||||
|
||||
path::string_type filename;
|
||||
file_status file_stat, symlink_file_stat;
|
||||
std::error_code result = dir_itr_first(it.m_imp->handle,
|
||||
|
||||
it.m_imp->buffer,
|
||||
|
||||
p.c_str(), filename, file_stat, symlink_file_stat);
|
||||
|
||||
if (result)
|
||||
{
|
||||
it.m_imp.reset();
|
||||
error(result.value(), p,
|
||||
ec, "boost::filesystem::directory_iterator::construct");
|
||||
return;
|
||||
}
|
||||
|
||||
if (it.m_imp->handle == 0)
|
||||
it.m_imp.reset(); // eof, so make end iterator
|
||||
else // not eof
|
||||
{
|
||||
it.m_imp->dir_entry.assign(p / filename, file_stat, symlink_file_stat);
|
||||
if (filename[0] == dot // dot or dot-dot
|
||||
&& (filename.size()== 1
|
||||
|| (filename[1] == dot
|
||||
&& filename.size()== 2)))
|
||||
{ it.increment(*ec); }
|
||||
}
|
||||
}
|
||||
|
||||
void directory_iterator_increment(directory_iterator& it,std::error_code* ec)
|
||||
{
|
||||
assert(it.m_imp.get()&&"attempt to increment end iterator");
|
||||
assert(it.m_imp->handle != 0 && "internal program error");
|
||||
|
||||
path::string_type filename;
|
||||
file_status file_stat, symlink_file_stat;
|
||||
std::error_code temp_ec;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
temp_ec = dir_itr_increment(it.m_imp->handle,
|
||||
|
||||
it.m_imp->buffer,
|
||||
filename, file_stat, symlink_file_stat);
|
||||
|
||||
if (temp_ec) // happens if filesystem is corrupt, such as on a damaged optical disc
|
||||
{
|
||||
path error_path(it.m_imp->dir_entry.path().parent_path()); // fix ticket #5900
|
||||
it.m_imp.reset();
|
||||
if (ec == 0)
|
||||
//BOOST_FILESYSTEM_THROW(
|
||||
// filesystem_error("boost::filesystem::directory_iterator::operator++",
|
||||
// error_path,
|
||||
// error_code(FILESYSTEM_ERRNO, std::system_category())));
|
||||
throw filesystem_error("boost::filesystem::operator++");
|
||||
ec->assign(FILESYSTEM_ERRNO, std::system_category());
|
||||
return;
|
||||
}
|
||||
else if (ec != 0) ec->clear();
|
||||
|
||||
if (it.m_imp->handle == 0) // eof, make end
|
||||
{
|
||||
it.m_imp.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(filename[0] == dot // !(dot or dot-dot)
|
||||
&& (filename.size()== 1
|
||||
|| (filename[1] == dot
|
||||
&& filename.size()== 2))))
|
||||
{
|
||||
it.m_imp->dir_entry.replace_filename(
|
||||
filename, file_stat, symlink_file_stat);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace detail
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
@@ -0,0 +1,598 @@
|
||||
/**
|
||||
@file JCFileSystem.h
|
||||
@brief
|
||||
@author James
|
||||
@version 1.0
|
||||
@date 2016_5_11
|
||||
*/
|
||||
#ifndef __JCFileSystem_H__
|
||||
#define __JCFileSystem_H__
|
||||
|
||||
#include "../buffer/JCBuffer.h"
|
||||
|
||||
#ifdef WIN32
|
||||
#include <filesystem>
|
||||
namespace fs = std::experimental::filesystem::v1;
|
||||
#else
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
#include <cerrno>
|
||||
#include <cassert>
|
||||
#include <system_error>
|
||||
#endif
|
||||
namespace laya
|
||||
{
|
||||
|
||||
/** @brief 同步读取本地文件
|
||||
* @param[in] 文件名字
|
||||
* @param[out] 返回的buffer
|
||||
* @param[in] Encode是表示文件内容的原本的编码方式。即用这种编码方式去解释内容
|
||||
* @return 是否读取成功
|
||||
*/
|
||||
bool readFileSync(const char* p_pszFile, JCBuffer& p_buf, int p_nEncode = JCBuffer::raw);
|
||||
|
||||
|
||||
/** @brief 同步读取本地文件
|
||||
* @param[in] 文件名字
|
||||
* @param[in] Encode是表示文件内容的原本的编码方式。即用这种编码方式去解释内容
|
||||
* @return 返回字符串
|
||||
*/
|
||||
std::string readFileSync1(const char* p_pszFile, const char* p_pszEncode);
|
||||
|
||||
/** @brief 同步写本地文件
|
||||
* @param[in] 文件名字
|
||||
* @param[in] 返回的buffer
|
||||
* @param[in] Encode是要把文件保存成什么的编码方式
|
||||
* @return 是否写入成功
|
||||
*/
|
||||
bool writeFileSync(const char* p_pszFile, JCBuffer& p_buf, int p_nEncode = JCBuffer::raw);
|
||||
|
||||
/** @brief 写本地文件
|
||||
* @param[in] 文件名
|
||||
* @param[in] 写入的buffer
|
||||
* @param[in] 长度
|
||||
* @param[in] encodetype
|
||||
* @return 是否写入成功
|
||||
*/
|
||||
bool writeFileSync1(const char* p_pszFile, char* p_pBuff, int p_nLen, int p_nEncode = JCBuffer::raw);
|
||||
|
||||
typedef void(*GLOBAL_ONCREATEFILEERROR)();
|
||||
|
||||
extern GLOBAL_ONCREATEFILEERROR global_onCreateFileError;
|
||||
}
|
||||
#ifdef WIN32
|
||||
#else // __APPLE__ANDROID
|
||||
namespace fs
|
||||
{
|
||||
#define FILESYSTEM_BITMASK(Bitmask) \
|
||||
\
|
||||
inline Bitmask operator| (Bitmask x , Bitmask y ) \
|
||||
{ return static_cast<Bitmask>( static_cast<std::int_least32_t>(x) \
|
||||
| static_cast<std::int_least32_t>(y)); } \
|
||||
\
|
||||
inline Bitmask operator& (Bitmask x , Bitmask y ) \
|
||||
{ return static_cast<Bitmask>( static_cast<std::int_least32_t>(x) \
|
||||
& static_cast<std::int_least32_t>(y)); } \
|
||||
\
|
||||
inline Bitmask operator^ (Bitmask x , Bitmask y ) \
|
||||
{ return static_cast<Bitmask>( static_cast<std::int_least32_t>(x) \
|
||||
^ static_cast<std::int_least32_t>(y)); } \
|
||||
\
|
||||
inline Bitmask operator~ (Bitmask x ) \
|
||||
{ return static_cast<Bitmask>(~static_cast<std::int_least32_t>(x)); } \
|
||||
\
|
||||
inline Bitmask & operator&=(Bitmask & x , Bitmask y) \
|
||||
{ x = x & y ; return x ; } \
|
||||
\
|
||||
inline Bitmask & operator|=(Bitmask & x , Bitmask y) \
|
||||
{ x = x | y ; return x ; } \
|
||||
\
|
||||
inline Bitmask & operator^=(Bitmask & x , Bitmask y) \
|
||||
{ x = x ^ y ; return x ; }
|
||||
enum file_type
|
||||
{
|
||||
status_error,
|
||||
# ifndef BOOST_FILESYSTEM_NO_DEPRECATED
|
||||
status_unknown = status_error,
|
||||
# endif
|
||||
file_not_found,
|
||||
regular_file,
|
||||
directory_file,
|
||||
// the following may not apply to some operating systems or file systems
|
||||
symlink_file,
|
||||
block_file,
|
||||
character_file,
|
||||
fifo_file,
|
||||
socket_file,
|
||||
reparse_file, // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
|
||||
type_unknown, // file does exist, but isn't one of the above types or
|
||||
// we don't have strong enough permission to find its type
|
||||
|
||||
_detail_directory_symlink // internal use only; never exposed to users
|
||||
};
|
||||
enum perms
|
||||
{
|
||||
no_perms = 0, // file_not_found is no_perms rather than perms_not_known
|
||||
|
||||
// POSIX equivalent macros given in comments.
|
||||
// Values are from POSIX and are given in octal per the POSIX standard.
|
||||
|
||||
// permission bits
|
||||
|
||||
owner_read = 0400, // S_IRUSR, Read permission, owner
|
||||
owner_write = 0200, // S_IWUSR, Write permission, owner
|
||||
owner_exe = 0100, // S_IXUSR, Executearch permission, owner
|
||||
owner_all = 0700, // S_IRWXU, Read, write, executearch by owner
|
||||
|
||||
group_read = 040, // S_IRGRP, Read permission, group
|
||||
group_write = 020, // S_IWGRP, Write permission, group
|
||||
group_exe = 010, // S_IXGRP, Executearch permission, group
|
||||
group_all = 070, // S_IRWXG, Read, write, executearch by group
|
||||
|
||||
others_read = 04, // S_IROTH, Read permission, others
|
||||
others_write = 02, // S_IWOTH, Write permission, others
|
||||
others_exe = 01, // S_IXOTH, Executearch permission, others
|
||||
others_all = 07, // S_IRWXO, Read, write, executearch by others
|
||||
|
||||
all_all = 0777, // owner_all|group_all|others_all
|
||||
|
||||
// other POSIX bits
|
||||
|
||||
set_uid_on_exe = 04000, // S_ISUID, Set-user-ID on execution
|
||||
set_gid_on_exe = 02000, // S_ISGID, Set-group-ID on execution
|
||||
sticky_bit = 01000, // S_ISVTX,
|
||||
// (POSIX XSI) On directories, restricted deletion flag
|
||||
// (V7) 'sticky bit': save swapped text even after use
|
||||
// (SunOS) On non-directories: don't cache this file
|
||||
// (SVID-v4.2) On directories: restricted deletion flag
|
||||
// Also see http://en.wikipedia.org/wiki/Sticky_bit
|
||||
|
||||
perms_mask = 07777, // all_all|set_uid_on_exe|set_gid_on_exe|sticky_bit
|
||||
|
||||
perms_not_known = 0xFFFF, // present when directory_entry cache not loaded
|
||||
|
||||
// options for permissions() function
|
||||
|
||||
add_perms = 0x1000, // adds the given permission bits to the current bits
|
||||
remove_perms = 0x2000, // removes the given permission bits from the current bits;
|
||||
// choose add_perms or remove_perms, not both; if neither add_perms
|
||||
// nor remove_perms is given, replace the current bits with
|
||||
// the given bits.
|
||||
|
||||
symlink_perms = 0x4000 // on POSIX, don't resolve symlinks; implied on Windows
|
||||
};
|
||||
FILESYSTEM_BITMASK(perms)
|
||||
class file_status
|
||||
{
|
||||
public:
|
||||
file_status()
|
||||
: m_value(status_error), m_perms(perms_not_known) {}
|
||||
explicit file_status(file_type v)
|
||||
: m_value(v), m_perms(perms_not_known) {}
|
||||
file_status(file_type v, perms prms)
|
||||
: m_value(v), m_perms(prms) {}
|
||||
|
||||
// As of October 2015 the interaction between noexcept and =default is so troublesome
|
||||
// for VC++, GCC, and probably other compilers, that =default is not used with noexcept
|
||||
// functions. GCC is not even consistent for the same release on different platforms.
|
||||
|
||||
file_status(const file_status& rhs)
|
||||
: m_value(rhs.m_value), m_perms(rhs.m_perms) {}
|
||||
file_status& operator=(const file_status& rhs)
|
||||
{
|
||||
m_value = rhs.m_value;
|
||||
m_perms = rhs.m_perms;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// observers
|
||||
file_type type() const { return m_value; }
|
||||
perms permissions() const { return m_perms; }
|
||||
|
||||
// modifiers
|
||||
void type(file_type v) { m_value = v; }
|
||||
void permissions(perms prms) { m_perms = prms; }
|
||||
|
||||
bool operator==(const file_status& rhs) const
|
||||
{
|
||||
return type() == rhs.type() &&
|
||||
permissions() == rhs.permissions();
|
||||
}
|
||||
bool operator!=(const file_status& rhs) const
|
||||
{
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
file_type m_value;
|
||||
enum perms m_perms;
|
||||
};
|
||||
class filesystem_error
|
||||
{
|
||||
public:
|
||||
filesystem_error(const std::string & what_arg)
|
||||
{
|
||||
}
|
||||
const char* what()
|
||||
{
|
||||
return m_what.c_str();
|
||||
}
|
||||
private:
|
||||
mutable std::string m_what;
|
||||
};
|
||||
|
||||
class path
|
||||
{
|
||||
public:
|
||||
typedef char value_type;
|
||||
static const value_type separator = '/';
|
||||
static const value_type preferred_separator = '/';
|
||||
static const value_type dot = '.';
|
||||
typedef std::basic_string<value_type> string_type;
|
||||
|
||||
path() {}
|
||||
path(const path& p) : m_pathname(p.m_pathname) {}
|
||||
path(const value_type* s) : m_pathname(s) {}
|
||||
path(value_type* s) : m_pathname(s) {}
|
||||
path(const string_type& s) : m_pathname(s) {}
|
||||
path(string_type& s) : m_pathname(s) {}
|
||||
|
||||
path& remove_filename();
|
||||
path filename() const;
|
||||
void clear() { m_pathname.clear(); }
|
||||
const value_type* c_str() const { return m_pathname.c_str(); }
|
||||
const std::string& generic_string() const { return m_pathname; }
|
||||
|
||||
path& operator=(const path& p)
|
||||
{
|
||||
m_pathname = p.m_pathname;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// value_type overloads
|
||||
|
||||
path& operator=(const value_type* ptr) // required in case ptr overlaps *this
|
||||
{
|
||||
m_pathname = ptr; return *this;
|
||||
}
|
||||
path& operator=(value_type* ptr) // required in case ptr overlaps *this
|
||||
{
|
||||
m_pathname = ptr; return *this;
|
||||
}
|
||||
path& operator=(const string_type& s) { m_pathname = s; return *this; }
|
||||
path& operator=(string_type& s) { m_pathname = s; return *this; }
|
||||
|
||||
// value_type overloads. Same rationale as for constructors above
|
||||
path& operator+=(const path& p) { m_pathname += p.m_pathname; return *this; }
|
||||
path& operator+=(const value_type* ptr) { m_pathname += ptr; return *this; }
|
||||
path& operator+=(value_type* ptr) { m_pathname += ptr; return *this; }
|
||||
path& operator+=(const string_type& s) { m_pathname += s; return *this; }
|
||||
path& operator+=(string_type& s) { m_pathname += s; return *this; }
|
||||
path& operator+=(value_type c) { m_pathname += c; return *this; }
|
||||
|
||||
path& operator/=(const path& p);
|
||||
|
||||
path& operator/=(const value_type* ptr);
|
||||
path& operator/=(value_type* ptr)
|
||||
{
|
||||
return this->operator/=(const_cast<const value_type*>(ptr));
|
||||
}
|
||||
path& operator/=(const string_type& s) { return this->operator/=(path(s)); }
|
||||
path& operator/=(string_type& s) { return this->operator/=(path(s)); }
|
||||
|
||||
bool empty() const { return m_pathname.empty(); }
|
||||
string_type::size_type m_append_separator_if_needed();
|
||||
|
||||
bool filename_is_dot() const;
|
||||
bool filename_is_dot_dot() const;
|
||||
string_type::size_type size() const { return m_pathname.size(); }
|
||||
path parent_path() const;
|
||||
private:
|
||||
string_type m_pathname;
|
||||
string_type::size_type m_parent_path_end() const;
|
||||
|
||||
};
|
||||
/*inline bool operator==(const path& lhs, const path& rhs) {return lhs.compare(rhs) == 0;}
|
||||
inline bool operator==(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) == 0;}
|
||||
inline bool operator==(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
|
||||
inline bool operator==(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) == 0;}
|
||||
inline bool operator==(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) == 0;}
|
||||
|
||||
inline bool operator!=(const path& lhs, const path& rhs) {return lhs.compare(rhs) != 0;}
|
||||
inline bool operator!=(const path& lhs, const path::string_type& rhs) {return lhs.compare(rhs) != 0;}
|
||||
inline bool operator!=(const path::string_type& lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
|
||||
inline bool operator!=(const path& lhs, const path::value_type* rhs) {return lhs.compare(rhs) != 0;}
|
||||
inline bool operator!=(const path::value_type* lhs, const path& rhs) {return rhs.compare(lhs) != 0;}
|
||||
|
||||
// TODO: why do == and != have additional overloads, but the others don't?
|
||||
|
||||
inline bool operator<(const path& lhs, const path& rhs) {return lhs.compare(rhs) < 0;}
|
||||
inline bool operator<=(const path& lhs, const path& rhs) {return !(rhs < lhs);}
|
||||
inline bool operator> (const path& lhs, const path& rhs) {return rhs < lhs;}
|
||||
inline bool operator>=(const path& lhs, const path& rhs) {return !(lhs < rhs);}*/
|
||||
namespace detail
|
||||
{
|
||||
|
||||
inline bool is_element_separator(path::value_type c)
|
||||
{
|
||||
return c == path::separator;
|
||||
}
|
||||
}
|
||||
inline path operator/(const path& lhs, const path& rhs) { return path(lhs) /= rhs; }
|
||||
inline bool is_directory_separator(path::value_type c)
|
||||
{
|
||||
return c == path::separator;
|
||||
}
|
||||
|
||||
file_status status(const path&p, std::error_code* ec = 0);
|
||||
|
||||
inline bool exists(file_status f)
|
||||
{
|
||||
return f.type() != status_error
|
||||
&& f.type() != file_not_found;
|
||||
}
|
||||
inline bool exists(const path& p) { return exists(status(p)); }
|
||||
|
||||
inline bool is_regular_file(file_status f)
|
||||
{
|
||||
return f.type() == regular_file;
|
||||
}
|
||||
inline bool is_directory(file_status f)
|
||||
{
|
||||
return f.type() == directory_file;
|
||||
}
|
||||
inline bool is_directory(const path& p) { return is_directory(status(p)); }
|
||||
inline bool is_regular_file(const path& p) { return is_regular_file(status(p)); }
|
||||
bool create_directories(const path& p, std::error_code* ec = 0);
|
||||
bool remove(const path& p, std::error_code* ec = 0);
|
||||
std::time_t last_write_time(const path& p, std::error_code* ec = 0);
|
||||
std::uintmax_t file_size(const path& p, std::error_code* ec = 0);
|
||||
std::uintmax_t remove_all(const path& p, std::error_code* ec = 0);
|
||||
inline bool path::filename_is_dot() const
|
||||
{
|
||||
// implicit dot is tricky, so actually call filename(); see path::filename() example
|
||||
// in reference.html
|
||||
path p(filename());
|
||||
return p.size() == 1 && *p.c_str() == dot;
|
||||
}
|
||||
|
||||
inline bool path::filename_is_dot_dot() const
|
||||
{
|
||||
return size() >= 2 && m_pathname[size() - 1] == dot && m_pathname[size() - 2] == dot
|
||||
&& (m_pathname.size() == 2 || detail::is_element_separator(m_pathname[size() - 3]));
|
||||
// use detail::is_element_separator() rather than detail::is_directory_separator
|
||||
// to deal with "c:.." edge case on Windows when ':' acts as a separator
|
||||
}
|
||||
//--------------------------------------------------------------------------------------//
|
||||
// //
|
||||
// directory_entry //
|
||||
// //
|
||||
//--------------------------------------------------------------------------------------//
|
||||
|
||||
// GCC has a problem with a member function named path within a namespace or
|
||||
// sub-namespace that also has a class named path. The workaround is to always
|
||||
// fully qualify the name path when it refers to the class name.
|
||||
|
||||
class directory_entry
|
||||
{
|
||||
public:
|
||||
typedef fs::path::value_type value_type; // enables class path ctor taking directory_entry
|
||||
|
||||
directory_entry() {}
|
||||
explicit directory_entry(const fs::path& p)
|
||||
: m_path(p), m_status(file_status()), m_symlink_status(file_status())
|
||||
{}
|
||||
directory_entry(const fs::path& p,
|
||||
file_status st, file_status symlink_st = file_status())
|
||||
: m_path(p), m_status(st), m_symlink_status(symlink_st) {}
|
||||
|
||||
directory_entry(const directory_entry& rhs)
|
||||
: m_path(rhs.m_path), m_status(rhs.m_status), m_symlink_status(rhs.m_symlink_status) {}
|
||||
|
||||
directory_entry& operator=(const directory_entry& rhs)
|
||||
{
|
||||
m_path = rhs.m_path;
|
||||
m_status = rhs.m_status;
|
||||
m_symlink_status = rhs.m_symlink_status;
|
||||
return *this;
|
||||
}
|
||||
// As of October 2015 the interaction between noexcept and =default is so troublesome
|
||||
// for VC++, GCC, and probably other compilers, that =default is not used with noexcept
|
||||
// functions. GCC is not even consistent for the same release on different platforms.
|
||||
|
||||
/*#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
directory_entry(directory_entry&& rhs)
|
||||
{
|
||||
m_path = std::move(rhs.m_path);
|
||||
m_status = std::move(rhs.m_status);
|
||||
m_symlink_status = std::move(rhs.m_symlink_status);
|
||||
}
|
||||
directory_entry& operator=(directory_entry&& rhs)
|
||||
{
|
||||
m_path = std::move(rhs.m_path);
|
||||
m_status = std::move(rhs.m_status);
|
||||
m_symlink_status = std::move(rhs.m_symlink_status);
|
||||
return *this;
|
||||
}
|
||||
#endif*/
|
||||
|
||||
void assign(const fs::path& p,file_status st = file_status(), file_status symlink_st = file_status())
|
||||
{
|
||||
m_path = p; m_status = st; m_symlink_status = symlink_st;
|
||||
}
|
||||
|
||||
void replace_filename(const fs::path& p,file_status st = file_status(), file_status symlink_st = file_status())
|
||||
{
|
||||
m_path.remove_filename();
|
||||
m_path /= p;
|
||||
m_status = st;
|
||||
m_symlink_status = symlink_st;
|
||||
}
|
||||
|
||||
# ifndef BOOST_FILESYSTEM_NO_DEPRECATED
|
||||
void replace_leaf(const fs::path& p,file_status st, file_status symlink_st)
|
||||
{
|
||||
replace_filename(p, st, symlink_st);
|
||||
}
|
||||
# endif
|
||||
|
||||
const fs::path& path() const { return m_path; }
|
||||
operator const fs::path&() const { return m_path; }
|
||||
file_status status() const { return m_get_status(); }
|
||||
file_status status(std::error_code& ec) const
|
||||
{
|
||||
return m_get_status(&ec);
|
||||
}
|
||||
file_status symlink_status() const { return m_get_symlink_status(); }
|
||||
file_status symlink_status(std::error_code& ec) const
|
||||
{
|
||||
return m_get_symlink_status(&ec);
|
||||
}
|
||||
|
||||
/*bool operator==(const directory_entry& rhs) const {return m_path == rhs.m_path; }
|
||||
bool operator!=(const directory_entry& rhs) const {return m_path != rhs.m_path;}
|
||||
bool operator< (const directory_entry& rhs) const {return m_path < rhs.m_path;}
|
||||
bool operator<=(const directory_entry& rhs) const {return m_path <= rhs.m_path;}
|
||||
bool operator> (const directory_entry& rhs) const {return m_path > rhs.m_path;}
|
||||
bool operator>=(const directory_entry& rhs) const {return m_path >= rhs.m_path;}*/
|
||||
|
||||
private:
|
||||
fs::path m_path;
|
||||
mutable file_status m_status; // stat()-like
|
||||
mutable file_status m_symlink_status; // lstat()-like
|
||||
|
||||
file_status m_get_status(std::error_code* ec = 0) const;
|
||||
file_status m_get_symlink_status(std::error_code* ec = 0) const;
|
||||
};
|
||||
// directory_entry
|
||||
//--------------------------------------------------------------------------------------//
|
||||
// //
|
||||
// directory_iterator helpers //
|
||||
// //
|
||||
//--------------------------------------------------------------------------------------//
|
||||
class directory_iterator;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
std::error_code dir_itr_close(void *& handle, void *& buffer);
|
||||
|
||||
struct dir_itr_imp
|
||||
{
|
||||
directory_entry dir_entry;
|
||||
void* handle;
|
||||
void* buffer; // see dir_itr_increment implementation
|
||||
dir_itr_imp() : handle(0), buffer(0)
|
||||
{}
|
||||
|
||||
~dir_itr_imp() // never throws
|
||||
{
|
||||
dir_itr_close(handle, buffer);
|
||||
}
|
||||
};
|
||||
|
||||
// see path::iterator: comment below
|
||||
void directory_iterator_construct(directory_iterator& it,
|
||||
const path& p, std::error_code* ec);
|
||||
void directory_iterator_increment(directory_iterator& it,
|
||||
std::error_code* ec);
|
||||
|
||||
} // namespace detail
|
||||
//--------------------------------------------------------------------------------------//
|
||||
// //
|
||||
// directory_iterator //
|
||||
// //
|
||||
//--------------------------------------------------------------------------------------//
|
||||
|
||||
class directory_iterator
|
||||
{
|
||||
public:
|
||||
|
||||
directory_iterator() {} // creates the "end" iterator
|
||||
|
||||
// iterator_facade derived classes don't seem to like implementations in
|
||||
// separate translation unit dll's, so forward to detail functions
|
||||
explicit directory_iterator(const path& p)
|
||||
: m_imp(new detail::dir_itr_imp)
|
||||
{
|
||||
detail::directory_iterator_construct(*this, p, 0);
|
||||
}
|
||||
directory_iterator(const path& p, std::error_code& ec)
|
||||
: m_imp(new detail::dir_itr_imp)
|
||||
{
|
||||
detail::directory_iterator_construct(*this, p, &ec);
|
||||
}
|
||||
|
||||
~directory_iterator() {}
|
||||
directory_iterator& increment(std::error_code& ec)
|
||||
{
|
||||
detail::directory_iterator_increment(*this, &ec);
|
||||
return *this;
|
||||
}
|
||||
const directory_entry& operator*() const
|
||||
{
|
||||
return this->dereference();
|
||||
}
|
||||
const directory_entry * operator->() const
|
||||
{
|
||||
return std::addressof(this->dereference());
|
||||
}
|
||||
|
||||
directory_iterator & operator++()
|
||||
{
|
||||
this->increment();
|
||||
return *this;
|
||||
}
|
||||
|
||||
directory_iterator operator++(int)
|
||||
{
|
||||
directory_iterator temp = *this;
|
||||
this->operator++();
|
||||
return temp;
|
||||
}
|
||||
|
||||
bool operator==(directory_iterator const& r)
|
||||
{
|
||||
return this->equal(r);
|
||||
}
|
||||
bool operator!=(directory_iterator const& r)
|
||||
{
|
||||
return !this->equal(r);
|
||||
}
|
||||
private:
|
||||
friend struct detail::dir_itr_imp;
|
||||
friend void detail::directory_iterator_construct(directory_iterator& it,
|
||||
const path& p, std::error_code* ec);
|
||||
friend void detail::directory_iterator_increment(directory_iterator& it,
|
||||
std::error_code* ec);
|
||||
|
||||
// shared_ptr provides the shallow-copy semantics required for single pass iterators
|
||||
// (i.e. InputIterators). The end iterator is indicated by !m_imp || !m_imp->handle
|
||||
std::shared_ptr< detail::dir_itr_imp > m_imp;
|
||||
|
||||
//friend class boost::iterator_core_access;
|
||||
|
||||
const directory_entry& dereference() const
|
||||
{
|
||||
assert(m_imp.get() && "attempt to dereference end iterator");
|
||||
return m_imp->dir_entry;
|
||||
}
|
||||
|
||||
void increment() { detail::directory_iterator_increment(*this, 0); }
|
||||
|
||||
bool equal(const directory_iterator& rhs) const
|
||||
{
|
||||
return m_imp == rhs.m_imp
|
||||
|| (!m_imp && rhs.m_imp && !rhs.m_imp->handle)
|
||||
|| (!rhs.m_imp && m_imp && !m_imp->handle);
|
||||
}
|
||||
|
||||
}; // directory_iterator
|
||||
}
|
||||
#endif
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#endif //__JCFileSystem_H__
|
||||
|
||||
//-----------------------------END FILE--------------------------------
|
||||
Reference in New Issue
Block a user