GeographicLib: Utility.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10#include
12
13#if defined(_MSC_VER)
14
15# pragma warning (disable: 4996)
16#endif
17
19
20 using namespace std;
21
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 bool greg = gregorian(y, m, d);
59 y += (m + 9) / 12 - 1;
60 m = (m + 9) % 12;
61 return
62 (1461 * y) / 4
63
64
65
66
67 + (greg ? (y / 100) / 4 - (y / 100) + 2 : 0)
68 + (153 * m + 2) / 5
69 + d - 1
70 - 305;
71
72 }
73
75 int s = day(y, m, d);
76 if (!check)
77 return s;
78 int y1, m1, d1;
79 date(s, y1, m1, d1);
80 if (!(s > 0 && y == y1 && m == m1 && d == d1))
82 str(y) + "-" + str(m) + "-" + str(d)
83 + (s > 0 ? "; use " +
84 str(y1) + "-" + str(m1) + "-" + str(d1) :
85 " before 0001-01-01"));
86 return s;
87 }
88
90 int c = 0;
91 bool greg = gregorian(s);
92 s += 305;
93 if (greg) {
94 s -= 2;
95
96
97 c = (4 * s + 3) / 146097;
98 s -= (c * 146097) / 4;
99 }
100 y = (4 * s + 3) / 1461;
101 s -= (1461 * y) / 4;
102 y += c * 100;
103 m = (5 * s + 2) / 153;
104 s -= (153 * m + 2) / 5;
105 d = s + 1;
106 y += (m + 2) / 12;
107 m = (m + 2) % 12 + 1;
108 }
109
110 void Utility::date(const std::string& s, int& y, int& m, int& d) {
111 if (s == "now") {
112 time_t t = time(0);
113 struct tm* now = gmtime(&t);
114 y = now->tm_year + 1900;
115 m = now->tm_mon + 1;
116 d = now->tm_mday;
117 return;
118 }
119 int y1, m1 = 1, d1 = 1;
120 const char* digits = "0123456789";
121 string::size_type p1 = s.find_first_not_of(digits);
122 if (p1 == string::npos)
123 y1 = val(s);
124 else if (s[p1] != '-')
125 throw GeographicErr("Delimiter not hyphen in date " + s);
126 else if (p1 == 0)
127 throw GeographicErr("Empty year field in date " + s);
128 else {
129 y1 = val(s.substr(0, p1));
130 if (++p1 == s.size())
131 throw GeographicErr("Empty month field in date " + s);
132 string::size_type p2 = s.find_first_not_of(digits, p1);
133 if (p2 == string::npos)
134 m1 = val(s.substr(p1));
135 else if (s[p2] != '-')
136 throw GeographicErr("Delimiter not hyphen in date " + s);
137 else if (p2 == p1)
138 throw GeographicErr("Empty month field in date " + s);
139 else {
140 m1 = val(s.substr(p1, p2 - p1));
141 if (++p2 == s.size())
142 throw GeographicErr("Empty day field in date " + s);
143 d1 = val(s.substr(p2));
144 }
145 }
146 y = y1; m = m1; d = d1;
147 }
148
150 unsigned
151 beg = 0,
152 end = unsigned(s.size());
153 while (beg < end && isspace(s[beg]))
154 ++beg;
155 while (beg < end && isspace(s[end - 1]))
156 --end;
157 return string(s, beg, end-beg);
158 }
159
161 string::size_type r = s.find(char(toupper(c)));
162 return r == string::npos ? -1 : int(r);
163 }
164
166 const char* p = strchr(s, toupper(c));
167 return p != NULL ? int(p - s) : -1;
168 }
169
171 std::string& key, std::string& value,
172 char equals, char comment) {
173 key.clear(); value.clear();
174 string::size_type n = comment ? line.find(comment) : line.size();
175 string linea = trim(line.substr(0, n));
176 if (linea.empty()) return false;
177 n = equals ? linea.find(equals) : linea.find_first_of(" \t\n\v\f\r");
178 key = trim(linea.substr(0, n));
179 if (key.empty()) return false;
180 if (n != string::npos) value = trim(linea.substr(n + 1));
181 return true;
182 }
183
185#if GEOGRAPHICLIB_PRECISION == 5
186 if (ndigits <= 0) {
187 char* digitenv = getenv("GEOGRAPHICLIB_DIGITS");
188 if (digitenv)
189 ndigits = strtol(digitenv, NULL, 0);
190 if (ndigits <= 0)
191 ndigits = 256;
192 }
193#endif
195 }
196
197}
Header for GeographicLib::Utility class.
Exception handling for GeographicLib.
static int set_digits(int ndigits)
static void date(int s, int &y, int &m, int &d)
Definition Utility.cpp:89
static int lookup(const std::string &s, char c)
Definition Utility.cpp:160
static bool ParseLine(const std::string &line, std::string &key, std::string &value, char equals='\0', char comment='#')
Definition Utility.cpp:170
static int set_digits(int ndigits=0)
Definition Utility.cpp:184
static std::string trim(const std::string &s)
Definition Utility.cpp:149
static int day(int y, int m=1, int d=1)
Definition Utility.cpp:22
static std::string str(T x, int p=-1)
Namespace for GeographicLib.