aboutsummaryrefslogtreecommitdiff
path: root/src/shared/shared.cc
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 18:05:18 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 18:05:18 +1100
commit1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (patch)
tree222dfcd07a1e40716127a347bbfd7119ce3d0984 /src/shared/shared.cc
initial commit
Diffstat (limited to 'src/shared/shared.cc')
-rw-r--r--src/shared/shared.cc57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/shared/shared.cc b/src/shared/shared.cc
new file mode 100644
index 0000000..55ccf1f
--- /dev/null
+++ b/src/shared/shared.cc
@@ -0,0 +1,57 @@
+#include "shared.hh"
+
+namespace shared {
+
+std::string make_string_lower(std::string str) noexcept {
+ std::ranges::transform(str.begin(), str.end(), str.begin(),
+ [](const auto c) { return std::tolower(c); });
+ return str;
+}
+
+std::string read_file(const std::string& path) {
+ std::ifstream input(path);
+ if (!input.is_open()) {
+ throw std::runtime_error("failed to read file: " + path);
+ }
+ return std::string{std::istreambuf_iterator<char>(input),
+ std::istreambuf_iterator<char>()};
+}
+
+std::ofstream open_file(const std::string& dir,
+ const std::ios_base::openmode mode) {
+ std::ofstream ret(dir, mode);
+ if (!ret.is_open()) {
+ throw std::runtime_error("failed to write file: " + dir);
+ }
+ return ret;
+}
+
+void compress_string(std::string& str) noexcept {
+ std::stringstream input(str);
+
+ boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
+ out.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(
+ boost::iostreams::gzip::best_compression)));
+ out.push(input);
+
+ std::stringstream result;
+ boost::iostreams::copy(out, result);
+
+ str = result.str();
+}
+
+void decompress_string(std::string& str) noexcept {
+ std::stringstream input;
+ input << str;
+
+ boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
+ in.push(boost::iostreams::gzip_decompressor());
+ in.push(input);
+
+ std::stringstream result;
+ boost::iostreams::copy(in, result);
+
+ str = result.str();
+}
+
+} // namespace shared