-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipar_intersect.cpp
More file actions
68 lines (59 loc) · 2.01 KB
/
ipar_intersect.cpp
File metadata and controls
68 lines (59 loc) · 2.01 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
63
64
65
66
67
68
// Program ipar_intersect
// ----------------------
// Reads a list of IP address ranges from standard input.
// Opens other lists of IP addresses from specified files.
// Logically intersects everything in the other lists with the first list.
// Writes out the result to standard output.
// None of the inputs have to be sorted.
#include <string>
#include <limits>
using namespace std;
#include "ipar_iplist.h"
#include "ipar_common.h"
int main (int argc, char* argv[])
{
IPAR::List mainlist;
// Loop over lines of input
if (int retval = IPAR::common_read (cin, mainlist) != 0) return retval;
// This will hold the complement of what follows
IPAR::List complem;
static const uint32_t bmin = numeric_limits<uint32_t>::min();
static const uint32_t bmax = numeric_limits<uint32_t>::max();
complem.add(IPAR::Range(bmin, bmax));
// Loop over input files to intersect
for (int iArg = 1 ; iArg < argc ; ++iArg)
{
IPAR::FileReader reader2(argv[iArg]);
if (!reader2) return 1;
// Loop over words in the intersect file
string word;
while (reader2 >> word)
{
// Assume the word is a range of IPv4 addresses
IPAR::Range iprange;
try {
iprange = IPAR::Range(word);
}
catch (const exception& ex) {
cerr << "ERROR: " << ex.what() << " at line "
<< reader2.line_no()
<< " of \"" << argv[iArg] << "\": " << endl;
cerr << reader2.current_line() << endl;
cerr << "Last input was \"" << word << "\"" << endl;
return 1;
}
complem.subtract(iprange);
} // End loop over words in the intersect file
} // End loop over input files to intersect
// Now subtract from the main list
for (auto iter = complem.cbegin() ; iter != complem.cend() ; ++iter)
{
mainlist.subtract_from (iter);
}
// Report
cerr << mainlist.num_operations() << " operations applied, ";
auto numLines = mainlist.num_output();
cout << mainlist;
cerr << mainlist.num_output() - numLines << " lines output" << endl;
return 0;
}