www.cemf.ir
bTypesFunctions.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #include <algorithm>
22 #include <iomanip>
23 #include <sstream>
24 
25 #include "bTypesFunctions.hpp"
26 
28 pFlow::countChar(const word& s, const char c)
29 {
30  return std::count(s.cbegin(), s.cend(), c);
31 }
32 
34 pFlow::countChar(const char* s, const char c)
35 {
36  return (
37  s == nullptr ? 0
39  );
40 }
41 
43 pFlow::toUpper(const word& inStr)
44 {
45  word oStr(inStr);
46  std::transform(inStr.begin(), inStr.end(), oStr.begin(), ::toupper);
47  return oStr;
48 }
49 
52 {
53  word oStr(inStr);
54  oStr[0] = std::toupper(oStr[0]);
55  return oStr;
56 }
57 
58 bool
59 pFlow::isYes(const word& str)
60 {
61  word s = toUpper(str);
62 
63  if (s == "YES" ||
64  s == "Y" ||
65  s == "OK" ||
66  s == "TRUE" ||
67  s == "ON" ||
68  s == "T")
69  return true;
70  return false;
71 }
72 
73 bool
74 pFlow::isNo(const word& str)
75 {
76  word s = toUpper(str);
77 
78  if (s == "NO" || s == "N" || "FALSE" || s == "OFF" || s == "F")
79  return true;
80  else
81  return false;
82 }
83 
85 pFlow::real2Fixed(const real& v, int32 numPrecision)
86 {
87  std::stringstream ss;
88 
89  ss << std::fixed << std::setprecision(numPrecision) << v;
90  return ss.str();
91 }
92 
94 pFlow::real2Word(const real& v, int32 numPrecision)
95 {
96  std::stringstream ss;
97  if (abs(v) < verySmallValue)
98  {
99  ss << "0";
100  }
101  else
102  {
103  ss << std::setprecision(numPrecision) << v;
104  }
105 
106  return ss.str();
107 }
108 
111 {
112  std::stringstream ss;
113 
114  ss << v;
115  return ss.str();
116 }
117 
120 {
121  auto dec = str.find('.');
122  if (dec == word::npos)
123  return str;
124 
125  auto len = str.size();
126  if (len == word::npos)
127  return str;
128 
129  auto firstZero = word::npos;
130  for (auto n = len - 1; n > dec; n--)
131  {
132  if (str[n] == '0')
133  {
134  firstZero = n;
135  }
136  else
137  {
138  break;
139  }
140  }
141 
142  if (firstZero == dec + 1)
143  firstZero = dec;
144 
145  return str.substr(0, firstZero);
146 }
147 
149 pFlow::real2FixedStripZeros(const real& v, int32 numPrecision)
150 {
151  word strVal = real2Fixed(v, numPrecision);
152  return removeDecimalZeros(strVal);
153 }
154 
156 pFlow::angleBracketsNames(const word& w1, const word& w2)
157 {
158  return w1 + "<" + w2 + ">";
159 }
160 
162 pFlow::angleBracketsNames2(const word& base, const word& w1, const word& w2)
163 {
164  return base + "<" + w1 + "," + w2 + ">";
165 }
166 
169  const word& base,
170  const word& w1,
171  const word& w2,
172  const word& w3
173 )
174 {
175  return base + "<" + w1 + "," + w2 + "," + w3 + ">";
176 }
177 
179 pFlow::groupNames(const word& bw, const word& tw, char sep)
180 {
181  return bw + sep + tw;
182 }
183 
185 pFlow::baseName(const word& w, char sep)
186 {
187  if (auto pos = w.find_last_of(sep); pos != word::npos)
188  {
189  return w.substr(0, pos);
190  }
191  else
192  {
193  return w;
194  }
195 }
196 
198 pFlow::tailName(const word& w, char sep)
199 {
200  if (auto pos = w.find_last_of(sep); pos != word::npos)
201  {
202  return w.substr(pos + 1);
203  }
204  else
205  {
206  return nullWord;
207  }
208 }
209 
210 bool
212 {
213  return (
214  !isspace(c) && c != '"' // string quote
215  && c != '\'' // string quote
216  //&& c != '/' // path separator
217  && c != ';' // end statement
218  && c != '{' // beg subdict
219  && c != '}' // end subdict
220  );
221 }
222 
223 bool
225 {
226  return (
227  !isspace(c) && c != ';' // end statement
228  && c != '{' // beg subdict
229  && c != '}' // end subdict
230  );
231 }
232 
233 bool
235 {
236  for (auto wi : w)
237  {
238  char c = wi;
239  if (!validWord(c))
240  return false;
241  }
242  return true;
243 }
244 
245 bool
247 {
248  for (auto wi : w)
249  {
250  char c = wi;
251  if (!validWordWithQuote(c))
252  return false;
253  }
254  return true;
255 }
256 
257 bool
259 {
260  try
261  {
262  val = std::stoul(w);
263  }
264  catch (...)
265  {
266  return false;
267  }
268  return true;
269 }
270 
271 bool
272 pFlow::readUint32(const char* buf, uint32& val)
273 {
274  word w(buf);
275  return readUint32(w, val);
276 }
277 
278 bool
279 pFlow::readInt64(const word& w, int64& val)
280 {
281  try
282  {
283  val = std::stoll(w);
284  }
285  catch (...)
286  {
287  return false;
288  }
289  return true;
290 }
291 
292 bool
293 pFlow::readInt64(const char* buf, int64& val)
294 {
295  word w(buf);
296  return readInt64(w, val);
297 }
298 
299 bool
300 pFlow::readInt32(const word& w, int32& val)
301 {
302  try
303  {
304  val = std::stoi(w);
305  }
306  catch (...)
307  {
308  return false;
309  }
310  return true;
311 }
312 
313 bool
314 pFlow::readInt32(const char* buf, int32& val)
315 {
316  word w(buf);
317  return readInt32(w, val);
318 }
319 
320 bool
321 pFlow::readInt8(const word& w, int8& val)
322 {
323  try
324  {
325  val = std::stoi(w);
326  }
327  catch (...)
328  {
329  return false;
330  }
331  return true;
332 }
333 
334 bool
335 pFlow::readInt8(const char* buf, int8& val)
336 {
337  word w(buf);
338  return readInt8(w, val);
339 }
340 
341 // #include <iostream>
342 bool
343 pFlow::readReal(const word& w, real& val)
344 {
345  try
346  {
347  val = std::stod(w);
348  }
349  catch (std::out_of_range& e)
350  {
351  val = static_cast<real>(std::stold(w));
352  }
353  catch (...)
354  {
355  return false;
356  }
357  return true;
358 }
359 
360 bool
361 pFlow::readReal(const char* buf, real& val)
362 {
363  char* c;
364 
365  val = std::strtod(buf, &c);
366  if (val == HUGE_VAL)
367  {
368  val = static_cast<real>(std::strtold(buf, &c));
369  if (val == HUGE_VAL || c == buf)
370  return false;
371  }
372  else if (c == buf)
373  {
374  return false;
375  }
376 
377  return true;
378 }
379 
380 bool
381 pFlow::readBoolian_Str(const word& w, bool& val)
382 {
383  if (bool t = isYes(w); t)
384  {
385  val = true;
386  return true;
387  }
388  if (bool f = isNo(w); f)
389  {
390  val = false;
391  return true;
392  }
393  return false;
394 }
395 
396 bool
397 pFlow::readBoolian_Str(const char* buf, bool& val)
398 {
399  word w(buf);
400  return readBoolian_Str(w, val);
401 }
pFlow::tailName
word tailName(const word &w, char sep='.')
Find tail name in a group separated by "." and return it.
Definition: bTypesFunctions.cpp:198
pFlow::angleBracketsNames3
word angleBracketsNames3(const word &base, const word &w1, const word &w2, const word &w3)
Output base<w1,sw2,w3>
Definition: bTypesFunctions.cpp:168
pFlow::verySmallValue
const real verySmallValue
Definition: numericConstants.hpp:32
count
auto count(const Vector< T, Allocator > &vec, const T &val)
pFlow::fixed
IOstream & fixed(IOstream &io)
Definition: IOstream.hpp:329
pFlow::dec
IOstream & dec(IOstream &io)
Definition: IOstream.hpp:311
pFlow::real2Fixed
word real2Fixed(const real &v, int32 numPrecision=6)
Convert floating point variable to string with fixed number of precisions.
Definition: bTypesFunctions.cpp:85
pFlow::readInt32
bool readInt32(const word &w, int32 &val)
Convert word to int32.
Definition: bTypesFunctions.cpp:300
pFlow::real
float real
Definition: builtinTypes.hpp:45
pFlow::real2FixedStripZeros
word real2FixedStripZeros(const real &v, int32 numPrecision=6)
Convert to fixed point variable and remove zeros.
Definition: bTypesFunctions.cpp:149
pFlow::validWordWithQuote
bool validWordWithQuote(char c)
Is c a valid character including quote?
Definition: bTypesFunctions.cpp:224
pFlow::readUint32
bool readUint32(const word &w, uint32 &val)
Convert word to uint32.
Definition: bTypesFunctions.cpp:258
pFlow::readBoolian_Str
bool readBoolian_Str(const word &w, bool &val)
Convert word to bool.
Definition: bTypesFunctions.cpp:381
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::readReal
bool readReal(const word &w, real &val)
Convert word to real.
Definition: bTypesFunctions.cpp:343
pFlow::toUpper
word toUpper(const word &inStr)
convert a word to all caps
Definition: bTypesFunctions.cpp:43
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:52
pFlow::validWord
bool validWord(char c)
Is the character valid for a word name?
Definition: bTypesFunctions.cpp:211
pFlow::nullWord
const word nullWord
null/empty word
Definition: bTypesFunctions.hpp:49
pFlow::readInt64
bool readInt64(const word &w, int64 &val)
Convert word to int64.
Definition: bTypesFunctions.cpp:279
pFlow::baseName
word baseName(const word &w, char sep='.')
Find the base in a group separated by "." and return it.
Definition: bTypesFunctions.cpp:185
pFlow::angleBracketsNames2
word angleBracketsNames2(const word &base, const word &w1, const word &w2)
Output base<w1,w2>
Definition: bTypesFunctions.cpp:162
length
INLINE_FUNCTION_HD T length(const triple< T > &v1)
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::angleBracketsNames
word angleBracketsNames(const word &w1, const word &w2)
Output <w1,w2>
Definition: bTypesFunctions.cpp:156
pFlow::isNo
bool isNo(const word &str)
Check if str equals "No", "N", "False", or "Off".
Definition: bTypesFunctions.cpp:74
pFlow::abs
Vector< T, Allocator > abs(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:84
pFlow::removeDecimalZeros
word removeDecimalZeros(const word &str)
Remove zeros from decimal part of a string number.
Definition: bTypesFunctions.cpp:119
pFlow::readInt8
bool readInt8(const word &w, int8 &val)
Convert word to int8.
Definition: bTypesFunctions.cpp:321
n
uint32 n
Definition: NBSLoop.hpp:24
pFlow::real2Word
word real2Word(const real &v, int32 numPrecision=6)
Convert floating point variable to string with general format.
Definition: bTypesFunctions.cpp:94
pFlow::groupNames
word groupNames(const word &bw, const word &tw, char sep='.')
Group words and output bw.tw.
Definition: bTypesFunctions.cpp:179
pFlow::firstCapital
word firstCapital(const word &inStr)
Definition: bTypesFunctions.cpp:51
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:48
pFlow::int322Word
word int322Word(const int32 &v)
Convert int32 to word.
Definition: bTypesFunctions.cpp:110
bTypesFunctions.hpp
pFlow::isYes
bool isYes(const word &str)
Check if str equals "Yes", "Y", "True", "Ok", "ON", or "T".
Definition: bTypesFunctions.cpp:59
pFlow::countChar
int32 countChar(const word &s, const char c)
Count numer of chars c in a word.
Definition: bTypesFunctions.cpp:28