-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
105 lines (89 loc) · 3.02 KB
/
parser.cpp
File metadata and controls
105 lines (89 loc) · 3.02 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
#include <istream>
#include <isstream>
#include <string>
#include <mpfr.h>
#include "parser.h"
namespace fractal {
parser::parser(const std::string& pExecutableName,
std::istream& pInputStream) :
mExecutableName(pExecutableName),
mInputStream(pInpuStream) { }
bool parser::get_params(std::string& pOutputFile,
mpfr_t& pXCenter,
mpfr_t& pYCenter,
mpfr_t& pWidth,
unsigned int& pMaxIters) const {
std::string line;
std::istringstream lineStream;
std::string xCenter, yCenter, width;
if (!parse_first_line(line)) {
return false;
}
pOutputFile = line;
parse_line(line);
lineStream.str(line);
lineStream.clear();
lineStream >> xCenter >> yCenter;
if (mpfr_init_set_str(pXCenter, xCenter.c_str(), 10, MPFR_RNDN) ||
mpfr_init_set_str(pYCenter, yCenter.c_str(), 10, MPFR_RNDN) ) {
usage();
}
parse_line(line);
lineStream.str(line);
lineStream.clear();
lineStream >> width;
if (mpfr_init_set_str(pWidth, width.c_str(), 10, MPFR_RNDN)) {
usage();
}
if (mpfr_sgn(pWidth) == 0) {
std::cerr << "Invalid input: <width> is zero!" << std::endl;
throw fractal::bad_usage();
}
if (mpfr_sgn(pWidth) < 0) {
mpfr_neg(pWidth, pWidth, MPFR_RNDN);
}
parse_line(line);
lineStream.str(line);
lineStream.clear();
lineStream >> pMaxIters;
if (pMaxIters == 0) {
usage();
}
std::getline(std::cin, line);
return true;
}
bool parser::parse_first_line(std::string& line) const {
std::getline(std::cin, line);
if (!std::cin.good()) {
return false;
}
if (line.empty()) {
usage();
}
return true;
}
void parser::parse_line(std::string& line) const {
std::getline(std::cin, line);
if (!std::cin.good()) {
usage();
}
if (line.empty()) {
usage();
}
}
[[noreturn]] void parser::usage() const {
std::cerr << "Usage: " << mExecutableName << "\n";
std::cerr << "<output-file>\\n\n";
std::cerr << "<x-center> <y-center>\\n\n";
std::cerr << "<width>\\n\n";
std::cerr << "<max-iters>\\n\n";
std::cerr << "\n";
std::cerr << "Renders a rectangular section of the Mandebrot set, "
"centered on the complex number (<x-center> + <y-center>*j), "
"which is <width> wide on the x-axis, and uses at most "
"<max-iters> iterations for each pixel. The image is then output "
"into <output-file> in BMP format with 24-bit depth, "
"1920x1080 pixels large." << std::endl;
throw fractal::bad_usage();
}
}