diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-24 14:13:34 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-24 14:13:34 +0200 |
| commit | b0d0e894a4394eed25dbb5cead12edd08a510a92 (patch) | |
| tree | 788915894a49d5267391f3bf6b9c41a1c5fe40b8 | |
| parent | d02bd0eb656b885bdf48088529183c40df8bd9c1 (diff) | |
feat! use `chunk_string` in the CGI module. Add additional constructors
for `chunk_string`
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | compile_flags.txt | 3 | ||||
| -rw-r--r-- | dev/examples/cgi/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | dev/examples/cgi/cgi.cc | 11 | ||||
| -rw-r--r-- | dev/examples/fix/fix.cc | 2 | ||||
| -rw-r--r-- | dev/lib/core/includes.hpp | 4 | ||||
| -rw-r--r-- | dev/lib/except/error.hpp | 4 | ||||
| -rw-r--r-- | dev/lib/fix/parser.hpp | 2 | ||||
| -rw-r--r-- | dev/lib/io/print.hpp | 1 | ||||
| -rw-r--r-- | dev/lib/logic/math.hpp | 4 | ||||
| -rw-r--r-- | dev/lib/memory/tracked_ptr.hpp | 30 | ||||
| -rw-r--r-- | dev/lib/utility/cgi_writer.hpp | 36 | ||||
| -rw-r--r-- | dev/lib/utility/chunk_string.hpp | 17 | ||||
| -rw-r--r-- | dev/tests/chunk_string/CMakeLists.txt | 3 |
13 files changed, 70 insertions, 51 deletions
diff --git a/compile_flags.txt b/compile_flags.txt index b820259..1c9f6fb 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,4 +1,5 @@ -Idev -std=c++20 -DEMBFS_28BIT_LBA --xc++
\ No newline at end of file +-xc++ +-I/opt/homebrew/Cellar/boost/1.87.0/include
\ No newline at end of file diff --git a/dev/examples/cgi/CMakeLists.txt b/dev/examples/cgi/CMakeLists.txt index 391899f..9c3cbf6 100644 --- a/dev/examples/cgi/CMakeLists.txt +++ b/dev/examples/cgi/CMakeLists.txt @@ -6,7 +6,11 @@ project( VERSION 1.0 LANGUAGES CXX) +find_package(Boost REQUIRED COMPONENTS container) + add_executable(CGI cgi.cc) +target_link_libraries(CGI PRIVATE Boost::container) + set_property(TARGET CGI PROPERTY CXX_STANDARD 20) target_include_directories(CGI PUBLIC ../../) diff --git a/dev/examples/cgi/cgi.cc b/dev/examples/cgi/cgi.cc index 4872cb5..7d8353a 100644 --- a/dev/examples/cgi/cgi.cc +++ b/dev/examples/cgi/cgi.cc @@ -5,11 +5,8 @@ */ #include <lib/utility/cgi_writer.hpp> -#include <fstream> -#include <sstream> -#include <string> -const std::string error_html = R"( +static snu::basic_chunk_string<char> text_sample = R"( <!DOCTYPE html> <html> <head> @@ -72,11 +69,7 @@ const std::string error_html = R"( int main(int argc, char** argv) { snu::cgi::basic_writer<> writer; - - std::stringstream ss_file; - ss_file << error_html; - - writer.html(ss_file); + writer << text_sample; return 0; } diff --git a/dev/examples/fix/fix.cc b/dev/examples/fix/fix.cc index ce48186..8730cad 100644 --- a/dev/examples/fix/fix.cc +++ b/dev/examples/fix/fix.cc @@ -4,7 +4,7 @@ licensed under the MIT license */ -#include <lib/net/network.hpp> +#include <lib/net/modem.hpp> #include <lib/fix/parser.hpp> #include <iostream> #include <unistd.h> diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp index 4beec1c..3238342 100644 --- a/dev/lib/core/includes.hpp +++ b/dev/lib/core/includes.hpp @@ -12,7 +12,3 @@ #include <boost/core/demangle.hpp> #include <boost/core/null_deleter.hpp> #include <boost/container/allocator.hpp> - -#include <filesystem> -#include <utility> -#include <memory> diff --git a/dev/lib/except/error.hpp b/dev/lib/except/error.hpp index 486d2e6..862dc3e 100644 --- a/dev/lib/except/error.hpp +++ b/dev/lib/except/error.hpp @@ -9,12 +9,12 @@ #include <stdexcept> -namespace snu::except +namespace snu { using runtime_error = std::runtime_error; using fix_error = runtime_error; using math_error = runtime_error; using cgi_error = runtime_error; -} // namespace snu::except +} // namespace snu #endif // _SNU_ERR_HPP
\ No newline at end of file diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index 8181359..1ace160 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -209,6 +209,8 @@ namespace snu::fix ::kill(::getpid(), SIGTRAP); } } + + using fix_tag_type = std::uint32_t; } // namespace snu::fix #endif // ifndef _SNU_FIX_PARSER_HPP diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index eb425c1..840b373 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -8,7 +8,6 @@ #ifndef _SNU_PRINT_HPP #define _SNU_PRINT_HPP -#include <initializer_list> #include <iostream> #include <ostream> diff --git a/dev/lib/logic/math.hpp b/dev/lib/logic/math.hpp index cab995a..155251b 100644 --- a/dev/lib/logic/math.hpp +++ b/dev/lib/logic/math.hpp @@ -9,7 +9,7 @@ #include <cmath> -namespace snu::math +namespace snu { template <std::size_t T> struct is_non_boolean_integer final @@ -32,4 +32,4 @@ namespace snu::math constexpr inline auto not_a_number = NAN; constexpr inline auto positive_infinity = INFINITY; constexpr inline auto negative_infinity = -positive_infinity; -} // namespace snu::math
\ No newline at end of file +} // namespace snu
\ No newline at end of file diff --git a/dev/lib/memory/tracked_ptr.hpp b/dev/lib/memory/tracked_ptr.hpp index da7e46c..7030f40 100644 --- a/dev/lib/memory/tracked_ptr.hpp +++ b/dev/lib/memory/tracked_ptr.hpp @@ -9,7 +9,7 @@ #include <cstddef> #include <utility> -#include <memory> +#include <new> #include <atomic> #include <sys/types.h> @@ -45,19 +45,24 @@ namespace snu::memory template <typename... U> void retain(T*& ptr, U&&... args) { - ptr = new T(args...); + ptr = new T(std::forward<U>(args)...); if (ptr) { ++allocated_count_; + return; } - else - { - throw std::bad_alloc(); - } + + throw std::bad_alloc(); + } + + template <typename... U> + void must_retain(T*& ptr, U&&... args) noexcept + { + this->retain(ptr, args...); } - void dispose(T*& ptr) + void dispose(T*& ptr) noexcept { if (ptr) { @@ -96,10 +101,17 @@ namespace snu::memory { T* ptr = nullptr; allocator_.retain(ptr, std::forward<U>(args)...); + return ptr; } - void dispose(T*& ptr) + template <typename... U> + T* must_retain(U&&... args) noexcept + { + return this->retain(std::forward<U>(args)...); + } + + void dispose(T*& ptr) noexcept { allocator_.dispose(ptr); } @@ -190,7 +202,7 @@ namespace snu::memory } private: - T* ptr_ = nullptr; + T* ptr_{nullptr}; }; template <typename T> diff --git a/dev/lib/utility/cgi_writer.hpp b/dev/lib/utility/cgi_writer.hpp index 69d0170..70b32cf 100644 --- a/dev/lib/utility/cgi_writer.hpp +++ b/dev/lib/utility/cgi_writer.hpp @@ -7,9 +7,10 @@ #ifndef _SNU_CGI_WRITER_HPP #define _SNU_CGI_WRITER_HPP -#include <cstdio> -#include <string> +#include <lib/io/print.hpp> +#include <lib/utility/chunk_string.hpp> #include <sstream> +#include <format> namespace snu { @@ -20,18 +21,16 @@ namespace snu class basic_writer { private: - basic_writer& eval_(const std::basic_string<char>& mime, const std::basic_stringstream<char_type>& ss) noexcept + basic_writer& eval_(const snu::basic_chunk_string<char_type>& mime, const snu::basic_chunk_string<char_type>& ss) noexcept { - std::printf("Content-Type: %s\r\n", mime.c_str()); - std::printf("Server: %s\r\n", "socl-cgi-system"); - std::printf("Content-Length: %ld\r\n\r\n", ss.str().size()); + std::basic_stringstream<char_type> ss_out; - auto ss_cc = ss.str(); + ss_out << std::format("Content-Type: {}\r\n", mime.str()); + ss_out << std::format("Server: {}\r\n", "SOCL-CGI/1.0"); + ss_out << std::format("Content-Length: {}\r\n\r\n", ss.str().size()); + ss_out << ss.str(); - for (auto& ch : ss_cc) - { - std::printf("%c", ch); - } + snu::io::print(ss_out.str()); return *this; } @@ -44,27 +43,32 @@ namespace snu basic_writer(const basic_writer&) = default; public: - basic_writer& binary(const std::basic_stringstream<char_type>& ss_in) + friend void operator<<(basic_writer& self, const snu::basic_chunk_string<char_type>& ss_in) + { + self = self.eval_("text/plain", ss_in); + } + + basic_writer& binary(const snu::basic_chunk_string<char_type>& ss_in) { return this->eval_("application/octet-stream", ss_in); } - basic_writer& html(const std::basic_stringstream<char_type>& ss_in) + basic_writer& html(const snu::basic_chunk_string<char_type>& ss_in) { return this->eval_("text/html", ss_in); } - basic_writer& xml(const std::basic_stringstream<char_type>& ss_in) + basic_writer& xml(const snu::basic_chunk_string<char_type>& ss_in) { return this->eval_("application/xml", ss_in); } - basic_writer& json(const std::basic_stringstream<char_type>& ss_in) + basic_writer& json(const snu::basic_chunk_string<char_type>& ss_in) { return this->eval_("application/json", ss_in); } - basic_writer& js(const std::basic_stringstream<char_type>& ss_in) + basic_writer& js(const snu::basic_chunk_string<char_type>& ss_in) { return this->eval_("text/javascript", ss_in); } diff --git a/dev/lib/utility/chunk_string.hpp b/dev/lib/utility/chunk_string.hpp index 6b060ae..79e0dd6 100644 --- a/dev/lib/utility/chunk_string.hpp +++ b/dev/lib/utility/chunk_string.hpp @@ -29,8 +29,19 @@ namespace snu constexpr const static auto max_chunk_size = 4096; public: - explicit basic_chunk_string() = default; - virtual ~basic_chunk_string() = default; + basic_chunk_string() = default; + + basic_chunk_string(const char_type* in) + { + this->operator+=(in); + } + + basic_chunk_string(const std::basic_string<char_type>& in) + { + this->operator+=(in); + } + + ~basic_chunk_string() = default; basic_chunk_string& operator=(const basic_chunk_string&) = default; basic_chunk_string(const basic_chunk_string&) = default; @@ -56,7 +67,7 @@ namespace snu return *this; } - const std::basic_string<char_type>& str() noexcept + const std::basic_string<char_type>& str() const noexcept { return packed_chunks_; } diff --git a/dev/tests/chunk_string/CMakeLists.txt b/dev/tests/chunk_string/CMakeLists.txt index ae0e1e7..5a4a27c 100644 --- a/dev/tests/chunk_string/CMakeLists.txt +++ b/dev/tests/chunk_string/CMakeLists.txt @@ -21,8 +21,5 @@ target_link_libraries(BasicChunkUsage PRIVATE gtest_main Boost::container) set_property(TARGET BasicChunkUsage PROPERTY CXX_STANDARD 20) target_include_directories(BasicChunkUsage PUBLIC ../../) -# FIXME: Make a pkg-config for the boost library here! -target_include_directories(BasicChunkUsage PUBLIC /opt/homebrew/Cellar/boost/1.87.0/include) - include(GoogleTest) gtest_discover_tests(BasicChunkUsage) |
