GeographicLib  1.38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Utility.cpp
Go to the documentation of this file.
1 /**
2  * \file Utility.cpp
3  * \brief Implementation for GeographicLib::Utility class
4  *
5  * Copyright (c) Charles Karney (2011) <charles@karney.com> and licensed under
6  * the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
10 #include <cstdlib>
12 
13 #if defined(_MSC_VER)
14 // Squelch warnings about unsafe use of getenv
15 # pragma warning (disable: 4996)
16 #endif
17 
18 namespace GeographicLib {
19 
20  using namespace std;
21 
22  bool Utility::ParseLine(const std::string& line,
23  std::string& key, std::string& val) {
24  const char* spaces = " \t\n\v\f\r";
25  string::size_type n0 = line.find_first_not_of(spaces);
26  if (n0 == string::npos)
27  return false; // Blank line
28  string::size_type n1 = line.find_first_of('#', n0);
29  if (n0 == n1)
30  return false; // Only a comment
31  val = line.substr(n0, n1 == string::npos ? n1 : n1 - n0);
32  n0 = val.find_first_of(spaces);
33  key = val.substr(0, n0);
34  if (n0 == string::npos) {
35  val = "";
36  return true;
37  }
38  n0 = val.find_first_not_of(spaces, n0);
39  if (n0 == string::npos) {
40  val = "";
41  return true;
42  }
43  n1 = val.find_last_not_of(spaces);
44  val = val.substr(n0, n1 + 1 - n0);
45  return true;
46  }
47 
48  int Utility::set_digits(int ndigits) {
49 #if GEOGRAPHICLIB_PRECISION == 5
50  if (ndigits <= 0) {
51  char* digitenv = getenv("GEOGRAPHICLIB_DIGITS");
52  if (digitenv)
53  ndigits = strtol(digitenv, NULL, 0);
54  if (ndigits <= 0)
55  ndigits = 256;
56  }
57 #endif
58  return Math::set_digits(ndigits);
59  }
60 
61 } // namespace GeographicLib