laura is hosted by Hepforge, IPPP Durham
Laura++  v1r0
A maximum likelihood fitting package for performing Dalitz-plot analysis.
LauTextFileParser.cc
Go to the documentation of this file.
1 
2 // Copyright University of Warwick 2008 - 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 // Authors:
7 // Thomas Latham
8 // John Back
9 // Paul Harrison
10 
15 // Class for defining strings that can also handle numbers
16 // Useful for writing lines of output text that has a combination of
17 // words and numbers
18 
19 #include "LauTextFileParser.hh"
20 #include "LauString.hh"
21 
22 #include <fstream>
23 
24 using std::cout;
25 using std::cerr;
26 using std::endl;
27 
29  theFile_(s),
30  totalLines_(0),
31  lineNumber_(0)
32 {
33 }
34 
35 LauTextFileParser::LauTextFileParser(const std::string& s) :
36  theFile_(s),
37  totalLines_(0),
38  lineNumber_(0)
39 {
40 }
41 
43  theFile_(s.Data()),
44  totalLines_(0),
45  lineNumber_(0)
46 {
47 }
48 
50 {
51  LineMap::iterator mapIter;
52  for (mapIter = lineMap_.begin(); mapIter != lineMap_.end(); ++mapIter) {
53  std::vector<std::string> line = mapIter->second;
54  line.clear();
55  }
56  lineMap_.clear();
57 }
58 
60 {
61  // Read the input file and store each (non-comment) line in the internal map.
62  // Store the strings/numbers from each line in a vector, using a whitespace
63  // separator to get each value.
64 
65  std::ifstream getData(theFile_.c_str());
66 
67  std::string whiteSpace(" ");
68  int lineNo(0);
69 
70  cout<<"Processing input file "<<theFile_<<endl;
71 
72  // Loop while the input file is readable
73  while (getData.good()) {
74 
75  if (getData.peek() == '\n') {
76 
77  // Finish reading line
78  char c;
79  getData.get(c);
80 
81  // Stop while loop if we have reached the end of the file
82  if (getData.eof()) {break;}
83 
84  } else if (getData.peek() == '#') {
85 
86  // Skip comment line
87  getData.ignore(1000, '\n');
88  getData.putback('\n');
89 
90  // Stop while loop if we have reached the end of the file
91  if (getData.eof()) {break;}
92 
93  } else {
94 
95  // Read data line
96  char line[100];
97  getData.getline(line, 100);
98 
99  // Stop while loop if we have reached the end of the file
100  if (getData.eof()) {break;}
101 
102  LauString lineString(line);
103  // Split up the line according to white spaces
104  std::vector<std::string> lineVect = lineString.split(whiteSpace);
105 
106  // Store this line in the internal map
107  lineMap_[lineNo] = lineVect;
108  lineNo++;
109 
110  }
111 
112  }
113 
114  totalLines_ = lineNo;
115  cout<<"Finished storing "<<totalLines_<<" lines from "<<theFile_<<endl;
116 }
117 
118 std::vector<std::string> LauTextFileParser::getNextLine()
119 {
120  // Get the next line of white-space separated strings/numbers
121  ++lineNumber_;
122 
123  std::vector<std::string> theLine = this->getLine(lineNumber_);
124  return theLine;
125 }
126 
127 std::vector<std::string> LauTextFileParser::getLine(int lineNo)
128 {
129  // Retrieve the vector of white-space separated strings/numbers
130  // for the given (non-commented) line number
131  std::vector<std::string> line;
132 
133  LineMap::iterator mapIter = lineMap_.find(lineNo-1);
134  if (mapIter != lineMap_.end()) {
135  line = mapIter->second;
136  }
137 
138  return line;
139 }
140 
File containing declaration of LauTextFileParser class.
virtual ~LauTextFileParser()
Destructor.
File containing declaration of LauString class.
Class for defining a string.
Definition: LauString.hh:29
std::vector< std::string > split(const std::string &splitter) const
Split up a string according to a split string (e.g. &quot;:&quot; or &quot; &quot;)
Definition: LauString.cc:94
std::vector< std::string > getNextLine()
Retrieve the next line.
int lineNumber_
The current line number.
LineMap lineMap_
The parsed text.
std::vector< std::string > getLine(int lineNo)
Retrieve the specified line.
std::string theFile_
The name of the file.
LauTextFileParser(const char *s)
Constructor.
int totalLines_
The total number of lines in the file.
void processFile()
Parse the file.