Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

ustr.hpp

00001 /* ==================================================== ======== ======= *
00002  *
00003  *  ustr.hpp
00004  *  Ubit Project  [Elc][2003]
00005  *  Author: Eric Lecolinet
00006  *
00007  *  Part of the Ubit Toolkit: A Brick Construction Game Model for Creating GUIs
00008  *
00009  *  (C) 1999-2003 Eric Lecolinet @ ENST Paris
00010  *  WWW: http://www.enst.fr/~elc/ubit   Email: elc@enst.fr (subject: ubit)
00011  *
00012  * ***********************************************************************
00013  * COPYRIGHT NOTICE : 
00014  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE 
00015  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 
00016  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU 
00017  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; 
00018  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
00019  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
00020  * ***********************************************************************
00021  *
00022  * ==================================================== [Elc:03] ======= *
00023  * ==================================================== ======== ======= */
00024 
00025 #ifndef _ustr_hpp_
00026 #define _ustr_hpp_
00027 //pragma ident  "@(#)ustr.hpp   ubit:03.06.04"
00028 #include <ubit/uelem.hpp>
00029 
00030 /* ==================================================== ======== ======= */
00038 class UStr: public UElem {
00039 public:
00040   static const unsigned int npos = static_cast<unsigned int>(-1);
00041   static UStr none, newline;
00042   
00043   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00044 
00045   UStr();
00046   UStr(const char*);
00047   UStr(const UStr&);
00048   UStr(const std::string&);
00049   UStr(const UIntgBase&);
00050   UStr(const UFloatBase&);
00056   friend UStr& ustr()                       {return *new UStr();}
00057   friend UStr& ustr(const char* _s)         {return *new UStr(_s);}
00058   friend UStr& ustr(const UStr& _s)         {return *new UStr(_s);}
00059   friend UStr& ustr(const std::string& _s)  {return *new UStr(_s);}
00061 
00062   virtual ~UStr();
00064 
00065   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00066 
00067   virtual void clear();
00069 
00070   virtual bool empty() const {return (len == 0);}
00072 
00073   int length() const {return len;}
00075 
00076   friend std::ostream& operator<<(std::ostream&, const UStr&);
00078 
00079   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00080 
00081   UStr& operator=(const char* s2)         {set(s2); return *this;}
00082   UStr& operator=(const UStr& s2)         {set(s2); return *this;}
00083   UStr& operator=(const std::string& s2)  {set(s2); return *this;}
00084   UStr& operator=(const UIntgBase& v2)    {set(v2); return *this;}
00085   UStr& operator=(const UFloatBase& v2)   {set(v2); return *this;}
00087 
00088   /* ces operateurs sont ambigus a cause de char* dans le cas: str = 0
00089      UStr& operator=(int v2)                 {set(v2); return *this;}
00090      UStr& operator=(double v2)              {set(v2); return *this;}
00091   */
00092 
00093   virtual void set(const char* s2);
00094   virtual void set(const UStr& s2);
00095   virtual void set(const std::string& s2);
00096   virtual void set(const UIntgBase& v2);
00097   virtual void set(const UFloatBase& v2);
00098   virtual void setNum(int v2);
00099   virtual void setNum(double v2);
00101 
00102   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00103 
00104   bool operator==(const char* s2) const        {return equals(s2);}
00105   bool operator==(const UStr& s2) const        {return equals(s2);}
00106   bool operator==(const std::string& s2) const {return equals(s2);}
00107 
00108   virtual bool equals(const char*) const;
00109   virtual bool equals(const std::string&) const;
00110   virtual bool equals(const UStr&) const;
00112 
00113   virtual int compare(const char*) const;
00114   virtual int compare(const std::string&) const;
00115   virtual int compare(const UStr&) const;
00116   
00117   virtual int compareTo(const char*, bool ignore_case = false) const;
00118   virtual int compareTo(const std::string&, bool ignore_case = false) const;
00119   virtual int compareTo(const UStr&, bool ignore_case = false) const;
00125   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00126 
00127   const char* chars() const {return s;}
00128   const char* c_str() const {return s;}
00130 
00131   virtual char charAt(int pos) const;
00132   virtual char at(int pos) const {return charAt(pos);}
00139   virtual char setCharAt(int pos, char c, bool update = true);
00146   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00147 
00148   virtual bool copyTo(UStr& to, int pos = 0,
00149                       unsigned int nbchars = npos) const;
00150   virtual bool copyTo(std::string& to, int pos = 0,
00151                       unsigned int nbchars = npos) const;
00159   virtual UStr substring(int from_pos = 0, unsigned int nbchars = npos) const;
00160   virtual std::string toString(int from_pos = 0, unsigned int nbchars = npos) const;
00166   virtual UStr split(int pos, bool delete_char_at_pos = false);
00168   
00169   virtual void trim(bool strip_beginning = true, bool strip_end = true);
00170   virtual void strip(bool strip_beginning = true, bool strip_end = true);
00172   
00173   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00174 
00175   int find(char);
00176   int rfind(char);
00177   int find(const char*);
00178   int find(const UStr&);
00179   int find(const std::string&);
00181   
00182   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00183 
00184   friend UStr operator&(const UStr& s1, const UStr& s2);
00185   friend UStr operator&(const UStr& s1, const char* s2);
00186   friend UStr operator&(const char* s1, const UStr& s2);
00187   friend UStr operator&(const UStr& s1, const std::string& s2);
00188   friend UStr operator&(const std::string& s1, const UStr& s2);
00189   friend UStr operator&(const UStr&, int);
00190   friend UStr operator&(const UStr&, float);
00191   friend UStr operator&(const UStr&, const UIntgBase&);
00192   friend UStr operator&(const UStr&, const UFloatBase&);
00201   UStr& operator&=(const UStr& s2)        {append(s2); return *this;}
00202   UStr& operator&=(const char* s2)        {append(s2); return *this;}
00203   UStr& operator&=(const std::string& s2) {append(s2); return *this;}
00204   UStr& operator&=(int v2)                {append(v2); return *this;}
00205   UStr& operator&=(float v2)              {append(v2); return *this;}
00206   UStr& operator&=(const UIntgBase& v2)   {append(v2); return *this;}
00207   UStr& operator&=(const UFloatBase& v2)  {append(v2); return *this;}
00216   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00217 
00218   virtual void append(char);
00219   virtual void append(const char*);
00220   virtual void append(const char*, unsigned int nbchars);
00221   virtual void append(const UStr&);
00222   virtual void append(const UStr&, unsigned int nbchars);
00223   virtual void append(const std::string&);
00224   virtual void append(const std::string&, unsigned int nbchars);
00225   virtual void append(int);
00226   virtual void append(float);
00227   virtual void append(const UIntgBase&);
00228   virtual void append(const UFloatBase&);
00230 
00231   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00232 
00233   virtual bool insert(int to_pos, char);
00234   virtual bool insert(int to_pos, const char* from,
00235                       int from_pos = 0, unsigned int from_nbchars = npos);
00236   virtual bool insert(int to_pos, const UStr& from,
00237                       int from_pos = 0, unsigned int from_nbchars = npos);
00238   virtual bool insert(int to_pos, const std::string& from,
00239                       int from_pos = 0, unsigned int from_nbchars = npos);
00247   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00248 
00249   virtual void remove(int pos, unsigned int nbchars);
00256   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00257 
00258   virtual void replace(int pos, unsigned int nbchars, const UStr&);
00259   virtual void replace(int pos, unsigned int nbchars, const char*);
00260   virtual void replace(int pos, unsigned int nbchars, const std::string&);
00262 
00263   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00264   // file name manipulation
00265 
00266   virtual UStr getFileName(bool with_suffix = true) const;
00268 
00269   virtual UStr getFileSuffix() const;
00271 
00272   virtual UStr getFileDir(bool with_final_slash = true) const;
00274 
00275   virtual UStr getFilePath(const UStr& new_suffix) const;
00277 
00278   virtual long getFileType() const;
00279   virtual long getFileType(bool& is_regular_file, bool& is_directory) const;
00281 
00282   virtual bool isFileFound(bool is_regular_file = true) const;
00284 
00285   virtual int readFile(const UStr& pathname);
00287 
00288   static int readFile(const UStr& filename, std::vector<UStr*>&slist,
00289                        bool strip_beg_line = false, bool strip_end_line = false);
00291 
00292   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00293 
00294   virtual void changed(bool update_now);
00303   virtual void update();
00305   
00306   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00307   // implementation
00308 
00309 #ifndef NO_DOC
00310   UStr(const char*, u_modes bmodes);
00311 
00312   virtual class UStr* strCast() {return this;}
00313   virtual void syncVals(char*,int);
00314 
00315   virtual void addingTo(class ULink *selflink, UGroup *parent);
00316   virtual void removingFrom(class ULink *selflink, UGroup *parent);
00318 
00319   virtual bool checkFormat(int pos, int newchar);
00320   virtual bool checkFormat(int pos, const char*);
00322 
00323   virtual bool setImpl(const char*, int len, bool call_callbacks);
00324   virtual bool insertImpl(int to_pos, char newchar, bool upd);
00325   virtual bool insertImpl(int to_pos, const char* s2, int from_pos,
00326                           unsigned int from_len, bool upd);
00327   virtual bool replaceImpl(int pos, unsigned int nbchars,
00328                            const char* s2, bool upd);
00329   
00330   // prototype for non-warped text (UView)
00331   virtual void getSize(class UContext*, u_dim *w, u_dim *h) const;
00332 
00333   // prototype for warped text (UFlowView)
00334   virtual void getSize(class UContext*, u_dim *w, u_dim *h, 
00335                        u_dim available_width, int offset, 
00336                        int *sublen, int *change_line) const;
00337 
00338   // prototype for non-warped text (UView)
00339   virtual void paint(class UWinGraph&, class UContext*, const URegion &r) const;
00340 
00341   // prototype for warped text (UFlowView)
00342   virtual void paint(class UWinGraph&, class UContext*, const URegion &r,
00343                      int offset, int cellen) const;
00344 private:
00345   char* s;
00346   int len;  
00347 #endif
00348 };
00349 
00350 /* === useful functions =============================== ======== ======= */
00351 
00352 struct CStr {
00353   static char *strdup(const char *s1);
00355 
00356   static char *strdupcat(const char *s1, const char *s2);
00358 
00359   static char *strdupcat(const char *s1, char sep, const char *s2);
00361 
00362   static char *strext(char *pathname);
00363   static const char *strext(const char *pathname);
00369   static void strparse(char* path_name, int path_len,
00370                 char*& dir_name, int& dir_len,
00371                 char*& base_name, int& base_len,
00372                 char*& suffix, int& suffix_len);
00376 };
00377 
00378 #endif
00379 /* ==================================================== [TheEnd] ======= */
00380 /* ==================================================== [Elc:03] ======= */

Generated on Thu May 19 11:39:59 2005 for Ubit[Eric.Lecolinet@enst.fr] by  doxygen 1.4.2