QMCPACK
SimpleParser.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 // Miguel Morales, moralessilva2@llnl.gov, Lawrence Livermore National Laboratory
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 // Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
12 //
13 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
14 //////////////////////////////////////////////////////////////////////////////////////
15 
16 
17 #ifndef OHMMS_SIMPLEPARSER_H
18 #define OHMMS_SIMPLEPARSER_H
19 
20 #include <cstdlib>
21 #include <string>
22 #include <iostream>
23 #include <fstream>
24 #include <iomanip>
25 #include <vector>
26 #include <list>
27 #include <sstream>
28 
29 char* readLine(char* s, int max, std::istream& fp);
30 
31 int getwords(std::vector<std::string>& slist, std::istream& fp, int dummy = 0, const std::string& extra_tokens = "");
32 int getwordsWithMergedNumbers(std::vector<std::string>& slist,
33  std::istream& fp,
34  int dummy = 0,
35  const std::string& extra_tokens = "");
36 int getwords(std::vector<std::string>& slist, std::istream& fp, std::string& aline);
37 int getwords(std::vector<std::string>& slist, std::istream& fpos, const char* field, const char* terminate);
38 int getXwords(std::vector<std::string>& slist, std::istream& fp);
39 
40 unsigned parsewords(const char* inbuf, std::vector<std::string>& slist, const std::string& extra_tokens = "");
41 unsigned parsewords(const char* inbuf, std::list<std::string>& slist);
42 
43 void readXmol(std::istream&, double*, int);
44 
46 {
47  static const int bufferSize = 200;
49  std::vector<std::string> currentWords;
50 
51  inline void skiplines(std::istream& is, int n)
52  {
53  while (n > 0)
54  {
55  is.getline(dbuffer, bufferSize);
56  --n;
57  }
58  }
59  template<class T>
60  inline void getValue(std::istream& is, T& aval)
61  {
62  is.getline(dbuffer, bufferSize);
63  std::istringstream a(dbuffer);
64  a >> aval;
65  }
66 
67  template<class T1, class T2>
68  inline void getValue(std::istream& is, T1& aval, T2& bval)
69  {
70  is.getline(dbuffer, bufferSize);
71  std::istringstream a(dbuffer);
72  a >> aval >> bval;
73  }
74 
75  template<class IT>
76  inline void getValues(std::istream& is, IT first, IT last)
77  {
78  while (first != last)
79  {
80  is.getline(dbuffer, bufferSize);
81  std::istringstream a(dbuffer);
82  while (first != last && a >> *first)
83  {
84  first++;
85  }
86  }
87  }
88 
89  int search(std::istream& is, const std::string& keyword)
90  {
91  bool notfound = true;
92  while (notfound)
93  {
94  std::string aline;
95  getline(is, aline, '\n');
96  if (!is)
97  {
98  std::cout << "KEYWORD " << keyword << " : NOT FOUND. " << std::endl;
99  abort();
100  }
101  if (aline.find(keyword) < aline.size())
102  {
103  notfound = false;
104  }
105  }
106  return 1;
107  }
108 
109  int search(std::istream& is, const std::string& keyword, std::string& the_line)
110  {
111  bool notfound = true;
112  while (notfound)
113  {
114  std::string aline;
115  getline(is, aline, '\n');
116  if (!is)
117  {
118  std::cout << "KEYWORD " << keyword << " : NOT FOUND. " << std::endl;
119  abort();
120  }
121  if (aline.find(keyword) < aline.size())
122  {
123  notfound = false;
124  the_line = aline;
125  }
126  }
127  return 1;
128  }
129 
130  bool lookFor(std::istream& is, const std::string& keyword)
131  {
132  bool notfound = true;
133  while (notfound)
134  {
135  std::string aline;
136  getline(is, aline, '\n');
137  if (aline.find(keyword) != std::string::npos)
138  // < aline.size()) {
139  {
140  notfound = false;
141  }
142  //if(! is){
143  if (is.eof())
144  {
145  return false;
146  }
147  }
148  return true;
149  }
150 
151  bool lookFor(std::istream& is, const std::string& keyword, std::string& the_line)
152  {
153  bool notfound = true;
154  while (notfound)
155  {
156  std::string aline;
157  getline(is, aline, '\n');
158  if (aline.find(keyword) != std::string::npos)
159  // < aline.size()) {
160  {
161  notfound = false;
162  the_line = aline;
163  }
164  //if(! is){
165  if (is.eof())
166  {
167  return false;
168  }
169  }
170  return true;
171  }
172 };
173 
174 #endif
int getwords(std::vector< std::string > &slist, std::istream &fp, int dummy=0, const std::string &extra_tokens="")
int getXwords(std::vector< std::string > &slist, std::istream &fp)
std::vector< std::string > currentWords
Definition: SimpleParser.h:49
char dbuffer[bufferSize]
Definition: SimpleParser.h:48
unsigned parsewords(const char *inbuf, std::vector< std::string > &slist, const std::string &extra_tokens="")
void skiplines(std::istream &is, int n)
Definition: SimpleParser.h:51
bool lookFor(std::istream &is, const std::string &keyword, std::string &the_line)
Definition: SimpleParser.h:151
void readXmol(std::istream &, double *, int)
char * readLine(char *s, int max, std::istream &fp)
static const int bufferSize
Definition: SimpleParser.h:47
int search(std::istream &is, const std::string &keyword)
Definition: SimpleParser.h:89
int getwordsWithMergedNumbers(std::vector< std::string > &slist, std::istream &fp, int dummy=0, const std::string &extra_tokens="")
void getValue(std::istream &is, T &aval)
Definition: SimpleParser.h:60
bool lookFor(std::istream &is, const std::string &keyword)
Definition: SimpleParser.h:130
void getValues(std::istream &is, IT first, IT last)
Definition: SimpleParser.h:76
int search(std::istream &is, const std::string &keyword, std::string &the_line)
Definition: SimpleParser.h:109
void getValue(std::istream &is, T1 &aval, T2 &bval)
Definition: SimpleParser.h:68