From a9d3ac0b0a2e0245d7377f8de0d5cabb973350cd Mon Sep 17 00:00:00 2001 From: Jan Christoph Uhde Date: Sun, 15 Oct 2023 18:33:50 +0200 Subject: [PATCH 1/2] fix load_library --- include/ext/util/load_library.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/ext/util/load_library.hpp b/include/ext/util/load_library.hpp index 74e0cf2..231c6f2 100644 --- a/include/ext/util/load_library.hpp +++ b/include/ext/util/load_library.hpp @@ -50,7 +50,7 @@ typedef const char* const_utf8_e_str; * windows) * @return dl_handle or NULL on fail */ -dl_handle dl_open(const_utf8_e_str filename, int flag = RTLD_LAZY) { +inline dl_handle dl_open(const_utf8_e_str filename, int flag = RTLD_LAZY) { #ifdef EXT_UNIX return ::dlopen(filename, flag); #elif EXT_WINDOWS @@ -64,7 +64,7 @@ dl_handle dl_open(const_utf8_e_str filename, int flag = RTLD_LAZY) { } //! open library - overload for std::string -dl_handle dl_open(const std::string& filename, int flag = RTLD_LAZY) { +inline dl_handle dl_open(const std::string& filename, int flag = RTLD_LAZY) { return dl_open(filename.c_str(), flag); } @@ -73,7 +73,7 @@ dl_handle dl_open(const std::string& filename, int flag = RTLD_LAZY) { * Functions that returns textual error * @return textual description of the error in utf-8 encoded std::string */ -std::string dl_error(void) { +inline std::string dl_error(void) { #ifdef EXT_UNIX // returns a static buffer - do not free!!!! char const* buffer = ::dlerror(); @@ -111,7 +111,7 @@ std::string dl_error(void) { * @return dl_address pointer to requested symbol * or NULL if symbol is not found */ -dl_address dl_sym(dl_handle handle, const_utf8_e_str symbol) { +inline dl_address dl_sym(dl_handle handle, const_utf8_e_str symbol) { #ifdef EXT_UNIX return ::dlsym(handle, symbol); #elif EXT_WINDOWS @@ -125,12 +125,12 @@ dl_address dl_sym(dl_handle handle, const_utf8_e_str symbol) { } //! get address of symbol - overload for std::string -dl_address dl_sym(dl_handle handle, const std::string& symbol) { +inline dl_address dl_sym(dl_handle handle, const std::string& symbol) { return dl_sym(handle, symbol.c_str()); } //! get address of symbol or throw logic_error -dl_address dl_sym_e(dl_handle handle, const std::string& symbol) { +inline dl_address dl_sym_e(dl_handle handle, const std::string& symbol) { dl_address address = dl_sym(handle, symbol.c_str()); if (address == NULL) { throw std::logic_error(dl_error()); @@ -146,7 +146,7 @@ dl_address dl_sym_e(dl_handle handle, const std::string& symbol) { * @param[in] handle handle of lib to close * @return returns NULL on fail */ -dl_rv dl_close(dl_handle handle) { +inline dl_rv dl_close(dl_handle handle) { #ifdef EXT_UNIX return ::dlclose(handle); #elif EXT_WINDOWS From ffb10956262878b9d3fc465db6648850479434a3 Mon Sep 17 00:00:00 2001 From: Jan Christoph Uhde Date: Sun, 15 Oct 2023 19:03:42 +0200 Subject: [PATCH 2/2] add: let the user know if we did not reach the end of file. --- include/ext/util/io.hpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/include/ext/util/io.hpp b/include/ext/util/io.hpp index 2d4e22d..d04ee8b 100644 --- a/include/ext/util/io.hpp +++ b/include/ext/util/io.hpp @@ -13,12 +13,16 @@ namespace ext { namespace util { -inline std::string istream_to_string(std::istream& in, bool remove_spaces = false) { +inline std::pair istream_to_string(std::istream& in, bool remove_spaces = false) { + constexpr std::size_t buf_size = 4096; + std::string result; - char buffer[4096]; + std::string::value_type buffer[buf_size]; + while (in.read(buffer, sizeof(buffer))) { result.append(buffer, sizeof(buffer)); } + // number of chars in last operation result.append(buffer, in.gcount()); @@ -28,10 +32,11 @@ inline std::string istream_to_string(std::istream& in, bool remove_spaces = fals }; result.erase(std::remove_if(result.begin(), result.end(), is_space), result.end()); } - return result; + + return {result, in.eof()}; } -inline std::string ifstream_to_string(std::ifstream& in, bool remove_spaces = false) { +inline std::pair ifstream_to_string(std::ifstream& in, bool remove_spaces = false) { if (!in.is_open()) { throw std::logic_error("You try to read from a closed stream!"); }