Alexandria  2.18
Please provide a description of the project.
AsciiParser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
26 #include <boost/algorithm/string.hpp>
27 #include <boost/regex.hpp>
28 #include <fstream>
29 #include <iostream>
30 #include <sstream>
31 
32 #include "boost/lexical_cast.hpp"
33 
35 #include "StringFunctions.h"
36 #include "Table/AsciiReader.h"
37 #include "XYDataset/AsciiParser.h"
38 
39 using boost::regex;
40 using boost::regex_match;
41 
42 namespace Euclid {
43 namespace XYDataset {
44 
45 //
46 // Get dataset name from ASCII file
47 //
49 
50  std::ifstream sfile(file);
51  // Check file exists
52  if (!sfile) {
53  throw Elements::Exception() << "File does not exist : " << file;
54  }
55 
56  std::string line{};
57  std::string dataset_name{};
58  // Check dataset name is in the file
59  // Convention: read until found first non empty line, removing empty lines.
60  while (line.empty() && sfile.good()) {
61  std::getline(sfile, line);
62  }
63 
64  boost::regex expression(m_regex_name);
65  boost::smatch s_match;
66  if (boost::regex_match(line, s_match, expression)) {
67  dataset_name = s_match[1].str();
68  } else {
69  // Dataset name is the filename without extension and path
70  std::string str{};
71  str = removeAllBeforeLastSlash(file);
72  dataset_name = removeExtension(str);
73  }
74 
75  return dataset_name;
76 }
77 
79  std::ifstream sfile(file);
80  if (!sfile) {
81  throw Elements::Exception() << "File does not exist : " << file;
82  }
83 
84  std::string value{};
85  std::string line{};
86  std::string dataset_name{};
87  std::string reg_ex_str = "^\\s*#\\s*" + key_word + "\\s+(\\w+)\\s*$";
88  boost::regex expression(reg_ex_str);
89 
90  while (sfile.good()) {
91  std::getline(sfile, line);
92  boost::smatch s_match;
93  if (!line.empty() && boost::regex_match(line, s_match, expression)) {
94  // extract the parameter value
95  size_t start_position = line.find(key_word) + key_word.length();
96  value = line.substr(start_position);
97  boost::trim(value);
98  break;
99  }
100  }
101  return value;
102 }
103 
104 //
105 // Get dataset from ASCII file
106 //
108 
109  std::unique_ptr<XYDataset> dataset_ptr{};
110  std::ifstream sfile(file);
111  // Check file exists
112  if (sfile) {
113  // Read file into a Table object
114  auto table = Table::AsciiReader{sfile}.fixColumnTypes({typeid(double), typeid(double)}).read();
115  // Put the Table data into vector pair
117  for (auto row : table) {
118  vector_pair.push_back(std::make_pair(boost::get<double>(row[0]), boost::get<double>(row[1])));
119  }
120  dataset_ptr = std::unique_ptr<XYDataset>{new XYDataset(vector_pair)};
121  }
122 
123  return dataset_ptr;
124 }
125 
127  bool is_a_dataset_file = false;
128  std::ifstream sfile(file);
129  // Check file exists
130  if (sfile) {
131  std::string line{};
132  // Convention: read until found first non empty line, removing empty lines.
133  // Escape also the dataset name and comment lines
134  boost::regex expression("\\s*#.*");
135  boost::smatch s_match;
136  while ((line.empty() || boost::regex_match(line, s_match, expression)) && sfile.good()) {
137  std::getline(sfile, line);
138  }
139  if (sfile.good()) {
140  // We should have 2 double values only on one line
141  try {
142  std::stringstream ss(line);
143  std::string empty_string{};
144  std::string d1, d2;
145  ss >> d1 >> d2 >> empty_string;
146  boost::lexical_cast<double>(d1);
147  boost::lexical_cast<double>(d2);
148  if (!empty_string.empty()) {
149  is_a_dataset_file = false;
150  } else {
151  is_a_dataset_file = true;
152  }
153  } catch (...) {
154  is_a_dataset_file = false;
155  }
156  } // Eof sfile.good()
157  } // Eof sfile
158  return is_a_dataset_file;
159 }
160 
161 } // namespace XYDataset
162 } // end of namespace Euclid
std::string
STL class.
Euclid::XYDataset::XYDataset
This module provides an interface for accessing two dimensional datasets (pairs of (X,...
Definition: XYDataset.h:59
Euclid::XYDataset::AsciiParser::getDataset
std::unique_ptr< XYDataset > getDataset(const std::string &file) override
Get a XYDataset object reading data from an ASCII file.
Definition: AsciiParser.cpp:107
std::vector
STL class.
std::string::length
T length(T... args)
AsciiParser.h
StringFunctions.h
std::stringstream
STL class.
Euclid::XYDataset::removeAllBeforeLastSlash
std::string removeAllBeforeLastSlash(const std::string &input_str)
Definition: StringFunctions.cpp:111
AsciiReader.h
Euclid::XYDataset::AsciiParser::getParameter
std::string getParameter(const std::string &file, const std::string &key_word) override
Get the parameter identified by a given key_word value from a file.
Definition: AsciiParser.cpp:78
std::vector::push_back
T push_back(T... args)
Exception.h
Elements::Exception
Euclid::XYDataset::AsciiParser::getName
std::string getName(const std::string &file) override
Get the dataset name of a ASCII file.
Definition: AsciiParser.cpp:48
Euclid::XYDataset::removeExtension
std::string removeExtension(const std::string &input_str)
Definition: StringFunctions.cpp:91
Euclid::XYDataset::AsciiParser::m_regex_name
std::string m_regex_name
Definition: AsciiParser.h:131
std::ifstream::good
T good(T... args)
Euclid::Table::AsciiReader
TableReader implementation for reading ASCII tables from streams.
Definition: AsciiReader.h:87
Euclid::XYDataset::AsciiParser::isDatasetFile
bool isDatasetFile(const std::string &file) override
Check that the ASCII file is a dataset file(with at least one line with 2 double values)
Definition: AsciiParser.cpp:126
std::getline
T getline(T... args)
std::make_pair
T make_pair(T... args)
std::unique_ptr
STL class.
Euclid
Definition: InstOrRefHolder.h:29
std::ifstream
STL class.