CoastalME (Coastal Modelling Environment)
Simulates the long-term behaviour of complex coastlines
Loading...
Searching...
No Matches
utils_global.cpp
Go to the documentation of this file.
1
12
13/* ==============================================================================================================================
14
15 This file is part of CoastalME, the Coastal Modelling Environment.
16
17 CoastalME is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23==============================================================================================================================*/
24#include <cmath>
25
26#include <cfloat>
27
28#include <cstdio>
29using std::sprintf;
30
31#include <sstream>
32using std::stringstream;
33using std::istringstream;
34
35#include <ios>
36using std::fixed;
37using std::right;
38
39#include <iomanip>
40using std::setw;
41
42#include "cme.h"
43
44//===============================================================================================================================
46//===============================================================================================================================
47double dRound(double const d)
48{
49 // Rounds positive or negative doubles correctly
50 return ((d < 0.0) ? ceil(d - 0.5) : floor(d + 0.5));
51}
52
53//===============================================================================================================================
55//===============================================================================================================================
56int nRound(double const d)
57{
58 // Rounds positive or negative doubles correctly
59 return static_cast<int>((d < 0.0) ? ceil(d - 0.5) : floor(d + 0.5));
60}
61
62// bool bIsWhole(double d)
63// {
64// // From http://answers.yahoo.com/question/index?qid=20110320132617AAMdb7u
65// return (static_cast<int>(d) == d);
66// }
67
68//===============================================================================================================================
70//===============================================================================================================================
71bool bIsStringValidDouble(string& str)
72{
73 istringstream iStr(str);
74 double dDummy;
75
76 if (! (iStr >> dDummy))
77 return false;
78
79 return true;
80}
81
82//===============================================================================================================================
84//===============================================================================================================================
85bool bIsStringValidInt(string& str)
86{
87 // Trim leading whitespace
88 size_t nPos = str.find_first_not_of(" \t");
89
90 if (nPos != string::npos)
91 str = str.substr(nPos);
92
93 // If the first character is the sign, remove it
94 if ((str[0] == '-') || (str[0] == '+'))
95 str.erase(0, 1);
96
97 // Now check that the string contains only numbers
98 return (str.find_first_not_of("0123456789") == string::npos);
99}
100
101//===============================================================================================================================
103//===============================================================================================================================
104ostream& operator<< (ostream& ostr, const FillToWidth &args)
105{
106 ostr.fill(args.chFill);
107 ostr.width(args.nWidth);
108
109 return ostr;
110}
111
112//===============================================================================================================================
114//===============================================================================================================================
115string strDbl(double const dX, int const nDigits)
116{
117 stringstream ss;
118 ss << fixed;
119 ss.precision(nDigits); // Set the number of places after decimal
120 ss << dX;
121 return ss.str();
122}
123
124//===============================================================================================================================
126//===============================================================================================================================
127string strDblRight(double const dX, int const nDigits, int const nWidth, bool const bShowDash)
128{
129 stringstream ss;
130 ss << fixed << right;
131 ss.fill(' ');
132 ss.width(nWidth - 1);
133
134 if (bFPIsEqual(dX, 0.0, TOLERANCE))
135 {
136 if (bShowDash)
137 ss << "-";
138
139 else
140 ss << SPACE;
141 }
142
143 else
144 {
145 ss.precision(nDigits); // Set number of places after decimal
146 ss << dX;
147 }
148
149 ss << " "; // Add a final space
150 return ss.str();
151}
152
153//===============================================================================================================================
155//===============================================================================================================================
156string strIntRight(int const nX, int const nWidth)
157{
158 stringstream ss;
159 ss << fixed << right;
160 ss.fill(' '); // Fill space around displayed number
161 ss.width(nWidth - 1); // Set width around displayed number
162 ss << nX;
163 ss << " "; // Add a final space
164 return ss.str();
165}
166
167//===============================================================================================================================
169//===============================================================================================================================
170string strCentre(const char* pchIn, int const nWidth)
171{
172 string strIn(pchIn);
173 stringstream ss, spaces;
174 int nPadding = nWidth - static_cast<int>(strIn.size());
175
176 for (int i = 0; i < nPadding / 2; ++i)
177 spaces << " ";
178
179 ss << spaces.str() << strIn << spaces.str();
180
181 if (nPadding > 0 && nPadding % 2 != 0) // If odd number, add one space
182 ss << " ";
183
184 return ss.str();
185}
186
187//===============================================================================================================================
189//===============================================================================================================================
190string strCentre(const string& strIn, int const nWidth)
191{
192 stringstream ss, spaces;
193 int nPadding = nWidth - static_cast<int>(strIn.size());
194
195 for (int i = 0; i < nPadding / 2; ++i)
196 spaces << " ";
197
198 ss << spaces.str() << strIn << spaces.str();
199
200 if (nPadding > 0 && nPadding % 2 != 0) // If odd number, add one space
201 ss << " ";
202
203 return ss.str();
204}
205
206//===============================================================================================================================
208//===============================================================================================================================
209string strRight(const string& strIn, int const nWidth)
210{
211 stringstream ss, spaces;
212 int nPadding = nWidth - static_cast<int>(strIn.size()) - 1;
213
214 for (int i = 0; i < nPadding; ++i)
215 spaces << " ";
216
217 ss << spaces.str() << strIn;
218 ss << " ";
219 return ss.str();
220}
221
222//===============================================================================================================================
224//===============================================================================================================================
225string strRight(const char* pchIn, int const nWidth)
226{
227 string strIn(pchIn);
228 stringstream ss, spaces;
229 int nPadding = nWidth - static_cast<int>(strIn.size()) - 1;
230
231 for (int i = 0; i < nPadding; ++i)
232 spaces << " ";
233
234 ss << spaces.str() << strIn;
235 ss << " ";
236 return ss.str();
237}
238
239//===============================================================================================================================
241//===============================================================================================================================
242string strLeft(const string& strIn, int const nWidth)
243{
244 stringstream ss, spaces;
245 int nPadding = nWidth - static_cast<int>(strIn.size());
246
247 for (int i = 0; i < nPadding; ++i)
248 spaces << " ";
249
250 ss << strIn << spaces.str();
251 return ss.str();
252}
253
254//===============================================================================================================================
256//===============================================================================================================================
257string strLeft(const char* pchIn, int const nWidth)
258{
259 string strIn(pchIn);
260 stringstream ss, spaces;
261 int nPadding = nWidth - static_cast<int>(strIn.size());
262
263 for (int i = 0; i < nPadding; ++i)
264 spaces << " ";
265
266 ss << strIn << spaces.str();
267 return ss.str();
268}
269
270//===============================================================================================================================
272//===============================================================================================================================
273string strRightPerCent(double const d1, double const d2, int const nWidth, int const nDigits, bool const bShowDash)
274{
275 stringstream ss;
276 ss << fixed << right;
277
278 // Are either of the inputs zero?
279 if ((bFPIsEqual(d1, 0.0, TOLERANCE)) || (bFPIsEqual(d2, 0.0, TOLERANCE)))
280 {
281 ss.fill(' ');
282 ss.width(nWidth - 1);
283
284 if (bShowDash)
285 ss << "-";
286
287 else
288 ss << SPACE;
289 }
290
291 else
292 {
293 // Non-zero, so calculate the percentage
294 double dResult = 100 * d1 / d2;
295
296 stringstream ssResult;
297 ssResult << fixed << right;
298 ssResult.precision(nDigits);
299 ssResult << "(" << dResult << "%)";
300
301 long int nResultWidth = ssResult.str().size();
302
303 for (int i = 0; i < (nWidth - nResultWidth - 1); i++)
304 ss << SPACE;
305
306 ss << ssResult.str();
307 }
308
309 // Append a final space
310 ss << " ";
311
312 return ss.str();
313}
This file contains global definitions for CoastalME.
double const TOLERANCE
Definition cme.h:811
bool bFPIsEqual(const T d1, const T d2, const T dEpsilon)
Definition cme.h:1284
char const SPACE
Definition cme.h:451
double dRound(double const d)
Correctly rounds doubles.
bool bIsStringValidInt(string &str)
Checks to see if a string can be read as a valid integer, from https://stackoverflow....
int nRound(double const d)
Version of the above that returns an int.
string strLeft(const string &strIn, int const nWidth)
Left-aligns string within a field of given width, pads with blank spaces to enforce alignment....
string strDblRight(double const dX, int const nDigits, int const nWidth, bool const bShowDash)
Converts double to string with specified number of decimal places, within a field of given width,...
string strRightPerCent(double const d1, double const d2, int const nWidth, int const nDigits, bool const bShowDash)
Calculates a percentage from two numbers then, if the result is non-zero, right-aligns the result as ...
string strDbl(double const dX, int const nDigits)
Converts double to string with specified number of places after the decimal. From https://stackoverfl...
string strCentre(const char *pchIn, int const nWidth)
Centre-aligns char array within a field of given width, pads with blank spaces to enforce alignment....
ostream & operator<<(ostream &ostr, const FillToWidth &args)
Operator that inserts a given fill character, to a given width, into an output stream....
string strRight(const string &strIn, int const nWidth)
Right-aligns string within a field of given width, pads with blank spaces to enforce alignment....
bool bIsStringValidDouble(string &str)
Checks to see if a string can be read as a valid double number. Does not find trailing (i....
string strIntRight(int const nX, int const nWidth)
Converts int to string within a field of given width, pads with blank spaces to enforce alignment....