Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

stringparser.h

Go to the documentation of this file.
00001 
00007 #ifndef __STRINGPARSER_H__
00008 #define __STRINGPARSER_H__
00009 
00010 #include <string>
00011 #include <strstream>
00012 #include <vector>
00013 
00014 
00053 class StringParser {
00054 public:
00055   StringParser() { };
00056   virtual ~StringParser() { }
00057 
00059   typedef std::vector<string>           arglist;
00061   typedef std::vector<string>::iterator arglist_iterator;
00063   typedef std::vector<string>::iterator argptr;
00064   
00065   StringParser(const StringParser&) { } 
00068   virtual StringParser* clone() const { return new StringParser(*this); };
00069   
00070   /* Family of parse functions differing only by their return datatypes */
00071 
00073   virtual double&  parse(const string& s, double&  x) const {  return x=basicparse(s,x);  }
00075   virtual int&     parse(const string& s, int&     x) const {  return x=basicparse(s,x);  }
00077   virtual string&  parse(const string& s, string&  x) const {  return x=s;  }
00079   virtual double*& parse(const string& s, double*& x) const {  basicparse(s,*x); return x;  }
00081   virtual arglist& parse(const string& s, arglist& x) const {
00082     istrstream ist(s.c_str(),s.length());
00083     x.resize(0);
00084     string w;
00085     while (ist>>w)
00086       x.push_back(w);
00087     return x;
00088   }
00089 
00091   virtual string parse(const string& s) const {  string dummy;  return parse(s,dummy);  }
00092   
00096   virtual void error(const string&) const {  }
00097   
00098 private:
00099 
00107   static double& basicparse(const string& s, double& x)
00108     {  istrstream ss(s.c_str(),s.length()); ss >> x; return x;  }
00109 
00110   static int& basicparse(const string& s, int& x)
00111     {  istrstream ss(s.c_str(),s.length()); ss >> x; return x;  }
00112   
00113 };
00114 
00115 
00116 
00117 
00134 class StringArgs {
00135   StringParser::arglist a; // Only used if we are constructed from string
00136   StringParser::argptr beg;
00137   StringParser::argptr end;
00138   
00139 public:
00141   const StringParser& p; 
00142 
00143   StringArgs(const StringParser& parser, StringParser::argptr b, StringParser::argptr e)
00144     :  beg(b), end(e), p(parser)  { }
00145   
00146   StringArgs(const StringParser& parser, const string& s) : p(parser)  {
00147     p.parse(s,a);
00148     beg=a.begin();
00149     end=a.end();
00150   }
00151 
00153   StringArgs(const StringArgs& other)
00154     : a(other.a), beg(other.beg), end(other.end), p(*other.p.clone()) {
00155       /*
00156         Copy constructor is tricky -- if the other was using an internal
00157         container, need to construct equivalent iterators for our own
00158         copy.
00159       */ 
00160       if (!other.a.empty()) {
00161         beg=a.begin()+(other.beg-other.a.begin());
00162         end=a.begin()+(other.end-other.a.begin());
00163       }
00164     }
00165 
00167   bool empty() const {  return (beg==end);  }
00168   
00170   bool oneremaining() const {  return (beg+1==end);  }
00171   
00173   template<class T>
00174   T next(const T& default_val) {
00175     if (empty())
00176       return default_val;
00177     else {
00178       T result;
00179       p.parse(*(beg++), result);
00180       return result;
00181     }
00182   }
00183 
00185   template<class T>
00186   T* next(const T& default_val, T*& ptr) {
00187     if (empty())
00188       *ptr = default_val;
00189     else
00190       p.parse(*(beg++), ptr);
00191     return ptr;
00192   }
00193 
00195   template<class T>
00196   bool nextis(const T& test_val) {
00197     if (!empty()) {
00198       T result;
00199       p.parse(*beg, result);
00200       if (test_val == result) {
00201         beg++;
00202         return true;
00203       }
00204     }
00205     return false;
00206   }
00207 
00210   template<class T>
00211   bool lookaheadone(const T& test_val) {
00212     if (empty() || (beg+1)==end)
00213       return false;
00214 
00215     T result;
00216     p.parse(*(beg+1), result);
00217     return (test_val == result);
00218   }
00219 
00221   StringArgs recursiveparser() {  return StringArgs(p, next(string("")));  }
00222 };
00223 
00224 
00225 #endif /* __STRINGPARSER_H__ */

Generated at Mon Aug 21 00:30:55 2000 for RF-LISSOM by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000