Skip to content

Commit f558210

Browse files
committed
fix: handle hostname getter properly
1 parent 9ae1beb commit f558210

File tree

2 files changed

+42
-53
lines changed

2 files changed

+42
-53
lines changed

tools/cli/adaparse.cpp

+32-42
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,17 @@ bool print_part(Callable&& fmt_or_adaparse_print, std::string_view get_part,
5252
if (get_part == "protocol") {
5353
fmt_or_adaparse_print("{}\n", url.get_protocol());
5454
return true;
55-
}
56-
if (get_part == "password") {
55+
} else if (get_part == "password") {
5756
fmt_or_adaparse_print("{}\n", url.get_password());
5857
return true;
59-
}
60-
if (get_part == "pathname") {
58+
} else if (get_part == "pathname") {
6159
fmt_or_adaparse_print("{}\n", url.get_pathname());
6260
return true;
6361
}
62+
} else if (get_part == "hostname") {
63+
fmt_or_adaparse_print("{}\n", url.get_hostname());
64+
return true;
6465
}
65-
} else if (get_part == "hostname") {
66-
fmt_or_adaparse_print("{}\n", url.get_hostname());
67-
return true;
68-
6966
} else if (get_part == "username") {
7067
fmt_or_adaparse_print("{}\n", url.get_username());
7168
return true;
@@ -88,12 +85,12 @@ int piped_file(Callable&& adaparse_print, const cxxopts::ParseResult result,
8885

8986
uint64_t before = nano();
9087

91-
size_t total_bytes_read = 0;
92-
size_t bytes_read_this_loop_iteration;
93-
size_t offset = 0;
94-
size_t lines = 0;
95-
size_t blocks = 0;
96-
std::string get_part;
88+
size_t total_bytes_read{0};
89+
size_t bytes_read_this_loop_iteration{0};
90+
size_t offset{0};
91+
size_t lines{0};
92+
size_t blocks{0};
93+
std::string get_part{};
9794
if (result.count("get")) {
9895
get_part = result["get"].as<std::string>();
9996
}
@@ -121,7 +118,7 @@ int piped_file(Callable&& adaparse_print, const cxxopts::ParseResult result,
121118
while (li.find_another_complete_line()) {
122119
std::string_view line = li.grab_line();
123120

124-
ada::result<ada::url_aggregator> url = ada::parse(line);
121+
auto url = ada::parse<ada::url_aggregator>(line);
125122
if (!url) {
126123
adaparse_print("Invalid URL: {}\n", line);
127124
} else if (!get_part.empty()) {
@@ -138,7 +135,7 @@ int piped_file(Callable&& adaparse_print, const cxxopts::ParseResult result,
138135
// have a line of length offset at cachebuffer.get()
139136
std::string_view line(cachebuffer.get(), offset);
140137

141-
ada::result<ada::url_aggregator> url = ada::parse(line);
138+
auto url = ada::parse<ada::url_aggregator>(line);
142139
if (!url) {
143140
adaparse_print("Invalid URL:{}\n", line);
144141
} else if (!get_part.empty()) {
@@ -207,30 +204,22 @@ int main(int argc, char** argv) {
207204
cxxopts::Options options("adaparse",
208205
"Command-line version of the Ada URL parser");
209206

210-
options.add_options()("d,diagram", "Print a diagram of the result",
211-
cxxopts::value<bool>()->default_value("false"))(
212-
"u,url", "URL Parameter (required)", cxxopts::value<std::string>())(
213-
"h,help", "Print usage")(
214-
"g,get", "Get a specific part of the URL (e.g., 'origin', 'host', etc.)",
215-
cxxopts::value<std::string>())(
216-
"b,benchmark", "Display chronometer for piped_file function",
217-
cxxopts::value<bool>()->default_value("false"))(
218-
"p,path", "Takes in a path to a file and process all the URL within",
219-
cxxopts::value<std::string>())(
220-
"o,output", "Takes in a path and outputs to a text file.",
221-
cxxopts::value<std::string>()->default_value("__no_output__"))
222-
223-
;
207+
// clang-format off
208+
options.add_options()
209+
("d,diagram", "Print a diagram of the result", cxxopts::value<bool>()->default_value("false"))
210+
("u,url", "URL", cxxopts::value<std::string>())
211+
("g,get", "Get a specific part of the URL (e.g., 'origin', 'host', etc.)", cxxopts::value<std::string>())
212+
("b,benchmark", "Display chronometer for piped_file function", cxxopts::value<bool>()->default_value("false"))
213+
("p,path", "Takes in a path to a file and process all the URL within", cxxopts::value<std::string>())
214+
("o,output", "Takes in a path and outputs to a text file.", cxxopts::value<std::string>()->default_value("/dev/null"))
215+
("h,help", "Print usage");
216+
// clang-format on
217+
224218
options.parse_positional({"url"});
225219

226220
auto result = options.parse(argc, argv);
227221

228222
std::string output_filename = result["output"].as<std::string>();
229-
230-
result["output"].as<std::string>() == "__no_output__"
231-
? output_filename = "/dev/null"
232-
: output_filename = result["output"].as<std::string>();
233-
234223
auto out = fmt::output_file(output_filename);
235224
bool has_result = result.count("output");
236225

@@ -255,15 +244,14 @@ int main(int argc, char** argv) {
255244
}
256245

257246
if (result.count("path")) {
258-
std::string filepath = result["path"].as<std::string>();
259-
FILE* file = fopen(filepath.c_str(), "r");
247+
auto file_path = result["path"].as<std::string>();
248+
auto file = fopen(file_path.c_str(), "r");
260249
if (file) {
261250
piped_file(adaparse_print, result, file);
262251
fclose(file);
263252
return EXIT_SUCCESS;
264253
} else {
265-
int err = errno;
266-
fmt::print(stderr, "Error opening file: {}\n", strerror(err));
254+
fmt::print(stderr, "Error opening file: {}\n", strerror(errno));
267255
return EXIT_FAILURE;
268256
}
269257
}
@@ -274,14 +262,15 @@ int main(int argc, char** argv) {
274262
return EXIT_SUCCESS;
275263
}
276264

277-
std::string url_string = result["url"].as<std::string>();
265+
auto input_url = result["url"].as<std::string>();
278266
bool to_diagram = result["diagram"].as<bool>();
279267

280-
ada::result<ada::url_aggregator> url = ada::parse(url_string);
268+
auto url = ada::parse<ada::url_aggregator>(input_url);
281269
if (!url) {
282-
fmt::print(stderr, "Invalid URL: {}\n", url_string);
270+
fmt::print(stderr, "Invalid URL: {}\n", input_url);
283271
return EXIT_FAILURE;
284272
}
273+
285274
if (result.count("get")) {
286275
std::string get_part = result["get"].as<std::string>();
287276

@@ -292,6 +281,7 @@ int main(int argc, char** argv) {
292281
return print_part(print_lambda, get_part, url.value()) ? EXIT_SUCCESS
293282
: EXIT_FAILURE;
294283
};
284+
295285
if (to_diagram) {
296286
fmt::print("{}\n", url->to_diagram());
297287
} else {

tools/cli/line_iterator.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
21
#ifndef LINE_ITERATOR_H
32
#define LINE_ITERATOR_H
43

54
#include <string_view>
65

76
struct line_iterator {
8-
std::string_view all_text;
7+
std::string_view all_text{};
98
size_t next_end_of_line{0};
109
line_iterator(const char *_buffer, size_t _len) : all_text(_buffer, _len) {}
10+
1111
inline bool find_another_complete_line() noexcept {
1212
next_end_of_line = all_text.find('\n');
1313
return next_end_of_line != std::string_view::npos;
1414
}
15+
1516
inline operator bool() const noexcept {
1617
return next_end_of_line != std::string_view::npos;
1718
}
19+
1820
inline std::string_view grab_line() noexcept {
19-
std::string_view thisline =
20-
all_text.substr(0, next_end_of_line); // advance to next EOL
21-
all_text.remove_prefix(next_end_of_line +
22-
1); // remove anything prior to said EOL
23-
// this is a view!!!
24-
return thisline;
21+
auto line = all_text.substr(0, next_end_of_line); // advance to next EOL
22+
// remove anything prior to said EOL
23+
all_text.remove_prefix(next_end_of_line + 1);
24+
return line;
2525
}
26-
inline size_t tail() const noexcept {
27-
return all_text.size();
28-
} // return whats remaining
26+
27+
inline size_t tail() const noexcept { return all_text.size(); }
2928
};
3029

3130
#endif

0 commit comments

Comments
 (0)