use UTF-8 for string representation

This commit is contained in:
Jinhao
2015-12-31 01:09:52 +08:00
parent 0a396c12c2
commit a42ebe19b4
38 changed files with 182 additions and 416 deletions

View File

@@ -237,28 +237,6 @@ namespace nana { namespace experimental {
return rmdir(dir, true);
}
bool mkdir_helper(const nana::string& dir, bool & if_exist)
{
#if defined(NANA_WINDOWS)
if (::CreateDirectory(dir.c_str(), 0))
{
if_exist = false;
return true;
}
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_exist = false;
return true;
}
if_exist = (errno == EEXIST);
#endif
return false;
}
#if defined(NANA_WINDOWS)
void filetime_to_c_tm(FILETIME& ft, struct tm& t)
{
@@ -513,13 +491,34 @@ namespace nana { namespace experimental {
}
return buf;
}
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
auto pstr = ::getenv("PWD");
#elif defined(NANA_POSIX)
char buf[260];
auto pstr = ::getcwd(buf, 260);
if (pstr)
return pstr;
int bytes = 260 + 260;
while (ERANGE == errno)
{
std::unique_ptr<char[]> buf(new char[bytes]);
auto pstr = ::getcwd(buf.get(), bytes);
if (pstr)
return path(pstr);
bytes += 260;
}
#endif
return path();
}
void current_path(const path& p)
{
#if defined(NANA_WINDOWS)
::SetCurrentDirectoryW(p.c_str());
#elif defined(NANA_POSIX)
::chdir(p.c_str());
#endif
}
}//end namespace filesystem
} //end namespace experimental
}//end namespace nana

View File

@@ -51,80 +51,6 @@ namespace filesystem
const wchar_t* splstr = L"/\\";
#endif
/*
//class path
path::path(){}
path::path(const nana::string& text)
#if defined(NANA_WINDOWS)
:text_(text)
{
#else
:text_(nana::charset(text))
{
#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);
}
bool path::empty() const
{
#if defined(NANA_WINDOWS)
return (::GetFileAttributes(text_.c_str()) == INVALID_FILE_ATTRIBUTES);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
struct stat sta;
return (::stat(text_.c_str(), &sta) == -1);
#endif
}
path path::root() 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
}
int path::what() const
{
#if defined(NANA_WINDOWS)
unsigned long attr = ::GetFileAttributes(text_.c_str());
if(INVALID_FILE_ATTRIBUTES == attr)
return type::not_exist;
if(FILE_ATTRIBUTE_DIRECTORY & attr)
return type::directory;
return type::file;
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
struct stat sta;
if(-1 == ::stat(text_.c_str(), &sta))
return type::not_exist;
if((S_IFDIR & sta.st_mode) == S_IFDIR)
return type::directory;
if((S_IFREG & sta.st_mode) == S_IFREG)
return type::file;
return type::not_exist;
#endif
}
nana::string path::name() 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
}
//end class path
*/
namespace detail
{
//rm_dir_recursive
@@ -148,28 +74,6 @@ namespace filesystem
return rmdir(dir.c_str(), true);
}
bool mkdir_helper(const std::string& dir, bool & if_exist)
{
#if defined(NANA_WINDOWS)
if(::CreateDirectory(utf8_cast(dir).c_str(), 0))
{
if_exist = false;
return true;
}
if_exist = (::GetLastError() == ERROR_ALREADY_EXISTS);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
if(0 == ::mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
{
if_exist = false;
return true;
}
if_exist = (errno == EEXIST);
#endif
return false;
}
#if defined(NANA_WINDOWS)
void filetime_to_c_tm(FILETIME& ft, struct tm& t)
{
@@ -269,36 +173,10 @@ namespace filesystem
return ret;
}
nana::string root(const nana::string& path)
{
std::size_t index = path.size();
if(index)
{
const nana::char_t * str = path.c_str();
for(--index; index > 0; --index)
{
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;
}
}
return index?path.substr(0, index + 1):nana::string();
}
nana::string path_user()
std::wstring path_user()
{
#if defined(NANA_WINDOWS)
nana::char_t path[MAX_PATH];
wchar_t path[MAX_PATH];
if(SUCCEEDED(SHGetFolderPath(0, CSIDL_PROFILE, 0, SHGFP_TYPE_CURRENT, path)))
return path;
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
@@ -306,32 +184,7 @@ namespace filesystem
if(s)
return nana::charset(std::string(s, std::strlen(s)), nana::unicode::utf8);
#endif
return nana::string();
}
nana::string path_current()
{
#if defined(NANA_WINDOWS)
nana::char_t buf[MAX_PATH];
DWORD len = ::GetCurrentDirectory(MAX_PATH, buf);
if(len)
{
if(len > MAX_PATH)
{
nana::char_t * p = new nana::char_t[len + 1];
::GetCurrentDirectory(len + 1, p);
nana::string s = p;
delete [] p;
return s;
}
return buf;
}
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
const char * s = ::getenv("PWD");
if(s)
return nana::charset(std::string(s, std::strlen(s)), nana::unicode::utf8);
#endif
return nana::string();
return std::wstring();
}
}//end namespace filesystem
}//end namespace nana