24 # pragma warning (disable: 4127 4701)
27 #include "RhumbSolve.usage"
29 using namespace GeographicLib;
45 int main(
int argc,
char* argv[]) {
48 bool linecalc =
false, inverse =
false, dms =
false, exact =
true;
54 std::string istring, ifile, ofile, cdelim;
55 char lsep =
';', dmssep = char(0);
57 for (
int m = 1; m < argc; ++m) {
58 std::string arg(argv[m]);
62 }
else if (arg ==
"-l") {
65 if (m + 3 >= argc)
return usage(1,
true);
71 catch (
const std::exception& e) {
72 std::cerr <<
"Error decoding arguments of -l: " << e.what() <<
"\n";
76 }
else if (arg ==
"-e") {
77 if (m + 2 >= argc)
return usage(1,
true);
79 a = Utility::num<real>(std::string(argv[m + 1]));
80 f = Utility::fract<real>(std::string(argv[m + 2]));
82 catch (
const std::exception& e) {
83 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
88 else if (arg ==
"-d") {
91 }
else if (arg ==
"-:") {
94 }
else if (arg ==
"-p") {
95 if (++m == argc)
return usage(1,
true);
97 prec = Utility::num<int>(std::string(argv[m]));
99 catch (
const std::exception&) {
100 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
103 }
else if (arg ==
"-s")
105 else if (arg ==
"--input-string") {
106 if (++m == argc)
return usage(1,
true);
108 }
else if (arg ==
"--input-file") {
109 if (++m == argc)
return usage(1,
true);
111 }
else if (arg ==
"--output-file") {
112 if (++m == argc)
return usage(1,
true);
114 }
else if (arg ==
"--line-separator") {
115 if (++m == argc)
return usage(1,
true);
116 if (std::string(argv[m]).size() != 1) {
117 std::cerr <<
"Line separator must be a single character\n";
121 }
else if (arg ==
"--comment-delimiter") {
122 if (++m == argc)
return usage(1,
true);
124 }
else if (arg ==
"--version") {
126 << argv[0] <<
": GeographicLib version "
127 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
130 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
133 if (!ifile.empty() && !istring.empty()) {
134 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
137 if (ifile ==
"-") ifile.clear();
138 std::ifstream infile;
139 std::istringstream instring;
140 if (!ifile.empty()) {
141 infile.open(ifile.c_str());
142 if (!infile.is_open()) {
143 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
146 }
else if (!istring.empty()) {
147 std::string::size_type m = 0;
149 m = istring.find(lsep, m);
150 if (m == std::string::npos)
154 instring.str(istring);
156 std::istream* input = !ifile.empty() ? &infile :
157 (!istring.empty() ? &instring : &std::cin);
159 std::ofstream outfile;
160 if (ofile ==
"-") ofile.clear();
161 if (!ofile.empty()) {
162 outfile.open(ofile.c_str());
163 if (!outfile.is_open()) {
164 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
168 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
170 const Rhumb rh(a, f, exact);
178 while (std::getline(*input, s)) {
180 std::istringstream str(s);
186 rhl.Position(s12, lat2, lon2);
187 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep) <<
"\n";
189 catch (
const std::exception& e) {
191 *output <<
"ERROR: " << e.what() <<
"\n";
195 }
else if (inverse) {
196 while (std::getline(*input, s)) {
198 std::istringstream str(s);
199 std::string slat1, slon1, slat2, slon2;
200 if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
207 rh.
Inverse(lat1, lon1, lat2, lon2, s12, azi12);
211 catch (
const std::exception& e) {
213 *output <<
"ERROR: " << e.what() <<
"\n";
218 while (std::getline(*input, s)) {
220 std::istringstream str(s);
221 std::string slat1, slon1, sazi;
222 if (!(str >> slat1 >> slon1 >> sazi >> s12))
229 rh.
Direct(lat1, lon1, azi12, s12, lat2, lon2);
230 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep) <<
"\n";
232 catch (
const std::exception& e) {
234 *output <<
"ERROR: " << e.what() <<
"\n";
241 catch (
const std::exception& e) {
242 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
246 std::cerr <<
"Caught unknown exception\n";