diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-09-04 11:25:04 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-04 11:25:04 +0200 |
| commit | 6789dd7d88a192e3f55b95798cb393e7d12f368a (patch) | |
| tree | 7f04815ad5214f97d0fb2becceceed7ce8089b3d /dev/lib | |
| parent | 443588a42fe9cf48b5f63184b94afe483cb0e761 (diff) | |
| parent | fda7082c54ad46a56ac885d4686b82bad8dbc7c9 (diff) | |
Merge pull request #4 from amlel-el-mahrouss/devv1.0.43
OCL — v1.0.43
Diffstat (limited to 'dev/lib')
| -rw-r--r-- | dev/lib/core/allocator_system.hpp | 75 | ||||
| -rw-r--r-- | dev/lib/core/error_handler.hpp | 35 | ||||
| -rw-r--r-- | dev/lib/core/includes.hpp | 10 | ||||
| -rw-r--r-- | dev/lib/except/error.hpp | 12 | ||||
| -rw-r--r-- | dev/lib/fix/parser.hpp | 28 | ||||
| -rw-r--r-- | dev/lib/io/print.hpp | 17 | ||||
| -rw-r--r-- | dev/lib/logic/equiv.hpp | 16 | ||||
| -rw-r--r-- | dev/lib/logic/math.hpp | 6 | ||||
| -rw-r--r-- | dev/lib/logic/opt.hpp | 12 | ||||
| -rw-r--r-- | dev/lib/memory/tracked_ptr.hpp | 44 | ||||
| -rw-r--r-- | dev/lib/net/modem.hpp | 156 | ||||
| -rw-r--r-- | dev/lib/net/network.hpp | 137 | ||||
| -rw-r--r-- | dev/lib/net/url.hpp | 10 | ||||
| -rw-r--r-- | dev/lib/tests/gtest.hpp | 6 | ||||
| -rw-r--r-- | dev/lib/tests/hpptest.hpp | 15 | ||||
| -rw-r--r-- | dev/lib/utility/cgi.hpp | 69 | ||||
| -rw-r--r-- | dev/lib/utility/cgi_writer.hpp | 79 | ||||
| -rw-r--r-- | dev/lib/utility/chunk_string.hpp | 102 | ||||
| -rw-r--r-- | dev/lib/utility/crc32.hpp | 12 | ||||
| -rw-r--r-- | dev/lib/utility/embfs.hpp | 14 |
20 files changed, 554 insertions, 301 deletions
diff --git a/dev/lib/core/allocator_system.hpp b/dev/lib/core/allocator_system.hpp new file mode 100644 index 0000000..1243ed5 --- /dev/null +++ b/dev/lib/core/allocator_system.hpp @@ -0,0 +1,75 @@ +/* + * File: core/allocator_system.hpp + * Purpose: Allocator System container. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss. Licensed under the BSL 1.0 license + */ + +#ifndef _OCL_ALLOCATOR_SYSTEM_HPP +#define _OCL_ALLOCATOR_SYSTEM_HPP + +#include <lib/core/includes.hpp> +#include <stdexcept> +#include <memory> + +namespace ocl +{ + template <typename type> + struct new_op final + { + inline auto operator()() -> type* + { + return new type; + } + + template <typename... var_type> + inline auto var_alloc(var_type... args) -> type* + { + return new type{args...}; + } + }; + + template <typename type> + struct delete_op final + { + inline auto operator()(type* t) -> void + { + delete t; + } + }; + + template <typename ret_type, typename allocator_new, typename allocator_delete> + class allocator_system + { + allocator_new alloc_; + allocator_delete del_; + + public: + allocator_system() = default; + ~allocator_system() = default; + + allocator_system& operator=(const allocator_system&) = delete; + allocator_system(const allocator_system&) = delete; + + ret_type* claim() noexcept + { + return alloc_(); + } + + template <typename... var_type> + auto construct(var_type... args) -> std::shared_ptr<ret_type> + { + return std::shared_ptr<ret_type>(alloc_.template var_alloc<var_type...>(args...), allocator_delete{}); + } + + void unclaim(ret_type* ptr) + { + del_(ptr); + } + }; + + template <typename type> + using standard_allocator_type = allocator_system<type, new_op<type>, delete_op<type>>; +} // namespace ocl + +#endif // ifndef _OCL_ALLOCATOR_SYSTEM_HPP diff --git a/dev/lib/core/error_handler.hpp b/dev/lib/core/error_handler.hpp new file mode 100644 index 0000000..593e54a --- /dev/null +++ b/dev/lib/core/error_handler.hpp @@ -0,0 +1,35 @@ +/* +* File: core/basic_error_handler.hpp +* Purpose: Error handler container. +* Author: Amlal El Mahrouss (amlal@nekernel.org) +* Copyright 2025, Amlal El Mahrouss. +*/ + +#ifndef _OCL_ERROR_HANDLER_HPP +#define _OCL_ERROR_HANDLER_HPP + +#include <lib/core/includes.hpp> +#include <stdexcept> + +namespace ocl +{ + struct basic_error_handler; + + struct basic_error_handler + { + explicit basic_error_handler() = default; + virtual ~basic_error_handler() = default; + + basic_error_handler& operator=(const basic_error_handler&) = delete; + basic_error_handler(const basic_error_handler&) = delete; + + virtual void operator()(const std::basic_string<char>& msg) + { + ((void)msg); + } + }; + + using standard_error_handler = basic_error_handler; +} // namespace ocl + +#endif // ifndef _OCL_ERROR_HANDLER_HPP diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp index e482a7c..f988fc1 100644 --- a/dev/lib/core/includes.hpp +++ b/dev/lib/core/includes.hpp @@ -1,8 +1,8 @@ /* * File: core/includes.hpp - * Purpose: Core includes for the SOCL library. - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + * Purpose: Core includes for the OCL library. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss */ #pragma once @@ -11,6 +11,4 @@ #include <boost/core/nvp.hpp> #include <boost/core/demangle.hpp> #include <boost/core/null_deleter.hpp> -#include <memory> -#include <utility> -#include <filesystem> +#include <boost/container/allocator.hpp> diff --git a/dev/lib/except/error.hpp b/dev/lib/except/error.hpp index 486d2e6..16bf5eb 100644 --- a/dev/lib/except/error.hpp +++ b/dev/lib/except/error.hpp @@ -1,20 +1,20 @@ /* * File: opt.hpp * Author: Amlal El Mahrouss, - * Copyright 2023-2025, Amlal El Mahrouss/SNU Systems Corp. + * Copyright 2023-2025, Amlal El Mahrouss */ -#ifndef _SNU_ERR_HPP -#define _SNU_ERR_HPP +#ifndef _OCL_ERR_HPP +#define _OCL_ERR_HPP #include <stdexcept> -namespace snu::except +namespace ocl { 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 ocl -#endif // _SNU_ERR_HPP
\ No newline at end of file +#endif // _OCL_ERR_HPP
\ No newline at end of file diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index 8181359..723506e 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -1,12 +1,12 @@ /* * File: fix/parser.hpp * Purpose: Financial Information Exchange parser in C++ - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss */ -#ifndef _SNU_FIX_PARSER_HPP -#define _SNU_FIX_PARSER_HPP +#ifndef _OCL_FIX_PARSER_HPP +#define _OCL_FIX_PARSER_HPP #include <cstddef> #include <cassert> @@ -18,7 +18,7 @@ #include <unistd.h> #include <signal.h> -namespace snu::fix +namespace ocl::fix { template <typename char_type> class basic_visitor; @@ -60,8 +60,8 @@ namespace snu::fix template <typename char_type = char> struct basic_range final { - char_type* bytes_; - size_t length_; + char_type* bytes_{nullptr}; + size_t length_{}; bool is_valid() noexcept { @@ -77,7 +77,7 @@ namespace snu::fix /// @brief Convert basic_range to usable string. /// @note This function assumes that the basic_range is valid and contains ASCII bytes. template <typename char_type = char> - std::basic_string<char_type> to_string(basic_range<char_type>& basic_range) noexcept + inline std::basic_string<char_type> to_string(basic_range<char_type>& basic_range) noexcept { if (basic_range.length_ < 0) return std::basic_string<char_type>{}; @@ -201,14 +201,16 @@ namespace snu::fix } }; - template <typename char_type = char> - inline void must_pass(basic_range_data<char_type>& basic_range) + template <typename char_type = char, typename error_handler> + inline void must_pass(basic_range_data<char_type>& basic_range, error_handler& handler) { if (!basic_range.is_valid()) { - ::kill(::getpid(), SIGTRAP); + handler("Invalid FIX packet"); } } -} // namespace snu::fix -#endif // ifndef _SNU_FIX_PARSER_HPP + using fix_tag_type = std::uint32_t; +} // namespace ocl::fix + +#endif // ifndef _OCL_FIX_PARSER_HPP diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index eb425c1..4b32ddb 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -1,18 +1,17 @@ /* * File: print.hpp - * Purpose: SNU Print library - * Author: Amlal El Mahrouss. (founder@snu.systems) - * Copyright 2025, SNU Systems Corp. + * Purpose: OCL Print library + * Author: Amlal El Mahrouss. (amlal@nekernel.org) + * Copyright 2025 */ -#ifndef _SNU_PRINT_HPP -#define _SNU_PRINT_HPP +#ifndef _OCL_PRINT_HPP +#define _OCL_PRINT_HPP -#include <initializer_list> #include <iostream> #include <ostream> -namespace snu::io +namespace ocl::io { template <typename T, typename... Args> inline void print(T fmt, Args... other) noexcept @@ -37,6 +36,6 @@ namespace snu::io { print(fmt...); } -} // namespace snu::io +} // namespace ocl::io -#endif // ifndef _SNU_PRINT_HPP +#endif // ifndef _OCL_PRINT_HPP diff --git a/dev/lib/logic/equiv.hpp b/dev/lib/logic/equiv.hpp index 2f63673..5b022f8 100644 --- a/dev/lib/logic/equiv.hpp +++ b/dev/lib/logic/equiv.hpp @@ -1,13 +1,14 @@ /* * File: equiv.hpp * Purpose: Equivalence runtime c++ header. - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss */ #pragma once -namespace snu::equiv +/// @brief OCL equivalence namespace. +namespace ocl::equiv { template <typename T> struct basic_hash_trait @@ -59,7 +60,7 @@ namespace snu::equiv struct equiv_is_int8 { private: - T left_ = 255, right_ = 255; + T left_ = 127, right_ = 127; public: using result = T; @@ -74,7 +75,8 @@ namespace snu::equiv struct equiv_not_int8 { private: - T left_ = 255, right_ = 255; + // these shall overflow if not int8. + T left_ = 127, right_ = 127; public: using result = T; @@ -96,7 +98,7 @@ namespace snu::equiv constexpr result hash() { - return left_ / right_; + return left_ / right_ == 1; } }; -} // namespace snu::equiv
\ No newline at end of file +} // namespace ocl::equiv diff --git a/dev/lib/logic/math.hpp b/dev/lib/logic/math.hpp index cab995a..e796eae 100644 --- a/dev/lib/logic/math.hpp +++ b/dev/lib/logic/math.hpp @@ -1,7 +1,7 @@ /* * File: math.hpp * Purpose: Mathematics c++ header. - * Author: Amlal El Mahrouss (founder@snu.systems) + * Author: Amlal El Mahrouss (amlal@nekernel.org) * Copyright 2025, Amlal El Mahrouss. */ @@ -9,7 +9,7 @@ #include <cmath> -namespace snu::math +namespace ocl { 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 ocl
\ No newline at end of file diff --git a/dev/lib/logic/opt.hpp b/dev/lib/logic/opt.hpp index a8e66b4..442756c 100644 --- a/dev/lib/logic/opt.hpp +++ b/dev/lib/logic/opt.hpp @@ -1,16 +1,16 @@ /* * File: opt.hpp * Author: Amlal El Mahrouss, - * Copyright 2023-2025, Amlal El Mahrouss/SNU Systems Corp. + * Copyright 2023-2025, Amlal El Mahrouss */ -#ifndef _SNU_OPT_HPP -#define _SNU_OPT_HPP +#ifndef _OCL_OPT_HPP +#define _OCL_OPT_HPP #include <lib/except/error.hpp> #include <utility> -namespace snu +namespace ocl { enum class return_type { @@ -116,6 +116,6 @@ namespace snu { return return_type::err; } -} // namespace snu +} // namespace ocl -#endif /* ifndef _SNU_OPT_HPP */ +#endif /* ifndef _OCL_OPT_HPP */ diff --git a/dev/lib/memory/tracked_ptr.hpp b/dev/lib/memory/tracked_ptr.hpp index c767b67..d2f8450 100644 --- a/dev/lib/memory/tracked_ptr.hpp +++ b/dev/lib/memory/tracked_ptr.hpp @@ -1,22 +1,22 @@ /* * File: memory/tracked_ptr.hpp * Purpose: Custom smart pointer implementation in C++ - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss */ #pragma once #include <cstddef> #include <utility> -#include <memory> +#include <new> #include <atomic> #include <sys/types.h> #include <unistd.h> #include <signal.h> -namespace snu::memory +namespace ocl::memory { template <typename T> class tracked_allocator; @@ -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); } @@ -128,7 +140,7 @@ namespace snu::memory this->reset(); } - tracked_ptr(const tracked_ptr&) = delete; + tracked_ptr(const tracked_ptr&) = delete; tracked_ptr& operator=(const tracked_ptr&) = delete; public: @@ -190,13 +202,13 @@ namespace snu::memory } private: - T* ptr_ = nullptr; + T* ptr_{nullptr}; }; template <typename T> - inline auto make_tracked(T arg) -> tracked_ptr<T> + inline auto make_tracked() -> tracked_ptr<T> { - return tracked_ptr<T>(std::forward<T>(arg)); + return tracked_ptr<T>(); } template <typename U, typename... T> @@ -220,4 +232,4 @@ namespace snu::memory ::kill(::getpid(), SIGTRAP); } } -} // namespace snu::memory
\ No newline at end of file +} // namespace ocl::memory diff --git a/dev/lib/net/modem.hpp b/dev/lib/net/modem.hpp new file mode 100644 index 0000000..074f182 --- /dev/null +++ b/dev/lib/net/modem.hpp @@ -0,0 +1,156 @@ +/* + * File: net/modem.hpp + * Purpose: Modem concept in modern C++ + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss + */ + +#ifndef _OCL_NET_NETWORK_HPP +#define _OCL_NET_NETWORK_HPP + +#include <unistd.h> +#include <arpa/inet.h> +#include <sys/socket.h> +#include <string> +#include <utility> +#include <cstddef> + +#define OCL_MODEM_INTERFACE : public ocl::net::basic_modem + +namespace ocl::net +{ + class basic_modem; + + typedef int64_t socket_type; + + /// @brief Modem container concept, a container to read and write on a network stream. + class basic_modem + { + private: + socket_type fd_{}; + bool server_fd_{false}; + bool bad_{true}; + + public: + const bool& bad{bad_}; + + explicit basic_modem() = default; + + virtual ~basic_modem() + { + this->destroy(); + } + + basic_modem& operator=(const basic_modem&) = delete; + basic_modem(const basic_modem&) = delete; + + static constexpr auto local_address_ip4 = "127.0.0.1"; + static constexpr auto local_address_ip6 = "::1"; + static constexpr const auto backlog_count = 5U; + + bool is_valid() const noexcept + { + return this->fd_ != -1 && !this->bad_; + } + + template <typename ptr_type> + bool receive(ptr_type& out, std::size_t len) noexcept + { + static_assert(std::is_pointer<ptr_type>::value, "ptr_type is not a pointer!"); + + if (!out) + return false; + + if (!len) + return false; + + socket_type cl_{fd_}; + + if (this->server_fd_) + cl_ = ::accept(fd_, nullptr, nullptr); + + auto ret = ::recv(cl_, out, len, 0); + + return ret > 0L; + } + + template <typename ptr_type> + bool transmit(ptr_type& out, std::size_t len) noexcept + { + static_assert(std::is_pointer<ptr_type>::value, "ptr_type is not a pointer!"); + + if (!out) + return false; + + if (!len) + return false; + + auto ret = ::send(fd_, out, len, 0); + + bad_ = !(ret >= 0L); + + return ret >= 0L; + } + + template <typename char_type> + bool transmit(const std::basic_string<char_type>& out) noexcept + { + if (out.empty()) + return false; + + auto ret = ::send(fd_, out.data(), out.size(), 0); + + bad_ = !(ret >= 0L); + + return ret >= 0L; + } + + template <int32_t af, int32_t kind, int32_t port> + bool construct(const char* addr = basic_modem::local_address_ip4, const bool& is_server = false) noexcept + { + static_assert(af != 0, "Address family is zero"); + static_assert(kind != 0, "Kind is zero"); + + fd_ = ::socket(af, kind, 0); + server_fd_ = is_server; + + if (fd_ == -1) + return false; + + struct sockaddr_in addr_; + std::memset(&addr_, 0, sizeof(struct sockaddr_in)); + + addr_.sin_addr.s_addr = ::inet_addr(addr); + addr_.sin_port = htons(port); + addr_.sin_family = af; + + if (!is_server) + { + const auto ret = ::connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(addr_)); + return ret == 0L; + } + + ::bind(fd_, (struct sockaddr*)&addr_, sizeof(addr_)); + ::listen(fd_, basic_modem::backlog_count); + + bad_ = false; + + return true; + } + + bool destroy() noexcept + { + if (!fd_) + return false; + + ::shutdown(fd_, SHUT_RDWR); + ::close(fd_); + + fd_ = 0L; + + return true; + } + }; +} // namespace ocl::net + +#endif // ifndef _OCL_NET_NETWORK_HPP diff --git a/dev/lib/net/network.hpp b/dev/lib/net/network.hpp deleted file mode 100644 index 67f3740..0000000 --- a/dev/lib/net/network.hpp +++ /dev/null @@ -1,137 +0,0 @@ -/* - * File: net/network.hpp - * Purpose: Modem concept in modern C++ - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. - */ - -#ifndef _SNU_NET_NETWORK_HPP -#define _SNU_NET_NETWORK_HPP - -#include <unistd.h> -#include <arpa/inet.h> -#include <sys/socket.h> -#include <string> -#include <utility> -#include <cstddef> - -#define SNU_MODEM_INTERFACE : protected snu::net::basic_modem - -namespace snu::net -{ - template <typename char_type> - class basic_modem; - - typedef int64_t socket_type; - - /// @brief Delivery modem concept, a container to read and write on a network stream. - template <typename char_type> - class basic_modem - { - public: - explicit basic_modem() = default; - virtual ~basic_modem() = default; - - basic_modem& operator=(const basic_modem&) = default; - basic_modem(const basic_modem&) = default; - - static constexpr auto local_address_ip6 = "127.0.0.1"; - static constexpr auto local_address_ip4 = "::1"; - - static constexpr auto backlog_count = 18U; - - socket_type fd_{}; - - bool is_valid() const noexcept - { - return this->fd_ != -1; - } - - template <typename ptr_type> - bool receive(ptr_type& out, std::size_t len) noexcept - { - static_assert(std::is_pointer<ptr_type>::value, "ptr_type is not a pointer!"); - - if (!out) - return false; - - if (!len) - return false; - - auto ret = ::recv(fd_, out, len, 0); - - return ret > 0; - } - - template <typename ptr_type> - bool transmit(ptr_type& out, std::size_t len) noexcept - { - static_assert(std::is_pointer<ptr_type>::value, "char_type is not a pointer!"); - - if (!out) - return false; - - if (!len) - return false; - - auto ret = ::send(fd_, out, len, 0); - - return ret > 0; - } - - template <typename ptr_type> - bool transmit(std::basic_string<ptr_type> out) noexcept - { - if (out.empty()) - return false; - - auto ret = ::send(fd_, out.data(), out.size(), 0); - - return ret > 0; - } - - template <int32_t af, int32_t kind, int32_t ip_proto, int32_t port> - bool construct(const char* addr = basic_modem::local_address_ip4, const bool& is_server = false) noexcept - { - static_assert(af != 0, "Address family is zero"); - static_assert(kind != 0, "Type is zero"); - - fd_ = ::socket(af, kind, ip_proto); - - if (fd_ == -1) - return false; - - struct sockaddr_in addr_ - { - }; - - addr_.sin_addr.s_addr = ::inet_addr(addr); - addr_.sin_port = port; - - if (!is_server) - { - const auto ret = ::connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(decltype(addr_))); - return ret == 0; - } - - ::listen(fd_, basic_modem::backlog_count); - - return true; - } - - bool destroy() noexcept - { - if (!fd_) - return false; - - ::shutdown(fd_, SHUT_RDWR); - ::close(fd_); - - fd_ = 0U; - - return true; - } - }; -} // namespace snu::net - -#endif // ifndef _SNU_NET_NETWORK_HPP diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index f6dba95..ff6aebe 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -1,8 +1,8 @@ /* * File: net/url.hpp * Purpose: URL container in modern C++ - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss */ #pragma once @@ -11,9 +11,9 @@ #include <iostream> #include <sstream> -/// @author Amlal El Mahrouss (founder@snu.systems) +/// @author Amlal El Mahrouss (amlal@nekernel.org) -namespace snu::net +namespace ocl::net { template <typename char_type> class basic_url; @@ -61,4 +61,4 @@ namespace snu::net return ss_.size() > 0; } }; -} // namespace snu::net +} // namespace ocl::net diff --git a/dev/lib/tests/gtest.hpp b/dev/lib/tests/gtest.hpp index 14474c0..deb2ddf 100644 --- a/dev/lib/tests/gtest.hpp +++ b/dev/lib/tests/gtest.hpp @@ -1,8 +1,8 @@ /* * File: tests/gtest.hpp - * Purpose: Google Test wrapper for the SOCL library. - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + * Purpose: Google Test wrapper for the OCL library. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss */ #include <gtest/gtest.h> diff --git a/dev/lib/tests/hpptest.hpp b/dev/lib/tests/hpptest.hpp index 4c99ce6..f520339 100644 --- a/dev/lib/tests/hpptest.hpp +++ b/dev/lib/tests/hpptest.hpp @@ -1,22 +1,21 @@ /* * File: tests/hpptest.hpp - * Purpose: HPP Test wrapper for the SOCL library. - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + * Purpose: HPP Test wrapper for the OCL library. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss */ #pragma once - -#ifdef SOCL_HPPTEST -namespace snu::hpptest +#ifdef OCL_HPPTEST +namespace ocl::hpptest { typedef bool condition_type; template <condition_type expr = true> consteval inline void must_pass() { - SOCL_HPPTEST_ASSERT(expr); + OCL_HPPTEST_ASSERT(expr); } -} // namespace snu::hpptest +} // namespace ocl::hpptest #endif diff --git a/dev/lib/utility/cgi.hpp b/dev/lib/utility/cgi.hpp deleted file mode 100644 index 57625b8..0000000 --- a/dev/lib/utility/cgi.hpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * File: cgi.hpp - * Author: Amlal El Mahrouss, - * Copyright 2023-2025, Amlal El Mahrouss. - */ - -#ifndef _SNU_CGI_HPP -#define _SNU_CGI_HPP - -#include <cstdio> -#include <string> -#include <sstream> - -namespace snu -{ - namespace cgi - { - /// @brief CGI Writer class, writes to stdout; as CGI expects. - template <typename char_type = char> - class basic_writer - { - private: - basic_writer& eval_(const std::basic_string<char>& mime, const std::basic_stringstream<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::printf("%s", ss.str().c_str()); - - return *this; - } - - public: - explicit basic_writer() = default; - ~basic_writer() = default; - - basic_writer& operator=(const basic_writer&) = default; - basic_writer(const basic_writer&) = default; - - public: - basic_writer& binary(const std::basic_stringstream<char_type>& ss_in) - { - return this->eval_("application/octet-stream", ss_in); - } - - basic_writer& html(const std::basic_stringstream<char_type>& ss_in) - { - return this->eval_("text/html", ss_in); - } - - basic_writer& xml(const std::basic_stringstream<char_type>& ss_in) - { - return this->eval_("application/xml", ss_in); - } - - basic_writer& json(const std::basic_stringstream<char_type>& ss_in) - { - return this->eval_("application/json", ss_in); - } - - basic_writer& js(const std::basic_stringstream<char_type>& ss_in) - { - return this->eval_("text/javascript", ss_in); - } - }; - } // namespace cgi -} // namespace snu - -#endif // ifndef _SNU_CGI_HPP
\ No newline at end of file diff --git a/dev/lib/utility/cgi_writer.hpp b/dev/lib/utility/cgi_writer.hpp new file mode 100644 index 0000000..126b299 --- /dev/null +++ b/dev/lib/utility/cgi_writer.hpp @@ -0,0 +1,79 @@ +/* + * File: cgi_writer.hpp + * Author: Amlal El Mahrouss, + * Copyright 2023-2025, Amlal El Mahrouss. + */ + +#ifndef _OCL_CGI_WRITER_HPP +#define _OCL_CGI_WRITER_HPP + +#include <lib/io/print.hpp> +#include <lib/utility/chunk_string.hpp> +#include <sstream> +#include <format> + +namespace ocl +{ + namespace cgi + { + /// @brief CGI Writer class, writes to stdout; as CGI expects. + template <typename char_type = char> + class basic_writer + { + private: + basic_writer& eval_(const ocl::basic_chunk_string<char_type>& mime, const ocl::basic_chunk_string<char_type>& ss) noexcept + { + std::basic_stringstream<char_type> ss_out; + + ss_out << std::format("Content-Type: {}\r\n", mime.str()); + ss_out << std::format("Server: {}\r\n", "OCL-CGI/1.0"); + ss_out << std::format("Content-Length: {}\r\n\r\n", ss.str().size()); + ss_out << ss.str(); + + ocl::io::print(ss_out.str()); + + return *this; + } + + public: + explicit basic_writer() = default; + ~basic_writer() = default; + + basic_writer& operator=(const basic_writer&) = default; + basic_writer(const basic_writer&) = default; + + public: + friend void operator<<(basic_writer& self, const ocl::basic_chunk_string<char_type>& ss_in) + { + self = self.eval_("text/plain", ss_in); + } + + basic_writer& binary(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("application/octet-stream", ss_in); + } + + basic_writer& html(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("text/html", ss_in); + } + + basic_writer& xml(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("application/xml", ss_in); + } + + basic_writer& json(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("application/json", ss_in); + } + + basic_writer& js(const ocl::basic_chunk_string<char_type>& ss_in) + { + return this->eval_("text/javascript", ss_in); + } + }; + } // namespace cgi +} // namespace ocl + +#endif // ifndef _OCL_CGI_WRITER_HPP diff --git a/dev/lib/utility/chunk_string.hpp b/dev/lib/utility/chunk_string.hpp new file mode 100644 index 0000000..4fe5cc2 --- /dev/null +++ b/dev/lib/utility/chunk_string.hpp @@ -0,0 +1,102 @@ +/* +* File: core/chunk_string.hpp + * Purpose: String implementation for the OCL C++ library. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss + */ + +#ifndef OCL_UTILITY_CHUNK_STRING_HPP +#define OCL_UTILITY_CHUNK_STRING_HPP + +#include <lib/core/includes.hpp> + +namespace ocl +{ + template <typename char_type, std::size_t max_chunk_size = 8196> + class basic_chunk_string; + + template <typename char_type, std::size_t max_chunk_size> + class basic_chunk_string final + { + private: + char_type packed_chunks_[max_chunk_size] = {0}; + std::size_t chunk_total_{}; + + bool bad_{false}; + + public: + const bool& bad{bad_}; + + 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&) = delete; + basic_chunk_string(const basic_chunk_string&) = delete; + + public: + basic_chunk_string& operator+=(const std::basic_string<char_type>& in) + { + if (in.empty() || bad_) + return *this; + + if (in.size() > max_chunk_size) + { + bad_ = true; + return *this; + } + + if (chunk_total_ > max_chunk_size) + { + bad_ = true; + return *this; + } + + const auto& sz = in.size(); + const static auto size_max_chunk = max_chunk_size; + const auto& ptr = in.data(); + + if (chunk_total_ < size_max_chunk) + { + std::memcpy(packed_chunks_ + chunk_total_, ptr, sz); + chunk_total_ += sz; + } + + return *this; + } + + std::basic_string<char_type> str() const noexcept + { + static std::basic_string<char_type> ret; + + if (ret.size() > 0) + ret.clear(); + + ret += packed_chunks_; + + return ret; + } + + void print() noexcept + { + ocl::io::print(packed_chunks_); + } + }; + + template <typename char_type> + inline void print(basic_chunk_string<char_type>& fmt) noexcept + { + fmt.print(); + } +} // namespace ocl +#endif // ifndef OCL_UTILITY_CHUNK_STRING_HPP diff --git a/dev/lib/utility/crc32.hpp b/dev/lib/utility/crc32.hpp index f7e5fc9..ea09b94 100644 --- a/dev/lib/utility/crc32.hpp +++ b/dev/lib/utility/crc32.hpp @@ -5,17 +5,17 @@ * Copyright 2025, Amlal El Mahrouss. */ -#ifndef _SNU_CRC32_HPP -#define _SNU_CRC32_HPP +#ifndef _OCL_CRC32_HPP +#define _OCL_CRC32_HPP #include <cstdint> #include <string> #include <cstddef> /// @brief Crc32 implementation in C++ -/// @author Amlal EL Mahrouss (founder@snu.systems) +/// @author Amlal EL Mahrouss (amlal@nekernel.org) -namespace snu::crc32 +namespace ocl::crc32 { namespace detail { @@ -76,6 +76,6 @@ namespace snu::crc32 { return detail::crc32(in.c_str(), in.size()); } -} // namespace snu::crc32 +} // namespace ocl::crc32 -#endif // !_SNU_CRC32_HPP
\ No newline at end of file +#endif // !_OCL_CRC32_HPP
\ No newline at end of file diff --git a/dev/lib/utility/embfs.hpp b/dev/lib/utility/embfs.hpp index 3acc867..0f20596 100644 --- a/dev/lib/utility/embfs.hpp +++ b/dev/lib/utility/embfs.hpp @@ -1,20 +1,20 @@ /* * File: embfs.hpp * Purpose: Embedded File System. - * Author: Amlal El Mahrouss (founder@snu.systems) + * Author: Amlal El Mahrouss (amlal@nekernel.org) * Copyright 2025, Amlal El Mahrouss. */ -#ifndef _SNU_EMBFS_HPP -#define _SNU_EMBFS_HPP +#ifndef _OCL_EMBFS_HPP +#define _OCL_EMBFS_HPP #include <cstdint> #include <cstddef> /// @brief A filesystem designed for tiny storage medias. -/// @author Amlal EL Mahrouss (founder@snu.systems) +/// @author Amlal EL Mahrouss (amlal@nekernel.org) -namespace snu::embfs +namespace ocl::embfs { namespace traits { @@ -75,6 +75,6 @@ namespace snu::embfs /// @brief Indexed node linear array. typedef embfs_inode embfs_inode_arr_t[_inode_arr_len]; } // namespace traits -} // namespace snu::embfs +} // namespace ocl::embfs -#endif // ifndef _SNU_EMBFS_HPP
\ No newline at end of file +#endif // ifndef _OCL_EMBFS_HPP
\ No newline at end of file |
