diff options
Diffstat (limited to 'src/gpg/GpgConstants.cpp')
-rw-r--r-- | src/gpg/GpgConstants.cpp | 47 |
1 files changed, 40 insertions, 7 deletions
diff --git a/src/gpg/GpgConstants.cpp b/src/gpg/GpgConstants.cpp index e3de1d06..4a34ca34 100644 --- a/src/gpg/GpgConstants.cpp +++ b/src/gpg/GpgConstants.cpp @@ -28,6 +28,10 @@ #include <boost/algorithm/string/predicate.hpp> #include <boost/filesystem.hpp> +#include <codecvt> +#include <iostream> +#include <locale> +#include <string> const char* GpgFrontend::GpgConstants::PGP_CRYPT_BEGIN = "-----BEGIN PGP MESSAGE-----"; @@ -112,14 +116,19 @@ static inline std::string trim(std::string& s) { std::string GpgFrontend::read_all_data_in_file(const std::string& path) { using namespace boost::filesystem; +#ifdef WINDOWS + std::wstring_convert<std::codecvt_utf8_utf16<char16_t>> converter; + std::wstring w_path = converter.from_bytes(path); + class path file_info(w_path.c_str()); +#else class path file_info(path.c_str()); +#endif - if (!exists(file_info) || !is_regular_file(path)) - throw std::runtime_error("no permission"); + if (!exists(file_info) || !is_regular_file(path)) return {}; std::ifstream in_file; in_file.open(path, std::ios::in); - if (!in_file.good()) throw std::runtime_error("cannot open file"); + if (!in_file.good()) return {}; std::istreambuf_iterator<char> begin(in_file); std::istreambuf_iterator<char> end; std::string in_buffer(begin, end); @@ -129,7 +138,16 @@ std::string GpgFrontend::read_all_data_in_file(const std::string& path) { bool GpgFrontend::write_buffer_to_file(const std::string& path, const std::string& out_buffer) { - std::ofstream out_file(boost::filesystem::path(path).string(), std::ios::out); +#ifdef WINDOWS + std::wstring_convert<std::codecvt_utf8_utf16<char16_t>> converter; + std::wstring w_path = converter.from_bytes(path); + std::ofstream out_file(boost::filesystem::path(w_path).string(), + std::ios::out); +#else + std::ofstream out_file(boost::filesystem::path(path).string(), + std::ios::out | std::ios::trunc); +#endif + if (!out_file.good()) return false; out_file.write(out_buffer.c_str(), out_buffer.size()); out_file.close(); @@ -137,8 +155,16 @@ bool GpgFrontend::write_buffer_to_file(const std::string& path, } std::string GpgFrontend::get_file_extension(const std::string& path) { - // Create a Path object from given string +#ifdef WINDOWS + std::wstring_convert<std::codecvt_utf8_utf16<char16_t>> converter; + std::wstring w_path = converter.from_bytes(path); + // Create a path object from given string + boost::filesystem::path path_obj(w_path); +#else + // Create a path object from given string boost::filesystem::path path_obj(path); +#endif + // Check if file name in the path object has extension if (path_obj.has_extension()) { // Fetch the extension from path object and return @@ -149,15 +175,22 @@ std::string GpgFrontend::get_file_extension(const std::string& path) { } std::string GpgFrontend::get_only_file_name_with_path(const std::string& path) { - // Create a Path object from given string +#ifdef WINDOWS + std::wstring_convert<std::codecvt_utf8_utf16<char16_t>> converter; + std::wstring w_path = converter.from_bytes(path); + // Create a path object from given string + boost::filesystem::path path_obj(w_path); +#else + // Create a path object from given string boost::filesystem::path path_obj(path); +#endif // Check if file name in the path object has extension if (path_obj.has_filename()) { // Fetch the extension from path object and return return (path_obj.parent_path() / path_obj.stem()).string(); } // In case of no extension return empty string - throw std::runtime_error("invalid file path"); + return {}; } /* |