summaryrefslogtreecommitdiffhomepage
path: root/libcommon
diff options
context:
space:
mode:
Diffstat (limited to 'libcommon')
-rw-r--r--libcommon/Makefile5
-rw-r--r--libcommon/file.cpp46
-rw-r--r--libcommon/file.h15
-rw-r--r--libcommon/stringutil.cpp66
-rw-r--r--libcommon/stringutil.h10
5 files changed, 141 insertions, 1 deletions
diff --git a/libcommon/Makefile b/libcommon/Makefile
index 3314549..932ee90 100644
--- a/libcommon/Makefile
+++ b/libcommon/Makefile
@@ -59,7 +59,10 @@ LIBS+= \
endif
PROGSRC=\
- mime.cpp
+ file.cpp \
+ mime.cpp \
+ stringutil.cpp \
+ tempfile.cpp
TESTSRC=\
test-webserver.cpp \
diff --git a/libcommon/file.cpp b/libcommon/file.cpp
new file mode 100644
index 0000000..47ab8be
--- /dev/null
+++ b/libcommon/file.cpp
@@ -0,0 +1,46 @@
+#include "file.h"
+
+#include <fstream>
+
+namespace fs = std::filesystem;
+
+using namespace std::string_literals;
+
+std::string File::getFile(const fs::path& filename)
+{
+ std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate);
+
+ if (file.is_open()) {
+ std::ifstream::pos_type fileSize = file.tellg();
+ file.seekg(0, std::ios::beg);
+
+ std::string bytes(fileSize, ' ');
+ file.read(reinterpret_cast<char*>(bytes.data()), fileSize);
+
+ return bytes;
+
+ } else {
+ throw std::runtime_error("Opening "s + filename.string() + " for reading");
+ }
+}
+
+void File::setFile(const fs::path& filename, const std::string& s)
+{
+ File::setFile(filename, s.data(), s.size());
+}
+
+void File::setFile(const fs::path& filename, const char* data, size_t size)
+{
+ std::ofstream file(filename.string(), std::ios::out | std::ios::binary);
+ if (file.is_open()) {
+ file.write(data, size);
+ } else {
+ throw std::runtime_error("Opening "s + filename.string() + " for writing");
+ }
+}
+
+void File::setFile(const fs::path& filename, const std::vector<uint8_t>& data)
+{
+ File::setFile(filename, reinterpret_cast<const char*>(data.data()), data.size());
+}
+
diff --git a/libcommon/file.h b/libcommon/file.h
new file mode 100644
index 0000000..e7e4cf6
--- /dev/null
+++ b/libcommon/file.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <cstdint>
+#include <filesystem>
+#include <string>
+#include <vector>
+
+namespace File {
+
+std::string getFile(const std::filesystem::path& filename);
+void setFile(const std::filesystem::path& filename, const std::string& s);
+void setFile(const std::filesystem::path& filename, const char* data, size_t size);
+void setFile(const std::filesystem::path& filename, const std::vector<uint8_t>& data);
+
+}
diff --git a/libcommon/stringutil.cpp b/libcommon/stringutil.cpp
new file mode 100644
index 0000000..f87fa00
--- /dev/null
+++ b/libcommon/stringutil.cpp
@@ -0,0 +1,66 @@
+#include "stringutil.h"
+
+#include <boost/algorithm/string/predicate.hpp>
+#include <boost/algorithm/string/case_conv.hpp>
+
+#include <cstdarg>
+
+std::string strfmt(const char* fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ int size = std::vsnprintf(nullptr, 0, fmt, args);
+ va_end(args);
+
+ std::string result(size, ' ');
+
+ va_start(args, fmt);
+ std::vsnprintf(result.data(), size + 1, fmt, args);
+ va_end(args);
+
+ return result;
+}
+
+std::vector<std::string> split(std::string value, const std::string separators)
+{
+ std::vector<std::string> result;
+
+ size_t pos0 = 0;
+ size_t pos1 = 0;
+ while (pos0 < value.size()) {
+ pos1 = value.find_first_of(separators, pos0);
+ if (pos1 == std::string::npos)
+ pos1 = value.size();
+ std::string part = value.substr(pos0, pos1 - pos0);
+ //std::cout << "DEBUG: " << part << std::endl << std::flush;
+ if (part != "")
+ result.push_back(part);
+ pos0 = value.find_first_not_of(separators, pos1);
+ if (pos0 == std::string::npos)
+ pos0 = value.size();
+ }
+
+ return result;
+}
+
+std::string join(std::vector<std::string> vs, std::string separator)
+{
+ std::string s;
+ for (const auto& line : vs) {
+ if (s.size() > 0)
+ s += separator;
+ s += line;
+ }
+
+ return s;
+}
+
+bool startsWithAnyOfLower(const std::string &s, const std::vector<std::string> &list) {
+ for (const std::string& element : list) {
+ if (boost::algorithm::starts_with(boost::algorithm::to_lower_copy(s), boost::algorithm::to_lower_copy(element)))
+ return true;
+ }
+ return false;
+}
+
diff --git a/libcommon/stringutil.h b/libcommon/stringutil.h
new file mode 100644
index 0000000..5110e2e
--- /dev/null
+++ b/libcommon/stringutil.h
@@ -0,0 +1,10 @@
+#pragma once
+
+#include <string>
+#include <vector>
+
+std::string strfmt(const char* fmt, ...);
+
+std::vector<std::string> split(std::string value, const std::string separators = "\r\n ");
+std::string join(std::vector<std::string> vs, std::string separator = "\n");
+bool startsWithAnyOfLower(const std::string &s, const std::vector<std::string> &list);