Alexandria  2.16
Please provide a description of the project.
AsciiParser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 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/regex.hpp>
27 #include <boost/algorithm/string.hpp>
28 #include <fstream>
29 #include <iostream>
30 #include <sstream>
31 
32 #include "boost/lexical_cast.hpp"
33 
35 #include "Table/AsciiReader.h"
36 #include "XYDataset/AsciiParser.h"
37 #include "StringFunctions.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  }
69  else {
70  // Dataset name is the filename without extension and path
71  std::string str {};
72  str = removeAllBeforeLastSlash(file);
73  dataset_name = removeExtension(str);
74  }
75 
76  return dataset_name;
77 }
78 
79 
81  std::ifstream sfile(file);
82  if (!sfile) {
83  throw Elements::Exception() << "File does not exist : " << file;
84  }
85 
86  std::string value{};
87  std::string line{};
88  std::string dataset_name{};
89  std::string reg_ex_str = "^\\s*#\\s*" + key_word + "\\s+(\\w+)\\s*$";
90  boost::regex expression(reg_ex_str);
91 
92  while (sfile.good()) {
93  std::getline(sfile, line);
94  boost::smatch s_match;
95  if (!line.empty() && boost::regex_match(line, s_match, expression)) {
96  // extract the parameter value
97  size_t start_position = line.find(key_word)+key_word.length();
98  value = line.substr (start_position);
99  boost::trim(value);
100  break;
101  }
102  }
103  return value;
104 }
105 
106 //
107 // Get dataset from ASCII file
108 //
110 
111  std::unique_ptr<XYDataset> dataset_ptr {};
112  std::ifstream sfile(file);
113  // Check file exists
114  if (sfile) {
115  // Read file into a Table object
116  auto table = Table::AsciiReader{sfile}.fixColumnTypes({typeid(double), typeid(double)}).read();
117  // Put the Table data into vector pair
119  for (auto row : table) {
120  vector_pair.push_back(std::make_pair( boost::get<double>(row[0]), boost::get<double>(row[1]) ));
121  }
122  dataset_ptr = std::unique_ptr<XYDataset> { new XYDataset(vector_pair) };
123  }
124 
125  return dataset_ptr;
126 }
127 
129  bool is_a_dataset_file = false;
130  std::ifstream sfile(file);
131  // Check file exists
132  if (sfile) {
133  std::string line{};
134  // Convention: read until found first non empty line, removing empty lines.
135  // Escape also the dataset name and comment lines
136  boost::regex expression("\\s*#.*");
137  boost::smatch s_match;
138  while ((line.empty() || boost::regex_match(line, s_match, expression)) && sfile.good()) {
139  std::getline(sfile, line);
140  }
141  if (sfile.good()) {
142  // We should have 2 double values only on one line
143  try {
144  std::stringstream ss(line);
145  std::string empty_string{};
146  std::string d1, d2;
147  ss >> d1 >> d2 >> empty_string;
148  boost::lexical_cast<double>(d1);
149  boost::lexical_cast<double>(d2);
150  if (!empty_string.empty()){
151  is_a_dataset_file = false;
152  }
153  else {
154  is_a_dataset_file = true;
155  }
156  }
157  catch(...){
158  is_a_dataset_file = false;
159  }
160  }// Eof sfile.good()
161  } // Eof sfile
162  return is_a_dataset_file;
163 }
164 
165 } // XYDataset namespace
166 } // 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:60
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:109
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:115
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:80
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:94
Euclid::XYDataset::AsciiParser::m_regex_name
std::string m_regex_name
Definition: AsciiParser.h:135
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:128
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.