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" ||
278 else if (t ==
"INF" || t ==
"1.#INF" || t ==
"INFINITY")
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));
332 template<
typename ExtT,
typename IntT,
bool bigendp>
334#if GEOGRAPHICLIB_PRECISION < 4
336 if (
sizeof(IntT) ==
sizeof(ExtT) &&
337 std::numeric_limits<IntT>::is_integer ==
338 std::numeric_limits<ExtT>::is_integer)
341 str.read(
reinterpret_cast<char*
>(array), num *
sizeof(ExtT));
346 for (
size_t i = num; i--;)
353 const int bufsize = 1024;
354 ExtT buffer[bufsize];
358 int n = (std::min)(k, bufsize);
359 str.read(
reinterpret_cast<char*
>(buffer), n *
sizeof(ExtT));
362 for (
int j = 0; j < n; ++j) {
366#if GEOGRAPHICLIB_PRECISION > 2
368 if (
typeid(ExtT) ==
typeid(
double) &&
411 std::ostringstream
str;
412 str << std::scientific
413 << std::setprecision(std::numeric_limits<ExtT>::digits10-1)
419 array[i++] = IntT(x);
423 array[i++] = IntT(x);
427 array[i++] = IntT(x);
449 template<
typename ExtT,
typename IntT,
bool bigendp>
451 if (array.size() > 0)
467 template<
typename ExtT,
typename IntT,
bool bigendp>
470#if GEOGRAPHICLIB_PRECISION < 4
471 if (
sizeof(IntT) ==
sizeof(ExtT) &&
472 std::numeric_limits<IntT>::is_integer ==
473 std::numeric_limits<ExtT>::is_integer &&
477 str.write(
reinterpret_cast<const char*
>(array), num *
sizeof(ExtT));
484 const int bufsize = 1024;
485 ExtT buffer[bufsize];
489 int n = (std::min)(k, bufsize);
490 for (
int j = 0; j < n; ++j)
494 str.write(
reinterpret_cast<const char*
>(buffer), n *
sizeof(ExtT));
514 template<
typename ExtT,
typename IntT,
bool bigendp>
516 if (array.size() > 0)
542 static bool ParseLine(
const std::string& line,
543 std::string& key, std::string& value,
544 char equals =
'\0',
char comment =
'#');
567 static int set_digits(
int ndigits = 0);
596 std::string t(
trim(s));
597 if (t.empty())
return false;
600 std::istringstream is(t);
602 int pos = int(is.tellg());
603 if (!(pos < 0 || pos ==
int(t.size())))
609 for (std::string::iterator p = t.begin(); p != t.end(); ++p)
610 *p = char(std::tolower(*p));
613 if (t ==
"f" || t ==
"false")
return false;
616 if (t ==
"n" || t ==
"nil" || t ==
"no")
return false;
619 if (t ==
"off")
return false;
620 else if (t ==
"on")
return true;
623 if (t ==
"t" || t ==
"true")
return true;
626 if (t ==
"y" || t ==
"yes")
return true;
649 return x < 0 ? std::string(
"-inf") :
650 (x > 0 ? std::string(
"inf") : std::string(
"nan"));
651 std::ostringstream s;
652 if (p >= 0) s << std::fixed << std::setprecision(p);
653 s << x;
return s.str();
659# 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 date(int s, int &y, int &m, int &d)
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 int day(int y, int m=1, int d=1)
static std::string str(T x, int p=-1)
Namespace for GeographicLib.