-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipar_common.cpp
More file actions
129 lines (108 loc) · 2.42 KB
/
ipar_common.cpp
File metadata and controls
129 lines (108 loc) · 2.42 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
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
128
129
#include <exception>
#include <iostream>
#include <istream>
#include "ipar_common.h"
namespace IPAR {
OutputStyle o_style (const std::string& arg)
{
OutputStyle style;
if (arg == "-cidr")
{
style = Scidr;
}
else if (arg == "-dashes")
{
style = Sdashes;
}
else if (arg == "-hex")
{
style = Shex;
}
else
{
style = Sunknown;
}
return style;
}
//////////////////////////////////////
// Implementation of TextReader class
//////////////////////////////////////
TextReader::TextReader(std::istream& ist)
: mIst(ist), mSst{}, mLine{}, mLineNo(0), mLineOk(false), mFileOk(ist)
{
}
TextReader& TextReader::operator>> (std::string& word)
{
while (mFileOk)
{
if (!mLineOk)
{
if (!(mFileOk = bool(getline(mIst, mLine)))) break;
mSst.str(mLine);
mSst.clear();
++mLineNo;
}
if (!(mLineOk = bool(mSst >> word))) continue;
// Ignore comments to end of line
if (word[0] == '#')
{
mLineOk = false;
word = "";
continue;
}
// Testing complete. Return last word read.
break;
}
return *this;
}
TextReader::operator bool() const
{
return mFileOk;
}
std::string TextReader::current_line() const
{
return mLine;
}
unsigned int TextReader::line_no() const
{
return mLineNo;
}
//////////////////////////////////////
// Implementation of FileReader class
//////////////////////////////////////
FileReader::FileReader(const std::string& filename)
: mIfs(filename, std::ifstream::in), mTr(mIfs)
{
if (!mIfs)
{
std::cerr << "ERROR: could not open input file \"" << filename
<< "\" for reading" << std::endl;
}
}
///////////////////////////////////////////
// Implementation of stand-alone functions
///////////////////////////////////////////
int common_read (std::istream& ist, List& iplist)
{
// Loop over lines of input
IPAR::TextReader reader(ist);
std::string word;
IPAR::Range iprange;
while (reader >> word)
{
// Assume the word is a range of IPv4 addresses
try {
iprange = IPAR::Range(word);
}
catch (const std::exception& ex) {
std::cerr << "ERROR: " << ex.what() << " at line "
<< reader.line_no() << " of input: " << std::endl;
std::cerr << reader.current_line() << std::endl;
std::cerr << "Last input was \"" << word << "\"" << std::endl;
return 1;
}
iplist.add(iprange);
}
return 0;
}
} // namespace IPAR