-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·77 lines (66 loc) · 2.29 KB
/
main.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/hex.hpp>
using std::cout;
using std::endl;
using std::exception;
using std::cerr;
namespace bpo = boost::program_options;
bpo::options_description
getOptionsDescription(std::string *valuestr, std::string *ofstr, uint64_t *bs, uint64_t *count) {
bpo::options_description description("Valid parameters");
description.add_options()
("help", "display help")
("value", bpo::value<std::string>(valuestr), "Required. The value to generate (e.g. 0xFF)")
("bs", bpo::value<uint64_t>(bs), "Block size in bytes.")
("count", bpo::value<uint64_t>(count), "Number of blocks to generate.");
return description;
}
int main(int ac, char **av) {
try {
std::string valuestr;
std::string ofstr;
uint64_t bs;
uint64_t count;
auto description = getOptionsDescription(&valuestr, &ofstr, &bs, &count);
bpo::variables_map variables_map;
bpo::store(bpo::parse_command_line(ac, reinterpret_cast<const char *const *>(av), description), variables_map);
bpo::notify(variables_map);
if (variables_map.count("help")) {
cout << description << endl;
return 0;
}
if (variables_map.count("value")) {
} else {
cerr << "Parameter 'value' is required." << endl;
return 1;
}
boost::replace_all(valuestr, "0x", "");
uint16_t value;
std::istringstream(valuestr) >> std::hex >> value;
if (!variables_map.count("bs")) {
cerr << "Parameter 'bs' is required." << endl;
return 1;
}
uint64_t writeCount;
if (!variables_map.count("count")) {
cerr << "Parameter 'count' is required." << endl;
return 1;
} else {
writeCount = count;
}
char *buf = new char[bs+1];
memset(buf, value, bs);
for (auto i = 0; i < writeCount; i++)
cout << buf;
delete [] buf;
} catch (exception &ex) {
cerr << "Error: " << endl << ex.what() << endl;
return 1;
} catch (...) {
cerr << "Drink more coffee." << endl;
return 1;
}
return 0;
}