diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:16:21 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:16:21 +0200 |
commit | 88a2d0007db222c339f0b6a17794a2014a241892 (patch) | |
tree | 75105ef49b3a52b7ee176a1ad480e7652e49825f /wx+ | |
parent | 4.2 (diff) | |
download | FreeFileSync-88a2d0007db222c339f0b6a17794a2014a241892.tar.gz FreeFileSync-88a2d0007db222c339f0b6a17794a2014a241892.tar.bz2 FreeFileSync-88a2d0007db222c339f0b6a17794a2014a241892.zip |
4.3
Diffstat (limited to 'wx+')
-rw-r--r-- | wx+/format_unit.cpp | 2 | ||||
-rw-r--r-- | wx+/format_unit.h | 2 | ||||
-rw-r--r-- | wx+/serialize.h | 114 |
3 files changed, 32 insertions, 86 deletions
diff --git a/wx+/format_unit.cpp b/wx+/format_unit.cpp index 994a2b29..361dbc62 100644 --- a/wx+/format_unit.cpp +++ b/wx+/format_unit.cpp @@ -135,7 +135,7 @@ std::wstring zen::remainingTimeToShortString(double timeInSec) } -std::wstring zen::percentageToShortString(double fraction) +std::wstring zen::fractionToShortString(double fraction) { return replaceCpy(_("%x%"), L"%x", printNumber<std::wstring>(L"%3.2f", fraction * 100.0), false); } diff --git a/wx+/format_unit.h b/wx+/format_unit.h index 4adf74f8..953910b6 100644 --- a/wx+/format_unit.h +++ b/wx+/format_unit.h @@ -15,7 +15,7 @@ namespace zen { std::wstring filesizeToShortString(UInt64 filesize); std::wstring remainingTimeToShortString(double timeInSec); -std::wstring percentageToShortString(double fraction); //within [0, 1] +std::wstring fractionToShortString(double fraction); //within [0, 1] template <class NumberType> std::wstring toStringSep(NumberType number); //convert number to std::wstring including thousands separator diff --git a/wx+/serialize.h b/wx+/serialize.h index cec70278..020b8709 100644 --- a/wx+/serialize.h +++ b/wx+/serialize.h @@ -7,11 +7,8 @@ #ifndef SERIALIZE_H_INCLUDED #define SERIALIZE_H_INCLUDED -#include <vector> #include <cstdint> -#include <memory> #include <wx/stream.h> -#include <zen/file_error.h> #include <zen/file_io.h> @@ -26,18 +23,13 @@ template <class S> void writeString(wxOutputStream& stream, const S& str); //############# wxWidgets stream adapter ############# -// can be used as base classes (have virtual destructors) class FileInputStream : public wxInputStream { public: - FileInputStream(const Zstring& filename) : //throw FileError - fileObj(filename) {} + FileInputStream(const Zstring& filename) : fileObj(filename) {} //throw FileError private: - virtual size_t OnSysRead(void* buffer, size_t bufsize) - { - return fileObj.read(buffer, bufsize); //throw FileError - } + virtual size_t OnSysRead(void* buffer, size_t bufsize) { return fileObj.read(buffer, bufsize); } //throw FileError zen::FileInput fileObj; }; @@ -46,8 +38,7 @@ private: class FileOutputStream : public wxOutputStream { public: - FileOutputStream(const Zstring& filename) : //throw FileError - fileObj(filename, zen::FileOutput::ACC_OVERWRITE) {} + FileOutputStream(const Zstring& filename) : fileObj(filename, zen::FileOutput::ACC_OVERWRITE) {} //throw FileError private: virtual size_t OnSysWrite(const void* buffer, size_t bufsize) @@ -60,11 +51,11 @@ private: }; - -class ReadInputStream //throw FileError +//wxInputStream proxy throwing FileError on error +class CheckedReader { -protected: - ReadInputStream(wxInputStream& stream, const Zstring& errorObjName) : stream_(stream), errorObjName_(errorObjName) {} +public: + CheckedReader(wxInputStream& stream, const Zstring& errorObjName) : stream_(stream), errorObjName_(errorObjName) {} template <class T> T readNumberC() const; //throw FileError, checked read operation @@ -72,23 +63,19 @@ protected: template <class S> S readStringC() const; //throw FileError, checked read operation - typedef std::shared_ptr<std::vector<char> > CharArray; //there's no guarantee std::string has a ref-counted implementation... so use this "thing" - CharArray readArrayC() const; //throw FileError - +private: void check() const; - wxInputStream& getStream() { return stream_; } - -private: wxInputStream& stream_; const Zstring& errorObjName_; //used for error text only }; -class WriteOutputStream //throw FileError +//wxOutputStream proxy throwing FileError on error +class CheckedWriter { -protected: - WriteOutputStream(const Zstring& errorObjName, wxOutputStream& stream) : stream_(stream), errorObjName_(errorObjName) {} +public: + CheckedWriter(wxOutputStream& stream, const Zstring& errorObjName) : stream_(stream), errorObjName_(errorObjName) {} template <class T> void writeNumberC(T number) const; //throw FileError, checked write operation @@ -96,13 +83,9 @@ protected: template <class S> void writeStringC(const S& str) const; //throw FileError, checked write operation - void writeArrayC(const std::vector<char>& buffer) const; //throw FileError - +private: void check() const; - wxOutputStream& getStream() { return stream_; } - -private: wxOutputStream& stream_; const Zstring& errorObjName_; //used for error text only! }; @@ -130,11 +113,6 @@ private: - - - - - //-----------------------implementation------------------------------- template <class T> inline T readPOD(wxInputStream& stream) @@ -155,21 +133,15 @@ void writePOD(wxOutputStream& stream, const T& pod) template <class S> inline S readString(wxInputStream& stream) { + //don't even consider UTF8 conversions here! "string" is expected to handle arbitrary binary data! + typedef typename S::value_type CharType; const auto strLength = readPOD<std::uint32_t>(stream); - if (strLength <= 1000) - { - CharType buffer[1000]; - stream.Read(buffer, sizeof(CharType) * strLength); - return S(buffer, strLength); - } - else - { - std::vector<CharType> buffer(strLength); //throw std::bad_alloc - stream.Read(&buffer[0], sizeof(CharType) * strLength); - return S(&buffer[0], strLength); - } + S output; + output.resize(strLength); //throw std::bad_alloc + stream.Read(&*output.begin(), sizeof(CharType) * strLength); + return output; } @@ -182,7 +154,7 @@ void writeString(wxOutputStream& stream, const S& str) inline -void ReadInputStream::check() const +void CheckedReader::check() const { if (stream_.GetLastError() != wxSTREAM_NO_ERROR) throw zen::FileError(_("Error reading from synchronization database:") + L" \n" + L"\"" + errorObjName_ + L"\""); @@ -191,7 +163,7 @@ void ReadInputStream::check() const template <class T> inline -T ReadInputStream::readNumberC() const //checked read operation +T CheckedReader::readNumberC() const //checked read operation { T output = readPOD<T>(stream_); check(); @@ -200,13 +172,15 @@ T ReadInputStream::readNumberC() const //checked read operation template <class S> inline -S ReadInputStream::readStringC() const //checked read operation +S CheckedReader::readStringC() const //checked read operation { S output; try { - output = readString<S>(stream_); //throw (std::bad_alloc) + output = readString<S>(stream_); //throw std::bad_alloc check(); + if (stream_.LastRead() != output.length() * sizeof(typename S::value_type)) //some additional check + throw FileError(_("Error reading from synchronization database:") + L" \n" + L"\"" + errorObjName_ + L"\""); } catch (std::exception&) { @@ -216,24 +190,8 @@ S ReadInputStream::readStringC() const //checked read operation } -inline -ReadInputStream::CharArray ReadInputStream::readArrayC() const -{ - const std::uint32_t byteCount = readNumberC<std::uint32_t>(); - CharArray buffer(new std::vector<char>(byteCount)); - if (byteCount > 0) - { - stream_.Read(&(*buffer)[0], byteCount); - check(); - if (stream_.LastRead() != byteCount) //some additional check - throw FileError(_("Error reading from synchronization database:") + L" \n" + L"\"" + errorObjName_ + L"\""); - } - return buffer; -} - - template <class T> inline -void WriteOutputStream::writeNumberC(T number) const //checked write operation +void CheckedWriter::writeNumberC(T number) const //checked write operation { writePOD<T>(stream_, number); check(); @@ -241,29 +199,17 @@ void WriteOutputStream::writeNumberC(T number) const //checked write operation template <class S> inline -void WriteOutputStream::writeStringC(const S& str) const //checked write operation +void CheckedWriter::writeStringC(const S& str) const //checked write operation { writeString(stream_, str); check(); + if (stream_.LastWrite() != str.length() * sizeof(typename S::value_type)) //some additional check + throw FileError(_("Error writing to synchronization database:") + L" \n" + L"\"" + errorObjName_ + L"\""); } inline -void WriteOutputStream::writeArrayC(const std::vector<char>& buffer) const -{ - writeNumberC<std::uint32_t>(static_cast<std::uint32_t>(buffer.size())); - if (buffer.size() > 0) - { - stream_.Write(&buffer[0], buffer.size()); - check(); - if (stream_.LastWrite() != buffer.size()) //some additional check - throw FileError(_("Error writing to synchronization database:") + L" \n" + L"\"" + errorObjName_ + L"\""); - } -} - - -inline -void WriteOutputStream::check() const +void CheckedWriter::check() const { if (stream_.GetLastError() != wxSTREAM_NO_ERROR) throw FileError(_("Error writing to synchronization database:") + L" \n" + L"\"" + errorObjName_ + L"\""); |