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