use std::filesystem::path instead of std::string

This commit is contained in:
Jinhao
2019-03-07 01:10:02 +08:00
parent 051525fc6f
commit 0a3fb34e09
7 changed files with 111 additions and 121 deletions

View File

@@ -936,7 +936,7 @@ namespace nana { namespace experimental { namespace filesystem
auto stat = status(p, err);
if (err != std::error_code())
throw filesystem_error("nana::experimental::filesystem::status", p, err);
throw filesystem_error("nana::filesystem::status", p, err);
return stat;
}
@@ -1002,6 +1002,16 @@ namespace nana { namespace experimental { namespace filesystem
std::uintmax_t file_size(const path& p)
{
std::error_code err;
auto bytes = file_size(p, err);
if (err)
throw filesystem_error("nana::filesystem::status", p, err);
return bytes;
}
std::uintmax_t file_size(const path& p, std::error_code& ec)
{
#if defined(NANA_WINDOWS)
//Some compilation environment may fail to link to GetFileSizeEx
typedef BOOL(__stdcall *GetFileSizeEx_fptr_t)(HANDLE, PLARGE_INTEGER);
@@ -1019,23 +1029,25 @@ namespace nana { namespace experimental { namespace filesystem
return li.QuadPart;
}
}
return 0;
ec.assign(static_cast<int>(::GetLastError()), std::generic_category());
#elif defined(NANA_POSIX)
FILE * stream = ::fopen(p.c_str(), "rb");
long long size = 0;
if (stream)
{
long long bytes = 0;
# if defined(NANA_LINUX)
fseeko64(stream, 0, SEEK_END);
size = ftello64(stream);
bytes = ftello64(stream);
# elif defined(NANA_POSIX)
fseeko(stream, 0, SEEK_END);
size = ftello(stream);
bytes = ftello(stream);
# endif
::fclose(stream);
return bytes;
}
return size;
ec.assign(static_cast<int>(::errno), std::generic_category());
#endif
return static_cast<std::uintmax_t>(-1);
}

View File

@@ -1275,7 +1275,7 @@ namespace nana {
event_handler_ = ptr;
}
bool text_editor::load(const char* fs)
bool text_editor::load(const path_type& fs)
{
if (!impl_->textbase.load(fs))
return false;

View File

@@ -1,7 +1,7 @@
/*
* A Textbox Implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2018 Jinhao(cnjinhao@hotmail.com)
* Copyright(C) 2003-2019 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
@@ -233,31 +233,31 @@ namespace drawerbase {
create(wd, r, visible);
}
void textbox::load(std::string file)
void textbox::load(const std::filesystem::path& file)
{
internal_scope_guard lock;
auto editor = get_drawer_trigger().editor();
if (editor && editor->load(file.data()))
if (editor && editor->load(file))
{
if (editor->try_refresh())
API::update_window(handle());
}
}
void textbox::store(std::string file)
void textbox::store(const std::filesystem::path& file)
{
internal_scope_guard lock;
auto editor = get_drawer_trigger().editor();
if (editor)
editor->textbase().store(std::move(file), false, nana::unicode::utf8); //3rd parameter is just for syntax, it will be ignored
editor->textbase().store(file, false, nana::unicode::utf8); //3rd parameter is just for syntax, it will be ignored
}
void textbox::store(std::string file, nana::unicode encoding)
void textbox::store(const std::filesystem::path& file, nana::unicode encoding)
{
internal_scope_guard lock;
auto editor = get_drawer_trigger().editor();
if (editor)
editor->textbase().store(std::move(file), true, encoding);
editor->textbase().store(file, true, encoding);
}
textbox::colored_area_access_interface* textbox::colored_area_access()
@@ -300,7 +300,8 @@ namespace drawerbase {
if (end_caret)
editor->move_caret_end(true);
editor->textbase().reset();
//Reset the edited status and the saved filename
editor->textbase().reset_status(false);
if (editor->try_refresh())
API::update_window(this->handle());
@@ -308,7 +309,7 @@ namespace drawerbase {
return *this;
}
std::string textbox::filename() const
textbox::path_type textbox::filename() const
{
internal_scope_guard lock;
auto editor = get_drawer_trigger().editor();
@@ -330,7 +331,7 @@ namespace drawerbase {
internal_scope_guard lock;
auto editor = get_drawer_trigger().editor();
if (editor)
editor->textbase().edited_reset();
editor->textbase().reset_status(true);
return *this;
}