Microflow 3D  v1.0
MF::RW::CSVReader Class Reference

The object of CSVReader class parses text files with node coordinates for uid-threads definition. More...

#include <CSVReader.h>

Public Member Functions

 CSVReader ()=default
 
 ~CSVReader ()=default
 

Static Public Member Functions

static void DataReadFromCSVFile (const std::string &DataFilePath, const std::vector< std::string > &VariableNameList, std::vector< double > *pDataArray)
 Reads data from text file. More...
 

Detailed Description

The object of CSVReader class parses text files with node coordinates for uid-threads definition.

Definition at line 22 of file CSVReader.h.

Constructor & Destructor Documentation

◆ CSVReader()

MF::RW::CSVReader::CSVReader ( )
default

◆ ~CSVReader()

MF::RW::CSVReader::~CSVReader ( )
default

Member Function Documentation

◆ DataReadFromCSVFile()

void MF::RW::CSVReader::DataReadFromCSVFile ( const std::string &  DataFilePath,
const std::vector< std::string > &  VariableNameList,
std::vector< double > *  pDataArray 
)
static

Reads data from text file.

Definition at line 13 of file CSVReader.cpp.

Referenced by MF::Solver_CPU::ConsoleWriter::ExtendedInfo(), MF::GB::BoundaryFromThread::VDBBoundaryAdd(), MF::GB::FluidFromThread::VDBFluidAdd(), and MF::GB::SolidFromThread::VDBSolidAdd().

13  {
14 
15  std::string buffer = "";
16  std::string substring = "";
17  std::size_t pos1 = 0;
18  std::size_t pos2 = 0;
19  std::size_t pos3 = 0;
20  unsigned int NodeNumber = 0;
21  unsigned int ColumnNr = 0;
22  unsigned int ColumnNumber[VariableNameList.size()];
23  double DoubleDataTable[VariableNameList.size()];
24 
25  // Initialization
26  for (unsigned long int i = 0; i < VariableNameList.size(); i++) {
27  ColumnNumber[i] = -1;
28  DoubleDataTable[i] = 0;
29  }
30 
31  // File open
32  std::ifstream FileInput(DataFilePath);
33  if (!FileInput.good()) {
34  std::cout << "Data file not found: " << DataFilePath << std::endl;
35  exit(EXIT_FAILURE);
36  }
37 
38  // Header processing
39  // -------------------------------------------------------------------
40 
41  // Column numbers distinction
42  std::getline(FileInput, buffer); //One line reading
43  for (unsigned long int i = 0; i < VariableNameList.size(); i++) {
44  pos1 = 0;
45  pos2 = 0;
46  pos3 = 0;
47  substring = "";
48  while (substring != VariableNameList.at(i)) {
49  pos2 = buffer.find(",", pos1);
50  if (pos2 != std::string::npos)
51  substring = buffer.substr(pos1, pos2 - pos1);
52  else
53  substring = buffer.substr(pos1);
54  pos3 = 0;
55  while (pos3 != std::string::npos) // quotation marks removal
56  {
57  pos3 = substring.find("\"");
58  if (pos3 != std::string::npos)
59  substring = substring.erase(pos3, 1);
60  }
61  if ((substring != VariableNameList.at(i)) & (pos2 == std::string::npos)) // If file end and data name not found
62  {
63  std::cout << "Column name " << VariableNameList.at(i) << " not found in the file: " << DataFilePath << std::endl;
64  exit(EXIT_FAILURE);
65  }
66 
67  pos1 = pos2 + 1;
68  ColumnNumber[i]++;
69  }
70  }
71 
72  // Data processing
73  //-----------------------------------------------------
74 
75  while (!FileInput.eof()) {
76  std::getline(FileInput, buffer); //One line reading
77  pos1 = buffer.find_first_of("-0123456789"); //Check if the line starts from number
78  if (pos1 == 0) {
79  pos1 = 0;
80  pos2 = 0;
81  ColumnNr = 0;
82  while (pos2 != std::string::npos) {
83  pos2 = buffer.find(",", pos1);
84  if (pos2 != std::string::npos)
85  substring = buffer.substr(pos1, pos2 - pos1);
86  else
87  substring = buffer.substr(pos1);
88  for (unsigned long int i = 0; i < VariableNameList.size(); i++)
89  if (ColumnNumber[i] == ColumnNr)
90  DoubleDataTable[i] = boost::lexical_cast<double>(substring); // string to number
91  ColumnNr++;
92  pos1 = pos2 + 1;
93  }
94  for (unsigned long int i = 0; i < VariableNameList.size(); i++)
95  pDataArray->push_back(DoubleDataTable[i]);
96  NodeNumber++;
97  }
98  }
99 
100  std::cout << NodeNumber << " data values are read from the file: " << DataFilePath << std::endl;
101  FileInput.close();
102 }

The documentation for this class was generated from the following files: