33 # pragma warning (disable: 4127 4701)
36 #include "CartConvert.usage"
38 int main(
int argc,
char* argv[]) {
40 using namespace GeographicLib;
43 bool localcartesian =
false, reverse =
false;
47 real lat0 = 0, lon0 = 0, h0 = 0;
48 std::string istring, ifile, ofile, cdelim;
51 for (
int m = 1; m < argc; ++m) {
52 std::string arg(argv[m]);
55 else if (arg ==
"-l") {
56 localcartesian =
true;
57 if (m + 3 >= argc)
return usage(1,
true);
61 h0 = Utility::num<real>(std::string(argv[m + 3]));
63 catch (
const std::exception& e) {
64 std::cerr <<
"Error decoding arguments of -l: " << e.what() <<
"\n";
68 }
else if (arg ==
"-e") {
69 if (m + 2 >= argc)
return usage(1,
true);
71 a = Utility::num<real>(std::string(argv[m + 1]));
72 f = Utility::fract<real>(std::string(argv[m + 2]));
74 catch (
const std::exception& e) {
75 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
79 }
else if (arg ==
"--input-string") {
80 if (++m == argc)
return usage(1,
true);
82 }
else if (arg ==
"--input-file") {
83 if (++m == argc)
return usage(1,
true);
85 }
else if (arg ==
"--output-file") {
86 if (++m == argc)
return usage(1,
true);
88 }
else if (arg ==
"--line-separator") {
89 if (++m == argc)
return usage(1,
true);
90 if (std::string(argv[m]).size() != 1) {
91 std::cerr <<
"Line separator must be a single character\n";
95 }
else if (arg ==
"--comment-delimiter") {
96 if (++m == argc)
return usage(1,
true);
98 }
else if (arg ==
"--version") {
100 << argv[0] <<
": GeographicLib version "
101 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
104 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
107 if (!ifile.empty() && !istring.empty()) {
108 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
111 if (ifile ==
"-") ifile.clear();
112 std::ifstream infile;
113 std::istringstream instring;
114 if (!ifile.empty()) {
115 infile.open(ifile.c_str());
116 if (!infile.is_open()) {
117 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
120 }
else if (!istring.empty()) {
121 std::string::size_type m = 0;
123 m = istring.find(lsep, m);
124 if (m == std::string::npos)
128 instring.str(istring);
130 std::istream* input = !ifile.empty() ? &infile :
131 (!istring.empty() ? &instring : &std::cin);
133 std::ofstream outfile;
134 if (ofile ==
"-") ofile.clear();
135 if (!ofile.empty()) {
136 outfile.open(ofile.c_str());
137 if (!outfile.is_open()) {
138 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
142 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
149 while (std::getline(*input, s)) {
151 std::string eol(
"\n");
152 if (!cdelim.empty()) {
153 std::string::size_type m = s.find(cdelim);
154 if (m != std::string::npos) {
155 eol =
" " + s.substr(m) +
"\n";
159 std::istringstream str(s);
161 real lat, lon, h, x = 0, y = 0, z = 0;
162 std::string stra, strb, strc;
163 if (!(str >> stra >> strb >> strc))
166 x = Utility::num<real>(stra);
167 y = Utility::num<real>(strb);
168 z = Utility::num<real>(strc);
171 h = Utility::num<real>(strc);
178 lc.
Reverse(x, y, z, lat, lon, h);
180 ec.
Reverse(x, y, z, lat, lon, h);
186 lc.
Forward(lat, lon, h, x, y, z);
188 ec.
Forward(lat, lon, h, x, y, z);
194 catch (
const std::exception& e) {
195 *output <<
"ERROR: " << e.what() <<
"\n";
201 catch (
const std::exception& e) {
202 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
206 std::cerr <<
"Caught unknown exception\n";