summaryrefslogtreecommitdiffhomepage
path: root/dev/lib/utility
diff options
context:
space:
mode:
Diffstat (limited to 'dev/lib/utility')
-rw-r--r--dev/lib/utility/cgi.hpp69
-rw-r--r--dev/lib/utility/cgi_writer.hpp79
-rw-r--r--dev/lib/utility/chunk_string.hpp102
-rw-r--r--dev/lib/utility/crc32.hpp12
-rw-r--r--dev/lib/utility/embfs.hpp14
5 files changed, 194 insertions, 82 deletions
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