Malloy
Toggle main menu visibility
Loading...
Searching...
No Matches
lib
malloy
core
utils.hpp
1
#pragma once
2
3
#include <boost/asio/buffer.hpp>
4
#include <boost/beast/core/bind_handler.hpp>
5
#include <boost/beast/core/buffers_to_string.hpp>
6
7
#include <filesystem>
8
#include <fstream>
9
10
namespace
malloy
11
{
12
using
boost::asio::buffer;
13
using
boost::beast::buffers_to_string;
14
using
boost::beast::bind_front_handler;
15
22
[[nodiscard]]
23
static
24
inline
25
std::string
26
file_contents(
const
std::filesystem::path& path)
27
{
28
// Sanity check
29
if
(!std::filesystem::is_regular_file(path))
30
return
{ };
31
32
// Open the file
33
// Note that we have to use binary mode as we want to return a string
34
// representing matching the bytes of the file on the file system.
35
std::ifstream file(path, std::ios::in | std::ios::binary);
36
if
(!file.is_open())
37
return
{ };
38
39
// Read contents
40
std::string content{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
41
42
// Close the file
43
file.close();
44
45
return
content;
46
}
47
54
[[nodiscard]]
55
static
56
inline
57
std::uint8_t
58
hex2dec(std::uint8_t c)
59
{
60
if
(c >=
'0'
&& c <=
'9'
)
61
c -=
'0'
;
62
63
else
if
(c >=
'a'
&& c <=
'f'
)
64
c -=
'a'
- 10;
65
66
else
if
(c >=
'A'
&& c <=
'F'
)
67
c -=
'A'
- 10;
68
69
return
c;
70
}
71
83
[[nodiscard]]
84
inline
85
std::vector<std::string_view>
86
split
(std::string_view str, std::string_view delimiter)
87
{
88
// Sanity check str
89
if
(str.empty())
90
return
{ };
91
92
// Sanity check delimiter
93
if
(delimiter.empty())
94
return
{ str };
95
96
// Split
97
std::vector<std::string_view> parts;
98
std::string_view::size_type pos = 0;
99
while
(pos != std::string_view::npos) {
100
// Look for substring
101
const
auto
pos_found = str.find(delimiter, pos);
102
103
// Drop leading delimiters
104
if
(pos_found == 0) {
105
pos += delimiter.size();
106
continue
;
107
}
108
109
// Capture string
110
parts.emplace_back(str.substr(pos, pos_found-pos));
111
112
// Drop trailing delimiters
113
if
(pos_found + delimiter.size() >= str.size())
114
break
;
115
116
// Move on
117
if
(pos_found == std::string_view::npos)
118
break
;
119
pos = pos_found + delimiter.size();
120
}
121
122
return
parts;
123
}
124
136
// ToDo: Consider using Boost.URL instead
137
static
138
inline
139
void
140
url_decode(std::string& str)
141
{
142
size_t
w = 0;
143
for
(
size_t
r = 0 ; r < str.size() ; ++r) {
144
uint8_t v = str[r];
145
if
(str[r] ==
'%'
) {
146
v = hex2dec(str[++r]) << 4;
147
v |= hex2dec(str[++r]);
148
}
149
str[w++] = v;
150
}
151
str.resize(w);
152
}
153
160
// ToDo: This is still a left-over from the very first day that malloy was started as a PoC. This needs some
161
// serious overhaul. See issue #4.
162
[[nodiscard]]
163
static
164
inline
165
std::string_view
166
mime_type(
const
std::filesystem::path& path)
167
{
168
// Extract file extension
169
const
std::filesystem::path& ext = path.extension();
170
171
if
(ext ==
".7z"
)
return
"application/x-7z-compressed"
;
172
if
(ext ==
".bin"
)
return
"application/octet-stream"
;
173
if
(ext ==
".bmp"
)
return
"image/bmp"
;
174
if
(ext ==
".bz"
)
return
"application/x-bzip"
;
175
if
(ext ==
".bz2"
)
return
"application/x-bzip2"
;
176
if
(ext ==
".css"
)
return
"text/css"
;
177
if
(ext ==
".css.min"
)
return
"text/css"
;
178
if
(ext ==
".csv"
)
return
"text/csv"
;
179
if
(ext ==
".gif"
)
return
"image/gif"
;
180
if
(ext ==
".gz"
)
return
"application/gzip"
;
181
if
(ext ==
".ico"
)
return
"image/vnd.microsoft.icon"
;
182
if
(ext ==
".htm"
)
return
"text/html"
;
183
if
(ext ==
".html"
)
return
"text/html"
;
184
if
(ext ==
".mp3"
)
return
"audio/mpeg"
;
185
if
(ext ==
".mp4"
)
return
"video/mp4"
;
186
if
(ext ==
".mpeg"
)
return
"video/mpeg"
;
187
if
(ext ==
".rar"
)
return
"application/vnd.rar"
;
188
if
(ext ==
".php"
)
return
"text/html"
;
189
if
(ext ==
".txt"
)
return
"text/plain"
;
190
if
(ext ==
".js"
)
return
"application/javascript"
;
191
if
(ext ==
".json"
)
return
"application/json"
;
192
if
(ext ==
".xml"
)
return
"application/xml"
;
193
if
(ext ==
".swf"
)
return
"application/x-shockwave-flash"
;
194
if
(ext ==
".flv"
)
return
"video/x-flv"
;
195
if
(ext ==
".png"
)
return
"image/png"
;
196
if
(ext ==
".jpe"
)
return
"image/jpeg"
;
197
if
(ext ==
".jpeg"
)
return
"image/jpeg"
;
198
if
(ext ==
".jpg"
)
return
"image/jpeg"
;
199
if
(ext ==
".tiff"
)
return
"image/tiff"
;
200
if
(ext ==
".tif"
)
return
"image/tiff"
;
201
if
(ext ==
".svg"
)
return
"image/svg+xml"
;
202
if
(ext ==
".svgz"
)
return
"image/svg+xml"
;
203
if
(ext ==
".zip"
)
return
"application/zip"
;
204
205
// Recommended fall-back
206
return
"application/octet-stream"
;
207
}
208
209
}
malloy
Definition
controller.hpp:32
malloy::split
std::vector< std::string_view > split(std::string_view str, std::string_view delimiter)
Definition
utils.hpp:86
Generated by
1.18.0