laura is hosted by Hepforge, IPPP Durham
Laura++  v3r5
A maximum likelihood fitting package for performing Dalitz-plot analysis.
LauTextFileParser.cc
Go to the documentation of this file.
1 
2 /*
3 Copyright 2008 University of Warwick
4 
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */
17 
18 /*
19 Laura++ package authors:
20 John Back
21 Paul Harrison
22 Thomas Latham
23 */
24 
29 // Class for defining strings that can also handle numbers
30 // Useful for writing lines of output text that has a combination of
31 // words and numbers
32 
33 #include "LauTextFileParser.hh"
34 #include "LauString.hh"
35 
36 #include <fstream>
37 
38 using std::cout;
39 using std::cerr;
40 using std::endl;
41 
43  theFile_(s),
44  totalLines_(0),
45  lineNumber_(0)
46 {
47 }
48 
49 LauTextFileParser::LauTextFileParser(const std::string& s) :
50  theFile_(s),
51  totalLines_(0),
52  lineNumber_(0)
53 {
54 }
55 
57  theFile_(s.Data()),
58  totalLines_(0),
59  lineNumber_(0)
60 {
61 }
62 
64 {
65  LineMap::iterator mapIter;
66  for (mapIter = lineMap_.begin(); mapIter != lineMap_.end(); ++mapIter) {
67  std::vector<std::string> line = mapIter->second;
68  line.clear();
69  }
70  lineMap_.clear();
71 }
72 
74 {
75  // Read the input file and store each (non-comment) line in the internal map.
76  // Store the strings/numbers from each line in a vector, using a whitespace
77  // separator to get each value.
78 
79  std::ifstream getData(theFile_.c_str());
80 
81  std::string whiteSpace(" ");
82  UInt_t lineNo(0);
83 
84  cout<<"Processing input file "<<theFile_<<endl;
85 
86  // Loop while the input file is readable
87  while (getData.good()) {
88 
89  if (getData.peek() == '\n') {
90 
91  // Finish reading line
92  char c;
93  getData.get(c);
94 
95  // Stop while loop if we have reached the end of the file
96  if (getData.eof()) {break;}
97 
98  } else if (getData.peek() == '#') {
99 
100  // Skip comment line
101  getData.ignore(1000, '\n');
102  getData.putback('\n');
103 
104  // Stop while loop if we have reached the end of the file
105  if (getData.eof()) {break;}
106 
107  } else {
108 
109  // Read data line
110  char line[100];
111  getData.getline(line, 100);
112 
113  // Stop while loop if we have reached the end of the file
114  if (getData.eof()) {break;}
115 
116  LauString lineString(line);
117  // Split up the line according to white spaces
118  std::vector<std::string> lineVect = lineString.split(whiteSpace);
119 
120  // Store this line in the internal map
121  lineMap_[lineNo] = lineVect;
122  lineNo++;
123 
124  }
125 
126  }
127 
128  totalLines_ = lineNo;
129  cout<<"Finished storing "<<totalLines_<<" lines from "<<theFile_<<endl;
130 }
131 
132 std::vector<std::string> LauTextFileParser::getNextLine()
133 {
134  // Get the next line of white-space separated strings/numbers
135  ++lineNumber_;
136 
137  std::vector<std::string> theLine = this->getLine(lineNumber_);
138  return theLine;
139 }
140 
141 std::vector<std::string> LauTextFileParser::getLine(UInt_t lineNo)
142 {
143  // Retrieve the vector of white-space separated strings/numbers
144  // for the given (non-commented) line number
145  std::vector<std::string> line;
146 
147  LineMap::iterator mapIter = lineMap_.find(lineNo-1);
148  if (mapIter != lineMap_.end()) {
149  line = mapIter->second;
150  }
151 
152  return line;
153 }
154 
File containing declaration of LauTextFileParser class.
virtual ~LauTextFileParser()
Destructor.
File containing declaration of LauString class.
Class for defining a string.
Definition: LauString.hh:43
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:120
std::vector< std::string > getNextLine()
Retrieve the next line.
UInt_t totalLines_
The total number of lines in the file.
UInt_t lineNumber_
The current line number.
LineMap lineMap_
The parsed text.
std::string theFile_
The name of the file.
std::vector< std::string > getLine(UInt_t lineNo)
Retrieve the specified line.
LauTextFileParser(const char *s)
Constructor.
void processFile()
Parse the file.