Skip to content

Commit

Permalink
Utilized Formatter Class to Replace (o)stringstreams
Browse files Browse the repository at this point in the history
Files like tooltesting.cpp, uuid.cpp, archive.cpp, debug.h, and createZimExample.cpp are unchanged due to some special characteristics that need discussion
Fix #432
  • Loading branch information
ShaopengLin committed Mar 3, 2024
1 parent 8c24319 commit a058b22
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 124 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() << '\n'
<< e.what();
} catch (...) {
return "Unknown asynchronous exception";
}
Expand Down
5 changes: 2 additions & 3 deletions src/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,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 @@ -22,6 +22,7 @@
#include "compression.h"

#include "envvalue.h"
#include "tools.h"

#include <stdexcept>

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
25 changes: 11 additions & 14 deletions src/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
#include <zim/item.h>
#include "fileimpl.h"
#include "log.h"

#include <sstream>
#include "tools.h"

log_define("zim.entry")

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
17 changes: 6 additions & 11 deletions src/file_compound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
*/

#include "file_compound.h"
#include "tools.h"

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

#ifdef _WIN32
Expand Down Expand Up @@ -61,11 +61,8 @@ 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 +112,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
30 changes: 14 additions & 16 deletions src/file_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
#include <zim/error.h>
#include "file_reader.h"
#include "file_compound.h"
#include "tools.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,7 +74,7 @@ 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;
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";
Expand All @@ -84,7 +84,7 @@ char MultiPartFileReader::read(offset_t offset) const {
s << " - physical local offset is " << physical_local_offset.v << "\n";
s << " - 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, s);
};
return ret;
}
Expand All @@ -107,7 +107,7 @@ 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;
Formatter s;
s << "Cannot read chars.\n";
s << " - File part is " << part->filename() << "\n";
s << " - File part size is " << part->size().v << "\n";
Expand All @@ -119,7 +119,7 @@ void MultiPartFileReader::read(char* dest, offset_t offset, zsize_t size) const
s << " - physical local offset is " << physical_local_offset.v << "\n";
s << " - 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, s);
};
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;
Formatter s;
s << "Cannot read a char.\n";
s << " - Reading offset at " << offset.v << "\n";
s << " - 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, s);
};
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;
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";
std::error_code ec(errno, std::generic_category());
throw std::system_error(ec, s.str());
throw std::system_error(ec, s);
};
}

Expand Down
6 changes: 1 addition & 5 deletions src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,7 @@ class Grouping
const std::string& FileImpl::getMimeType(uint16_t idx) const
{
if (idx >= mimeTypes.size())
{
std::ostringstream msg;
msg << "unknown mime type code " << idx;
throw ZimFileFormatError(msg.str());
}
throw ZimFileFormatError(Formatter() << "unknown mime type code " << idx);

return mimeTypes[idx];
}
Expand Down
7 changes: 2 additions & 5 deletions src/fs_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/

#include "fs_unix.h"
#include "tools.h"
#include <stdexcept>
#include <sstream>

#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -138,10 +138,7 @@ bool FS::removeFile(path_t path) {

std::string getFilePathFromFD(int fd)
{
std::ostringstream oss;
oss << "/dev/fd/" << fd;

return oss.str();
return Formatter() << "/dev/fd/" << fd;
}

}; // zim namespace
Expand Down
28 changes: 12 additions & 16 deletions src/fs_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include "fs_windows.h"
#include "tools.h"
#include <stdexcept>

#include <windows.h>
Expand All @@ -27,7 +28,6 @@
#include <fileapi.h>

#include <iostream>
#include <sstream>

namespace zim {

Expand Down Expand Up @@ -135,11 +135,10 @@ std::unique_ptr<wchar_t[]> FS::toWideChar(path_t path)
auto wdata = std::unique_ptr<wchar_t[]>(new wchar_t[size]);
auto ret = MultiByteToWideChar(CP_UTF8, 0,
path.c_str(), -1, wdata.get(), size);
if (0 == ret) {
std::ostringstream oss;
oss << "Cannot convert path to wchar : " << GetLastError();
throw std::runtime_error(oss.str());
}
if (0 == ret)
throw std::runtime_error(Formatter() << "Cannot convert path to wchar : "
<< GetLastError());

return wdata;
}

Expand All @@ -154,11 +153,10 @@ FD FS::openFile(path_t filepath)
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY|FILE_FLAG_RANDOM_ACCESS,
NULL);
if (handle == INVALID_HANDLE_VALUE) {
std::ostringstream oss;
oss << "Cannot open file : " << GetLastError();
throw std::runtime_error(oss.str());
}
if (handle == INVALID_HANDLE_VALUE)
throw std::runtime_error(Formatter() oss << "Cannot open file : "
<< GetLastError());

return FD(handle);
}

Expand All @@ -173,11 +171,9 @@ bool FS::makeDirectory(path_t path)
void FS::rename(path_t old_path, path_t new_path)
{
auto ret = MoveFileExW(toWideChar(old_path).get(), toWideChar(new_path).get(), MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH);
if (!ret) {
std::ostringstream oss;
oss << "Cannot move file " << old_path << " to " << new_path;
throw std::runtime_error(oss.str());
}
if (!ret)
throw std::runtime_error(Formatter() << "Cannot move file " << old_path
<< " to " << new_path);
}

std::string FS::join(path_t base, path_t name)
Expand Down
9 changes: 5 additions & 4 deletions src/narrowdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define ZIM_NARROWDOWN_H

#include "debug.h"
#include "tools.h"

#include <algorithm>
#include <vector>
Expand Down Expand Up @@ -107,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) {
std::stringstream ss;
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.str());
throw ZimFileFormatError(ss);
}
if ( entries.empty() ) {
addEntry(key, i);
Expand All @@ -120,10 +121,10 @@ class NarrowDown
{
const std::string pseudoKey = shortestStringInBetween(key, nextKey);
if (pred(pseudoKey, entries.back())) {
std::stringstream ss;
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.str());
throw ZimFileFormatError(ss);
}
ASSERT(entries.back().lindex, <, i);
addEntry(pseudoKey, i);
Expand Down
Loading

0 comments on commit a058b22

Please sign in to comment.