remove macro STR

This commit is contained in:
Jinhao
2015-11-29 22:38:22 +08:00
parent 12358a5dc0
commit c86a00bea5
31 changed files with 410 additions and 331 deletions

View File

@@ -24,12 +24,12 @@ namespace filesystem
{}
#if defined(NANA_WINDOWS)
fileinfo::fileinfo(const WIN32_FIND_DATA& wfd)
: name(wfd.cFileName), size(wfd.nFileSizeLow),
: name(utf8_cast(wfd.cFileName)), size(wfd.nFileSizeLow),
directory((FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
{
}
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
fileinfo::fileinfo(const nana::string& name, const struct stat& fst)
fileinfo::fileinfo(const ::std::string& name, const struct stat& fst)
:name(name), size(fst.st_size), directory(0 != S_ISDIR(fst.st_mode))
{
}

View File

@@ -43,51 +43,50 @@ namespace nana {
{
//Because of No wide character version of POSIX
#if defined(NANA_LINUX) || defined(NANA_MACOS)
typedef std::string string_t;
const char* splstr = "/\\";
const char* splstr = "/";
#else
typedef nana::string string_t;
const nana::char_t* splstr = STR("/\\");
const wchar_t* splstr = L"/\\";
#endif
//class path
path::path() {}
path::path(const nana::string& text)
#if defined(NANA_WINDOWS)
: text_(text)
path::path(const value_type* source)
: path(string_type{ source })
{}
path::path(const string_type& source)
: pathstr_(source)
{
#else
:text_(nana::charset(text))
auto pos = pathstr_.find_last_of(splstr);
for (; (pos != string_type::npos) && (pos + 1 == pathstr_.size()); pos = pathstr_.find_last_of(splstr))
pathstr_.erase(pos);
}
int path::compare(const path& p) const
{
#endif
auto pos = text_.find_last_of(splstr);
for (; (pos != string_t::npos) && (pos + 1 == text_.size()); pos = text_.find_last_of(splstr))
text_.erase(pos);
return pathstr_.compare(p.pathstr_);
}
bool path::empty() const
{
#if defined(NANA_WINDOWS)
return (::GetFileAttributes(text_.c_str()) == INVALID_FILE_ATTRIBUTES);
return (::GetFileAttributes(pathstr_.c_str()) == INVALID_FILE_ATTRIBUTES);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
struct stat sta;
return (::stat(text_.c_str(), &sta) == -1);
return (::stat(pathstr_.c_str(), &sta) == -1);
#endif
}
path path::root() const
path path::parent_path() const
{
#if defined(NANA_WINDOWS)
return path(filesystem::root(text_));
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
return path(filesystem::root(nana::charset(text_)));
#endif
return{filesystem::parent_path(pathstr_)};
}
file_type path::what() const
{
#if defined(NANA_WINDOWS)
unsigned long attr = ::GetFileAttributes(text_.c_str());
unsigned long attr = ::GetFileAttributes(pathstr_.c_str());
if (INVALID_FILE_ATTRIBUTES == attr)
return file_type::not_found; //??
@@ -97,7 +96,7 @@ namespace nana {
return file_type::regular;
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
struct stat sta;
if (-1 == ::stat(text_.c_str(), &sta))
if (-1 == ::stat(pathstr_.c_str(), &sta))
return file_type::not_found; //??
if ((S_IFDIR & sta.st_mode) == S_IFDIR)
@@ -110,38 +109,85 @@ namespace nana {
#endif
}
nana::string path::filename() const
path path::filename() const
{
string_t::size_type pos = text_.find_last_of(splstr);
#if defined(NANA_WINDOWS)
return text_.substr(pos + 1);
#else
return nana::charset(text_.substr(pos + 1));
#endif
auto pos = pathstr_.find_last_of(splstr);
if (pos != pathstr_.npos)
{
if (pos + 1 == pathstr_.size())
{
value_type tmp[2] = {preferred_separator, 0};
if (pathstr_.npos != pathstr_.find_last_not_of(splstr, pos))
tmp[0] = '.';
return{ tmp };
}
return{ pathstr_.substr(pos + 1) };
}
return{ pathstr_ };
}
const path::value_type* path::c_str() const
{
return native().c_str();
}
const path::string_type& path::native() const
{
return pathstr_;
}
path::operator string_type() const
{
return native();
}
//end class path
bool operator==(const path& lhs, const path& rhs)
{
return (lhs.compare(rhs) == 0);
}
bool operator!=(const path& lhs, const path& rhs)
{
return (lhs.native() != rhs.native());
}
bool operator<(const path& lhs, const path& rhs)
{
return (lhs.compare(rhs) < 0);
}
bool operator>(const path& lhs, const path& rhs)
{
return (rhs.compare(lhs) < 0);
}
namespace detail
{
//rm_dir_recursive
//@brief: remove a directory, if it is not empty, recursively remove it's subfiles and sub directories
bool rm_dir_recursive(nana::string&& dir)
template<typename CharT>
bool rm_dir_recursive(const CharT* dir)
{
std::vector<directory_iterator::value_type> files;
nana::string path = dir;
std::basic_string<CharT> path = dir;
path += '\\';
std::copy(directory_iterator(dir), directory_iterator(), std::back_inserter(files));
for (auto & f : files)
{
auto subpath = path + f.path().filename().native();
if (f.attr.directory)
rm_dir_recursive(path + f.path().filename());
rm_dir_recursive(subpath.c_str());
else
rmfile((path + f.path().filename()).c_str());
rmfile(subpath.c_str());
}
return rmdir(dir.c_str(), true);
return rmdir(dir, true);
}
bool mkdir_helper(const nana::string& dir, bool & if_exist)
@@ -299,7 +345,7 @@ namespace nana {
nana::string root;
#if defined(NANA_WINDOWS)
if (path.size() > 3 && path[1] == STR(':'))
if (path.size() > 3 && path[1] == L':')
root = path.substr(0, 3);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
if (path[0] == STR('/'))
@@ -310,11 +356,11 @@ namespace nana {
while (true)
{
beg = path.find_first_not_of(STR("/\\"), beg);
beg = path.find_first_not_of(L"/\\", beg);
if (beg == path.npos)
break;
std::size_t pos = path.find_first_of(STR("/\\"), beg + 1);
std::size_t pos = path.find_first_of(L"/\\", beg + 1);
if (pos != path.npos)
{
root += path.substr(beg, pos - beg);
@@ -324,9 +370,9 @@ namespace nana {
return false;
#if defined(NANA_WINDOWS)
root += STR('\\');
root += L'\\';
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
root += STR('/');
root += L'/';
#endif
}
else
@@ -362,18 +408,18 @@ namespace nana {
#endif
}
bool rmdir(const nana::char_t* dir, bool fails_if_not_empty)
bool rmdir(const char* dir_utf8, bool fails_if_not_empty)
{
bool ret = false;
if (dir)
if (dir_utf8)
{
#if defined(NANA_WINDOWS)
ret = (::RemoveDirectory(dir) == TRUE);
auto dir = utf8_cast(dir_utf8);
ret = (::RemoveDirectory(dir.c_str()) == TRUE);
if (!fails_if_not_empty && (::GetLastError() == ERROR_DIR_NOT_EMPTY))
ret = detail::rm_dir_recursive(dir);
ret = detail::rm_dir_recursive(dir.c_str());
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
std::string mbstr = nana::charset(dir);
if (::rmdir(mbstr.c_str()))
if (::rmdir(dir_utf8))
{
if (!fails_if_not_empty && (errno == EEXIST || errno == ENOTEMPTY))
ret = detail::rm_dir_recursive(dir);
@@ -385,30 +431,26 @@ namespace nana {
return ret;
}
nana::string root(const nana::string& path)
bool rmdir(const wchar_t* dir, bool fails_if_not_empty)
{
std::size_t index = path.size();
if (index)
bool ret = false;
if (dir)
{
const nana::char_t * str = path.c_str();
for (--index; index > 0; --index)
#if defined(NANA_WINDOWS)
ret = (::RemoveDirectory(dir) == TRUE);
if (!fails_if_not_empty && (::GetLastError() == ERROR_DIR_NOT_EMPTY))
ret = detail::rm_dir_recursive(dir);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
if (::rmdir(utf8_cast(dir).c_str()))
{
nana::char_t c = str[index];
if (c != '\\' && c != '/')
break;
}
for (--index; index > 0; --index)
{
nana::char_t c = str[index];
if (c == '\\' || c == '/')
break;
if (!fails_if_not_empty && (errno == EEXIST || errno == ENOTEMPTY))
ret = detail::rm_dir_recursive(dir);
}
else
ret = true;
#endif
}
return index ? path.substr(0, index + 1) : nana::string();
return ret;
}
nana::string path_user()

View File

@@ -13,6 +13,7 @@
#include <nana/filesystem/fs_utility.hpp>
#include <nana/filesystem/file_iterator.hpp>
#include <nana/deploy.hpp>
#include <vector>
#if defined(NANA_WINDOWS)
#include <windows.h>
@@ -47,7 +48,7 @@ namespace filesystem
const char* splstr = "/\\";
#else
typedef nana::string string_t;
const nana::char_t* splstr = STR("/\\");
const nana::char_t* splstr = L"/\\";
#endif
//class path
path::path(){}
@@ -125,10 +126,10 @@ namespace filesystem
{
//rm_dir_recursive
//@brief: remove a directory, if it is not empty, recursively remove it's subfiles and sub directories
bool rm_dir_recursive(nana::string&& dir)
bool rm_dir_recursive(std::string&& dir)
{
std::vector<file_iterator::value_type> files;
nana::string path = dir;
auto path = dir;
path += '\\';
std::copy(file_iterator(dir), file_iterator(), std::back_inserter(files));
@@ -144,10 +145,10 @@ namespace filesystem
return rmdir(dir.c_str(), true);
}
bool mkdir_helper(const nana::string& dir, bool & if_exist)
bool mkdir_helper(const std::string& dir, bool & if_exist)
{
#if defined(NANA_WINDOWS)
if(::CreateDirectory(dir.c_str(), 0))
if(::CreateDirectory(utf8_cast(dir).c_str(), 0))
{
if_exist = false;
return true;
@@ -155,7 +156,7 @@ namespace filesystem
if_exist = (::GetLastError() == ERROR_ALREADY_EXISTS);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
if(0 == ::mkdir(static_cast<std::string>(nana::charset(dir)).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
if(0 == ::mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
{
if_exist = false;
return true;
@@ -288,14 +289,14 @@ namespace filesystem
return false;
}
bool mkdir(const nana::string& path, bool & if_exist)
bool mkdir(const std::string& path, bool & if_exist)
{
if_exist = false;
if(path.size() == 0) return false;
nana::string root;
std::string root;
#if defined(NANA_WINDOWS)
if(path.size() > 3 && path[1] == STR(':'))
if(path.size() > 3 && path[1] == ':')
root = path.substr(0, 3);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
if(path[0] == STR('/'))
@@ -306,11 +307,11 @@ namespace filesystem
while(true)
{
beg = path.find_first_not_of(STR("/\\"), beg);
beg = path.find_first_not_of("/\\", beg);
if(beg == path.npos)
break;
std::size_t pos = path.find_first_of(STR("/\\"), beg + 1);
std::size_t pos = path.find_first_of("/\\", beg + 1);
if(pos != path.npos)
{
root += path.substr(beg, pos - beg);
@@ -320,9 +321,9 @@ namespace filesystem
return false;
#if defined(NANA_WINDOWS)
root += STR('\\');
root += L'\\';
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
root += STR('/');
root += L'/';
#endif
}
else
@@ -339,32 +340,32 @@ namespace filesystem
return mkstat;
}
bool rmfile(const nana::char_t* file)
bool rmfile(const char* file)
{
#if defined(NANA_WINDOWS)
bool ret = false;
if(file)
{
ret = (::DeleteFile(file) == TRUE);
ret = (::DeleteFile(utf8_cast(file).c_str()) == TRUE);
if(!ret)
ret = (ERROR_FILE_NOT_FOUND == ::GetLastError());
}
return ret;
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
if(std::remove(static_cast<std::string>(nana::charset(file)).c_str()))
if(std::remove(file))
return (errno == ENOENT);
return true;
#endif
}
bool rmdir(const nana::char_t* dir, bool fails_if_not_empty)
bool rmdir(const char* dir, bool fails_if_not_empty)
{
bool ret = false;
if(dir)
{
#if defined(NANA_WINDOWS)
ret = (::RemoveDirectory(dir) == TRUE);
ret = (::RemoveDirectory(utf8_cast(dir).c_str()) == TRUE);
if(!fails_if_not_empty && (::GetLastError() == ERROR_DIR_NOT_EMPTY))
ret = detail::rm_dir_recursive(dir);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)