Microflow 3D  v1.0
CSVReader.cpp
Go to the documentation of this file.
1 // ==============================================================================================
2 // Microflow 3D, http://www.microflow.pwr.edu.pl/
3 // Created by Roman Szafran on 17.05.19.
4 // Copyright (c) 2019 Wroclaw University of Science and Technology.
5 // Distributed under the Apache License, Version 2.0. You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0 or see accompanying file license.txt.
7 // Redistributions of source code must retain the above copyright and license notice.
8 // ==============================================================================================
9 
10 #include "CSVReader.h"
11 
12 
13 void MF::RW::CSVReader::DataReadFromCSVFile(const std::string & DataFilePath, const std::vector <std::string> & VariableNameList, std::vector<double>* pDataArray) {
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 }
103 
static void DataReadFromCSVFile(const std::string &DataFilePath, const std::vector< std::string > &VariableNameList, std::vector< double > *pDataArray)
Reads data from text file.
Definition: CSVReader.cpp:13