Maze Builder Docs 8.10.23
Loading...
Searching...
No Matches
output_formats.h
Go to the documentation of this file.
1#ifndef OUTPUT_FORMATS_H
2#define OUTPUT_FORMATS_H
3
4#include <algorithm>
5#include <array>
6#include <cctype>
7#include <iterator>
8#include <ranges>
9#include <stdexcept>
10#include <string>
11#include <string_view>
12
15namespace mazes
16{
18 enum class output_format : unsigned int
19 {
20 JPG = 0,
21 JPEG = 1,
22 BMP = 2,
23 PLAIN_TEXT = 3,
24 PLAIN_TEXT_ALT = 4,
25 PNG = 5,
26 JSON = 6,
27 OBJ = 7,
28 STDOUT = 8,
29 B64 = 9,
30 TOTAL = 10
31 };
32
33 constexpr std::array<std::string_view, static_cast<size_t>(output_format::TOTAL)> OUTPUT_FORMAT_LABELS_LOWERCASE = {
34 "jpg",
35 "jpeg",
36 "bmp",
37 "text",
38 "txt",
39 "png",
40 "json",
41 "obj",
42 "stdout",
43 "b64"};
44
48 inline std::string_view to_sv_from_output_format(output_format of)
49 {
50 switch (of)
51 {
52 case output_format::JPG:
53 return OUTPUT_FORMAT_LABELS_LOWERCASE[0];
54 case output_format::JPEG:
55 return OUTPUT_FORMAT_LABELS_LOWERCASE[1];
56 case output_format::BMP:
57 return OUTPUT_FORMAT_LABELS_LOWERCASE[2];
58 case output_format::PLAIN_TEXT:
59 return OUTPUT_FORMAT_LABELS_LOWERCASE[3];
60 case output_format::PLAIN_TEXT_ALT:
61 return OUTPUT_FORMAT_LABELS_LOWERCASE[4];
62 case output_format::PNG:
63 return OUTPUT_FORMAT_LABELS_LOWERCASE[5];
64 case output_format::JSON:
65 return OUTPUT_FORMAT_LABELS_LOWERCASE[6];
66 case output_format::OBJ:
67 return OUTPUT_FORMAT_LABELS_LOWERCASE[7];
68 case output_format::STDOUT:
69 return OUTPUT_FORMAT_LABELS_LOWERCASE[8];
70 case output_format::B64:
71 return OUTPUT_FORMAT_LABELS_LOWERCASE[9];
72 default:
73 throw std::invalid_argument("Invalid output_format: " + std::to_string(static_cast<unsigned int>(of)));
74 }
75 }
76
80 inline output_format to_output_format_from_sv(const std::string_view sv)
81 {
82 if (sv.empty())
83 {
84 throw std::invalid_argument("Invalid output_format: empty");
85 }
86
87 std::string normalized{};
88 normalized.reserve(sv.size());
89 for (const unsigned char ch : sv)
90 {
91 normalized.push_back(static_cast<char>(std::tolower(ch)));
92 }
93
94 if (const auto it = std::ranges::find(OUTPUT_FORMAT_LABELS_LOWERCASE, normalized); it != OUTPUT_FORMAT_LABELS_LOWERCASE.cend())
95 {
96 const auto index = static_cast<size_t>(std::distance(OUTPUT_FORMAT_LABELS_LOWERCASE.cbegin(), it));
97 switch (index)
98 {
99 case 0:
100 return output_format::JPG;
101 case 1:
102 return output_format::JPEG;
103 case 2:
104 return output_format::BMP;
105 case 3:
106 return output_format::PLAIN_TEXT;
107 case 4:
108 return output_format::PLAIN_TEXT_ALT;
109 case 5:
110 return output_format::PNG;
111 case 6:
112 return output_format::JSON;
113 case 7:
114 return output_format::OBJ;
115 case 8:
116 return output_format::STDOUT;
117 case 9:
118 return output_format::B64;
119 default:
120 break;
121 }
122 }
123 throw std::invalid_argument("Invalid output_format: " + std::string{sv});
124 }
125
130 inline output_format stdout_format_from_sv(const std::string_view sv) noexcept
131 {
132 std::string normalized{};
133 normalized.reserve(sv.size());
134 for (const unsigned char ch : sv)
135 {
136 normalized.push_back(static_cast<char>(std::tolower(ch)));
137 }
138
139 if (normalized == OUTPUT_FORMAT_LABELS_LOWERCASE[static_cast<size_t>(output_format::STDOUT)])
140 {
141 return output_format::STDOUT;
142 }
143 if (normalized == OUTPUT_FORMAT_LABELS_LOWERCASE[static_cast<size_t>(output_format::OBJ)])
144 {
145 return output_format::OBJ;
146 }
147 if (normalized == OUTPUT_FORMAT_LABELS_LOWERCASE[static_cast<size_t>(output_format::B64)])
148 {
149 return output_format::B64;
150 }
151 return output_format::TOTAL;
152 }
153
158 inline output_format output_format_or_default(const std::string_view sv,
159 const output_format fallback = output_format::PLAIN_TEXT) noexcept
160 {
161 if (sv.empty())
162 {
163 return fallback;
164 }
165
166 try
167 {
168 return to_output_format_from_sv(sv);
169 }
170 catch (...)
171 {
172 return fallback;
173 }
174 }
175} // namespace mazes
176
177#endif // OUTPUT_FORMATS_H
output_format output_format_or_default(const std::string_view sv, const output_format fallback=output_format::PLAIN_TEXT) noexcept
Converts a string to an output_format enum, or returns a fallback if invalid.
Definition output_formats.h:158
std::string_view to_sv_from_output_format(output_format of)
Convert an output_format enum to a string.
Definition output_formats.h:48
output_format
Enum class for output_format types.
Definition output_formats.h:19
output_format to_output_format_from_sv(const std::string_view sv)
Convert a string to an output_format enum.
Definition output_formats.h:80
output_format stdout_format_from_sv(const std::string_view sv) noexcept
Check whether a bare format keyword (no file extension) targets stdout.
Definition output_formats.h:130