From 4a1bd84c71b5bad048533b421d54ac04a38c9a2b Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Fri, 9 Aug 2019 22:12:32 +0200 Subject: [PATCH] added api for importing contacts from file --- contrib/epee/include/file_io_utils.h | 65 ++++++++++---------- src/gui/qt-daemon/application/mainwindow.cpp | 26 ++++++++ src/gui/qt-daemon/application/mainwindow.h | 1 + 3 files changed, 61 insertions(+), 31 deletions(-) diff --git a/contrib/epee/include/file_io_utils.h b/contrib/epee/include/file_io_utils.h index cb4e0a21..86b9d6e9 100644 --- a/contrib/epee/include/file_io_utils.h +++ b/contrib/epee/include/file_io_utils.h @@ -289,6 +289,40 @@ namespace file_io_utils } } + template + 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(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(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; diff --git a/src/gui/qt-daemon/application/mainwindow.cpp b/src/gui/qt-daemon/application/mainwindow.cpp index 792261fd..580375cb 100644 --- a/src/gui/qt-daemon/application/mainwindow.cpp +++ b/src/gui/qt-daemon/application/mainwindow.cpp @@ -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(); diff --git a/src/gui/qt-daemon/application/mainwindow.h b/src/gui/qt-daemon/application/mainwindow.h index a242c62b..8f55c38f 100644 --- a/src/gui/qt-daemon/application/mainwindow.h +++ b/src/gui/qt-daemon/application/mainwindow.h @@ -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);