10#if !defined(GEOGRAPHICLIB_UTILITY_HPP)
11#define GEOGRAPHICLIB_UTILITY_HPP 1
23# pragma warning (push)
24# pragma warning (disable: 4127 4996)
37 static bool gregorian(
int y,
int m,
int d) {
44 return 100 * (100 * y + m) + d >= 17520914;
46 static bool gregorian(
int s) {
60 static int day(
int y,
int m = 1,
int d = 1);
73 static int day(
int y,
int m,
int d,
bool check);
83 static void date(
int s,
int& y,
int& m,
int& d);
97 static void date(
const std::string& s,
int& y,
int& m,
int& d);
108 static int dow(
int y,
int m,
int d) {
return dow(day(y, m, d)); }
139 catch (
const std::exception&) {}
142 int t = day(y, m, d,
true);
143 return T(y) + T(t - day(y)) / T(day(y + 1) - day(y));
161 template<
typename T>
static std::string
str(T x,
int p = -1) {
162 std::ostringstream s;
163 if (p >= 0) s << std::fixed << std::setprecision(p);
164 s << std::boolalpha << x;
return s.str();
173 static std::string trim(
const std::string& s);
186 static int lookup(
const std::string& s,
char c);
199 static int lookup(
const char* s,
char c);
225 template<
typename T>
static T
val(
const std::string& s) {
229 std::string errmsg, t(trim(s));
231 std::istringstream is(t);
233 errmsg =
"Cannot decode " + t;
236 int pos = int(is.tellg());
237 if (!(pos < 0 || pos ==
int(t.size()))) {
238 errmsg =
"Extra text " + t.substr(pos) +
" at end of " + t;
243 x = std::numeric_limits<T>::is_integer ? 0 : nummatch<T>(t);
260 template<
typename T>
static T
nummatch(
const std::string& s) {
264 for (std::string::iterator p = t.begin(); p != t.end(); ++p)
265 *p = char(std::toupper(*p));
266 for (
size_t i = s.length(); i--;)
267 t[i] = char(std::toupper(s[i]));
268 int sign = t[0] ==
'-' ? -1 : 1;
269 std::string::size_type p0 = t[0] ==
'-' || t[0] ==
'+' ? 1 : 0;
270 std::string::size_type p1 = t.find_last_not_of(
'0');
271 if (p1 == std::string::npos || p1 + 1 < p0 + 3)
274 t = t.substr(p0, p1 + 1 - p0);
275 if (t ==
"NAN" || t ==
"1.#QNAN" || t ==
"1.#SNAN" || t ==
"1.#IND" ||
277 return Math::NaN<T>();
278 else if (t ==
"INF" || t ==
"1.#INF" || t ==
"INFINITY")
279 return sign * Math::infinity<T>();
298 template<
typename T>
static T
fract(
const std::string& s) {
299 std::string::size_type delim = s.find(
'/');
301 !(delim != std::string::npos && delim >= 1 && delim + 2 <= s.size()) ?
304 val<T>(s.substr(0, delim)) / val<T>(s.substr(delim + 1));
320 template<
typename ExtT,
typename IntT,
bool bigendp>
321 static void readarray(std::istream& str, IntT array[],
size_t num) {
322#if GEOGRAPHICLIB_PRECISION < 4
323 if (
sizeof(IntT) ==
sizeof(ExtT) &&
324 std::numeric_limits<IntT>::is_integer ==
325 std::numeric_limits<ExtT>::is_integer)
328 str.read(
reinterpret_cast<char*
>(array), num *
sizeof(ExtT));
332 for (
size_t i = num; i--;)
333 array[i] = Math::swab<IntT>(array[i]);
339 const int bufsize = 1024;
340 ExtT buffer[bufsize];
344 int n = (std::min)(k, bufsize);
345 str.read(
reinterpret_cast<char*
>(buffer), n *
sizeof(ExtT));
348 for (
int j = 0; j < n; ++j)
351 Math::swab<ExtT>(buffer[j]));
371 template<
typename ExtT,
typename IntT,
bool bigendp>
372 static void readarray(std::istream& str, std::vector<IntT>& array) {
373 if (array.size() > 0)
374 readarray<ExtT, IntT, bigendp>(str, &array[0], array.size());
389 template<
typename ExtT,
typename IntT,
bool bigendp>
390 static void writearray(std::ostream& str,
const IntT array[],
size_t num)
392#if GEOGRAPHICLIB_PRECISION < 4
393 if (
sizeof(IntT) ==
sizeof(ExtT) &&
394 std::numeric_limits<IntT>::is_integer ==
395 std::numeric_limits<ExtT>::is_integer &&
399 str.write(
reinterpret_cast<const char*
>(array), num *
sizeof(ExtT));
406 const int bufsize = 1024;
407 ExtT buffer[bufsize];
411 int n = (std::min)(k, bufsize);
412 for (
int j = 0; j < n; ++j)
415 Math::swab<ExtT>(ExtT(array[i++]));
416 str.write(
reinterpret_cast<const char*
>(buffer), n *
sizeof(ExtT));
436 template<
typename ExtT,
typename IntT,
bool bigendp>
437 static void writearray(std::ostream& str, std::vector<IntT>& array) {
438 if (array.size() > 0)
439 writearray<ExtT, IntT, bigendp>(str, &array[0], array.size());
464 static bool ParseLine(
const std::string& line,
465 std::string& key, std::string& value,
466 char equals =
'\0',
char comment =
'#');
487 static int set_digits(
int ndigits = 0);
498 template<>
inline std::string Utility::val<std::string>(
const std::string& s)
515 template<>
inline bool Utility::val<bool>(
const std::string& s) {
516 std::string t(
trim(s));
517 if (t.empty())
return false;
520 std::istringstream is(t);
522 int pos = int(is.tellg());
523 if (!(pos < 0 || pos ==
int(t.size())))
529 for (std::string::iterator p = t.begin(); p != t.end(); ++p)
530 *p = char(std::tolower(*p));
533 if (t ==
"f" || t ==
"false")
return false;
536 if (t ==
"n" || t ==
"nil" || t ==
"no")
return false;
539 if (t ==
"off")
return false;
540 else if (t ==
"on")
return true;
543 if (t ==
"t" || t ==
"true")
return true;
546 if (t ==
"y" || t ==
"yes")
return true;
566 template<>
inline std::string Utility::str<Math::real>(
Math::real x,
int p) {
569 return x < 0 ? std::string(
"-inf") :
570 (x > 0 ? std::string(
"inf") : std::string(
"nan"));
571 std::ostringstream s;
572#if GEOGRAPHICLIB_PRECISION == 4
578 using std::signbit;
using std::fabs;
579 using std::round;
using std::fmod;
580 int n = signbit(x) ? -1 : 1; x = fabs(x);
583 if (2 * (ix - x) == 1 && fmod(ix,
Math::real(2)) == 1) --ix;
584 s << std::fixed << std::setprecision(1) << n*ix;
585 std::string r(s.str());
587 return r.substr(0, (std::max)(
int(r.size()) - 2, 0));
590 if (p >= 0) s << std::fixed << std::setprecision(p);
591 s << x;
return s.str();
597# pragma warning (pop)
Header for GeographicLib::Constants class.
#define GEOGRAPHICLIB_EXPORT
Exception handling for GeographicLib.
static const bool bigendian
Some utility routines for GeographicLib.
static void readarray(std::istream &str, std::vector< IntT > &array)
static void writearray(std::ostream &str, std::vector< IntT > &array)
static T fractionalyear(const std::string &s)
static void readarray(std::istream &str, IntT array[], size_t num)
static void writearray(std::ostream &str, const IntT array[], size_t num)
static int dow(int y, int m, int d)
static T fract(const std::string &s)
static T val(const std::string &s)
static T nummatch(const std::string &s)
static std::string trim(const std::string &s)
static std::string str(T x, int p=-1)
Namespace for GeographicLib.