MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
csvReader.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #include "csvReader.h"
27 
32 = default;
33 
40 void csvReader::read(const std::string& filename)
41 {
42  // strings to catch each line of a file and separate it into fields
43  std::string line, field;
44  // array of values for one line only
45  std::vector<std::string> v;
46  // Create ifstream object to open the file
47  std::ifstream file;
48  file.open(filename);
49  if (file.is_open())
50  {
51  // get first three bytes of the ifstream object (of your file)
52  int ch1 = file.get();
53  int ch2 = file.get();
54  int ch3 = file.get();
55  // if TRUE, the file contains UTF-8 BOM (problem usually encountered on Windows OS). A BOM is a mark which
56  // distinguishes different file encodings from each other. They usually start with a combination of different
57  // bytes (for UTF-8-BOM it is \uFEFF, but asking for it does not work, so it is seperated into three different
58  // bytes). Another workaround is changing the encoding to UTF-8 without BOM which some readers are capable of.
59  if (ch1 == 0xEF && ch2 == 0xBB && ch3 == 0xBF)
60  {
61  // skip first three bytes if BOM exists
62  file.seekg(3);
63  // drop a log message to tell the user that he has a byte-order-mark
64  logger(INFO, "Your file contains a byte-order-mark. The first three bytes of this file will be skipped");
65  }
66  else
67  {
68  // ensure that the ifstream object starts at the first byte of the csv file
69  file.seekg(0);
70  }
71  while (getline(file, line)) // get next line in file
72  {
73  v.clear();
74  std::stringstream ss(line);
75  while (getline(ss, field, ','))
76  {
77  // break line into comma delimitted fields
78  v.push_back(field); // add each field to the 1D array
79  }
80  if (hasHeader_)
81  {
82  headerVector_ = v; // add the 1D array to the 2D array
83  hasHeader_ = false;
84  }
85  else
86  {
87  numArray_.push_back(v); // add the 1D array to the 2D array
88  }
89  }
90  }
91  else
92  {
93  logger(ERROR, "File could not be opened");
94  }
95 }
96 
101 std::vector<std::string> csvReader::getHeaderVector()
102 {
103  return headerVector_;
104 }
105 
109 void csvReader::setHeader(bool hasHeader)
110 {
111  hasHeader_ = hasHeader;
112 }
113 
118 std::vector<std::vector<std::string>> csvReader::getNumArray()
119 {
120  return numArray_;
121 }
122 
128 std::vector<Mdouble> csvReader::getFirstColumn(Mdouble scalingFactor)
129 {
130  for (int i = 0; i < numArray_.size(); i++)
131  {
132 
133  // converts vector<string> to vector<double>
134  // if std::stod throws an exception of an invalid argument there might be an encoding issue.
135  CSVFirstColumn_.push_back(std::stod(numArray_[i][0].c_str()) / scalingFactor);
136  }
137  return CSVFirstColumn_;
138 }
139 
145 std::vector<Mdouble> csvReader::getSecondColumn(Mdouble scalingFactor)
146 {
147  for (int i = 0; i < numArray_.size(); i++)
148  {
149  // converts vector<string> to vector<double>
150  // if std::stod throws an exception of an invalid argument there might be an encoding issue.
151  CSVSecondColumn_.push_back(std::stod(numArray_[i][1].c_str()) / scalingFactor);
152  }
153  return CSVSecondColumn_;
154 }
std::vector< std::vector< std::string > > numArray_
Definition: csvReader.h:89
std::vector< std::string > headerVector_
Definition: csvReader.h:95
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
double Mdouble
Definition: GeneralDefine.h:34
csvReader()
Constructor.
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
void read(const std::string &filename)
Reads .csv files into Mercury.
Definition: csvReader.cc:40
std::vector< Mdouble > CSVSecondColumn_
Definition: csvReader.h:105
void setHeader(bool headings)
Set the boolean hasHeader_.
Definition: csvReader.cc:109
std::vector< Mdouble > getFirstColumn(Mdouble scalingFactor)
Get first column of a .csv file and return it as a double.
Definition: csvReader.cc:128
std::vector< std::string > getHeaderVector()
Get the Header string vector of a .csv file.
Definition: csvReader.cc:101
std::vector< Mdouble > CSVFirstColumn_
Definition: csvReader.h:100
std::vector< std::vector< std::string > > getNumArray()
Get the 2D array with the .csv file values.
Definition: csvReader.cc:118
std::vector< Mdouble > getSecondColumn(Mdouble scalingFactor)
Get second column of a .csv file and return it as a double.
Definition: csvReader.cc:145
bool hasHeader_
Definition: csvReader.h:110