33 bool Tools::convert(
const string & str,
int & t){
34 istringstream istr(str.c_str());
39 return remaining.length()==0;
42 bool Tools::convert(
const string & str,
long int & t){
43 istringstream istr(str.c_str());
48 return remaining.length()==0;
51 bool Tools::convert(
const string & str,
unsigned & t){
52 istringstream istr(str.c_str());
57 return remaining.length()==0;
62 bool r=convert(str,i);
67 bool Tools::convert(
const string & str,
float & t){
74 bool Tools::convert(
const string & str,
double & t){
75 if(str==
"PI" || str==
"+PI" || str==
"+pi" || str==
"pi"){
77 }
else if(str==
"-PI" || str==
"-pi"){
79 }
else if( str.find(
"PI")!=std::string::npos ){
80 std::size_t pi_start=str.find_first_of(
"PI");
81 if(str.substr(pi_start)!=
"PI")
return false;
82 istringstream nstr(str.substr(0,pi_start));
83 double ff=0.0;
bool ok=nstr>>ff;
86 std::string remains; nstr>>remains;
87 return remains.length()==0;
88 }
else if( str.find(
"pi")!=std::string::npos ){
89 std::size_t pi_start=str.find_first_of(
"pi");
90 if(str.substr(pi_start)!=
"pi")
return false;
91 istringstream nstr(str.substr(0,pi_start));
92 double ff=0.0;
bool ok=nstr>>ff;
95 std::string remains; nstr>>remains;
96 return remains.length()==0;
98 istringstream istr(str.c_str());
100 if(!ok)
return false;
103 return remaining.length()==0;
107 bool Tools::convert(
const string & str,
string & t){
112 vector<string> Tools::getWords(
const string & line,
const char* separators,
int * parlevel,
const char* parenthesis){
113 plumed_massert(strlen(parenthesis)==1,
"multiple parenthesis type not available");
114 plumed_massert(parenthesis[0]==
'(' || parenthesis[0]==
'[' || parenthesis[0]==
'{',
115 "only ( [ { allowed as parenthesis");
116 if(!separators) separators=
" \t\n";
117 const string sep(separators);
118 char openpar=parenthesis[0];
120 if(openpar==
'(') closepar=
')';
121 if(openpar==
'[') closepar=
']';
122 if(openpar==
'{') closepar=
'}';
123 vector<string> words;
125 int parenthesisLevel=0;
126 if(parlevel) parenthesisLevel=*parlevel;
127 for(
unsigned i=0;i<line.length();i++){
129 bool onParenthesis=
false;
130 if(line[i]==openpar || line[i]==closepar) onParenthesis=
true;
131 if(line[i]==closepar){
133 plumed_massert(parenthesisLevel>=0,
"Extra closed parenthesis in '" + line +
"'");
135 if(parenthesisLevel==0)
for(
unsigned j=0;j<sep.length();j++)
if(line[i]==sep[j]) found=
true;
137 if(!(parenthesisLevel==0 && (found||onParenthesis))) word.push_back(line[i]);
138 if(line[i]==openpar) parenthesisLevel++;
139 if(found && word.length()>0){
140 if(!parlevel) plumed_massert(parenthesisLevel==0,
"Unmatching parenthesis in '" + line +
"'");
141 words.push_back(word);
146 if(!parlevel) plumed_massert(parenthesisLevel==0,
"Unmatching parenthesis in '" + line +
"'");
147 words.push_back(word);
149 if(parlevel) *parlevel=parenthesisLevel;
153 bool Tools::getParsedLine(
IFile& ifile,vector<string> & words){
159 bool mergenext=
false;
160 while((stat=ifile.
getline(line))){
163 if(line.length()==0)
continue;
164 vector<string>
w=getWords(line,NULL,&parlevel);
165 if(w.empty())
continue;
166 if(inside && *(w.begin())==
"..."){
168 if(w.size()==2) plumed_massert(w[1]==words[0],
"second word in terminating \"...\" lines, if present, should be equal to first word of directive");
169 plumed_massert(w.size()<=2,
"terminating \"...\" lines cannot consist of more than two words");
171 }
else if(*(w.end()-1)==
"..."){
176 if(mergenext && words.size()>0 && w.size()>0){
177 words[words.size()-1]+=
" "+w[0];
180 for(
unsigned i=i0;i<w.size();++i) words.push_back(w[i]);
181 mergenext=(parlevel>0);
184 plumed_massert(parlevel==0,
"non matching parenthesis");
185 if(words.size()>0)
return true;
190 bool Tools::getline(FILE* fp,
string & line){
192 const int bufferlength=1024;
193 char buffer[bufferlength];
195 for(
int i=0;i<bufferlength;i++) buffer[i]=
'\0';
196 while((ret=fgets(buffer,bufferlength,fp))){
198 unsigned ss=strlen(buffer);
199 if(ss>0)
if(buffer[ss-1]==
'\n')
break;
201 if(line.length()>0)
if(*(line.end()-1)==
'\n') line.erase(line.end()-1);
205 void Tools::trim(
string & s){
206 size_t n=s.find_last_not_of(
" \t");
210 void Tools::trimComments(
string & s){
211 size_t n=s.find_first_of(
"#");
215 bool Tools::getKey(vector<string>& line,
const string & key,
string & s){
217 for(vector<string>::iterator p=line.begin();p!=line.end();++p){
218 if((*p).length()==0)
continue;
219 string x=(*p).substr(0,key.length());
221 if((*p).length()==key.length())
return false;
222 string tmp=(*p).substr(key.length(),(*p).length());
231 void Tools::interpretRanges(std::vector<std::string>&s){
233 for(vector<string>::iterator p=s.begin();p!=s.end();++p){
234 vector<string> words;
235 words=getWords(*p,
"-");
238 if(words.size()==2 && convert(words[0],a)){
240 vector<string> bwords=getWords(words[1],
":");
241 if(bwords.size()==2 && convert(bwords[0],b) && convert(bwords[1],c)) found=
true;
242 else if(convert(words[1],b)){
247 plumed_massert(b>=a,
"interpreting ranges "+ *p +
", second number should be larger than first number");
248 plumed_massert(c>0,
"interpreting ranges "+ *p +
", stride should be positive");
249 for(
int i=a;i<=b;i+=c){
256 if(!found) news.push_back(*p);
261 void Tools::interpretLabel(vector<string>&s){
262 if(s.size()<2)
return;
264 unsigned l=s0.length();
268 s[1]=
"LABEL="+s0.substr(0,l-1);
272 vector<string> Tools::ls(
const string&d){
274 vector<string> result;
275 if ((dir=opendir(d.c_str()))){
277 while ((ent = readdir (dir)))
if(
string(ent->d_name)!=
"." && string(ent->d_name)!=
"..") result.push_back(ent->d_name);
283 void Tools::stripLeadingAndTrailingBlanks( std::string& str ){
284 std::size_t first=str.find_first_not_of(
' ');
285 std::size_t last=str.find_last_not_of(
' ');
286 if( first<=last && first!=std::string::npos) str=str.substr(first,last+1);
289 std::string Tools::extension(
const std::string&s){
290 size_t n=s.find_last_of(
".");
292 if(n!=std::string::npos && n+1<s.length() && n+5>=s.length()){
294 if(ext.find(
"/")!=std::string::npos) ext=
"";
295 string base=s.substr(0,n);
296 if(base.length()==0) ext=
"";
297 if(base.length()>0 && base[base.length()-1]==
'/') ext=
"";
Simple class to store the index of an atom.
IFile & getline(std::string &)
Get a full line as a string.
const double pi(3.141592653589793238462643383279502884197169399375105820974944592307)
PI.
AtomNumber & setSerial(unsigned)
Sets the atom number by serial, returning a reference to the AtomNumber itself.