Skip to content

Commit

Permalink
patch1 Introduced Formatter in libzim
Browse files Browse the repository at this point in the history
Resolved comments and fixed compilation error on Windows Platform Fix #432
  • Loading branch information
ShaopengLin committed Mar 4, 2024
1 parent a058b22 commit 0127513
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 75 deletions.
2 changes: 1 addition & 1 deletion include/zim/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ namespace zim
std::rethrow_exception(exception);
} catch (const std::exception& e) {
return Formatter()
<< "Asynchronous error: " << typeid(e).name() << '\n'
<< "Asynchronous error: " << typeid(e).name() << std::endl
<< e.what();
} catch (...) {
return "Unknown asynchronous exception";
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,9 @@
#ifndef DEBUG_H_
#define DEBUG_H_

#include "./tools.h"

#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdlib.h>

Expand All @@ -36,10 +37,10 @@
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
2 changes: 1 addition & 1 deletion src/file_compound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ FileCompound::FileCompound(const std::string& filename):
} catch (...) { }

if (empty())
throw std::runtime_error(Formatter() << "error " << errnoSave
throw std::runtime_error(Formatter() << "\"error " << errnoSave
<< " opening file \"" << filename);
}
}
Expand Down
66 changes: 33 additions & 33 deletions src/file_reader.cpp
Original file line number Diff line number Diff line change
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.
Formatter 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);
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) {
Formatter 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);
throw std::system_error(ec, fmt);
};
ASSERT(size_to_get, <=, size);
dest += size_to_get.v;
Expand Down Expand Up @@ -241,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.
Formatter 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);
throw std::system_error(ec, fmt);
};
return ret;
}
Expand All @@ -262,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) {
Formatter 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);
throw std::system_error(ec, fmt);
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/fs_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ FD FS::openFile(path_t filepath)
FILE_ATTRIBUTE_READONLY|FILE_FLAG_RANDOM_ACCESS,
NULL);
if (handle == INVALID_HANDLE_VALUE)
throw std::runtime_error(Formatter() oss << "Cannot open file : "
<< GetLastError());
throw std::runtime_error(Formatter()
<< "Cannot open file : " << GetLastError());

return FD(handle);
}
Expand Down
18 changes: 9 additions & 9 deletions src/narrowdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ class NarrowDown
// It is somehow a bug and have been fixed then, but we still have to be tolerent here and accept that
// two concecutive keys can be equal.
if (key > nextKey) {
Formatter ss;
ss << "Dirent table is not properly sorted:\n";
ss << " #" << i << ": " << key[0] << "/" << key.substr(1) << "\n";
ss << " #" << i+1 << ": " << nextKey[0] << "/" << nextKey.substr(1);
throw ZimFileFormatError(ss);
Formatter fmt;
fmt << "Dirent table is not properly sorted:\n";
fmt << " #" << i << ": " << key[0] << "/" << key.substr(1) << "\n";
fmt << " #" << i+1 << ": " << nextKey[0] << "/" << nextKey.substr(1);
throw ZimFileFormatError(fmt);
}
if ( entries.empty() ) {
addEntry(key, i);
Expand All @@ -121,10 +121,10 @@ class NarrowDown
{
const std::string pseudoKey = shortestStringInBetween(key, nextKey);
if (pred(pseudoKey, entries.back())) {
Formatter ss;
ss << "Dirent table is not properly sorted:\n";
ss << "PseudoKey " << pseudoKey << " should be after (or equal) previously generated " << pred.getKeyContent(entries.back()) << "\n";
throw ZimFileFormatError(ss);
Formatter fmt;
fmt << "Dirent table is not properly sorted:\n";
fmt << "PseudoKey " << pseudoKey << " should be after (or equal) previously generated " << pred.getKeyContent(entries.back()) << "\n";
throw ZimFileFormatError(fmt);
}
ASSERT(entries.back().lindex, <, i);
addEntry(pseudoKey, i);
Expand Down
15 changes: 15 additions & 0 deletions src/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ namespace zim {
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:
Expand Down
8 changes: 4 additions & 4 deletions src/writer/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ void Cluster::write(int out_fd) const
}

default:
Formatter msg;
msg << "invalid compression flag " << static_cast<uint8_t>(getCompression());
log_error(msg);
throw std::runtime_error(msg);
Formatter fmt_msg;
fmt_msg << "invalid compression flag " << static_cast<uint8_t>(getCompression());
log_error(fmt_msg);
throw std::runtime_error(fmt_msg);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/writer/counterHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ DirentHandler::Dirents CounterHandler::createDirents() const {

DirentHandler::ContentProviders CounterHandler::getContentProviders() const {
ContentProviders ret;
Formatter ss;
Formatter fmt;
bool first = true;
for(auto pair: m_mimetypeCounter) {
if (! first) {
ss << ";";
fmt << ";";
}
ss << pair.first << "=" << pair.second;
fmt << pair.first << "=" << pair.second;
first = false;
}
ret.push_back(std::unique_ptr<ContentProvider>(new StringProvider(ss)));
ret.push_back(std::unique_ptr<ContentProvider>(new StringProvider(fmt)));
return ret;
}

Expand Down
18 changes: 9 additions & 9 deletions src/writer/creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ namespace zim
auto existing_dirent_it = data->dirents.find(&tmpDirent);

if (existing_dirent_it == data->dirents.end()) {
Formatter ss;
ss << "Impossible to alias C/" << targetPath << " as C/" << path << '\n';
ss << "C/" << targetPath << " doesn't exist." << '\n';
throw InvalidEntry(ss);
Formatter fmt;
fmt << "Impossible to alias C/" << targetPath << " as C/" << path << std::endl;
fmt << "C/" << targetPath << " doesn't exist." << std::endl;
throw InvalidEntry(fmt);
}

auto dirent = data->createAliasDirent(path, title, **existing_dirent_it);
Expand Down Expand Up @@ -542,11 +542,11 @@ namespace zim
existing->markRemoved();
dirents.insert(dirent);
} else {
Formatter ss;
ss << "Impossible to add " << NsAsChar(dirent->getNamespace()) << "/" << dirent->getPath() << '\n';
ss << " dirent's title to add is : " << dirent->getTitle() << '\n';
ss << " existing dirent's title is : " << existing->getTitle() << '\n';
throw InvalidEntry(ss);
Formatter fmt;
fmt << "Impossible to add " << NsAsChar(dirent->getNamespace()) << "/" << dirent->getPath() << std::endl;
fmt << " dirent's title to add is : " << dirent->getTitle() << std::endl;
fmt << " existing dirent's title is : " << existing->getTitle() << std::endl;
throw InvalidEntry(fmt);
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/writer/defaultIndexData.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ namespace zim
return;
}
#if defined(ENABLE_XAPIAN)
Formatter ss;
Formatter fmt;
while (true) {
auto blob = mp_contentProvider->feed();
if(blob.size() == 0) {
break;
}
ss << blob;
fmt << blob;
}
MyHtmlParser htmlParser;
try {
htmlParser.parse_html(ss, "UTF-8", true);
htmlParser.parse_html(fmt, "UTF-8", true);
} catch(...) {}
m_hasIndexData = !htmlParser.dump.empty() && htmlParser.indexing_allowed && (htmlParser.dump.find("NOINDEX") == std::string::npos);
m_content = zim::removeAccents(htmlParser.dump);
Expand Down
6 changes: 3 additions & 3 deletions test/creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,11 @@ TEST(ZimCreator, interruptedZimCreation)
writer::Creator creator;
creator.configClusterSize(16*1024);
creator.startZimCreation(tmpFile.path());
Formatter oss;
Formatter fmt;
for ( size_t i = 0; i < 12345; ++i ) {
oss << i;
fmt << i;
}
const std::string content(oss);
const std::string content(fmt);
for ( char c = 'a'; c <= 'z'; ++c ) {
const std::string path(1, c);
creator.addItem(std::make_shared<TestItem>(path, path, content));
Expand Down

0 comments on commit 0127513

Please sign in to comment.