File: Utility.hpp

package info (click to toggle)
geographiclib 2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,572 kB
  • sloc: cpp: 27,765; sh: 5,463; makefile: 695; python: 12; ansic: 10
file content (661 lines) | stat: -rw-r--r-- 28,397 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/**
 * \file Utility.hpp
 * \brief Header for GeographicLib::Utility class
 *
 * Copyright (c) Charles Karney (2011-2024) <karney@alum.mit.edu> and licensed
 * under the MIT/X11 License.  For more information, see
 * https://geographiclib.sourceforge.io/
 **********************************************************************/

#if !defined(GEOGRAPHICLIB_UTILITY_HPP)
#define GEOGRAPHICLIB_UTILITY_HPP 1

#include <GeographicLib/Constants.hpp>
#include <iomanip>
#include <vector>
#include <sstream>
#include <cctype>
#include <ctime>
#include <cstring>

#if defined(_MSC_VER)
// Squelch warnings about constant conditional expressions
#  pragma warning (push)
#  pragma warning (disable: 4127)
#endif

namespace GeographicLib {

  /**
   * \brief Some utility routines for %GeographicLib
   *
   * Example of use:
   * \include example-Utility.cpp
   **********************************************************************/
  class GEOGRAPHICLIB_EXPORT Utility {
  private:
    static bool gregorian(int y, int m, int d) {
      // The original cut over to the Gregorian calendar in Pope Gregory XIII's
      // time had 1582-10-04 followed by 1582-10-15. Here we implement the
      // switch over used by the English-speaking world where 1752-09-02 was
      // followed by 1752-09-14. We also assume that the year always begins
      // with January 1, whereas in reality it often was reckoned to begin in
      // March.
      return 100 * (100 * y + m) + d >= 17520914; // or 15821015
    }
    static bool gregorian(int s) {
      return s >= 639799;       // 1752-09-14
    }
  public:

    /**
     * Convert a date to the day numbering sequentially starting with
     * 0001-01-01 as day 1.
     *
     * @param[in] y the year (must be positive).
     * @param[in] m the month, Jan = 1, etc. (must be positive).  Default = 1.
     * @param[in] d the day of the month (must be positive).  Default = 1.
     * @return the sequential day number.
     **********************************************************************/
    static int day(int y, int m = 1, int d = 1);

    /**
     * Convert a date to the day numbering sequentially starting with
     * 0001-01-01 as day 1.
     *
     * @param[in] y the year (must be positive).
     * @param[in] m the month, Jan = 1, etc. (must be positive).  Default = 1.
     * @param[in] d the day of the month (must be positive).  Default = 1.
     * @param[in] check whether to check the date.
     * @exception GeographicErr if the date is invalid and \e check is true.
     * @return the sequential day number.
     **********************************************************************/
    static int day(int y, int m, int d, bool check);

    /**
     * Given a day (counting from 0001-01-01 as day 1), return the date.
     *
     * @param[in] s the sequential day number (must be positive)
     * @param[out] y the year.
     * @param[out] m the month, Jan = 1, etc.
     * @param[out] d the day of the month.
     **********************************************************************/
    static void date(int s, int& y, int& m, int& d);

    /**
     * Given a date as a string in the format yyyy, yyyy-mm, or yyyy-mm-dd,
     * return the numeric values for the year, month, and day.  No checking is
     * done on these values.  The string "now" is interpreted as the present
     * date (in UTC).
     *
     * @param[in] s the date in string format.
     * @param[out] y the year.
     * @param[out] m the month, Jan = 1, etc.
     * @param[out] d the day of the month.
     * @exception GeographicErr is \e s is malformed.
     **********************************************************************/
    static void date(const std::string& s, int& y, int& m, int& d);

    /**
     * Given the date, return the day of the week.
     *
     * @param[in] y the year (must be positive).
     * @param[in] m the month, Jan = 1, etc. (must be positive).
     * @param[in] d the day of the month (must be positive).
     * @return the day of the week with Sunday, Monday--Saturday = 0,
     *   1--6.
     **********************************************************************/
    static int dow(int y, int m, int d) { return dow(day(y, m, d)); }

    /**
     * Given the sequential day, return the day of the week.
     *
     * @param[in] s the sequential day (must be positive).
     * @return the day of the week with Sunday, Monday--Saturday = 0,
     *   1--6.
     **********************************************************************/
    static int dow(int s) {
      return (s + 5) % 7;  // The 5 offset makes day 1 (0001-01-01) a Saturday.
    }

    /**
     * Convert a string representing a date to a fractional year.
     *
     * @tparam T the type of the argument.
     * @param[in] s the string to be converted.
     * @exception GeographicErr if \e s can't be interpreted as a date.
     * @return the fractional year.
     *
     * The string is first read as an ordinary number (e.g., 2010 or 2012.5);
     * if this is successful, the value is returned.  Otherwise the string
     * should be of the form yyyy-mm or yyyy-mm-dd and this is converted to a
     * number with 2010-01-01 giving 2010.0 and 2012-07-03 giving 2012.5.  The
     * string "now" is interpreted as the present date.
     **********************************************************************/
    template<typename T> static T fractionalyear(const std::string& s) {
      try {
        return val<T>(s);
      }
      catch (const std::exception&) {}
      int y, m, d;
      date(s, y, m, d);
      int t = day(y, m, d, true);
      return T(y) + T(t - day(y)) / T(day(y + 1) - day(y));
    }

    /**
     * Convert a object of type T to a string.
     *
     * @tparam T the type of the argument.
     * @param[in] x the value to be converted.
     * @param[in] p the precision used (default &minus;1).
     * @exception std::bad_alloc if memory for the string can't be allocated.
     * @return the string representation.
     *
     * If \e p &ge; 0, then the number fixed format is used with \e p bits of
     * precision.  With \e p < 0, there is no manipulation of the format,
     * except that <code>boolalpha</code> is used to represent bools as "true"
     * and "false".  There is an overload of this function if T is Math::real;
     * this deals with inf and nan.
     **********************************************************************/
    template<typename T> static std::string str(T x, int p = -1) {
      std::ostringstream s;
      if (p >= 0) s << std::fixed << std::setprecision(p);
      s << std::boolalpha << x; return s.str();
    }

    /**
     * Trim the white space from the beginning and end of a string.
     *
     * @param[in] s the string to be trimmed
     * @return the trimmed string
     **********************************************************************/
    static std::string trim(const std::string& s);

    /**
     * Lookup up a character in a string.
     *
     * @param[in] s the string to be searched.
     * @param[in] c the character to look for.
     * @return the index of the first occurrence character in the string or
     *   &minus;1 is the character is not present.
     *
     * \e c is converted to upper case before search \e s.  Therefore, it is
     * intended that \e s should not contain any lower case letters.
     **********************************************************************/
    static int lookup(const std::string& s, char c);

    /**
     * Lookup up a character in a char*.
     *
     * @param[in] s the char* string to be searched.
     * @param[in] c the character to look for.
     * @return the index of the first occurrence character in the string or
     *   &minus;1 is the character is not present.
     *
     * \e c is converted to upper case before search \e s.  Therefore, it is
     * intended that \e s should not contain any lower case letters.
     **********************************************************************/
    static int lookup(const char* s, char c);

    /**
     * Convert a string to type T.
     *
     * @tparam T the type of the return value.
     * @param[in] s the string to be converted.
     * @exception GeographicErr is \e s is not readable as a T.
     * @return object of type T.
     *
     * White space at the beginning and end of \e s is ignored.
     *
     * Special handling is provided for some types.
     *
     * If T is a floating point type, then inf and nan are recognized.
     *
     * If T is bool, then \e s should either be string a representing 0 (false)
     * or 1 (true) or one of the strings
     * - "false", "f", "nil", "no", "n", "off", or "" meaning false,
     * - "true", "t", "yes", "y", or "on" meaning true;
     * .
     * case is ignored.
     *
     * If T is std::string, then \e s is returned (with the white space at the
     * beginning and end removed).
     **********************************************************************/
    template<typename T> static T val(const std::string& s) {
      // If T is bool, then the specialization val<bool>() defined below is
      // used.
      T x;
      std::string errmsg, t(trim(s));
      do {                     // Executed once (provides the ability to break)
        std::istringstream is(t);
        if (!(is >> x)) {
          errmsg = "Cannot decode " + t;
          break;
        }
        int pos = int(is.tellg()); // Returns -1 at end of string?
        if (!(pos < 0 || pos == int(t.size()))) {
          errmsg = "Extra text " + t.substr(pos) + " at end of " + t;
          break;
        }
        return x;
      } while (false);
      x = std::numeric_limits<T>::is_integer ? 0 : nummatch<T>(t);
      if (x == 0)
        throw GeographicErr(errmsg);
      return x;
    }

    /**
     * Match "nan" and "inf" (and variants thereof) in a string.
     *
     * @tparam T the type of the return value (this should be a floating point
     *   type).
     * @param[in] s the string to be matched.
     * @return appropriate special value (&plusmn;&infin;, nan) or 0 if none is
     *   found.
     *
     * White space is not allowed at the beginning or end of \e s.
     **********************************************************************/
    template<typename T> static T nummatch(const std::string& s) {
      if (s.length() < 3)
        return 0;
      std::string t(s);
      for (std::string::iterator p = t.begin(); p != t.end(); ++p)
        *p = char(std::toupper(*p));
      for (size_t i = s.length(); i--;)
        t[i] = char(std::toupper(s[i]));
      int sign = t[0] == '-' ? -1 : 1;
      std::string::size_type p0 = t[0] == '-' || t[0] == '+' ? 1 : 0;
      std::string::size_type p1 = t.find_last_not_of('0');
      if (p1 == std::string::npos || p1 + 1 < p0 + 3)
        return 0;
      // Strip off sign and trailing 0s
      t = t.substr(p0, p1 + 1 - p0);  // Length at least 3
      if (t == "NAN" || t == "1.#QNAN" || t == "1.#SNAN" || t == "1.#IND" ||
          t == "1.#R")
        return Math::NaN<T>();
      else if (t == "INF" || t == "1.#INF" || t == "INFINITY")
        return sign * Math::infinity<T>();
      return 0;
    }

    /**
     * Read a simple fraction, e.g., 3/4, from a string to an object of type T.
     *
     * @tparam T the type of the return value.
     * @param[in] s the string to be converted.
     * @exception GeographicErr is \e s is not readable as a fraction of type
     *   T.
     * @return object of type T
     *
     * \note The msys shell under Windows converts arguments which look like
     * pathnames into their Windows equivalents.  As a result the argument
     * "-1/300" gets mangled into something unrecognizable.  A workaround is to
     * use a floating point number in the numerator, i.e., "-1.0/300".  (Recent
     * versions of the msys shell appear \e not to have this problem.)
     **********************************************************************/
    template<typename T> static T fract(const std::string& s) {
      std::string::size_type delim = s.find('/');
      return
        !(delim != std::string::npos && delim >= 1 && delim + 2 <= s.size()) ?
        val<T>(s) :
        // delim in [1, size() - 2]
        val<T>(s.substr(0, delim)) / val<T>(s.substr(delim + 1));
    }

    /**
     * Read data of type ExtT from a binary stream to an array of type IntT.
     * The data in the file is in (bigendp ? big : little)-endian format.
     *
     * @tparam ExtT the type of the objects in the binary stream (external).
     * @tparam IntT the type of the objects in the array (internal).
     * @tparam bigendp true if the external storage format is big-endian.
     * @param[in] str the input stream containing the data of type ExtT
     *   (external).
     * @param[out] array the output array of type IntT (internal).
     * @param[in] num the size of the array.
     * @exception GeographicErr if the data cannot be read.
     *
     * This routine is used to read binary data files for the Geoid,
     * GravityModel, and MagneticModel classes.  In the case of GravityModel
     * and MagneticMode, the data is published by a government agency as text
     * files, and the coefficient to realize the models are converted to a
     * double precision binary format to minimize storage and to simplify
     * reading the data.
     *
     * For GEOGRAPHIC_PRECISION == 2, the data is read faithfully.  For
     * GEOGRAPHICLIB_PRECISION > 2, external data of type double is interpreted
     * as an approximation of an exact decimal value; this exact number is
     * convered to a real number at the higher precision.
     **********************************************************************/
    template<typename ExtT, typename IntT, bool bigendp>
      static void readarray(std::istream& str, IntT array[], size_t num) {
#if GEOGRAPHICLIB_PRECISION < 4
      if constexpr (sizeof(IntT) == sizeof(ExtT) &&
          std::numeric_limits<IntT>::is_integer ==
          std::numeric_limits<ExtT>::is_integer)
        {
          // Data is compatible (aside from the issue of endian-ness).
          str.read(reinterpret_cast<char*>(array), num * sizeof(ExtT));
          if (!str.good())
            throw GeographicErr("Failure reading data");
          if constexpr (bigendp != Math::bigendian) {
            // endian mismatch -> swap bytes
            for (size_t i = num; i--;)
              array[i] = Math::swab<IntT>(array[i]);
          }
        }
      else
#endif
        {
          const int bufsize = 1024; // read this many values at a time
          ExtT buffer[bufsize];     // temporary buffer
          int k = int(num);         // data values left to read
          int i = 0;                // index into output array
          while (k) {
            int n = (std::min)(k, bufsize);
            str.read(reinterpret_cast<char*>(buffer), n * sizeof(ExtT));
            if (!str.good())
              throw GeographicErr("Failure reading data");
            for (int j = 0; j < n; ++j) {
              // fix endian-ness
              ExtT x = bigendp == Math::bigendian ? buffer[j] :
                Math::swab<ExtT>(buffer[j]);
#if GEOGRAPHICLIB_PRECISION > 2
              // typeid doesn't allow if constexpr here
              if (typeid(ExtT) == typeid(double) &&
                  typeid(IntT) == typeid(Math::real)) {
                // readarray is used to read in coefficient data rapidly.  Thus
                // 8.3n is stored in its IEEE double representation.  This is
                // fine is the working precision is double.  However, when
                // working at higher precision, how should be interpret the
                // constant 8.3 appearing in a published table?  Possibilities
                // are
                //
                // (a) treat this as an exact decimal number 83/10;
                //
                // (b) treat this as the approximate decimal representation of
                // an exact double precision number 2336242306698445/2^48 =
                // 8.300000000000000710542735760100185871124267578125
                //
                // Here use (a) if the number of significant digits in the
                // number is 15 or less.  Otherwise, we use (b).
                //
                // We implement this as follows.  Any double which can be
                // represented as a decimal number with precision 14 = digis10
                // - 1 (= 15 sig figs) is treated as an approximation to that
                // decimal number.  The high precision number is then obtained
                // by reading the decimal number at that precision.  Otherwise
                // the double is treated as exact.  The high precision number
                // is obtained by adding zeros in the binary fraction.
                //
                // N.B. printing with precision 14 = digis10 - 1 allows short
                // numbers to be represended with trailing zeros.  This isn't
                // necessarily the case with precision = digits10, e.g., 8.3
                // becomes 8.300000000000001.
                //
                // This prescription doesn't exactly implement the method
                // proposed.  If the published table of numbers includes
                // 8.300000000000001, this will be interpreted as 8.3.  This
                // doesn't apply to any published magnetic or gravity data.
                // E.g., the coefficients for EGM96, resp. EGM2008, are given
                // with precision 11, resp. 14.
                //
                // This conversion of doubles to Math::real comes at a
                // substantial cost.  It adds about 14 s to the time it takes
                // to read the egm2008 gravity model for quad and mpfr
                // precisions.  This is acceptable, however, because high
                // precision is only used for benchmarking.
                std::ostringstream str;
                str << std::scientific
                    << std::setprecision(std::numeric_limits<ExtT>::digits10-1)
                    << x;
                // Code for GEOGRAPHILIB_PRECISION > 2 and types double/real
                if (val<ExtT>(str.str()) == x)
                  array[i++] = val<IntT>(str.str());
                else
                  array[i++] = IntT(x);
              } else {
                // Code for GEOGRAPHILIB_PRECISION > 2 but types not
                // double/real
                array[i++] = IntT(x);
              }
#else
              // Code for GEOGRAPHILIB_PRECISION <= 2
              array[i++] = IntT(x);
#endif
            }
            k -= n;
          }
        }
      return;
    }

    /**
     * Read data of type ExtT from a binary stream to a vector array of type
     * IntT.  The data in the file is in (bigendp ? big : little)-endian
     * format.
     *
     * @tparam ExtT the type of the objects in the binary stream (external).
     * @tparam IntT the type of the objects in the array (internal).
     * @tparam bigendp true if the external storage format is big-endian.
     * @param[in] str the input stream containing the data of type ExtT
     *   (external).
     * @param[out] array the output vector of type IntT (internal).
     * @exception GeographicErr if the data cannot be read.
     **********************************************************************/
    template<typename ExtT, typename IntT, bool bigendp>
      static void readarray(std::istream& str, std::vector<IntT>& array) {
      if (array.size() > 0)
        readarray<ExtT, IntT, bigendp>(str, &array[0], array.size());
    }

    /**
     * Write data in an array of type IntT as type ExtT to a binary stream.
     * The data in the file is in (bigendp ? big : little)-endian format.
     *
     * @tparam ExtT the type of the objects in the binary stream (external).
     * @tparam IntT the type of the objects in the array (internal).
     * @tparam bigendp true if the external storage format is big-endian.
     * @param[out] str the output stream for the data of type ExtT (external).
     * @param[in] array the input array of type IntT (internal).
     * @param[in] num the size of the array.
     * @exception GeographicErr if the data cannot be written.
     **********************************************************************/
    template<typename ExtT, typename IntT, bool bigendp>
      static void writearray(std::ostream& str, const IntT array[], size_t num)
    {
#if GEOGRAPHICLIB_PRECISION < 4
      if constexpr (sizeof(IntT) == sizeof(ExtT) &&
                    std::numeric_limits<IntT>::is_integer ==
                    std::numeric_limits<ExtT>::is_integer &&
                    bigendp == Math::bigendian)
        {
          // Data is compatible (including endian-ness).
          str.write(reinterpret_cast<const char*>(array), num * sizeof(ExtT));
          if (!str.good())
            throw GeographicErr("Failure writing data");
        }
      else
#endif
        {
          const int bufsize = 1024; // write this many values at a time
          ExtT buffer[bufsize];     // temporary buffer
          int k = int(num);         // data values left to write
          int i = 0;                // index into output array
          while (k) {
            int n = (std::min)(k, bufsize);
            for (int j = 0; j < n; ++j)
              // cast to ExtT and fix endian-ness
              buffer[j] = bigendp == Math::bigendian ? ExtT(array[i++]) :
                Math::swab<ExtT>(ExtT(array[i++]));
            str.write(reinterpret_cast<const char*>(buffer), n * sizeof(ExtT));
            if (!str.good())
              throw GeographicErr("Failure writing data");
            k -= n;
          }
        }
      return;
    }

    /**
     * Write data in an array of type IntT as type ExtT to a binary stream.
     * The data in the file is in (bigendp ? big : little)-endian format.
     *
     * @tparam ExtT the type of the objects in the binary stream (external).
     * @tparam IntT the type of the objects in the array (internal).
     * @tparam bigendp true if the external storage format is big-endian.
     * @param[out] str the output stream for the data of type ExtT (external).
     * @param[in] array the input vector of type IntT (internal).
     * @exception GeographicErr if the data cannot be written.
     **********************************************************************/
    template<typename ExtT, typename IntT, bool bigendp>
      static void writearray(std::ostream& str, std::vector<IntT>& array) {
      if (array.size() > 0)
        writearray<ExtT, IntT, bigendp>(str, &array[0], array.size());
    }

    /**
     * Parse a KEY [=] VALUE line.
     *
     * @param[in] line the input line.
     * @param[out] key the KEY.
     * @param[out] value the VALUE.
     * @param[in] equals character representing "equals" to separate KEY and
     *   VALUE, if NULL (the default) use first space character.
     * @param[in] comment character to use as the comment character; if
     *   non-NULL, this character and everything after it is discarded; default
     *   is '#'.
     * @exception std::bad_alloc if memory for the internal strings can't be
     *   allocated.
     * @return whether a key was found.
     *
     * The \e comment character (default is '#') and everything after it are
     * discarded and the result trimmed of leading and trailing white space.
     * Use the \e equals delimiter character (or, if it is NULL -- the default,
     * the first white space) to separate \e key and \e value.  \e key and \e
     * value are trimmed of leading and trailing white space.  If \e key is
     * empty, then \e value is set to "" and false is returned.
     **********************************************************************/
    static bool ParseLine(const std::string& line,
                          std::string& key, std::string& value,
                          char equals = '\0', char comment = '#');

    /**
     * Set the binary precision of a real number.
     *
     * @param[in] ndigits the number of bits of precision.  If ndigits is 0
     *   (the default), then determine the precision from the environment
     *   variable GEOGRAPHICLIB_DIGITS.  If this is undefined, use ndigits =
     *   256 (i.e., about 77 decimal digits).
     * @return the resulting number of bits of precision.
     *
     * This only has an effect when GEOGRAPHICLIB_PRECISION >= 5.  The
     * precision should only be set once and before calls to any other
     * GeographicLib functions.  (Several functions, for example Math::pi(),
     * cache the return value in a static local variable.  The precision needs
     * to be set before a call to any such functions.)  In multi-threaded
     * applications, it is necessary also to set the precision in each thread
     * (see the example GeoidToGTX.cpp).  If GEOGRAPHICLIB_PRECISION > 5, then
     * the precision is set to GEOGRAPHICLIB_PRECISION, the compile-time value,
     * and \e ndigits is ignored.
     *
     * \note Use Math::digits() to return the current precision in bits.
     **********************************************************************/
    static int set_digits(int ndigits = 0);

  };

  /**
   * The specialization of Utility::val<T>() for strings.
   *
   * @param[in] s the string to be converted.
   * @exception GeographicErr is \e s is not readable as a T.
   * @return the string trimmed of its whitespace.
   **********************************************************************/
  template<> inline std::string Utility::val<std::string>(const std::string& s)
  { return trim(s); }

  /**
   * The specialization of Utility::val<T>() for bools.
   *
   * @param[in] s the string to be converted.
   * @exception GeographicErr is \e s is not readable as a T.
   * @return boolean value.
   *
   * \e s should either be string a representing 0 (false)
   * or 1 (true) or one of the strings
   * - "false", "f", "nil", "no", "n", "off", or "" meaning false,
   * - "true", "t", "yes", "y", or "on" meaning true;
   * .
   * case is ignored.
   **********************************************************************/
  template<> inline bool Utility::val<bool>(const std::string& s) {
    std::string t(trim(s));
    if (t.empty()) return false;
    bool x;
    {
      std::istringstream is(t);
      if (is >> x) {
        int pos = int(is.tellg()); // Returns -1 at end of string?
        if (!(pos < 0 || pos == int(t.size())))
          throw GeographicErr("Extra text " + t.substr(pos) +
                              " at end of " + t);
        return x;
      }
    }
    for (std::string::iterator p = t.begin(); p != t.end(); ++p)
      *p = char(std::tolower(*p));
    switch (t[0]) {             // already checked that t isn't empty
    case 'f':
      if (t == "f" || t == "false") return false;
      break;
    case 'n':
      if (t == "n" || t == "nil" || t == "no") return false;
      break;
    case 'o':
      if (t == "off") return false;
      else if (t == "on") return true;
      break;
    case 't':
      if (t == "t" || t == "true") return true;
      break;
    case 'y':
      if (t == "y" || t == "yes") return true;
      break;
    default:
      break;
    }
    throw GeographicErr("Cannot decode " + t + " as a bool");
  }

  /**
   * Convert a Math::real object to a string.
   *
   * @param[in] x the value to be converted.
   * @param[in] p the precision used (default &minus;1).
   * @exception std::bad_alloc if memory for the string can't be allocated.
   * @return the string representation.
   *
   * If \e p &ge; 0, then the number fixed format is used with p bits of
   * precision.  With p < 0, there is no manipulation of the format.  This is
   * an overload of str<T> which deals with inf and nan.
   **********************************************************************/
  template<> inline std::string Utility::str<Math::real>(Math::real x, int p) {
    using std::isfinite;
    if (!isfinite(x))
      return x < 0 ? std::string("-inf") :
        (x > 0 ? std::string("inf") : std::string("nan"));
    std::ostringstream s;
    if (p >= 0) s << std::fixed << std::setprecision(p);
    s << x; return s.str();
  }

} // namespace GeographicLib

#if defined(_MSC_VER)
#  pragma warning (pop)
#endif

#endif  // GEOGRAPHICLIB_UTILITY_HPP