OpenCAEPoro  0.2.0 Sep/22/2022
A simulator for multicomponent porous media flow
Macros | Functions
UtilInput.hpp File Reference

Supply basic tools used to input files. More...

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "OCPConst.hpp"

Go to the source code of this file.

Macros

#define ParamCheck1(exp)
 TODO: Replace it with error log. More...
 

Functions

constexpr long long Map_Str2Int (const char *mystr, const USI &len)
 
bool ReadLine (ifstream &ifs, vector< string > &result)
 
void DealDefault (vector< string > &result)
 
template<typename T >
void DealData (const vector< string > &vbuf, vector< OCP_USI > &obj, vector< T > &region)
 

Detailed Description

Supply basic tools used to input files.

Author
Shizhe Li
Date
Oct/01/2021

Copyright (C) 2021–present by the OpenCAEPoro team. All rights reserved.

Released under the terms of the GNU Lesser General Public License 3.0 or later.

Definition in file UtilInput.hpp.

Macro Definition Documentation

◆ ParamCheck1

#define ParamCheck1 (   exp)
Value:
std::cout << exp << " in " << __func__ << "() in " << __LINE__ << " in " \
<< __FILE__;

TODO: Replace it with error log.

Definition at line 28 of file UtilInput.hpp.

Function Documentation

◆ DealData()

template<typename T >
void DealData ( const vector< string > &  vbuf,
vector< OCP_USI > &  obj,
vector< T > &  region 
)

DealData change a series of product of integers into two arrays. For example, 8*1 16*2 8*3 16*4 -> obj <8, 16, 8, 16> & val <1, 2, 3, 4>.

Definition at line 59 of file UtilInput.hpp.

60 {
61  obj.resize(0);
62  region.resize(0);
63  for (auto& str : vbuf) {
64  auto pos = str.find('*');
65 
66  if (pos != string::npos) {
67  USI len = str.size();
68  OCP_USI num = stoi(str.substr(0, pos));
69  USI val = stoi(str.substr(pos + 1, len - (pos + 1)));
70  obj.push_back(num);
71  region.push_back(val);
72  }
73  }
74 }
unsigned int USI
Generic unsigned integer.
Definition: OCPConst.hpp:22
unsigned int OCP_USI
Long unsigned integer.
Definition: OCPConst.hpp:24

◆ DealDefault()

void DealDefault ( vector< string > &  result)

DealDefault is used to deal with the expression with asterisk, for example m*n -> <n,...,n> size m , m* -> <DEFAULT,..., DEFAULT> size m.

Definition at line 50 of file UtilInput.cpp.

51 {
52  vector<string> tmp;
53  for (auto& str : result) {
54  auto pos = str.find('*');
55  if (pos == string::npos) {
56  tmp.push_back(str);
57  } else {
58  USI num = atoi(str.substr(0, pos).c_str()); // non number -> 0
59  USI len = str.size();
60  string val = "DEFAULT";
61  if (num == 0) {
62  tmp.push_back(str);
63  } else {
64  if (pos != len - 1) {
65  val = str.substr(pos + 1, len - (pos + 1));
66  }
67  for (USI i = 0; i < num; i++) tmp.push_back(val);
68  }
69  }
70  }
71  swap(result, tmp);
72 }

◆ Map_Str2Int()

constexpr long long Map_Str2Int ( const char *  mystr,
const USI len 
)
inlineconstexpr

Map_str2int is used to map string to integer, which used to match the keyword efficiently in input file in the switch structure.

Definition at line 34 of file UtilInput.hpp.

35 {
36  long long res = 0;
37  long long t = 100;
38  for (USI i = 0; i < len; i++) {
39  res += (int)mystr[len - 1 - i] * t;
40  t *= 100;
41  }
42  return res;
43 }

◆ ReadLine()

bool ReadLine ( ifstream &  ifs,
vector< string > &  result 
)

ReadLine is the core function while inputting the file. It will capture the next line which is meanningful, for example, not blank line or comments, and then gets rid of some useless characters such as space, commas. Finally, the segments of rest string will be stored in result. And if return false, it indicates we have reach the end of file.

Definition at line 14 of file UtilInput.cpp.

15 {
16  result.resize(0);
17  string buf;
18 
19  while (!ifs.eof()) {
20  getline(ifs, buf);
21  if (buf.empty()) continue;
22  while (buf[0] == ' ' || buf[0] == '\t' || buf[0] == '\r') buf.erase(0, 1);
23  if (buf.empty() || buf[0] == '#') continue;
24  if (buf.size() > 1 && (buf[0] == '-' && buf[1] == '-')) continue;
25 
26  break;
27  }
28 
29  // file ends
30  if (buf.empty()) return false;
31 
32  // remove the string behind the '/'
33  auto pos = buf.find_first_of('/');
34  if (pos != string::npos) {
35  buf.erase(pos);
36  buf.push_back('/');
37  }
38 
39  // get rid of ' and ,
40  for (auto& s : buf) {
41  if (s == '\'' || s == ',') s = ' ';
42  }
43 
44  istringstream tmp(buf);
45  while (tmp >> buf) result.push_back(buf);
46 
47  return true;
48 }