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 getwords(std::vector<std::string>& slist, std::istream& fpos, const char* terminate); 39 int getXwords(std::vector<std::string>& slist, std::istream& fp); 40 int getXwords(std::vector<std::string>& slist, std::istream& fpos, const char* terminate); 41 42 unsigned parsewords(const char* inbuf, std::vector<std::string>& slist, const std::string& extra_tokens = ""); 43 unsigned parsewords(const char* inbuf, std::list<std::string>& slist); 44 45 void readXmol(std::istream&, double*, int); 46 47 struct OhmmsAsciiParser 48 { 49 static const int bufferSize = 200; 50 char dbuffer[bufferSize]; 51 std::vector<std::string> currentWords; 52 skiplinesOhmmsAsciiParser53 inline void skiplines(std::istream& is, int n) 54 { 55 while (n > 0) 56 { 57 is.getline(dbuffer, bufferSize); 58 --n; 59 } 60 } 61 template<class T> getValueOhmmsAsciiParser62 inline void getValue(std::istream& is, T& aval) 63 { 64 is.getline(dbuffer, bufferSize); 65 std::istringstream a(dbuffer); 66 a >> aval; 67 } 68 69 template<class T1, class T2> getValueOhmmsAsciiParser70 inline void getValue(std::istream& is, T1& aval, T2& bval) 71 { 72 is.getline(dbuffer, bufferSize); 73 std::istringstream a(dbuffer); 74 a >> aval >> bval; 75 } 76 77 template<class IT> getValuesOhmmsAsciiParser78 inline void getValues(std::istream& is, IT first, IT last) 79 { 80 while (first != last) 81 { 82 is.getline(dbuffer, bufferSize); 83 std::istringstream a(dbuffer); 84 while (first != last && a >> *first) 85 { 86 first++; 87 } 88 } 89 } 90 searchOhmmsAsciiParser91 int search(std::istream& is, const std::string& keyword) 92 { 93 bool notfound = true; 94 while (notfound) 95 { 96 std::string aline; 97 getline(is, aline, '\n'); 98 if (!is) 99 { 100 std::cout << "KEYWORD " << keyword << " : NOT FOUND. " << std::endl; 101 abort(); 102 } 103 if (aline.find(keyword) < aline.size()) 104 { 105 notfound = false; 106 } 107 } 108 return 1; 109 } 110 searchOhmmsAsciiParser111 int search(std::istream& is, const std::string& keyword, std::string& the_line) 112 { 113 bool notfound = true; 114 while (notfound) 115 { 116 std::string aline; 117 getline(is, aline, '\n'); 118 if (!is) 119 { 120 std::cout << "KEYWORD " << keyword << " : NOT FOUND. " << std::endl; 121 abort(); 122 } 123 if (aline.find(keyword) < aline.size()) 124 { 125 notfound = false; 126 the_line = aline; 127 } 128 } 129 return 1; 130 } 131 lookForOhmmsAsciiParser132 bool lookFor(std::istream& is, const std::string& keyword) 133 { 134 bool notfound = true; 135 while (notfound) 136 { 137 std::string aline; 138 getline(is, aline, '\n'); 139 if (aline.find(keyword) != std::string::npos) 140 // < aline.size()) { 141 { 142 notfound = false; 143 } 144 //if(! is){ 145 if (is.eof()) 146 { 147 return false; 148 } 149 } 150 return true; 151 } 152 lookForOhmmsAsciiParser153 bool lookFor(std::istream& is, const std::string& keyword, std::string& the_line) 154 { 155 bool notfound = true; 156 while (notfound) 157 { 158 std::string aline; 159 getline(is, aline, '\n'); 160 if (aline.find(keyword) != std::string::npos) 161 // < aline.size()) { 162 { 163 notfound = false; 164 the_line = aline; 165 } 166 //if(! is){ 167 if (is.eof()) 168 { 169 return false; 170 } 171 } 172 return true; 173 } 174 }; 175 176 #endif 177