24 # pragma warning (disable: 4127)
27 #include "Planimeter.usage"
29 int main(
int argc,
const char*
const argv[]) {
34 enum { GEODESIC, EXACT, AUTHALIC, RHUMB };
38 bool reverse =
false,
sign =
true, polyline =
false, longfirst =
false;
39 int linetype = GEODESIC;
41 std::string istring, ifile, ofile, cdelim;
44 for (
int m = 1;
m < argc; ++
m) {
45 std::string
arg(argv[
m]);
52 else if (
arg ==
"-e") {
53 if (
m + 2 >= argc)
return usage(1,
true);
55 a = Utility::val<real>(std::string(argv[
m + 1]));
56 f = Utility::fract<real>(std::string(argv[
m + 2]));
58 catch (
const std::exception&
e) {
59 std::cerr <<
"Error decoding arguments of -e: " <<
e.what() <<
"\n";
63 }
else if (
arg ==
"-w")
64 longfirst = !longfirst;
65 else if (
arg ==
"-p") {
66 if (++
m == argc)
return usage(1,
true);
68 prec = Utility::val<int>(std::string(argv[
m]));
70 catch (
const std::exception&) {
71 std::cerr <<
"Precision " << argv[
m] <<
" is not a number\n";
74 }
else if (
arg ==
"-G")
82 else if (
arg ==
"--input-string") {
83 if (++
m == argc)
return usage(1,
true);
85 }
else if (
arg ==
"--input-file") {
86 if (++
m == argc)
return usage(1,
true);
88 }
else if (
arg ==
"--output-file") {
89 if (++
m == argc)
return usage(1,
true);
91 }
else if (
arg ==
"--line-separator") {
92 if (++
m == argc)
return usage(1,
true);
93 if (std::string(argv[
m]).
size() != 1) {
94 std::cerr <<
"Line separator must be a single character\n";
98 }
else if (
arg ==
"--comment-delimiter") {
99 if (++
m == argc)
return usage(1,
true);
101 }
else if (
arg ==
"--version") {
102 std::cout << argv[0] <<
": GeographicLib version "
106 return usage(!(
arg ==
"-h" ||
arg ==
"--help"),
arg !=
"--help");
109 if (!ifile.empty() && !istring.empty()) {
110 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
113 if (ifile ==
"-") ifile.clear();
114 std::ifstream infile;
115 std::istringstream instring;
116 if (!ifile.empty()) {
117 infile.open(ifile.c_str());
118 if (!infile.is_open()) {
119 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
122 }
else if (!istring.empty()) {
123 std::string::size_type
m = 0;
125 m = istring.find(lsep,
m);
126 if (
m == std::string::npos)
130 instring.str(istring);
132 std::istream* input = !ifile.empty() ? &infile :
133 (!istring.empty() ? &instring : &std::cin);
135 std::ofstream outfile;
136 if (ofile ==
"-") ofile.clear();
137 if (!ofile.empty()) {
138 outfile.open(ofile.c_str());
139 if (!outfile.is_open()) {
140 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
144 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
147 if (linetype == AUTHALIC) {
155 PolygonArea poly(geod, polyline);
156 PolygonAreaExact polye(geode, polyline);
157 PolygonAreaRhumb polyr(rhumb, polyline);
163 std::string
s, eol(
"\n");
164 real perimeter, area;
166 while (std::getline(*input,
s)) {
167 if (!cdelim.empty()) {
168 std::string::size_type
m =
s.find(cdelim);
169 if (
m != std::string::npos) {
170 eol =
" " +
s.substr(
m) +
"\n";
174 bool endpoly =
s.empty();
177 p.Reset(
s,
true, longfirst);
187 linetype == EXACT ? polye.Compute(
reverse,
sign, perimeter, area) :
188 linetype == RHUMB ? polyr.Compute(
reverse,
sign, perimeter, area) :
197 linetype == EXACT ? polye.Clear() :
198 linetype == RHUMB ? polyr.Clear() : poly.Clear();
201 linetype == EXACT ? polye.AddPoint(
p.Latitude(),
p.Longitude()) :
202 linetype == RHUMB ? polyr.AddPoint(
p.Latitude(),
p.Longitude()) :
203 poly.AddPoint(linetype == AUTHALIC ?
210 linetype == EXACT ? polye.Compute(
reverse,
sign, perimeter, area) :
211 linetype == RHUMB ? polyr.Compute(
reverse,
sign, perimeter, area) :
220 linetype == EXACT ? polye.Clear() :
221 linetype == RHUMB ? polyr.Clear() : poly.Clear();
225 catch (
const std::exception&
e) {
226 std::cerr <<
"Caught exception: " <<
e.what() <<
"\n";
230 std::cerr <<
"Caught unknown exception\n";