-
Notifications
You must be signed in to change notification settings - Fork 0
/
bson-seek.cpp
127 lines (119 loc) · 3.19 KB
/
bson-seek.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Skips BSON documents in a file without actually parsing the elements of the
// skipped documents.
#include <endian.h>
#include <stdint.h>
#include <fstream>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <boost/program_options.hpp>
using namespace std;
namespace po = boost::program_options;
void process(istream& is, unsigned int skip)
{
for (unsigned i = 0; i < skip; ++i)
{
uint32_t size;
streamsize read;
read = is.readsome(reinterpret_cast<char*>(&size), sizeof(size));
if (read < sizeof(size))
{
if (read)
{
cerr << "Unexpected end of BSON." << endl;
exit(3);
}
cerr << "Reached end of file after skipping " << i << " documents." << endl;
return;
}
if (is.fail() || !is.good())
{
cerr << "Read error after skipping " << i << " documents." << endl;
exit (4);
}
size = le32toh(size);
if (size < sizeof(size))
{
cerr << "Malformed BSON detected (invalid document size) after skipping " << i
<< " documents." << endl;
exit(5);
}
is.seekg(size - sizeof(size), ios_base::cur);
if (is.fail())
{
cerr << "Malformed BSON detected (seek error) after skipping " << i << " documents."
<< endl;
exit(6);
}
}
while (is.good())
{
char buf[4 * 1024 * 1024];
streamsize read = is.readsome(buf, sizeof(buf));
if (read)
cout.write(buf, read);
else
break;
}
}
struct UnsignedWrapper
{
unsigned val;
};
void validate(boost::any& v, vector<string> const& values, UnsignedWrapper* target_type, int)
{
string s = po::validators::get_single_string(values);
boost::algorithm::trim(s);
if (s.size() && s[0] != '-')
{
istringstream is(s);
UnsignedWrapper value;
is >> value.val;
if (is.eof())
{
v = boost::any(value);
return;
}
}
throw po::validation_error(po::validation_error::invalid_option_value);
}
int main(int argc, char** argv) {
UnsignedWrapper skip;
string fname;
po::options_description od;
od.add_options()
("help", "Help message")
("skip",
po::value<UnsignedWrapper>(&skip)->required(),
"How many documents to skip.")
("input-file", po::value<string>(&fname),
"Input file. If not specified, stdin will be used.")
;
po::variables_map vm;
try
{
po::store(po::command_line_parser(argc, argv).options(od).run(), vm);
if (vm.count("help"))
{
cout << "Options:" << endl << od << endl;
return 1;
}
vm.notify();
}
catch (po::error& e)
{
cerr << e.what() << endl;
cout << "Options:" << endl << od << endl;
return 2;
}
if (vm.count("input-file"))
{
ifstream ifs(vm.at("input-file").as<string>().c_str());
process(ifs, skip.val);
}
else
{
process(cin, skip.val);
}
return 0;
}