10#if !defined(GEOGRAPHICLIB_UTILITY_HPP)
11#define GEOGRAPHICLIB_UTILITY_HPP 1
23# pragma warning (push)
24# pragma warning (disable: 4127)
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
324 if (
sizeof(IntT) ==
sizeof(ExtT) &&
325 std::numeric_limits<IntT>::is_integer ==
326 std::numeric_limits<ExtT>::is_integer)
329 str.read(
reinterpret_cast<char*
>(array), num *
sizeof(ExtT));
333 if (bigendp != Math::bigendian) {
334 for (
size_t i = num; i--;)
335 array[i] = Math::swab<IntT>(array[i]);
341 const int bufsize = 1024;
342 ExtT buffer[bufsize];
346 int n = (std::min)(k, bufsize);
347 str.read(
reinterpret_cast<char*
>(buffer), n *
sizeof(ExtT));
350 for (
int j = 0; j < n; ++j) {
352 ExtT x = bigendp == Math::bigendian ? buffer[j] :
353 Math::swab<ExtT>(buffer[j]);
354#if GEOGRAPHICLIB_PRECISION > 2
356 if (
typeid(ExtT) ==
typeid(
double) &&
399 std::ostringstream str;
400 str << std::scientific
401 << std::setprecision(std::numeric_limits<ExtT>::digits10-1)
403 if (val<ExtT>(str.str()) == x)
404 array[i++] = val<IntT>(str.str());
406 array[i++] = IntT(x);
410 array[i++] = IntT(x);
414 array[i++] = IntT(x);
436 template<
typename ExtT,
typename IntT,
bool bigendp>
437 static void readarray(std::istream& str, std::vector<IntT>& array) {
438 if (array.size() > 0)
439 readarray<ExtT, IntT, bigendp>(str, &array[0], array.size());
454 template<
typename ExtT,
typename IntT,
bool bigendp>
455 static void writearray(std::ostream& str,
const IntT array[],
size_t num)
457#if GEOGRAPHICLIB_PRECISION < 4
458 if (
sizeof(IntT) ==
sizeof(ExtT) &&
459 std::numeric_limits<IntT>::is_integer ==
460 std::numeric_limits<ExtT>::is_integer &&
461 bigendp == Math::bigendian)
464 str.write(
reinterpret_cast<const char*
>(array), num *
sizeof(ExtT));
471 const int bufsize = 1024;
472 ExtT buffer[bufsize];
476 int n = (std::min)(k, bufsize);
477 for (
int j = 0; j < n; ++j)
479 buffer[j] = bigendp == Math::bigendian ? ExtT(array[i++]) :
480 Math::swab<ExtT>(ExtT(array[i++]));
481 str.write(
reinterpret_cast<const char*
>(buffer), n *
sizeof(ExtT));
501 template<
typename ExtT,
typename IntT,
bool bigendp>
502 static void writearray(std::ostream& str, std::vector<IntT>& array) {
503 if (array.size() > 0)
504 writearray<ExtT, IntT, bigendp>(str, &array[0], array.size());
529 static bool ParseLine(
const std::string& line,
530 std::string& key, std::string& value,
531 char equals =
'\0',
char comment =
'#');
552 static int set_digits(
int ndigits = 0);
563 template<>
inline std::string Utility::val<std::string>(
const std::string& s)
580 template<>
inline bool Utility::val<bool>(
const std::string& s) {
581 std::string t(
trim(s));
582 if (t.empty())
return false;
585 std::istringstream is(t);
587 int pos = int(is.tellg());
588 if (!(pos < 0 || pos ==
int(t.size())))
594 for (std::string::iterator p = t.begin(); p != t.end(); ++p)
595 *p =
char(std::tolower(*p));
598 if (t ==
"f" || t ==
"false")
return false;
601 if (t ==
"n" || t ==
"nil" || t ==
"no")
return false;
604 if (t ==
"off")
return false;
605 else if (t ==
"on")
return true;
608 if (t ==
"t" || t ==
"true")
return true;
611 if (t ==
"y" || t ==
"yes")
return true;
631 template<>
inline std::string Utility::str<Math::real>(
Math::real x,
int p) {
634 return x < 0 ? std::string(
"-inf") :
635 (x > 0 ? std::string(
"inf") : std::string(
"nan"));
636 std::ostringstream s;
637 if (p >= 0) s << std::fixed << std::setprecision(p);
638 s << x;
return s.str();
644# pragma warning (pop)
Header for GeographicLib::Constants class.
#define GEOGRAPHICLIB_EXPORT
Exception handling for GeographicLib.
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.