Zipper concept clarified.

This commit is contained in:
tamasmeszaros 2018-09-19 13:43:15 +02:00
parent 6b655f9aa3
commit 2056f4c336
4 changed files with 94 additions and 81 deletions

View File

@ -73,19 +73,21 @@ template<class T = void> struct VeryFalse { static const bool value = false; };
// This has to be explicitly implemented in the gui layer or a default zlib // This has to be explicitly implemented in the gui layer or a default zlib
// based implementation is needed. // based implementation is needed.
template<class Backend> class Zipper { template<class Backend> class LayerWriter {
public: public:
Zipper(const std::string& /*zipfile_path*/) { LayerWriter(const std::string& /*zipfile_path*/) {
static_assert(VeryFalse<Backend>::value, static_assert(VeryFalse<Backend>::value,
"No zipper implementation provided!"); "No layer writer implementation provided!");
} }
void next_entry(const std::string& /*fname*/) {} void next_entry(const std::string& /*fname*/) {}
std::string get_name() { return ""; } std::string get_name() { return ""; }
template<class T> Zipper& operator<<(const T& /*arg*/) { bool is_ok() { return false; }
template<class T> LayerWriter& operator<<(const T& /*arg*/) {
return *this; return *this;
} }
@ -203,62 +205,29 @@ public:
inline void save(const std::string& path) { inline void save(const std::string& path) {
try { try {
Zipper<LyrFormat> zipper(path); LayerWriter<LyrFormat> writer(path);
std::string project = zipper.get_name(); std::string project = writer.get_name();
zipper.next_entry(project); writer.next_entry("config.ini");
zipper << createIniContent(project); writer << createIniContent(project);
for(unsigned i = 0; i < layers_rst_.size(); i++) { for(unsigned i = 0; i < layers_rst_.size(); i++) {
if(layers_rst_[i].second.rdbuf()->in_avail() > 0) { if(layers_rst_[i].second.rdbuf()->in_avail() > 0) {
char lyrnum[6]; char lyrnum[6];
std::sprintf(lyrnum, "%.5d", i); std::sprintf(lyrnum, "%.5d", i);
auto zfilename = project + lyrnum + ".png"; auto zfilename = project + lyrnum + ".png";
zipper.next_entry(zfilename); writer.next_entry(zfilename);
zipper << layers_rst_[i].second.rdbuf(); writer << layers_rst_[i].second.rdbuf();
layers_rst_[i].second.str(""); layers_rst_[i].second.str("");
} }
} }
zipper.close(); writer.close();
} catch(std::exception&) { } catch(std::exception& e) {
BOOST_LOG_TRIVIAL(error) << "Can't create zip file for layers! " BOOST_LOG_TRIVIAL(error) << e.what();
<< path;
return; return;
} }
// wxFileName filepath(path);
// wxFFileOutputStream zipfile(path);
// std::string project = filepath.GetName().ToStdString();
// if(!zipfile.IsOk()) {
// BOOST_LOG_TRIVIAL(error) << "Can't create zip file for layers! "
// << path;
// return;
// }
// wxZipOutputStream zipstream(zipfile);
// wxStdOutputStream pngstream(zipstream);
// zipstream.PutNextEntry("config.ini");
// pngstream << createIniContent(project);
// for(unsigned i = 0; i < layers_rst_.size(); i++) {
// if(layers_rst_[i].second.rdbuf()->in_avail() > 0) {
// char lyrnum[6];
// std::sprintf(lyrnum, "%.5d", i);
// auto zfilename = project + lyrnum + ".png";
// zipstream.PutNextEntry(zfilename);
// pngstream << layers_rst_[i].second.rdbuf();
// layers_rst_[i].second.str("");
// }
// }
// zipstream.Close();
// zipfile.Close();
} }
void saveLayer(unsigned lyr, const std::string& path) { void saveLayer(unsigned lyr, const std::string& path) {

View File

@ -18,9 +18,6 @@
#include <Model.hpp> #include <Model.hpp>
#include <Utils.hpp> #include <Utils.hpp>
#include <wx/stdstream.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
namespace Slic3r { namespace Slic3r {
@ -290,41 +287,21 @@ void PrintController::slice()
slice(pri); slice(pri);
} }
struct wxZipper {}; template<> class LayerWriter<Zipper> {
Zipper m_zip;
template<> class Zipper<wxZipper> {
wxFileName m_fpath;
wxFFileOutputStream m_zipfile;
wxZipOutputStream m_zipstream;
wxStdOutputStream m_pngstream;
public: public:
Zipper(const std::string& zipfile_path): inline LayerWriter(const std::string& zipfile_path): m_zip(zipfile_path) {}
m_fpath(zipfile_path),
m_zipfile(zipfile_path), inline void next_entry(const std::string& fname) { m_zip.next_entry(fname); }
m_zipstream(m_zipfile),
m_pngstream(m_zipstream) inline std::string get_name() const { return m_zip.get_name(); }
{
if(!m_zipfile.IsOk()) template<class T> inline LayerWriter& operator<<(const T& arg) {
throw std::runtime_error(L("Cannot create zip file.")); m_zip.stream() << arg; return *this;
} }
void next_entry(const std::string& fname) { inline void close() { m_zip.close(); }
m_zipstream.PutNextEntry(fname);
}
std::string get_name() {
return m_fpath.GetName().ToStdString();
}
template<class T> Zipper& operator<<(const T& arg) {
m_pngstream << arg; return *this;
}
void close() {
m_zipstream.Close();
m_zipfile.Close();
}
}; };
void PrintController::slice_to_png() void PrintController::slice_to_png()
@ -428,7 +405,7 @@ void PrintController::slice_to_png()
}); });
try { try {
print_to<FilePrinterFormat::PNG, wxZipper>( *print, exd.zippath, print_to<FilePrinterFormat::PNG, Zipper>( *print, exd.zippath,
exd.width_mm, exd.height_mm, exd.width_mm, exd.height_mm,
exd.width_px, exd.height_px, exd.width_px, exd.height_px,
exd.exp_time_s, exd.exp_time_first_s); exd.exp_time_s, exd.exp_time_first_s);
@ -439,6 +416,7 @@ void PrintController::slice_to_png()
scale_back(); scale_back();
if(print->canceled()) print->restart(); if(print->canceled()) print->restart();
print->set_status_default();
} }
const PrintConfig &PrintController::config() const const PrintConfig &PrintController::config() const

View File

@ -165,6 +165,23 @@ protected:
ProgresIndicatorPtr global_progressind_; ProgresIndicatorPtr global_progressind_;
}; };
class Zipper {
struct Impl;
std::unique_ptr<Impl> m_impl;
public:
Zipper(const std::string& zipfilepath);
~Zipper();
void next_entry(const std::string& fname);
std::string get_name() const;
std::ostream& stream();
void close();
};
/** /**
* @brief Implementation of the printing logic. * @brief Implementation of the printing logic.
*/ */

View File

@ -1,5 +1,9 @@
#include "AppController.hpp" #include "AppController.hpp"
#include <wx/stdstream.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include <thread> #include <thread>
#include <future> #include <future>
@ -98,6 +102,51 @@ bool AppControllerBoilerplate::report_issue(
wxDEFINE_EVENT(PROGRESS_STATUS_UPDATE_EVENT, wxCommandEvent); wxDEFINE_EVENT(PROGRESS_STATUS_UPDATE_EVENT, wxCommandEvent);
struct Zipper::Impl {
wxFileName m_fpath;
wxFFileOutputStream m_zipfile;
wxZipOutputStream m_zipstream;
wxStdOutputStream m_pngstream;
Impl(const std::string& zipfile_path):
m_fpath(zipfile_path),
m_zipfile(zipfile_path),
m_zipstream(m_zipfile),
m_pngstream(m_zipstream)
{
if(!m_zipfile.IsOk())
throw std::runtime_error(L("Cannot create zip file."));
}
};
Zipper::Zipper(const std::string &zipfilepath)
{
m_impl.reset(new Impl(zipfilepath));
}
Zipper::~Zipper() {}
void Zipper::next_entry(const std::string &fname)
{
m_impl->m_zipstream.PutNextEntry(fname);
}
std::string Zipper::get_name() const
{
return m_impl->m_fpath.GetName().ToStdString();
}
std::ostream &Zipper::stream()
{
return m_impl->m_pngstream;
}
void Zipper::close()
{
m_impl->m_zipstream.Close();
m_impl->m_zipfile.Close();
}
namespace { namespace {
/* /*