MINT2
ParsedParameterLine.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:17:56 GMT
4 #include <cctype>
5 #include <iostream>
6 
7 #include <algorithm>
8 
9 using namespace MINT;
10 
11 const std::string ParsedParameterLine::_commentStart="//";
12 const std::string ParsedParameterLine::_testString=" \"para Name\" 78 70 80 90 100 101 10 11 12\\\\ comment ";
13 
14 const std::string ParsedParameterLine::_testName="para Name";
15 
16 // note: valide parameter names start with a letter (isalpha())
17 using namespace std;
18 
20  return c==' ' || c=='\t' || c=='\n';
21 }
22 std::string ParsedParameterLine::removeLeadingBlanks(const string& s){
23  if(s.empty()) return s;
24  std::string::const_iterator firstNonBlank=s.begin();
25  for(firstNonBlank=s.begin();
26  firstNonBlank != s.end() && (isBlank(*firstNonBlank));
27  firstNonBlank++) ;
28  // cout << "firstNonBlank: " << *firstNonBlank << "." << endl;
29 
30  string new_s(firstNonBlank, s.end());
31  return new_s;
32 }
33 std::string ParsedParameterLine::removeTrailingBlanks(const string& s){
34  if(s.empty()) return s;
35  std::string::const_iterator lastNonBlank = s.end();
36  for(string::const_iterator it=s.begin();
37  it != s.end(); it++){
38  if(! isBlank(*it)) lastNonBlank = it;
39  }
40  if(lastNonBlank != s.end()) lastNonBlank++;
41 
42  string new_s(s.begin(), lastNonBlank);
43  return new_s;
44 }
46  return removeTrailingBlanks(removeLeadingBlanks(s));
47 }
48 
49 string ParsedParameterLine::removeComment(const string& s_in){
50  if(s_in.empty()) return s_in;
51  string s = s_in;
52  int commentStart = s.find(_commentStart);
53  //cout << " commentStart " << commentStart << endl;
54  if(commentStart >= 0 && commentStart < (int) s.size()){
55  s.resize(commentStart);
56  }
57  return s;
58 }
59 
61  return removeComment(removeLeadingBlanks(s_in));
62 }
63 
65  return (! _parsedStrings.empty()) && isalpha(name()[0]);
66 }
67 
68 int ParsedParameterLine::makeParsedStrings(const std::string& line
69  , std::vector<std::string>& fillThisList){
70 
71  string s = removeLeadingBlanks(
72  removeTrailingBlanks(
73  removeComment(line
74  )));
75 
76  if(s.empty()) return 0;
77 
78  s.push_back(' '); // makes sure we get last element
79  s = " " + s; // makes things easier when we start with quotes.
80  string::const_iterator prev=s.begin();
81  bool prevBlank=true;
82  bool insideQuotes=false;
83  // quotes are treated like blanks
84  // but blanks are ignored if we
85  // are inside quotes... makes sense?
86 
87  bool ignore=false;
88  // NOT ANYMORE: we ignore things inside sqare brackets []
89  // to be compatible with Mikhail's ASCII file
90  // format.
91 
92  for(string::const_iterator it=s.begin();
93  it != s.end(); it++){
94  //if((! insideQuotes) && *it == '[') ignore=true;
95  if( ((! insideQuotes) && isBlank(*it)) || *it == '"' || ignore){
96  if(! prevBlank){
97  string tmp_s(prev, it);
98  fillThisList.push_back(tmp_s);
99  }
100  prevBlank=true;
101  }else{
102  if(prevBlank) prev=it;
103  prevBlank=false;
104  }
105  //if((!insideQuotes) && *it == ']') ignore=false;
106  if(*it == '"') insideQuotes = ! insideQuotes;
107  }
108  if(insideQuotes){
109  cout << "WARNING in ParsedParameterLine::makeParsedStrings \n"
110  << " unbalanced quotes in string:\n"
111  << line << endl;
112  }
113  return fillThisList.size();
114 }
115 
116 bool ParsedParameterLine::makeParsedStrings(const std::string& line){
117  makeParsedStrings(line, _parsedStrings);
118  return ! _parsedStrings.empty();
119 }
120 
121 void ParsedParameterLine::print(ostream& os) const{
122  for(unsigned int i=0; i<_parsedStrings.size(); i++){
123  os << _parsedStrings[i];
124  if(i != _parsedStrings.size()-1) os << "; ";
125  }
126 }
127 
128 void ParsedParameterLine::stringParsingTest(std::ostream& os) const{
129  os << "Testing the String Parsing in ParsedParameterLine:" << endl;
130  os << " test string is (w/o the '>' and '<'):\n"
131  << " >" << _testString << "<" << endl;
132  string noBlanks = removeLeadingBlanks(_testString);
133  os << " ... removing leading blanks gives:\n"
134  << " >" << noBlanks << "<" << endl;
135  os << " ... removing trailing blanks gives:\n"
136  << " >" << removeTrailingBlanks(_testString) << "<" << endl;
137  string noComment = removeComment(_testString);
138  os << " ... string without comment:\n"
139  << " >" << noComment << "<"
140  << endl;
141  os << " ... string without comment run through removeComment again:\n"
142  << " >" << removeComment(noComment) << "<"
143  << endl;
144  os << " ... removeCommentAndLeadingBlanks\n"
145  << " >" << removeCommentAndLeadingBlanks(_testString) << "<"
146  << endl;
147 
148  vector<string> valueStringList;
149  makeParsedStrings(_testString, valueStringList);
150  os << " made value string list:\n";
151  for(unsigned int i=0; i< valueStringList.size(); i++){
152  os << " " << i << ") >" << valueStringList[i] << "< " << endl;
153  }
154  os << "test done." << endl;
155 
156  return;
157 }
158 
159 
160 std::ostream& operator<<(std::ostream& os, const ParsedParameterLine& ppl){
161  ppl.print(os);
162  return os;
163 }
164 
165 //
static std::string removeCommentAndLeadingBlanks(const std::string &line)
static std::string removeComment(const std::string &line)
static int makeParsedStrings(const std::string &line, std::vector< std::string > &fillThisList)
std::ostream & operator<<(std::ostream &os, const ParsedParameterLine &ppl)
static const double s
static std::string removeLeadingAndTrailingBlanks(const std::string &s)
static std::string removeTrailingBlanks(const std::string &s)
static std::string removeLeadingBlanks(const std::string &s)
static const std::string _testName
static const std::string _commentStart
virtual void stringParsingTest(std::ostream &os=std::cout) const
virtual void print(std::ostream &os=std::cout) const
static const std::string _testString