From 207abd7408fa7fd2924f6b540635d6ed6f92f47a Mon Sep 17 00:00:00 2001 From: "Shaopeng (Chris) Lin" Date: Tue, 5 Mar 2024 17:24:01 -0500 Subject: [PATCH] patch2 Introduced Formatter in libzim Fixed bugs in debug.h where Formatter would not be populated. Moved Formatter to include/zim to ensure correct installation #432 --- include/zim/error.h | 2 +- include/zim/tools.h | 47 ++++++++++++++++++++++++++++++++++- src/archive.cpp | 1 + src/compression.cpp | 2 +- src/debug.h | 8 +++--- src/entry.cpp | 2 +- src/file_compound.cpp | 2 +- src/file_reader.cpp | 2 +- src/fileimpl.cpp | 1 + src/fs_unix.cpp | 2 +- src/fs_windows.cpp | 2 +- src/narrowdown.h | 2 +- src/tools.h | 46 ---------------------------------- src/version.cpp | 2 +- src/writer/cluster.cpp | 3 +-- src/writer/counterHandler.cpp | 1 + src/writer/creator.cpp | 1 + src/writer/defaultIndexData.h | 1 + src/writer/xapianWorker.cpp | 2 +- test/creator.cpp | 3 ++- test/tools.h | 2 +- 21 files changed, 69 insertions(+), 65 deletions(-) diff --git a/include/zim/error.h b/include/zim/error.h index 83a5a7fbe..673a52028 100644 --- a/include/zim/error.h +++ b/include/zim/error.h @@ -21,7 +21,7 @@ #define ZIM_ERROR_H #include "zim.h" -#include +#include "tools.h" #include #include diff --git a/include/zim/tools.h b/include/zim/tools.h index e5341fdec..f67eb12a9 100644 --- a/include/zim/tools.h +++ b/include/zim/tools.h @@ -21,7 +21,7 @@ #define ZIM_TOOLS_H #include "zim.h" - +#include namespace zim { #if defined(LIBZIM_WITH_XAPIAN) @@ -34,6 +34,51 @@ namespace zim { LIBZIM_API void setICUDataDirectory(const std::string& path); #endif + + /** + * @brief Stringstream Class to use itself as the stream object + * returned by << operator. (std::stringstream returns an std::ostream). + * Allows a one-line stringstream to str conversion, e.g. use_str(Formatter() + * << "foo" << variable); + * + */ + class Formatter + { + public: + Formatter() {} + ~Formatter() {} + + template Formatter &operator<<(const Type &value) + { + stream_ << value; + return *this; + } + + /* Operator for function templates like std::endl */ + Formatter &operator<<(std::stringstream::__ostream_type &(*__pf)( + std::stringstream::__ostream_type &)) + { + stream_ << __pf; + return *this; + } + + /* Operator for working with other ostream like std::cerr */ + friend std::ostream &operator<<(std::ostream &os, const Formatter &obj) + { + os << obj.stream_.str(); + return os; + } + + operator std::string() const { return stream_.str(); } + + private: + /* Disable copy and assignment constructors */ + Formatter(const Formatter &) = delete; + Formatter &operator=(Formatter &) = delete; + + /* Simple composition with std::stringstream */ + std::stringstream stream_; + }; } #endif // ZIM_TOOLS_H diff --git a/src/archive.cpp b/src/archive.cpp index 3b47df4e6..388e82a20 100644 --- a/src/archive.cpp +++ b/src/archive.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "fileimpl.h" #include "tools.h" #include "log.h" diff --git a/src/compression.cpp b/src/compression.cpp index c738fa649..a931ee8ab 100644 --- a/src/compression.cpp +++ b/src/compression.cpp @@ -22,8 +22,8 @@ #include "compression.h" #include "envvalue.h" -#include "tools.h" +#include #include const std::string LZMA_INFO::name = "lzma"; diff --git a/src/debug.h b/src/debug.h index aa92d3dcf..4ec9ffb8a 100644 --- a/src/debug.h +++ b/src/debug.h @@ -20,8 +20,7 @@ #ifndef DEBUG_H_ #define DEBUG_H_ -#include "./tools.h" - +#include #include #include #include @@ -38,8 +37,9 @@ template void _on_assert_fail(const char* vara, const char* op, const char* varb, T a, U b, const char* file, int line) { zim::Formatter fmt; - std::cerr << fmt << "\nAssertion failed at " << file << ":" << line << "\n " - << vara << "[" << a << "] " << op << " " << varb << "[" << b << "]" + std::cerr << (fmt << "\nAssertion failed at " << file << ":" << line << "\n " + << vara << "[" << a << "] " << op << " " << varb << "[" << b + << "]") << std::endl; #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && defined(__GNU_LIBRARY__) diff --git a/src/entry.cpp b/src/entry.cpp index 57cbf33b4..dbe60ea4c 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -21,9 +21,9 @@ #include #include #include +#include #include "fileimpl.h" #include "log.h" -#include "tools.h" log_define("zim.entry") diff --git a/src/file_compound.cpp b/src/file_compound.cpp index 3469f490a..19199b309 100644 --- a/src/file_compound.cpp +++ b/src/file_compound.cpp @@ -19,11 +19,11 @@ */ #include "file_compound.h" -#include "tools.h" #include #include #include +#include #ifdef _WIN32 # include diff --git a/src/file_reader.cpp b/src/file_reader.cpp index 3fbcb62b5..d32895b50 100644 --- a/src/file_reader.cpp +++ b/src/file_reader.cpp @@ -20,9 +20,9 @@ #include #include +#include #include "file_reader.h" #include "file_compound.h" -#include "tools.h" #include "buffer.h" #include #include diff --git a/src/fileimpl.cpp b/src/fileimpl.cpp index e9825979f..61d43c7e9 100644 --- a/src/fileimpl.cpp +++ b/src/fileimpl.cpp @@ -21,6 +21,7 @@ #include "fileimpl.h" #include +#include #include "_dirent.h" #include "file_compound.h" #include "buffer_reader.h" diff --git a/src/fs_unix.cpp b/src/fs_unix.cpp index 47c6133bb..758c53341 100644 --- a/src/fs_unix.cpp +++ b/src/fs_unix.cpp @@ -18,11 +18,11 @@ */ #include "fs_unix.h" -#include "tools.h" #include #include #include +#include #include #include #include diff --git a/src/fs_windows.cpp b/src/fs_windows.cpp index 6ad80b64f..e6f6cfe00 100644 --- a/src/fs_windows.cpp +++ b/src/fs_windows.cpp @@ -18,7 +18,6 @@ */ #include "fs_windows.h" -#include "tools.h" #include #include @@ -26,6 +25,7 @@ #include #include #include +#include #include diff --git a/src/narrowdown.h b/src/narrowdown.h index fd30a7721..7486cd1cb 100644 --- a/src/narrowdown.h +++ b/src/narrowdown.h @@ -22,12 +22,12 @@ #define ZIM_NARROWDOWN_H #include "debug.h" -#include "tools.h" #include #include #include +#include namespace zim { diff --git a/src/tools.h b/src/tools.h index 9144885a9..4776b0d0f 100644 --- a/src/tools.h +++ b/src/tools.h @@ -27,7 +27,6 @@ #include #include #include "config.h" -#include #include @@ -71,51 +70,6 @@ namespace zim { return count; } - /** - * @brief Stringstream Class to use itself as the stream object - * returned by << operator. (std::stringstream returns an std::ostream). - * Allows a one-line stringstream to str conversion, e.g. use_str(Formatter() - * << "foo" << variable); - * - */ - class Formatter - { - public: - Formatter() {} - ~Formatter() {} - - template Formatter &operator<<(const Type &value) - { - stream_ << value; - return *this; - } - - /* Operator for function templates */ - Formatter &operator<<(std::stringstream::__ostream_type &(*__pf)( - std::stringstream::__ostream_type &)) - { - stream_ << __pf; - return *this; - } - - /* Operator for working with other ostream */ - friend std::ostream &operator<<(std::ostream &os, const Formatter &obj) - { - os << obj.stream_.str(); - return os; - } - - operator std::string() const { return stream_.str(); } - - private: - /* Disable copy and assignment constructors */ - Formatter(const Formatter &) = delete; - Formatter &operator=(Formatter &) = delete; - - /* Simple composition with std::stringstream */ - std::stringstream stream_; - }; - // Xapian based tools #if defined(ENABLE_XAPIAN) std::string removeAccents(const std::string& text); diff --git a/src/version.cpp b/src/version.cpp index 1ba5b31b9..3f192fc30 100644 --- a/src/version.cpp +++ b/src/version.cpp @@ -18,10 +18,10 @@ */ #include -#include "tools.h" #include #include +#include #include #include #include diff --git a/src/writer/cluster.cpp b/src/writer/cluster.cpp index ca7fe0b4e..1ef6de228 100644 --- a/src/writer/cluster.cpp +++ b/src/writer/cluster.cpp @@ -26,8 +26,7 @@ #include "../compression.h" #include - -#include +#include #include #include diff --git a/src/writer/counterHandler.cpp b/src/writer/counterHandler.cpp index c72ee512a..7f07893ef 100644 --- a/src/writer/counterHandler.cpp +++ b/src/writer/counterHandler.cpp @@ -22,6 +22,7 @@ #include #include +#include using namespace zim::writer; diff --git a/src/writer/creator.cpp b/src/writer/creator.cpp index f9ccfa407..e28d60b2c 100644 --- a/src/writer/creator.cpp +++ b/src/writer/creator.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include "../endian_tools.h" #include #include diff --git a/src/writer/defaultIndexData.h b/src/writer/defaultIndexData.h index 17babb239..f9397353a 100644 --- a/src/writer/defaultIndexData.h +++ b/src/writer/defaultIndexData.h @@ -21,6 +21,7 @@ #define ZIM_WRITER_DEFAULTINDEXDATA_H #include +#include #include "xapian/myhtmlparse.h" #include "../tools.h" diff --git a/src/writer/xapianWorker.cpp b/src/writer/xapianWorker.cpp index ff4095005..f2039f6fc 100644 --- a/src/writer/xapianWorker.cpp +++ b/src/writer/xapianWorker.cpp @@ -24,8 +24,8 @@ #include "xapianIndexer.h" #include -#include #include +#include static std::mutex s_dbaccessLock; diff --git a/test/creator.cpp b/test/creator.cpp index 08ee0afa2..dba1a6150 100644 --- a/test/creator.cpp +++ b/test/creator.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "tools.h" #include "../src/file_compound.h" @@ -358,7 +359,7 @@ TEST(ZimCreator, interruptedZimCreation) writer::Creator creator; creator.configClusterSize(16*1024); creator.startZimCreation(tmpFile.path()); - Formatter fmt; + zim::Formatter fmt; for ( size_t i = 0; i < 12345; ++i ) { fmt << i; } diff --git a/test/tools.h b/test/tools.h index 9ff10d05b..f228d284d 100644 --- a/test/tools.h +++ b/test/tools.h @@ -36,7 +36,6 @@ typedef SSIZE_T ssize_t; #endif #include "../src/buffer.h" -#include "../src/tools.h" #include #define ZIM_PRIVATE @@ -45,6 +44,7 @@ typedef SSIZE_T ssize_t; #include #include #include +#include namespace zim {