From 2b6f8123e925e3be8ce7c04eccdd49fc728314a5 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Tue, 3 Jan 2023 09:31:51 +0100 Subject: Separated out libcommon as libreichwein --- libcommon/Makefile | 39 ---------------------------- libcommon/file.cpp | 46 --------------------------------- libcommon/file.h | 15 ----------- libcommon/mime.cpp | 41 ------------------------------ libcommon/mime.h | 5 ---- libcommon/stringutil.cpp | 66 ------------------------------------------------ libcommon/stringutil.h | 10 -------- libcommon/tempfile.cpp | 42 ------------------------------ libcommon/tempfile.h | 17 ------------- libcommon/url.cpp | 32 ----------------------- libcommon/url.h | 6 ----- 11 files changed, 319 deletions(-) delete mode 100644 libcommon/Makefile delete mode 100644 libcommon/file.cpp delete mode 100644 libcommon/file.h delete mode 100644 libcommon/mime.cpp delete mode 100644 libcommon/mime.h delete mode 100644 libcommon/stringutil.cpp delete mode 100644 libcommon/stringutil.h delete mode 100644 libcommon/tempfile.cpp delete mode 100644 libcommon/tempfile.h delete mode 100644 libcommon/url.cpp delete mode 100644 libcommon/url.h (limited to 'libcommon') diff --git a/libcommon/Makefile b/libcommon/Makefile deleted file mode 100644 index ddff9fc..0000000 --- a/libcommon/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -# Static library to be included both in main program an in plugins (.so) - -include ../common.mk - -PROJECTNAME=libcommon - -CXXFLAGS+= -fvisibility=hidden -fPIC - -PROGSRC=\ - file.cpp \ - mime.cpp \ - stringutil.cpp \ - tempfile.cpp \ - url.cpp - -SRC=$(PROGSRC) - -all: $(PROJECTNAME).a - -$(PROJECTNAME).a: $(SRC:.cpp=.o) - ar rcs $@ $^ - -%.d: %.cpp - $(CXX) $(CXXFLAGS) -MM -MP -MF $@ -c $< - -%.o: %.cpp %.d - $(CXX) $(CXXFLAGS) -c $< -o $@ - -# dependencies - -ADD_DEP=Makefile - -# misc --------------------------------------------------- -clean: - -rm -f *.o *.a *.d - -.PHONY: clean all install - --include $(wildcard $(SRC:.cpp=.d)) diff --git a/libcommon/file.cpp b/libcommon/file.cpp deleted file mode 100644 index 47ab8be..0000000 --- a/libcommon/file.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "file.h" - -#include - -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(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& data) -{ - File::setFile(filename, reinterpret_cast(data.data()), data.size()); -} - diff --git a/libcommon/file.h b/libcommon/file.h deleted file mode 100644 index e7e4cf6..0000000 --- a/libcommon/file.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -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& data); - -} diff --git a/libcommon/mime.cpp b/libcommon/mime.cpp deleted file mode 100644 index f64d046..0000000 --- a/libcommon/mime.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "mime.h" - -#include -#include - -namespace beast = boost::beast; - -// Return a reasonable mime type based on the extension of a file. -std::string mime_type(const std::string& path) -{ - auto const ext = [&path] - { - auto const pos = path.rfind("."); - if (pos == std::string::npos) - return std::string{}; - return path.substr(pos); - }(); - if(boost::algorithm::istarts_with(ext, ".htm")) return "text/html"; - if(boost::algorithm::istarts_with(ext, ".html")) return "text/html"; - if(boost::algorithm::istarts_with(ext, ".php")) return "text/html"; - if(boost::algorithm::istarts_with(ext, ".css")) return "text/css"; - if(boost::algorithm::istarts_with(ext, ".txt")) return "text/plain"; - if(boost::algorithm::istarts_with(ext, ".js")) return "application/javascript"; - if(boost::algorithm::istarts_with(ext, ".json")) return "application/json"; - if(boost::algorithm::istarts_with(ext, ".xml")) return "application/xml"; - if(boost::algorithm::istarts_with(ext, ".swf")) return "application/x-shockwave-flash"; - if(boost::algorithm::istarts_with(ext, ".flv")) return "video/x-flv"; - if(boost::algorithm::istarts_with(ext, ".png")) return "image/png"; - if(boost::algorithm::istarts_with(ext, ".jpe")) return "image/jpeg"; - if(boost::algorithm::istarts_with(ext, ".jpeg")) return "image/jpeg"; - if(boost::algorithm::istarts_with(ext, ".jpg")) return "image/jpeg"; - if(boost::algorithm::istarts_with(ext, ".gif")) return "image/gif"; - if(boost::algorithm::istarts_with(ext, ".bmp")) return "image/bmp"; - if(boost::algorithm::istarts_with(ext, ".ico")) return "image/vnd.microsoft.icon"; - if(boost::algorithm::istarts_with(ext, ".tiff")) return "image/tiff"; - if(boost::algorithm::istarts_with(ext, ".tif")) return "image/tiff"; - if(boost::algorithm::istarts_with(ext, ".svg")) return "image/svg+xml"; - if(boost::algorithm::istarts_with(ext, ".svgz")) return "image/svg+xml"; - return "application/text"; -} - diff --git a/libcommon/mime.h b/libcommon/mime.h deleted file mode 100644 index c05eb45..0000000 --- a/libcommon/mime.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include - -std::string mime_type(const std::string& path); diff --git a/libcommon/stringutil.cpp b/libcommon/stringutil.cpp deleted file mode 100644 index f87fa00..0000000 --- a/libcommon/stringutil.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "stringutil.h" - -#include -#include - -#include - -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 split(std::string value, const std::string separators) -{ - std::vector 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 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 &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 deleted file mode 100644 index 5110e2e..0000000 --- a/libcommon/stringutil.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include -#include - -std::string strfmt(const char* fmt, ...); - -std::vector split(std::string value, const std::string separators = "\r\n "); -std::string join(std::vector vs, std::string separator = "\n"); -bool startsWithAnyOfLower(const std::string &s, const std::vector &list); diff --git a/libcommon/tempfile.cpp b/libcommon/tempfile.cpp deleted file mode 100644 index f425db2..0000000 --- a/libcommon/tempfile.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "tempfile.h" - -#include -#include -#include - -#include - -namespace fs = std::filesystem; -using namespace std::string_literals; - -fs::path Tempfile::GetPath() const -{ - return m_path; -} - -Tempfile::Tempfile(const std::filesystem::path& extension) -{ - try { - fs::path path { fs::temp_directory_path() / "tempfileXXXXXX"}; - if (!extension.empty()) - path += extension; - - fs::path::string_type name{path.native()}; - int fd = mkstemps(name.data(), extension.native().size()); - if (fd == -1) - std::runtime_error("mkstemps: "s + strerror(errno)); - close(fd); - m_path = std::string{name}; - } catch (const std::exception& ex) { - throw std::runtime_error("Tempfile error: "s + ex.what()); - } -} - -Tempfile::~Tempfile() -{ - try { - fs::remove_all(m_path); - } catch (const std::exception& ex) { - std::cerr << "Warning: Couldn't remove temporary file " << m_path << std::endl; - } -} diff --git a/libcommon/tempfile.h b/libcommon/tempfile.h deleted file mode 100644 index b9839a7..0000000 --- a/libcommon/tempfile.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include - -class Tempfile -{ - std::filesystem::path m_path; - - public: - std::filesystem::path GetPath() const; - - // extension: e.g. ".zip" - Tempfile(const std::filesystem::path& extension = std::filesystem::path{}); - ~Tempfile(); - }; - - diff --git a/libcommon/url.cpp b/libcommon/url.cpp deleted file mode 100644 index 5baf603..0000000 --- a/libcommon/url.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "url.h" - -std::string urlDecode(std::string s) -{ - std::string result; - - size_t pos = 0; - while (pos < s.size()) { - char c {s[pos]}; - if (c == '+') { - result += ' '; - } else if (c == '%' && pos + 2 < s.size()) { - try { - int i = stoi(s.substr(pos + 1, 2), 0, 16); - if (i < 0 || i > 255) - return result; - - result += static_cast(i); - } catch (...) { - return result; - } - - pos += 2; - } else { - result += c; - } - pos++; - } - - return result; -} - diff --git a/libcommon/url.h b/libcommon/url.h deleted file mode 100644 index bd60616..0000000 --- a/libcommon/url.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -#include - -std::string urlDecode(std::string s); - -- cgit v1.2.3