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 <sstream>
23 #include <iomanip>
24 
25 #include "bTypesFunctions.hpp"
26 
27 
28 pFlow::int32 pFlow::countChar(const word& s, const char c)
29 {
30  return std::count(s.cbegin(), s.cend(), c);
31 }
32 
33 
34 pFlow::int32 pFlow::countChar( const char* s, const char c)
35 {
36  return
37  (
38  s == nullptr
39  ? 0
41  );
42 }
43 
44 pFlow::word pFlow::real2Fixed(const real & v, int32 numPrecision)
45 {
46  std::stringstream ss;
47 
48  ss << std::fixed << std::setprecision(numPrecision) << v;
49  return ss.str();
50 }
51 
52 pFlow::word pFlow::real2Word(const real & v, int32 numPrecision)
53 {
54  std::stringstream ss;
55  if( abs(v) < verySmallValue )
56  {
57  ss <<"0";
58  }
59  else
60  {
61  ss << std::setprecision(numPrecision) << v;
62  }
63 
64  return ss.str();
65 }
66 
68 {
69  std::stringstream ss;
70 
71  ss << v;
72  return ss.str();
73 }
74 
76 {
77  auto dec = str.find('.');
78  if(dec == word::npos) return str;
79 
80  auto len = str.size();
81  if(len == word::npos) return str;
82 
83  auto firstZero = word::npos;
84  for(auto n=len-1; n>dec;n--)
85  {
86  if( str[n] == '0' )
87  {
88  firstZero = n;
89  }
90  else
91  {
92  break;
93  }
94  }
95 
96  if(firstZero == dec+1) firstZero = dec;
97 
98  return str.substr(0,firstZero);
99 }
100 
102 {
103  word strVal = real2Fixed(v, numPrecision);
104  return removeDecimalZeros(strVal);
105 }
106 
108 {
109  word oStr(inStr);
110  transform(inStr.begin(), inStr.end(), oStr.begin(), ::toupper);
111  return oStr;
112 }
113 
114 bool pFlow::isYes(const word & str)
115 {
116  word s = toUpper(str);
117 
118  if( s == "YES" || s == "OK" || s == "TRUE" || s == "ON" || s=="T") return true;
119  return false;
120 }
121 
122 bool pFlow::isNo(const word & str)
123 {
124  word s = toUpper(str);
125 
126  if( s == "NO" || s == "N" || "FALSE" || s == "OFF" || s == "F") return true;
127  return false;
128 }
129 
130 
132 {
133  return w1+"<"+w2+">";
134 }
135 
137 (
138  const word& base,
139  const word& w1,
140  const word& w2
141 )
142 {
143  return base+"<"+w1+","+w2+">";
144 }
145 
146 pFlow::word pFlow::angleBracketsNames3(const word& base, const word& w1, const word& w2, const word& w3)
147 {
148  return base+"<"+w1+","+w2+","+w3+">";
149 }
150 
151 pFlow::word pFlow::groupNames(const word& bw, const word& tw, char sep)
152 {
153  return bw + sep + tw;
154 }
155 
156 pFlow::word pFlow::baseName(const word& w, char sep)
157 {
158  if( auto pos = w.find_last_of(sep); pos != word::npos)
159  {
160  return w.substr(0,pos);
161  }
162  else
163  {
164  return w;
165  }
166 }
167 
168 pFlow::word pFlow::tailName(const word& w, char sep)
169 {
170  if( auto pos = w.find_last_of(sep); pos != word::npos)
171  {
172  return w.substr(pos+1);
173  }
174  else
175  {
176  return nullWord;
177  }
178 }
179 
180 bool pFlow::validWord(char c)
181 {
182  return
183  (
184  !isspace(c)
185  && c != '"' // string quote
186  && c != '\'' // string quote
187  //&& c != '/' // path separator
188  && c != ';' // end statement
189  && c != '{' // beg subdict
190  && c != '}' // end subdict
191  );
192 }
193 
195 {
196  return
197  (
198  !isspace(c)
199  && c != ';' // end statement
200  && c != '{' // beg subdict
201  && c != '}' // end subdict
202  );
203 }
204 
205 bool pFlow::validWord(const word& w)
206 {
207  for(auto wi:w)
208  {
209  char c = wi;
210  if ( !validWord(c) ) return false;
211  }
212  return true;
213 }
214 
216 {
217  for(auto wi:w)
218  {
219  char c = wi;
220  if ( !validWordWithQuote(c) ) return false;
221  }
222  return true;
223 }
224 
225 
226 bool pFlow::readLabel( const word& w, label & val)
227 {
228  try{
229  val = std::stoull(w);
230  }
231  catch (...)
232  {
233  return false;
234  }
235  return true;
236 }
237 
238 bool pFlow::readLabel( const char* buf, label & val)
239 {
240  word w(buf);
241  return readLabel(w, val);
242 }
243 
244 bool pFlow::readUint32( const word& w, uint32 & val)
245 {
246  try{
247  val = std::stoul(w);
248  }
249  catch (...)
250  {
251  return false;
252  }
253  return true;
254 }
255 
256 bool pFlow::readUint32( const char* buf, uint32 & val)
257 {
258  word w(buf);
259  return readUint32(w, val);
260 }
261 
262 bool pFlow::readInt64( const word& w, int64 & val)
263 {
264  try{
265  val = std::stoll(w);
266  }
267  catch (...)
268  {
269  return false;
270  }
271  return true;
272 }
273 
274 bool pFlow::readInt64( const char* buf, int64 & val)
275 {
276  word w(buf);
277  return readInt64(w, val);
278 }
279 
280 bool pFlow::readInt32( const word& w, int32 & val)
281 {
282  try{
283  val = std::stoi(w);
284  }
285  catch (...)
286  {
287  return false;
288  }
289  return true;
290 }
291 
292 bool pFlow::readInt32( const char* buf, int32 & val)
293 {
294  word w(buf);
295  return readInt32(w, val);
296 }
297 
298 
299 bool pFlow::readInt16( const word& w, int16 & val)
300 {
301  try{
302  val = static_cast<int16>(std::stoi(w));
303  }
304  catch (...)
305  {
306  return false;
307  }
308  return true;
309 }
310 
311 bool pFlow::readInt16( const char* buf, int16 & val)
312 {
313  word w(buf);
314  return readInt16(w, val);
315 }
316 
317 bool pFlow::readInt8( const word& w, int8 & val)
318 {
319  try{
320  val = static_cast<int8>(std::stoi(w));
321  }
322  catch (...)
323  {
324  return false;
325  }
326  return true;
327 }
328 
329 bool pFlow::readInt8( const char* buf, int8 & val)
330 {
331  word w(buf);
332  return readInt8(w, val);
333 }
334 #include <iostream>
335 bool pFlow::readReal( const word& w, real & val)
336 {
337  try{
338  val = std::stod(w);
339 
340  }
341  catch (std:: out_of_range& e)
342  {
343  val = static_cast<real>( std::stold(w) );
344  }
345  catch (...){
346  return false;
347  }
348  return true;
349 }
350 
351 bool pFlow::readReal( const char* buf, real & val )
352 {
353  char* c;
354 
355  val = std::strtod(buf, &c);
356  if(val == HUGE_VAL)
357  {
358  val = static_cast<real>( std::strtold(buf, &c) );
359  if(val == HUGE_VAL || c==buf)
360  return false;
361  }
362  else if(c == buf)
363  {
364  return false;
365  }
366 
367 
368  return true;
369 }
370 
371 
372 bool pFlow::readBoolian_Str( const word& w, bool & val)
373 {
374  if( bool t = isYes(w); t )
375  {
376  val = true;
377  return true;
378  }
379  if( bool f = isNo(w); f )
380  {
381  val = false;
382  return true;
383  }
384  return false;
385 }
386 
387 bool pFlow::readBoolian_Str( const char* buf, bool & val)
388 {
389  word w(buf);
390  return readBoolian_Str(w, val);
391 }
pFlow::tailName
word tailName(const word &w, char sep='.')
Definition: bTypesFunctions.cpp:168
pFlow::angleBracketsNames3
word angleBracketsNames3(const word &base, const word &w1, const word &w2, const word &w3)
Definition: bTypesFunctions.cpp:146
pFlow::verySmallValue
const real verySmallValue
Definition: numericConstants.hpp:34
count
auto count(const Vector< T, Allocator > &vec, const T &val)
pFlow::fixed
IOstream & fixed(IOstream &io)
Definition: IOstream.hpp:293
pFlow::dec
IOstream & dec(IOstream &io)
Definition: IOstream.hpp:275
pFlow::real2Fixed
word real2Fixed(const real &v, int32 numPrecision=6)
Definition: bTypesFunctions.cpp:44
pFlow::readInt32
bool readInt32(const word &w, int32 &val)
Definition: bTypesFunctions.cpp:280
pFlow::real
float real
Definition: builtinTypes.hpp:46
pFlow::real2FixedStripZeros
word real2FixedStripZeros(const real &v, int32 numPrecision=6)
Definition: bTypesFunctions.cpp:101
pFlow::validWordWithQuote
bool validWordWithQuote(char c)
Definition: bTypesFunctions.cpp:194
pFlow::readUint32
bool readUint32(const word &w, uint32 &val)
Definition: bTypesFunctions.cpp:244
pFlow::readBoolian_Str
bool readBoolian_Str(const word &w, bool &val)
Definition: bTypesFunctions.cpp:372
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::readReal
bool readReal(const word &w, real &val)
Definition: bTypesFunctions.cpp:335
pFlow::toUpper
word toUpper(const word &inStr)
Definition: bTypesFunctions.cpp:107
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::validWord
bool validWord(char c)
Definition: bTypesFunctions.cpp:180
pFlow::readLabel
bool readLabel(const word &w, label &val)
Definition: bTypesFunctions.cpp:226
pFlow::nullWord
const word nullWord
Definition: bTypesFunctions.hpp:41
pFlow::readInt64
bool readInt64(const word &w, int64 &val)
Definition: bTypesFunctions.cpp:262
pFlow::readInt16
bool readInt16(const word &w, int16 &val)
Definition: bTypesFunctions.cpp:299
pFlow::baseName
word baseName(const word &w, char sep='.')
Definition: bTypesFunctions.cpp:156
pFlow::angleBracketsNames2
word angleBracketsNames2(const word &base, const word &w1, const word &w2)
Definition: bTypesFunctions.cpp:137
n
int32 n
Definition: NBSCrossLoop.hpp:24
pFlow::int16
short int int16
Definition: builtinTypes.hpp:51
length
INLINE_FUNCTION_HD T length(const triple< T > &v1)
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::angleBracketsNames
word angleBracketsNames(const word &w1, const word &w2)
Definition: bTypesFunctions.cpp:131
pFlow::abs
INLINE_FUNCTION_HD real abs(real x)
Definition: math.hpp:43
pFlow::isNo
bool isNo(const word &str)
Definition: bTypesFunctions.cpp:122
pFlow::removeDecimalZeros
word removeDecimalZeros(const word &str)
Definition: bTypesFunctions.cpp:75
pFlow::readInt8
bool readInt8(const word &w, int8 &val)
Definition: bTypesFunctions.cpp:317
pFlow::real2Word
word real2Word(const real &v, int32 numPrecision=6)
Definition: bTypesFunctions.cpp:52
pFlow::groupNames
word groupNames(const word &bw, const word &tw, char sep='.')
Definition: bTypesFunctions.cpp:151
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:49
pFlow::int322Word
word int322Word(const int32 &v)
Definition: bTypesFunctions.cpp:67
bTypesFunctions.hpp
pFlow::isYes
bool isYes(const word &str)
Definition: bTypesFunctions.cpp:114
pFlow::countChar
int32 countChar(const word &s, const char c)
Definition: bTypesFunctions.cpp:28