diff --git a/include/nana/filesystem/filesystem.hpp b/include/nana/filesystem/filesystem.hpp index 2acd5a46..12387ede 100644 --- a/include/nana/filesystem/filesystem.hpp +++ b/include/nana/filesystem/filesystem.hpp @@ -100,7 +100,7 @@ namespace filesystem uintmax_t available; }; - using file_time_type = std::chrono::time_point< std::chrono::system_clock>;// trivial-clock> ; + using file_time_type = std::chrono::time_point;// trivial-clock> ; class file_status { @@ -415,7 +415,6 @@ namespace filesystem file_status status(const path& p); - bool file_attrib(const nana::string& file, attribute&); inline bool is_directory(file_status s) { return s.type() == file_type::directory ;} bool is_directory(const path& p); diff --git a/include/nana/filesystem/fs_utility.hpp b/include/nana/filesystem/fs_utility.hpp index 8723a5f4..6beb9b73 100644 --- a/include/nana/filesystem/fs_utility.hpp +++ b/include/nana/filesystem/fs_utility.hpp @@ -24,10 +24,6 @@ namespace filesystem tm modified; }; - bool file_attrib(const ::std::string& file, attribute&); - //long long filesize(const nana::string& file); //deprecated - - //bool mkdir(const ::std::string& dir, bool & if_exist); //deprecated bool modified_file_time(const ::std::string& file, struct tm&); std::wstring path_user(); diff --git a/source/filesystem/filesystem.cpp b/source/filesystem/filesystem.cpp index 5567fa07..3cd01865 100644 --- a/source/filesystem/filesystem.cpp +++ b/source/filesystem/filesystem.cpp @@ -281,13 +281,39 @@ namespace nana { namespace experimental { #endif }//end namespace detail + bool not_found_error(int errval) + { +#if defined(NANA_WINDOWS) + switch (errval) + { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_NAME: + case ERROR_INVALID_DRIVE: + case ERROR_NOT_READY: + case ERROR_INVALID_PARAMETER: + case ERROR_BAD_PATHNAME: + case ERROR_BAD_NETPATH: + return true; + } + return false; +#elif defined(NANA_POSIX) + return (errval == ENOENT || errval == ENOTDIR); +#else + static_assert(false, "Only Windows and Unix are supported now (Mac OS is experimental)"); +#endif + } + file_status status(const path& p) { #if defined(NANA_WINDOWS) auto attr = ::GetFileAttributesW(p.c_str()); - if(INVALID_FILE_ATTRIBUTES == attr) - return file_status{file_type::unknown}; - + if (INVALID_FILE_ATTRIBUTES == attr) + { + if (not_found_error(static_cast(::GetLastError()))) + return file_status{file_type::not_found}; + return file_status{ file_type::unknown }; + } return file_status{(FILE_ATTRIBUTE_DIRECTORY & attr) ? file_type::directory : file_type::regular, perms::all}; #elif defined(NANA_POSIX) struct stat path_stat; @@ -326,33 +352,6 @@ namespace nana { namespace experimental { #endif } - bool file_attrib(const nana::string& file, attribute& attr) - { -#if defined(NANA_WINDOWS) - WIN32_FILE_ATTRIBUTE_DATA fad; - if (::GetFileAttributesEx(file.c_str(), GetFileExInfoStandard, &fad)) - { - LARGE_INTEGER li; - li.u.LowPart = fad.nFileSizeLow; - li.u.HighPart = fad.nFileSizeHigh; - attr.size = li.QuadPart; - attr.directory = (0 != (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)); - detail::filetime_to_c_tm(fad.ftLastWriteTime, attr.modified); - return true; - } -#elif defined(NANA_LINUX) || defined(NANA_MACOS) - struct stat fst; - if (0 == ::stat(static_cast(nana::charset(file)).c_str(), &fst)) - { - attr.size = fst.st_size; - attr.directory = (0 != (040000 & fst.st_mode)); - attr.modified = *(::localtime(&fst.st_ctime)); - return true; - } -#endif - return false; - } - bool is_directory(const path& p) { return (status(p).type() == file_type::directory); @@ -396,6 +395,7 @@ namespace nana { namespace experimental { #endif } + bool modified_file_time(const std::wstring& file, struct tm& t) { #if defined(NANA_WINDOWS) @@ -440,59 +440,6 @@ namespace nana { namespace experimental { #endif } - /* - bool create_directory(const std::wstring& path, bool & if_exist) //deprecated - { - if_exist = false; - if (path.size() == 0) return false; - - std::wstring root; -#if defined(NANA_WINDOWS) - if (path.size() > 3 && path[1] == L':') - root = path.substr(0, 3); -#elif defined(NANA_LINUX) || defined(NANA_MACOS) - if (path[0] == L'/') - root = '/'; -#endif - bool mkstat = false; - std::size_t beg = root.size(); - - while (true) - { - beg = path.find_first_not_of(L"/\\", beg); - if (beg == path.npos) - break; - - std::size_t pos = path.find_first_of(L"/\\", beg + 1); - if (pos != path.npos) - { - root += path.substr(beg, pos - beg); - - mkstat = detail::mkdir_helper(root, if_exist); - if (mkstat == false && if_exist == false) - return false; - -#if defined(NANA_WINDOWS) - root += L'\\'; -#elif defined(NANA_LINUX) || defined(NANA_MACOS) - root += L'/'; -#endif - } - else - { - if (beg + 1 < path.size()) - { - root += path.substr(beg); - mkstat = detail::mkdir_helper(root, if_exist); - } - break; - } - beg = pos + 1; - } - return mkstat; - } - */ - bool rmfile(const path& p) { if(p.empty()) diff --git a/source/filesystem/fs_utility.cpp b/source/filesystem/fs_utility.cpp index 0af2c6d3..2ed306b6 100644 --- a/source/filesystem/fs_utility.cpp +++ b/source/filesystem/fs_utility.cpp @@ -192,73 +192,6 @@ namespace filesystem #endif }//end namespace detail - bool file_attrib(const std::string& file, attribute& attr) - { - throw_not_utf8(file); -#if defined(NANA_WINDOWS) - WIN32_FILE_ATTRIBUTE_DATA fad; - if(::GetFileAttributesEx(utf8_cast(file).c_str(), GetFileExInfoStandard, &fad)) - { - LARGE_INTEGER li; - li.u.LowPart = fad.nFileSizeLow; - li.u.HighPart = fad.nFileSizeHigh; - attr.bytes = li.QuadPart; - attr.is_directory = (0 != (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)); - detail::filetime_to_c_tm(fad.ftLastWriteTime, attr.modified); - return true; - } -#elif defined(NANA_LINUX) || defined(NANA_MACOS) - struct stat fst; - if(0 == ::stat(file.c_str(), &fst)) - { - attr.bytes = fst.st_size; - attr.is_directory = (0 != (040000 & fst.st_mode)); - attr.modified = *(::localtime(&fst.st_ctime)); - return true; - } -#endif - return false; - } - - /* - long long filesize(const std::string& file) //deprecated - { -#if defined(NANA_WINDOWS) - //Some compilation environment may fail to link to GetFileSizeEx - typedef BOOL (__stdcall *GetFileSizeEx_fptr_t)(HANDLE, PLARGE_INTEGER); - GetFileSizeEx_fptr_t get_file_size_ex = reinterpret_cast(::GetProcAddress(::GetModuleHandleA("Kernel32.DLL"), "GetFileSizeEx")); - if(get_file_size_ex) - { - HANDLE handle = ::CreateFile(utf8_cast(file).c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); - if(INVALID_HANDLE_VALUE != handle) - { - LARGE_INTEGER li; - if(!get_file_size_ex(handle, &li)) - li.QuadPart = 0; - - ::CloseHandle(handle); - return li.QuadPart; - } - } - return 0; -#else - auto stream = ::fopen(file.c_str(), "rb"); - long long size = 0; - if(stream) - { - #if defined(NANA_LINUX) - fseeko64(stream, 0, SEEK_END); - size = ftello64(stream); - #elif defined(NANA_MACOS) - fseeko(stream, 0, SEEK_END); - size = ftello(stream); - #endif - fclose(stream); - } - return size; -#endif - } - */ bool modified_file_time(const ::std::string& file, struct tm& t) { @@ -294,59 +227,6 @@ namespace filesystem return false; } - /* - bool mkdir(const std::string& path, bool & if_exist) //deprecated - { - if_exist = false; - if(path.size() == 0) return false; - - std::string root; -#if defined(NANA_WINDOWS) - if(path.size() > 3 && path[1] == ':') - root = path.substr(0, 3); -#elif defined(NANA_LINUX) || defined(NANA_MACOS) - if(path[0] == '/') - root = '/'; -#endif - bool mkstat = false; - std::size_t beg = root.size(); - - while(true) - { - beg = path.find_first_not_of("/\\", beg); - if(beg == path.npos) - break; - - std::size_t pos = path.find_first_of("/\\", beg + 1); - if(pos != path.npos) - { - root += path.substr(beg, pos - beg); - - mkstat = detail::mkdir_helper(root, if_exist); - if(mkstat == false && if_exist == false) - return false; - -#if defined(NANA_WINDOWS) - root += L'\\'; -#elif defined(NANA_LINUX) || defined(NANA_MACOS) - root += L'/'; -#endif - } - else - { - if(beg + 1 < path.size()) - { - root += path.substr(beg); - mkstat = detail::mkdir_helper(root, if_exist); - } - break; - } - beg = pos + 1; - } - return mkstat; - } - */ - bool rmfile(const char* file) { #if defined(NANA_WINDOWS) diff --git a/source/gui/filebox.cpp b/source/gui/filebox.cpp index a10e3fdc..bf8ed835 100644 --- a/source/gui/filebox.cpp +++ b/source/gui/filebox.cpp @@ -495,7 +495,6 @@ namespace nana file_container_.clear(); using namespace nana::filesystem; - attribute fattr; file_iterator end; for(file_iterator i(path); i != end; ++i) { @@ -503,17 +502,21 @@ namespace nana continue; item_fs m; m.name = i->name; - if(file_attrib(path + m.name, fattr)) + + namespace fs = ::nana::experimental::filesystem; + auto fattr = fs::status(path + m.name); + + if(fattr.type() != fs::file_type::not_found && fattr.type() != fs::file_type::unknown) { - m.bytes = fattr.bytes; - m.directory = fattr.is_directory; - m.modified_time = fattr.modified; + m.bytes = fs::file_size(path + m.name); + m.directory = fs::is_directory(fattr); + modified_file_time(path + m.name, m.modified_time); } else { m.bytes = 0; m.directory = i->directory; - modified_file_time(path + i->name, m.modified_time); + modified_file_time(path + i->name, m.modified_time); } file_container_.push_back(m); @@ -666,18 +669,6 @@ namespace nana return; } -/* - bool if_exist; - if(false == nana::filesystem::mkdir(fb_.addr_.filesystem + path, if_exist)) //deprecated - { - if(if_exist) - mb<path = ipstr; } return *this; diff --git a/source/gui/widgets/button.cpp b/source/gui/widgets/button.cpp index 0a35ba1b..70883b3b 100644 --- a/source/gui/widgets/button.cpp +++ b/source/gui/widgets/button.cpp @@ -214,7 +214,8 @@ namespace nana{ namespace drawerbase { wchar_t shortkey; std::string::size_type shortkey_pos; - std::wstring str = to_wstring(API::transform_shortkey_text(wdg_->caption(), shortkey, &shortkey_pos)); + std::string mbstr = API::transform_shortkey_text(wdg_->caption(), shortkey, &shortkey_pos); + std::wstring str = to_wstring(mbstr); nana::size ts = graph.text_extent_size(str); nana::size gsize = graph.size(); @@ -256,8 +257,8 @@ namespace nana{ namespace drawerbase if(shortkey) { - unsigned off_w = (shortkey_pos ? graph.text_extent_size(str, static_cast(shortkey_pos)).width : 0); - nana::size shortkey_size = graph.text_extent_size(txtptr + shortkey_pos, 1); + unsigned off_w = (shortkey_pos ? graph.text_extent_size(mbstr.c_str(), static_cast(shortkey_pos)).width : 0); + nana::size shortkey_size = graph.text_extent_size(to_wstring(mbstr.c_str() + shortkey_pos), 1); pos.x += off_w; pos.y += static_cast(shortkey_size.height); graph.set_color(colors::black); diff --git a/source/paint/detail/native_paint_interface.cpp b/source/paint/detail/native_paint_interface.cpp index 110673be..5a17879c 100644 --- a/source/paint/detail/native_paint_interface.cpp +++ b/source/paint/detail/native_paint_interface.cpp @@ -138,7 +138,7 @@ namespace detail return nana::size(size.cx, size.cy); #elif defined(NANA_X11) #if defined(NANA_UNICODE) - std::string utf8str = to_utf8(text); + std::string utf8str = to_utf8(std::wstring(text, len)); XGlyphInfo ext; XftFont * fs = reinterpret_cast(dw->font->handle); ::XftTextExtentsUtf8(nana::detail::platform_spec::instance().open_display(), fs,