-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-page.cpp
More file actions
62 lines (57 loc) · 1.46 KB
/
extract-page.cpp
File metadata and controls
62 lines (57 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "extract-page.hpp"
int main(const int argc, const char* const* const argv){
constexpr std::string_view usage_str =
"USAGE: [FILEPATH] [OPTPUT_TYPE] [OFFSET]:[PAGEID]\n"
"\n"
"OPTPUT_TYPE\n"
" x XML\n"
" h HTML\n"
;
off_t init_offset_bytes = 0;
bool extract_as_html = true;
const char* const filepath = argv[1];
if (unlikely(argc != 4)){
goto print_usage_and_exit;
}
if (argv[2][0] == 'x'){
extract_as_html = false;
} else if (argv[2][0] != 'h'){
goto print_usage_and_exit;
}
const char* searching_for_pageid__at;
{
const char* arg = argv[3];
while(*arg != ':'){
if (likely((*arg >= '0') and (*arg <= '9'))){
init_offset_bytes *= 10;
init_offset_bytes += *arg - '0';
} else {
goto print_usage_and_exit;
}
++arg;
}
++arg;
searching_for_pageid__at = arg;
while(*arg != ':'){
++arg;
}
}
if (false){
print_usage_and_exit:
write(2, usage_str.data(), usage_str.size());
return 1;
}
char* const output_buf = reinterpret_cast<char*>(malloc(1024*1024*10));
const int compressed_fd = open(filepath, O_RDONLY);
if (unlikely(compressed_fd == -1)){
write(2, "Cannot open file\n", 17);
return 1;
}
const std::string_view errstr(process_file(compressed_fd, output_buf, searching_for_pageid__at, init_offset_bytes, nullptr, extract_as_html));
if (unlikely(errstr.data()[0] == '\0')){
write(2, errstr.data()+1, errstr.size()-1);
return 1;
}
write(1, errstr.data(), errstr.size());
return 0;
}