Skip to content

Commit

Permalink
Merge pull request #862 from ShaopengLin/main
Browse files Browse the repository at this point in the history
Introduced Formatter in libzim
  • Loading branch information
mgautierfr authored Mar 20, 2024
2 parents f624344 + c234ff2 commit 8d978e1
Show file tree
Hide file tree
Showing 21 changed files with 191 additions and 169 deletions.
10 changes: 4 additions & 6 deletions include/zim/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#define ZIM_ERROR_H

#include "zim.h"
#include "tools.h"

#include <stdexcept>
#include <sstream>
#include <typeinfo>

namespace zim
Expand Down Expand Up @@ -135,11 +135,9 @@ namespace zim
try {
std::rethrow_exception(exception);
} catch (const std::exception& e) {
std::stringstream ss;
ss << "Asynchronous error: ";
ss << typeid(e).name() << std::endl;
ss << e.what();
return ss.str();
return Formatter()
<< "Asynchronous error: " << typeid(e).name() << std::endl
<< e.what();
} catch (...) {
return "Unknown asynchronous exception";
}
Expand Down
46 changes: 45 additions & 1 deletion include/zim/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define ZIM_TOOLS_H

#include "zim.h"

#include <sstream>
namespace zim {
#if defined(LIBZIM_WITH_XAPIAN)

Expand All @@ -34,6 +34,50 @@ 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 <typename Type> Formatter &operator<<(const Type &value)
{
stream_ << value;
return *this;
}

/* Operator for function templates like std::endl */
Formatter &operator<<(std::ostream& (* __pf)(std::ostream&))
{
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
6 changes: 3 additions & 3 deletions src/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <zim/entry.h>
#include <zim/item.h>
#include <zim/error.h>
#include <zim/tools.h>
#include "fileimpl.h"
#include "tools.h"
#include "log.h"
Expand Down Expand Up @@ -154,9 +155,8 @@ namespace zim
}

Item Archive::getIllustrationItem(unsigned int size) const {
std::ostringstream ss;
ss << "Illustration_" << size << "x" << size << "@" << 1;
auto r = m_impl->findx('M', ss.str());
auto r = m_impl->findx('M', Formatter() << "Illustration_" << size << "x"
<< size << "@" << 1);
if (r.first) {
return getEntryByPath(entry_index_type(r.second)).getItem();
}
Expand Down
6 changes: 3 additions & 3 deletions src/compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "envvalue.h"

#include <zim/tools.h>
#include <stdexcept>

const std::string LZMA_INFO::name = "lzma";
Expand Down Expand Up @@ -51,9 +52,8 @@ CompStatus LZMA_INFO::stream_run(stream_t* stream, CompStep step)
case LZMA_OK:
return CompStatus::OK;
default: {
std::ostringstream ss;
ss << "Unexpected lzma status : " << errcode;
throw std::runtime_error(ss.str());
throw std::runtime_error(zim::Formatter()
<< "Unexpected lzma status : " << errcode);
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#ifndef DEBUG_H_
#define DEBUG_H_

#include <zim/tools.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdlib.h>

Expand All @@ -36,10 +36,11 @@
template<typename T, typename U>
void _on_assert_fail(const char* vara, const char* op, const char* varb,
T a, U b, const char* file, int line) {
std::ostringstream ss;
ss << "\nAssertion failed at "<< file << ":" << line << "\n " <<
vara << "[" << a << "] " << op << " " << varb << "[" << b << "]";
std::cerr << ss.str() << std::endl;
zim::Formatter fmt;
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__)
void *callstack[64];
Expand All @@ -51,7 +52,7 @@ void _on_assert_fail(const char* vara, const char* op, const char* varb,
}
free(strings);
#endif
throw std::runtime_error(ss.str());
throw std::runtime_error(fmt);
}

# define ASSERT(left, operator, right) do { auto _left = left; auto _right = right; if (!((_left) operator (_right))) _on_assert_fail(#left, #operator, #right, _left, _right, __FILE__, __LINE__); } while(0)
Expand Down
25 changes: 11 additions & 14 deletions src/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
#include <zim/entry.h>
#include <zim/error.h>
#include <zim/item.h>
#include <zim/tools.h>
#include "fileimpl.h"
#include "log.h"

#include <sstream>

log_define("zim.entry")

using namespace zim;
Expand Down Expand Up @@ -57,14 +56,13 @@ bool Entry::isRedirect() const

Item Entry::getItem(bool follow) const
{
if (isRedirect()) {
if (! follow) {
std::ostringstream sstream;
sstream << "Entry " << getPath() << " is a redirect entry.";
throw InvalidType(sstream.str());
}
if (isRedirect())
{
if (!follow)
throw InvalidType(Formatter()
<< "Entry " << getPath() << " is a redirect entry.");
return getRedirect();
}
}

return Item(*this);
}
Expand All @@ -79,11 +77,10 @@ Item Entry::getRedirect() const {
}

entry_index_type Entry::getRedirectEntryIndex() const {
if (!isRedirect()) {
std::ostringstream sstream;
sstream << "Entry " << getPath() << " is not a redirect entry.";
throw InvalidType(sstream.str());
}
if (!isRedirect())
throw InvalidType(Formatter()
<< "Entry " << getPath() << " is not a redirect entry.");

return m_dirent->getRedirectIndex().v;
}

Expand Down
18 changes: 7 additions & 11 deletions src/file_compound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

#include <errno.h>
#include <string.h>
#include <sstream>
#include <sys/stat.h>
#include <zim/tools.h>

#ifdef _WIN32
# include <io.h>
Expand Down Expand Up @@ -61,11 +61,9 @@ FileCompound::FileCompound(const std::string& filename):
} catch (...) { }

if (empty())
{
std::ostringstream msg;
msg << "error " << errnoSave << " opening file \"" << filename;
throw std::runtime_error(msg.str());
}
throw std::runtime_error(Formatter()
<< "error " << errnoSave << " opening file \""
<< filename << "\"");
}
}

Expand Down Expand Up @@ -115,11 +113,9 @@ time_t FileCompound::getMTime() const {
int ret = ::stat(fname, &st);
#endif
if (ret != 0)
{
std::ostringstream msg;
msg << "stat failed with errno " << errno << " : " << strerror(errno);
throw std::runtime_error(msg.str());
}
throw std::runtime_error(Formatter() << "stat failed with errno " << errno
<< " : " << strerror(errno));

mtime = st.st_mtime;

return mtime;
Expand Down
80 changes: 39 additions & 41 deletions src/file_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

#include <zim/zim.h>
#include <zim/error.h>
#include <zim/tools.h>
#include "file_reader.h"
#include "file_compound.h"
#include "buffer.h"
#include <errno.h>
#include <string.h>
#include <cstring>
#include <fcntl.h>
#include <sstream>
#include <system_error>
#include <algorithm>

Expand Down Expand Up @@ -74,17 +74,17 @@ char MultiPartFileReader::read(offset_t offset) const {
fhandle.readAt(&ret, zsize_t(1), physical_local_offset);
} catch (std::runtime_error& e) {
//Error while reading.
std::ostringstream s;
s << "Cannot read a char.\n";
s << " - File part is " << part_pair->second->filename() << "\n";
s << " - File part size is " << part_pair->second->size().v << "\n";
s << " - File part range is " << part_pair->first.min << "-" << part_pair->first.max << "\n";
s << " - Reading offset at " << offset.v << "\n";
s << " - logical local offset is " << logical_local_offset.v << "\n";
s << " - physical local offset is " << physical_local_offset.v << "\n";
s << " - error is " << strerror(errno) << "\n";
Formatter fmt;
fmt << "Cannot read a char.\n";
fmt << " - File part is " << part_pair->second->filename() << "\n";
fmt << " - File part size is " << part_pair->second->size().v << "\n";
fmt << " - File part range is " << part_pair->first.min << "-" << part_pair->first.max << "\n";
fmt << " - Reading offset at " << offset.v << "\n";
fmt << " - logical local offset is " << logical_local_offset.v << "\n";
fmt << " - physical local offset is " << physical_local_offset.v << "\n";
fmt << " - error is " << strerror(errno) << "\n";
std::error_code ec(errno, std::generic_category());
throw std::system_error(ec, s.str());
throw std::system_error(ec, fmt);
};
return ret;
}
Expand All @@ -107,19 +107,19 @@ void MultiPartFileReader::read(char* dest, offset_t offset, zsize_t size) const
try {
part->fhandle().readAt(dest, size_to_get, physical_local_offset);
} catch (std::runtime_error& e) {
std::ostringstream s;
s << "Cannot read chars.\n";
s << " - File part is " << part->filename() << "\n";
s << " - File part size is " << part->size().v << "\n";
s << " - File part range is " << partRange.min << "-" << partRange.max << "\n";
s << " - size_to_get is " << size_to_get.v << "\n";
s << " - total size is " << size.v << "\n";
s << " - Reading offset at " << offset.v << "\n";
s << " - logical local offset is " << logical_local_offset.v << "\n";
s << " - physical local offset is " << physical_local_offset.v << "\n";
s << " - error is " << strerror(errno) << "\n";
Formatter fmt;
fmt << "Cannot read chars.\n";
fmt << " - File part is " << part->filename() << "\n";
fmt << " - File part size is " << part->size().v << "\n";
fmt << " - File part range is " << partRange.min << "-" << partRange.max << "\n";
fmt << " - size_to_get is " << size_to_get.v << "\n";
fmt << " - total size is " << size.v << "\n";
fmt << " - Reading offset at " << offset.v << "\n";
fmt << " - logical local offset is " << logical_local_offset.v << "\n";
fmt << " - physical local offset is " << physical_local_offset.v << "\n";
fmt << " - error is " << strerror(errno) << "\n";
std::error_code ec(errno, std::generic_category());
throw std::system_error(ec, s.str());
throw std::system_error(ec, fmt);
};
ASSERT(size_to_get, <=, size);
dest += size_to_get.v;
Expand Down Expand Up @@ -147,13 +147,11 @@ mmapReadOnly(int fd, offset_type offset, size_type size)
#endif

const auto p = (char*)mmap(NULL, size, PROT_READ, MAP_FLAGS, fd, offset);
if (p == MAP_FAILED )
{
std::ostringstream s;
s << "Cannot mmap size " << size << " at off " << offset
<< " : " << strerror(errno);
throw std::runtime_error(s.str());
}
if (p == MAP_FAILED)
throw std::runtime_error(Formatter()
<< "Cannot mmap size " << size << " at off "
<< offset << " : " << strerror(errno));

return p;
}

Expand Down Expand Up @@ -243,12 +241,12 @@ char FileReader::read(offset_t offset) const
_fhandle->readAt(&ret, zsize_t(1), offset);
} catch (std::runtime_error& e) {
//Error while reading.
std::ostringstream s;
s << "Cannot read a char.\n";
s << " - Reading offset at " << offset.v << "\n";
s << " - error is " << strerror(errno) << "\n";
Formatter fmt;
fmt << "Cannot read a char.\n";
fmt << " - Reading offset at " << offset.v << "\n";
fmt << " - error is " << strerror(errno) << "\n";
std::error_code ec(errno, std::generic_category());
throw std::system_error(ec, s.str());
throw std::system_error(ec, fmt);
};
return ret;
}
Expand All @@ -264,13 +262,13 @@ void FileReader::read(char* dest, offset_t offset, zsize_t size) const
try {
_fhandle->readAt(dest, size, offset);
} catch (std::runtime_error& e) {
std::ostringstream s;
s << "Cannot read chars.\n";
s << " - Reading offset at " << offset.v << "\n";
s << " - size is " << size.v << "\n";
s << " - error is " << strerror(errno) << "\n";
Formatter fmt;
fmt << "Cannot read chars.\n";
fmt << " - Reading offset at " << offset.v << "\n";
fmt << " - size is " << size.v << "\n";
fmt << " - error is " << strerror(errno) << "\n";
std::error_code ec(errno, std::generic_category());
throw std::system_error(ec, s.str());
throw std::system_error(ec, fmt);
};
}

Expand Down
Loading

0 comments on commit 8d978e1

Please sign in to comment.