-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from NuiCpp/feat/bin2hpp_compression
Added base64 and compressed index.hpp encoding.
- Loading branch information
Showing
16 changed files
with
323 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
if (CMAKE_VERSION VERSION_LESS "3.30") | ||
find_package(Boost 1.81.0 REQUIRED COMPONENTS system) | ||
find_package(Boost 1.81.0 REQUIRED COMPONENTS system iostreams) | ||
else() | ||
find_package(Boost CONFIG 1.81.0 REQUIRED COMPONENTS system) | ||
find_package(Boost CONFIG 1.81.0 REQUIRED COMPONENTS system iostreams) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "base64_encoder.hpp" | ||
#include "raw_encoder.hpp" | ||
#include "constants.hpp" | ||
|
||
#include <roar/utility/base64.hpp> | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
|
||
std::ostream& Base64Encoder::header(std::ostream& output) const | ||
{ | ||
output << | ||
R"(#pragma once | ||
#include <roar/utility/base64.hpp> | ||
#include <string_view> | ||
#include <string> | ||
static const std::string_view )" | ||
<< name_ << R"(_data[] = {)"; | ||
|
||
return output; | ||
} | ||
|
||
std::ostream& Base64Encoder::content(std::ostream& output, std::istream& input) const | ||
{ | ||
std::stringstream readStream; | ||
readStream << input.rdbuf(); | ||
|
||
auto encoded = Roar::base64Encode(std::move(readStream).str()); | ||
|
||
std::cout << "Encoded size: " << encoded.size() << "\n"; | ||
|
||
std::stringstream ss(std::move(encoded)); | ||
|
||
RawEncoder rawEncoder(name_); | ||
return rawEncoder.content(output, ss); | ||
} | ||
|
||
std::ostream& Base64Encoder::index(std::ostream& output) const | ||
{ | ||
output << "};\n\n"; | ||
output << "static std::string " << name_ << "()\n"; | ||
output << "{\n"; | ||
output << "\tstatic std::string memo;\n"; | ||
output << "\tif(!memo.empty())\n"; | ||
output << "\t\treturn memo;\n"; | ||
output << "\tfor (std::size_t i = 0; i != sizeof(" << name_ << "_data) / sizeof(std::string_view)" | ||
<< "; ++i) {\n"; | ||
output << "\t\tmemo += " << name_ << "_data[i];\n"; | ||
output << "\t}\n\n"; | ||
output << "\tmemo = Roar::base64Decode(memo);\n"; | ||
output << "\treturn memo;\n"; | ||
output << "}\n"; | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include "encoder.hpp" | ||
|
||
class Base64Encoder : public Encoder | ||
{ | ||
public: | ||
using Encoder::Encoder; | ||
|
||
std::ostream& header(std::ostream& output) const override; | ||
std::ostream& content(std::ostream& output, std::istream& input) const override; | ||
std::ostream& index(std::ostream& output) const override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "compressed_encoder.hpp" | ||
#include "raw_encoder.hpp" | ||
#include "constants.hpp" | ||
|
||
#include <roar/utility/base64.hpp> | ||
#include <boost/iostreams/filtering_stream.hpp> | ||
#include <boost/iostreams/copy.hpp> | ||
#include <boost/iostreams/filter/gzip.hpp> | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
|
||
std::ostream& CompressedEncoder::header(std::ostream& output) const | ||
{ | ||
output << | ||
R"(#pragma once | ||
#include <roar/utility/base64.hpp> | ||
#include <boost/iostreams/filtering_stream.hpp> | ||
#include <boost/iostreams/copy.hpp> | ||
#include <boost/iostreams/filter/gzip.hpp> | ||
#include <string_view> | ||
#include <string> | ||
#include <sstream> | ||
static const std::string_view )" | ||
<< name_ << R"(_data[] = {)"; | ||
|
||
return output; | ||
} | ||
|
||
std::ostream& CompressedEncoder::content(std::ostream& output, std::istream& input) const | ||
{ | ||
boost::iostreams::filtering_streambuf<boost::iostreams::input> in; | ||
in.push(boost::iostreams::gzip_compressor()); | ||
in.push(input); | ||
|
||
std::stringstream compressedRaw; | ||
boost::iostreams::copy(in, compressedRaw); | ||
|
||
auto encoded = Roar::base64Encode(std::move(compressedRaw).str()); | ||
std::stringstream ss{std::move(encoded)}; | ||
|
||
RawEncoder rawEncoder(name_); | ||
return rawEncoder.content(output, ss); | ||
} | ||
|
||
std::ostream& CompressedEncoder::index(std::ostream& output) const | ||
{ | ||
output << "};\n\n"; | ||
output << "static std::string " << name_ << "()\n"; | ||
output << "{\n"; | ||
output << "\tstatic std::string memo;\n"; | ||
output << "\tif(!memo.empty())\n"; | ||
output << "\t\treturn memo;\n\n"; | ||
output << "\tstd::string compressed;\n"; | ||
output << "\tfor (std::size_t i = 0; i != sizeof(" << name_ << "_data) / sizeof(std::string_view)" | ||
<< "; ++i) {\n"; | ||
output << "\t\tcompressed += " << name_ << "_data[i];\n"; | ||
output << "\t}\n\n"; | ||
output << "\tcompressed = Roar::base64Decode(compressed);\n"; | ||
output << "\tstd::stringstream compressedStream{compressed};\n"; | ||
output << "\tboost::iostreams::filtering_streambuf<boost::iostreams::input> in;\n"; | ||
output << "\tin.push(boost::iostreams::gzip_decompressor());\n"; | ||
output << "\tin.push(compressedStream);\n"; | ||
output << "\tstd::stringstream decompressed;\n"; | ||
output << "\tboost::iostreams::copy(in, decompressed);\n"; | ||
output << "\tmemo = std::move(decompressed).str();\n"; | ||
output << "\treturn memo;\n"; | ||
output << "}\n"; | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include "encoder.hpp" | ||
|
||
class CompressedEncoder : public Encoder | ||
{ | ||
public: | ||
using Encoder::Encoder; | ||
|
||
std::ostream& header(std::ostream& output) const override; | ||
std::ostream& content(std::ostream& output, std::istream& input) const override; | ||
std::ostream& index(std::ostream& output) const override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
constexpr std::size_t lineWidth = 120; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <iosfwd> | ||
#include <string> | ||
|
||
class Encoder | ||
{ | ||
public: | ||
Encoder(std::string name) | ||
: name_(std::move(name)) | ||
{} | ||
|
||
virtual std::ostream& header(std::ostream& output) const = 0; | ||
virtual std::ostream& content(std::ostream& output, std::istream& input) const = 0; | ||
virtual std::ostream& index(std::ostream& output) const = 0; | ||
|
||
virtual ~Encoder() = default; | ||
Encoder(Encoder const&) = default; | ||
Encoder(Encoder&&) = default; | ||
Encoder& operator=(Encoder const&) = default; | ||
Encoder& operator=(Encoder&&) = default; | ||
|
||
protected: | ||
std::string name_; | ||
}; |
Oops, something went wrong.