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::size_t;
30
31#include <sstream>
32using std::stringstream;
33using std::istringstream;
34
35#include <ios>
36using std::fixed;
37using std::right;
38
39#include "cme.h"
40
41//===============================================================================================================================
43//===============================================================================================================================
44double dRound(double const d)
45{
46 // Rounds positive or negative doubles correctly
47 return ((d < 0.0) ? ceil(d - 0.5) : floor(d + 0.5));
48}
49
50//===============================================================================================================================
52//===============================================================================================================================
53int nRound(double const d)
54{
55 // Rounds positive or negative doubles correctly
56 return static_cast<int>((d < 0.0) ? ceil(d - 0.5) : floor(d + 0.5));
57}
58
59// bool bIsWhole(double d)
60// {
61// // From http://answers.yahoo.com/question/index?qid=20110320132617AAMdb7u
62// return (static_cast<int>(d) == d);
63// }
64
65//===============================================================================================================================
67//===============================================================================================================================
68bool bIsStringValidDouble(string& str)
69{
70 istringstream iStr(str);
71 double dDummy;
72
73 if (!(iStr >> dDummy))
74 return false;
75
76 return true;
77}
78
79//===============================================================================================================================
81//===============================================================================================================================
82bool bIsStringValidInt(string& str)
83{
84 // Trim leading whitespace
85 size_t const nPos = str.find_first_not_of(" \t");
86
87 if (nPos != string::npos)
88 str = str.substr(nPos);
89
90 // If the first character is the sign, remove it
91 if ((str[0] == '-') || (str[0] == '+'))
92 str.erase(0, 1);
93
94 // Now check that the string contains only numbers
95 return (str.find_first_not_of("0123456789") == string::npos);
96}
97
98//===============================================================================================================================
100//===============================================================================================================================
101ostream& operator<<(ostream& ostr, const FillToWidth& args)
102{
103 ostr.fill(args.chFill);
104 ostr.width(args.nWidth);
105
106 return ostr;
107}
108
109//===============================================================================================================================
111//===============================================================================================================================
112string strDbl(double const dX, int const nDigits)
113{
114 stringstream ss;
115 ss << fixed;
116 ss.precision(nDigits); // Set the number of places after decimal
117 ss << dX;
118 return ss.str();
119}
120
121//===============================================================================================================================
123//===============================================================================================================================
124string strDblRight(double const dX, int const nDigits, int const nWidth, bool const bShowDash)
125{
126 stringstream ss;
127 ss << fixed << right;
128 ss.fill(' ');
129 ss.width(nWidth - 1);
130
131 if (bFPIsEqual(dX, 0.0, TOLERANCE))
132 {
133 if (bShowDash)
134 ss << "-";
135
136 else
137 ss << SPACE;
138 }
139
140 else
141 {
142 ss.precision(nDigits); // Set number of places after decimal
143 ss << dX;
144 }
145
146 ss << " "; // Add a final space
147 return ss.str();
148}
149
150//===============================================================================================================================
152//===============================================================================================================================
153string strIntRight(int const nX, int const nWidth)
154{
155 stringstream ss;
156 ss << fixed << right;
157 ss.fill(' '); // Fill space around displayed number
158 ss.width(nWidth - 1); // Set width around displayed number
159 ss << nX;
160 ss << " "; // Add a final space
161 return ss.str();
162}
163
164//===============================================================================================================================
166//===============================================================================================================================
167string strCentre(const char* pchIn, int const nWidth)
168{
169 string const strIn(pchIn);
170 stringstream ss, spaces;
171 int const nPadding = nWidth - static_cast<int>(strIn.size());
172
173 for (int i = 0; i < nPadding / 2; ++i)
174 spaces << " ";
175
176 ss << spaces.str() << strIn << spaces.str();
177
178 if (nPadding > 0 && nPadding % 2 != 0) // If odd number, add one space
179 ss << " ";
180
181 return ss.str();
182}
183
184//===============================================================================================================================
186//===============================================================================================================================
187string strCentre(const string& strIn, int const nWidth)
188{
189 stringstream ss, spaces;
190 int const nPadding = nWidth - static_cast<int>(strIn.size());
191
192 for (int i = 0; i < nPadding / 2; ++i)
193 spaces << " ";
194
195 ss << spaces.str() << strIn << spaces.str();
196
197 if (nPadding > 0 && nPadding % 2 != 0) // If odd number, add one space
198 ss << " ";
199
200 return ss.str();
201}
202
203//===============================================================================================================================
205//===============================================================================================================================
206string strRight(const string& strIn, int const nWidth)
207{
208 stringstream ss, spaces;
209 int const nPadding = nWidth - static_cast<int>(strIn.size()) - 1;
210
211 for (int i = 0; i < nPadding; ++i)
212 spaces << " ";
213
214 ss << spaces.str() << strIn;
215 ss << " ";
216 return ss.str();
217}
218
219//===============================================================================================================================
221//===============================================================================================================================
222string strRight(const char* pchIn, int const nWidth)
223{
224 string const strIn(pchIn);
225 stringstream ss, spaces;
226 int const nPadding = nWidth - static_cast<int>(strIn.size()) - 1;
227
228 for (int i = 0; i < nPadding; ++i)
229 spaces << " ";
230
231 ss << spaces.str() << strIn;
232 ss << " ";
233 return ss.str();
234}
235
236//===============================================================================================================================
238//===============================================================================================================================
239string strLeft(const string& strIn, int const nWidth)
240{
241 stringstream ss, spaces;
242 int const nPadding = nWidth - static_cast<int>(strIn.size());
243
244 for (int i = 0; i < nPadding; ++i)
245 spaces << " ";
246
247 ss << strIn << spaces.str();
248 return ss.str();
249}
250
251//===============================================================================================================================
253//===============================================================================================================================
254string strLeft(const char* pchIn, int const nWidth)
255{
256 string const strIn(pchIn);
257 stringstream ss, spaces;
258 int const nPadding = nWidth - static_cast<int>(strIn.size());
259
260 for (int i = 0; i < nPadding; ++i)
261 spaces << " ";
262
263 ss << strIn << spaces.str();
264 return ss.str();
265}
266
267//===============================================================================================================================
269//===============================================================================================================================
270string strRightPerCent(double const d1, double const d2, int const nWidth, int const nDigits, bool const bShowDash)
271{
272 stringstream ss;
273 ss << fixed << right;
274
275 // Are either of the inputs zero?
276 if ((bFPIsEqual(d1, 0.0, TOLERANCE)) || (bFPIsEqual(d2, 0.0, TOLERANCE)))
277 {
278 ss.fill(' ');
279 ss.width(nWidth - 1);
280
281 if (bShowDash)
282 ss << "-";
283
284 else
285 ss << SPACE;
286 }
287
288 else
289 {
290 // Non-zero, so calculate the percentage
291 double const dResult = 100 * d1 / d2;
292
293 stringstream ssResult;
294 ssResult << fixed << right;
295 ssResult.precision(nDigits);
296 ssResult << "(" << dResult << "%)";
297
298 long int const nResultWidth = ssResult.str().size();
299
300 for (int i = 0; i < (nWidth - nResultWidth - 1); i++)
301 ss << SPACE;
302
303 ss << ssResult.str();
304 }
305
306 // Append a final space
307 ss << " ";
308
309 return ss.str();
310}
This file contains global definitions for CoastalME.
double const TOLERANCE
Definition cme.h:823
bool bFPIsEqual(const T d1, const T d2, const T dEpsilon)
Definition cme.h:1303
char const SPACE
Definition cme.h:453
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....