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
15This file is part of CoastalME, the Coastal Modelling Environment.
16
17CoastalME 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
19This 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
21You 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 if (nPos != string::npos)
90 str = str.substr(nPos);
91
92 // If the first character is the sign, remove it
93 if ((str[0] == '-') || (str[0] == '+'))
94 str.erase(0, 1);
95
96 // Now check that the string contains only numbers
97 return (str.find_first_not_of("0123456789") == string::npos);
98}
99
100//===============================================================================================================================
102//===============================================================================================================================
103ostream& operator<< (ostream& ostr, const FillToWidth& args)
104{
105 ostr.fill(args.chFill);
106 ostr.width(args.nWidth);
107
108 return ostr;
109}
110
111//===============================================================================================================================
113//===============================================================================================================================
114string strDbl(double const dX, int const nDigits)
115{
116 stringstream ss;
117 ss << fixed;
118 ss.precision(nDigits); // Set the number of places after decimal
119 ss << dX;
120 return ss.str();
121}
122
123//===============================================================================================================================
125//===============================================================================================================================
126string strDblRight(double const dX, int const nDigits, int const nWidth, bool const bShowDash)
127{
128 stringstream ss;
129 ss << fixed << right;
130 ss.fill(' ');
131 ss.width(nWidth-1);
132
133 if (bFPIsEqual(dX, 0.0, TOLERANCE))
134 {
135 if (bShowDash)
136 ss << "-";
137 else
138 ss << SPACE;
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 strIn(pchIn);
170 stringstream ss, spaces;
171 int 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 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 nPadding = nWidth - static_cast<int>(strIn.size()) - 1;
210 for (int i = 0; i < nPadding; ++i)
211 spaces << " ";
212 ss << spaces.str() << strIn;
213 ss << " ";
214 return ss.str();
215}
216
217//===============================================================================================================================
219//===============================================================================================================================
220string strRight(const char* pchIn, int const nWidth)
221{
222 string strIn(pchIn);
223 stringstream ss, spaces;
224 int nPadding = nWidth - static_cast<int>(strIn.size()) - 1;
225 for (int i = 0; i < nPadding; ++i)
226 spaces << " ";
227 ss << spaces.str() << strIn;
228 ss << " ";
229 return ss.str();
230}
231
232//===============================================================================================================================
234//===============================================================================================================================
235string strLeft(const string& strIn, int const nWidth)
236{
237 stringstream ss, spaces;
238 int nPadding = nWidth - static_cast<int>(strIn.size());
239 for (int i = 0; i < nPadding; ++i)
240 spaces << " ";
241 ss << strIn << spaces.str();
242 return ss.str();
243}
244
245//===============================================================================================================================
247//===============================================================================================================================
248string strLeft(const char* pchIn, int const nWidth)
249{
250 string strIn(pchIn);
251 stringstream ss, spaces;
252 int nPadding = nWidth - static_cast<int>(strIn.size());
253 for (int i = 0; i < nPadding; ++i)
254 spaces << " ";
255 ss << strIn << spaces.str();
256 return ss.str();
257}
258
259//===============================================================================================================================
261//===============================================================================================================================
262string strRightPerCent(double const d1, double const d2, int const nWidth, int const nDigits, bool const bShowDash)
263{
264 stringstream ss;
265 ss << fixed << right;
266
267 // Are either of the inputs zero?
268 if ((bFPIsEqual(d1, 0.0, TOLERANCE)) || (bFPIsEqual(d2, 0.0, TOLERANCE)))
269 {
270 ss.fill(' ');
271 ss.width(nWidth-1);
272
273 if (bShowDash)
274 ss << "-";
275 else
276 ss << SPACE;
277 }
278 else
279 {
280 // Non-zero, so calculate the percentage
281 double dResult = 100 * d1 / d2;
282
283 stringstream ssResult;
284 ssResult << fixed << right;
285 ssResult.precision(nDigits);
286 ssResult << "(" << dResult << "%)";
287
288 long int nResultWidth = ssResult.str().size();
289
290 for (int i = 0; i < (nWidth - nResultWidth - 1); i++)
291 ss << SPACE;
292
293 ss << ssResult.str();
294 }
295
296 // Append a final space
297 ss << " ";
298
299 return ss.str();
300}
This file contains global definitions for CoastalME.
double const TOLERANCE
Definition cme.h:698
bool bFPIsEqual(const T d1, const T d2, const T dEpsilon)
Definition cme.h:1176
char const SPACE
Definition cme.h:341
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....