plis.h

00001 
00013 #ifndef _PLIS_H
00014 #define _PLIS_H
00015 
00016 #include <string.h>
00017 #include <string>
00018 #include <stdarg.h>
00019 #include <deque>
00020 #include <plis_regexp.h>
00021 
00022 #define PLIS_INLINE     inline
00023 
00024 namespace plis {
00025   class substring;
00026   class llip;
00027   
00035   class slip 
00036     {
00037       std::string pstr;
00038       
00039     public:
00040       slip():pstr(){}
00041       slip(const slip& s) : pstr(s.str()){}     
00042       slip(const char *s) : pstr(s){}
00043       slip(const char *s, int n) : pstr(s,n){}
00044       //    PLIS_INLINE slip(long l);
00045       slip(const char c) : pstr(1,c) {};
00046       slip(const substring& sb);
00047       
00049       slip& operator=(const char *s) {
00050         if (s==NULL)
00051           pstr = "";
00052         else
00053           pstr= s;
00054         return *this;
00055       }
00057       slip& operator=(const slip& s);
00058       slip& operator=(const substring& sb);
00059       
00061       operator const char*() const { return pstr.c_str(); }
00062       
00064       operator const std::string() const { return pstr; }
00065       
00067       const char operator[](int n) const{ return pstr[n]; }
00068       
00070       int length(void) const{ return pstr.size(); }
00071       
00073       char chop(void);
00074       
00076       void chomp(void);
00077       
00079       int index(const slip& s, int offset= 0);
00080       
00082       int rindex(const slip& s, int offset= -1);
00083       
00085       int wsplit(llip&);
00086       
00091       int strsplit(const slip&str, llip&sl);  // string split
00092       
00094       substring substr(int offset, int len= -1);
00095       PLIS_INLINE substring substr(const Range& r);
00096       
00098       int tr(const char *, const char *, const char *opts="");
00099       
00100       /* \brief Match a regular expression.
00101          \param r Regular expression
00102       */
00103       int m(Regexp& r);
00104       /* \brief Match a regular expression with optionts
00105          \param opts Optional parameters. May be one of:
00106          - 'i' - Case insensitive match.
00107       */
00112       int m(const char *, const char *opts=""); // the regexp match m/.../ equiv
00118       int m(const char *, llip& match_list, const char *opts="");
00119       
00120       /* \brief Match a regular expression.
00121          \param r Regular expression
00122          \retval match_list  The list of the sub groups found. Note that match_list[0]
00123          is the whole expression and $1 is placed in match_list[1], etc.
00124       */
00125       int m(Regexp&r, llip& match_list);
00128       int s(const char *, const char *, const char *opts="");
00129       
00132       llip split(const char *pat= "[ \\t\\n]+", int limit= -1);
00133       
00134       int operator<(const slip& s) const { return (strcmp(pstr.c_str(), s) < 0); }
00135       int operator>(const slip& s) const { return (strcmp(pstr.c_str(), s) > 0); }
00136       int operator<=(const slip& s) const { return (strcmp(pstr.c_str(), s) <= 0); }
00137       int operator>=(const slip& s) const { return (strcmp(pstr.c_str(), s) >= 0); }
00138       int operator==(const slip& s) const { return (strcmp(pstr.c_str(), s) == 0); }
00139       int operator==(const char *s) const { return (strcmp(pstr.c_str(), s) == 0); }
00140       int operator!=(const slip& s) const { return (strcmp(pstr.c_str(), s) != 0); }
00141       int operator!=(const char *s) const { return (strcmp(pstr.c_str(), s) != 0); }
00142       
00144       slip operator+(const slip& s) const;
00145       
00147       slip operator+(const char *s) const;
00148       
00150       slip operator+(char c) const;
00151       
00153       friend slip operator+(const char *s1, const slip& s2);
00154       
00156       friend std::istream& operator>>(std::istream&, slip&);
00157       
00159       slip& operator+=(const slip& s) { pstr.append(s.str().c_str(), s.length()); return *this;}
00160       
00162       slip& operator+=(const char *s) { pstr.append(s, strlen(s)); return *this;}
00163       
00165       slip& operator+=(char c){ pstr.append(1,c); return *this;}
00166       friend class substring;
00167       
00169       const std::string& str() const { return pstr; }
00170       
00172       const char *c_str() const { return pstr.c_str(); }
00173       
00175       int atoi();
00176       
00178       double atof();
00179       
00180     private:
00181       void insert(int pos, int len, const char *pt, int nlen);
00182       
00183     };
00184 
00187   class substring
00188     {
00189     public:
00190       int pos, len;
00191       slip& str;
00192       const char *pt;
00193       
00194       friend class slip;
00195       
00196     private:
00197       substring(slip& os, int p, int l) : str(os)
00198         {
00199           if(p > os.length()) p= os.length();
00200           if((p+l) > os.length()) l= os.length() - p;
00201           pos= p; len= l;
00202           if(p == os.length()) pt= 0; // append to end of string
00203           else pt= &(((const char*)os)[p]);
00204         }
00205     public:
00206       void operator=(const slip& s)
00207         {
00208           if(&str == &s){ // potentially overlapping
00209             std::string tmp((const char*)s);
00210             str.insert(pos, len, tmp.c_str(), tmp.length());
00211           }
00212           else str.insert(pos, len, (const char*)s, s.length());
00213         }
00214       
00215       void operator=(const substring& s)
00216         {
00217           if(&str == &s.str){ // potentially overlapping
00218             std::string tmp(s.pt, s.len);
00219             str.insert(pos, len, tmp.c_str(), tmp.length());
00220           }
00221           else str.insert(pos, len, s.pt, s.len);
00222         }
00223       
00224       void operator=(const char *s)
00225         {
00226           str.insert(pos, len, s, strlen(s));
00227         }
00228     };
00229 
00230   substring slip::substr(const Range& r)
00231     {
00232       return substr(r.start(), r.length());
00233     }
00234   
00238   class llip : public std::deque<slip>
00239     {
00240     public:
00241       llip(int sz=6) {};
00242       llip(const llip& sl) : std::deque<slip>(sl) {};
00243       
00245       slip join(const char *pat= " ");
00246       
00247       int split(const char *str, const char *pat= "[ \t\n]+", int limit= -1);
00248       
00250       int length() const { return size(); }
00252       int count() const { return size(); }
00254       void push(const slip& s) { this->push_back(s); }
00256       void push(const llip& sl);
00258       void unshift(const slip& s) { this->push_front(s); }
00260       void unshift(const llip& sl);
00261       
00263       operator const int() const { return size(); }
00264       
00266       slip shift() { slip s = this->front(); this->pop_front(); return s; }
00267       
00269       slip pop() { slip s = this->back(); this->pop_back(); return s; }
00270       
00271       void replace(int offset, int len, llip& l);
00272       llip& splice(int offset, int len);
00273       llip& splice(int offset);
00274       
00276       llip grep(const char *regex, const char *options = "");
00277     };
00278 
00279   slip join(llip&, const char *pat=" ");
00280   void push(llip& sl, const slip& s);
00281 
00287   slip slipprintf(const char *, ...);
00288 
00294   slip slipvprintf(const char *, va_list args);
00295 
00305   int slip_read_file(slip filename,
00306                      // output
00307                      slip& contents);
00308 };
00309 
00310 //std::istream& operator>>(std::istream&, plis::slip&);
00311 //std::ostream& operator<<(std::ostream&, const plis::slip&);
00312 //std::ostream& operator<<(std::ostream&, const plis::llip&);
00313 
00314 #define FOREACH(var, array)\
00315   for (int i=0; i<array.count() && (var=array[i],1);i++) 
00316 
00317 #endif
00318 

plis - Perl Like Structures