codex
Loading...
Searching...
No Matches
cmd.hpp
Go to the documentation of this file.
1#ifndef CODEX_BASE64_CMD_H
2#define CODEX_BASE64_CMD_H
3
4#include <concepts>
5#include <cstddef>
6#include <functional>
7#include <vector>
8
9#include "block.hpp"
10
14constexpr std::size_t BUF_SIZE = 32 * 1024;
15
16template<typename T> concept EncodeFunc =
17 std::invocable<T, std::string_view, char*>;
18
19template<typename T> concept DecodeFunc =
20 std::same_as<
21 std::invoke_result_t<T, std::string_view, char*>,
22 char*>;
23
27template<auto S, std::integral T>
28inline constexpr T round_to_block_size(T n);
29
36template<std::size_t N, std::size_t D>
37inline constexpr std::size_t output_size(std::size_t n);
38
39inline bool cmd_encode(EncodeFunc auto f);
40inline bool cmd_decode(DecodeFunc auto f);
41inline bool cmd_main(const char **argv, EncodeFunc auto ef, DecodeFunc auto df);
42
43template<auto S, std::integral T>
45 { return S * ((n + S - 1) / S); }
46
47template<std::size_t N, std::size_t D>
48constexpr std::size_t output_size(std::size_t n)
49 { return N * ((n + D - 1) / D); }
50
52 std::vector<char> in(3 * BUF_SIZE), out(output_size<4, 3>(in.size()));
53 return process_blocks(
54 stdin, stdout, 3, in, out,
55 [&in, &out, &f](std::size_t n) {
56 std::invoke(f, std::string_view{in.data(), n}, out.data());
57 return output_size<4, 3>(n);
58 });
59}
60
62 std::vector<char> in(4 * BUF_SIZE), out(output_size<3, 4>(in.size()));
63 return process_blocks(
64 stdin, stdout, 4, in, out,
65 [&in, &out, &f](std::size_t n) {
66 const auto *ret =
67 std::invoke(f, std::string_view{in.data(), n}, out.data());
68 if(!ret) [[unlikely]]
69 return (std::cerr << "invalid input\n"), std::size_t{};
70 return static_cast<std::size_t>(ret - out.data());
71 });
72}
73
74template<typename T>
75bool cmd_main(const char **argv) {
76 const bool encode = !argv[1];
77 if(!encode && strcmp(argv[1], "-d") != 0) {
78 std::cerr << "invalid command: " << argv[1] << '\n';
79 return 1;
80 }
81 return encode ? cmd_encode(&T::encode) : cmd_decode(&T::decode);
82}
83
84#define BASE64_MAIN(T) \
85 int main(int, const char **argv) { return !cmd_main<T>(argv); }
86
87#endif
#define f(x)
Definition 5.c:2
bool process_blocks(FILE *in, FILE *out, std::size_t size, std::span< char > in_s, std::span< char > out_s, ProcessingFunc auto f)
Reads block of a specific size, processes them, and outputs.
Definition block.hpp:80
bool cmd_encode(EncodeFunc auto f)
Definition cmd.hpp:51
bool cmd_decode(DecodeFunc auto f)
Definition cmd.hpp:61
bool cmd_main(const char **argv, EncodeFunc auto ef, DecodeFunc auto df)
constexpr std::size_t output_size(std::size_t n)
Calculates the size of the buffer required for the output, rounding up.
Definition cmd.hpp:48
constexpr T round_to_block_size(T n)
Rounds an integral value to a given block size, always towards infinity.
Definition cmd.hpp:44
constexpr std::size_t BUF_SIZE
Number of 3- or 4-byte blocks in the input buffer.
Definition cmd.hpp:14
Definition cmd.hpp:19
Definition cmd.hpp:16
void df(struct d *)
Definition main.cpp:7
codex::refl::field_tuple_t< E > T
Definition soa.cpp:9
Definition reflexpr.cpp:168