forked from lthn/blockchain
added api for importing contacts from file
This commit is contained in:
parent
a6ab2adf86
commit
4a1bd84c71
3 changed files with 61 additions and 31 deletions
|
|
@ -289,6 +289,40 @@ namespace file_io_utils
|
|||
}
|
||||
}
|
||||
|
||||
template<class t_string>
|
||||
bool load_file_to_string(const t_string& path_to_file, std::string& target_str)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::ifstream fstream;
|
||||
//fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
fstream.open(path_to_file, std::ios_base::binary | std::ios_base::in | std::ios::ate);
|
||||
if (!fstream.good())
|
||||
return false;
|
||||
std::ifstream::pos_type file_size = fstream.tellg();
|
||||
|
||||
if (file_size > 1000000000)
|
||||
return false;//don't get crazy
|
||||
size_t file_size_t = static_cast<size_t>(file_size);
|
||||
|
||||
target_str.resize(file_size_t);
|
||||
|
||||
fstream.seekg(0, std::ios::beg);
|
||||
fstream.read((char*)target_str.data(), target_str.size());
|
||||
if (!fstream.good())
|
||||
return false;
|
||||
|
||||
fstream.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
catch (...)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
inline
|
||||
bool load_form_handle(HANDLE hfile, std::string& str)
|
||||
|
|
@ -338,38 +372,7 @@ namespace file_io_utils
|
|||
}
|
||||
|
||||
|
||||
inline
|
||||
bool load_file_to_string(const std::string& path_to_file, std::string& target_str)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::ifstream fstream;
|
||||
//fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
fstream.open(path_to_file, std::ios_base::binary | std::ios_base::in | std::ios::ate);
|
||||
if (!fstream.good())
|
||||
return false;
|
||||
std::ifstream::pos_type file_size = fstream.tellg();
|
||||
|
||||
if(file_size > 1000000000)
|
||||
return false;//don't go crazy
|
||||
size_t file_size_t = static_cast<size_t>(file_size);
|
||||
|
||||
target_str.resize(file_size_t);
|
||||
|
||||
fstream.seekg (0, std::ios::beg);
|
||||
fstream.read((char*)target_str.data(), target_str.size());
|
||||
if (!fstream.good())
|
||||
return false;
|
||||
|
||||
fstream.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
catch(...)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
typedef HANDLE native_filesystem_handle;
|
||||
|
|
|
|||
|
|
@ -1158,6 +1158,32 @@ QString MainWindow::store_to_file(const QString& path, const QString& buff)
|
|||
CATCH_ENTRY2(API_RETURN_CODE_INTERNAL_ERROR);
|
||||
}
|
||||
|
||||
QString MainWindow::load_from_file(const QString& path)
|
||||
{
|
||||
TRY_ENTRY();
|
||||
try {
|
||||
std::string buff;
|
||||
bool r = epee::file_io_utils::load_file_to_string(path.toStdWString(), buff);
|
||||
if (r)
|
||||
return QString::fromStdString(buff);
|
||||
else
|
||||
return QString();
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
LOG_ERROR("FILED TO LOAD FROM FILE: " << path.toStdString() << " ERROR:" << ex.what());
|
||||
return QString(API_RETURN_CODE_ACCESS_DENIED) + ": " + ex.what();
|
||||
}
|
||||
|
||||
catch (...)
|
||||
{
|
||||
return API_RETURN_CODE_ACCESS_DENIED;
|
||||
}
|
||||
|
||||
|
||||
CATCH_ENTRY2(API_RETURN_CODE_INTERNAL_ERROR);
|
||||
}
|
||||
|
||||
QString MainWindow::get_app_data()
|
||||
{
|
||||
TRY_ENTRY();
|
||||
|
|
|
|||
|
|
@ -132,6 +132,7 @@ public:
|
|||
QString restore_wallet(const QString& param);
|
||||
QString is_pos_allowed();
|
||||
QString store_to_file(const QString& path, const QString& buff);
|
||||
QString load_from_file(const QString& path);
|
||||
QString is_file_exist(const QString& path);
|
||||
QString get_mining_estimate(const QString& obj);
|
||||
QString backup_wallet_keys(const QString& obj);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue