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

Utilities for reading input file. More...

#include "UtilInput.hpp"

Go to the source code of this file.

Functions

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

Detailed Description

Utilities for reading input file.

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.cpp.

Function Documentation

◆ 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 }
unsigned int USI
Generic unsigned integer.
Definition: OCPConst.hpp:22

◆ 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 }