00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _ustr_hpp_
00021 #define _ustr_hpp_
00022
00023 #include <ubit/udata.hpp>
00024 namespace ubit {
00025
00026
00077 class UStr: public UData {
00078 public:
00079 UBIT_DATA_ELEMENT_CLASS(UStr, UData)
00080
00081 static const unsigned int npos = static_cast<unsigned int>(-1);
00082 static UStr none, newline;
00083
00084 virtual unsigned short getNodeType() const {return TEXT_NODE;}
00085 virtual const UStr* getNodeName() const;
00086 virtual const UStr* getNodeValue() const {return this;}
00087 virtual const UStr* getData() const {return this;}
00088
00089 virtual UStr& onChange(UCall& c);
00094
00095
00096
00097 UStr();
00098 UStr(const UStr&);
00099 UStr(const char*);
00100 UStr(const std::string&);
00101 UStr(const UInteger&);
00102 UStr(const UFloat&);
00103 UStr(const UDouble&);
00108 friend UStr& ustr() {return *new UStr();}
00109 friend UStr& ustr(const UStr& _s) {return *new UStr(_s);}
00110 friend UStr& ustr(const char* _s) {return *new UStr(_s);}
00111 friend UStr& ustr(const std::string& _s) {return *new UStr(_s);}
00113
00114 virtual ~UStr();
00116
00117
00118
00119 virtual bool empty() const {return (len == 0);}
00120 virtual bool isEmpty() const {return (len == 0);}
00122
00123 int length() const {return len;}
00125
00126 friend std::ostream& operator<<(std::ostream&, const UStr&);
00128
00129 friend std::istream& operator>>(std::istream&, UStr&);
00131
00132
00133
00134
00135 bool operator==(const char* s2) const {return equals(s2);}
00136 bool operator==(const UStr& s2) const {return equals(s2);}
00137 bool operator==(const std::string& s2) const {return equals(s2);}
00139
00140 bool operator!=(const char* s2) const {return !equals(s2);}
00141 bool operator!=(const UStr& s2) const {return !equals(s2);}
00142 bool operator!=(const std::string& s2) const {return !equals(s2);}
00144
00145 virtual bool equals(const char*, bool ignore_case = false) const;
00146 virtual bool equals(const UStr&, bool ignore_case = false) const;
00147 virtual bool equals(const std::string&, bool ignore_case = false) const;
00149
00150 virtual int compare(const char*, bool ignore_case = false) const;
00151 virtual int compare(const UStr&, bool ignore_case = false) const;
00152 virtual int compare(const std::string&, bool ignore_case = false) const;
00154
00155 virtual int compareN(const char*, unsigned int n, bool ignore_case = false) const;
00156 virtual int compareN(const UStr&, unsigned int n, bool ignore_case = false) const;
00157 virtual int compareN(const std::string&, unsigned int n, bool ignore_case = false) const;
00159
00160
00161
00162 virtual int find(char s, int pos = 0) const;
00163 virtual int find(const UStr& s, int pos = 0) const;
00164 virtual int find(const char* s, int pos = 0) const;
00165 virtual int find(const std::string& s, int pos = 0) const;
00170 virtual int rfind(char s) const;
00175
00176
00177 UStr& operator=(const char* s2) {set(s2); return *this;}
00178 UStr& operator=(const UStr& s2) {set(s2); return *this;}
00179 UStr& operator=(const std::string& s2) {set(s2); return *this;}
00180 UStr& operator=(const UInteger& v2) {set(v2); return *this;}
00181 UStr& operator=(const UFloat& v2) {set(v2); return *this;}
00182 UStr& operator=(const UDouble& v2) {set(v2); return *this;}
00184
00185 virtual void clear();
00187
00188 virtual void set(const char*);
00189 virtual void set(const UStr&);
00190 virtual void set(const std::string&);
00191 virtual void set(const UInteger&);
00192 virtual void set(const UFloat&);
00193 virtual void set(const UDouble&);
00195
00196 virtual void setInt(int);
00197 void setLong(long);
00198 virtual void setFloat(float);
00199 virtual void setDouble(double);
00201
00202 virtual int toInt() const;
00203 virtual long toLong() const;
00204 virtual float toFloat() const;
00205 virtual double toDouble() const;
00207
00208 std::string toString(int pos=0, unsigned int nbchars = npos) const;
00209 bool toString(std::string& to, int pos=0, unsigned int nbchars = npos)const;
00215 UStr substring(int from_pos=0, unsigned int nbchars = npos) const;
00217
00218 virtual UStr toUpper() const;
00219 virtual UStr toLower() const;
00221
00222 void upper();
00223 void lower();
00224 void capitalize();
00226
00227 UStr split(int pos, bool delete_char_at_pos = false);
00233 void trim(bool trim_beginning = true, bool trim_end = true);
00235
00236 int tokenize(std::vector<UStr*>& tokens, const UStr& separators,
00237 bool trim_begin = true, bool trim_end = true) const;
00251
00252
00253 const char* chars() const {return s;}
00254 const char* c_str() const {return s;}
00256
00257 char charAt(int pos) const;
00258 char at(int pos) const {return charAt(pos);}
00259 char operator[](int pos) const {return charAt(pos);}
00266 char setCharAt(int pos, char c, bool update = true);
00273
00274
00275 virtual void append(char);
00276 virtual void append(const char*);
00277 virtual void append(const char*, unsigned int nbchars);
00278 virtual void append(const UStr&);
00279 virtual void append(const UStr&, unsigned int nbchars);
00280 virtual void append(const std::string&);
00281 virtual void append(const std::string&, unsigned int nbchars);
00282 virtual void append(int);
00283 virtual void append(double);
00284 virtual void append(const UInteger&);
00285 virtual void append(const UDouble&);
00287
00288 UStr& operator&=(const char c) {append(c); return *this;}
00289 UStr& operator&=(const char* s2) {append(s2); return *this;}
00290 UStr& operator&=(const UStr& s2) {append(s2); return *this;}
00291 UStr& operator&=(const std::string& s2) {append(s2); return *this;}
00292 UStr& operator&=(int i) {append(i); return *this;}
00293 UStr& operator&=(double d) {append(d); return *this;}
00294 UStr& operator&=(const UInteger& i) {append(i); return *this;}
00295 UStr& operator&=(const UDouble& d) {append(d); return *this;}
00301 friend UStr operator&(const UStr& s1, const UStr& s2);
00302 friend UStr operator&(const UStr& s1, const char* s2);
00303 friend UStr operator&(const char* s1, const UStr& s2);
00304 friend UStr operator&(const UStr& s1, const std::string& s2);
00305 friend UStr operator&(const std::string& s1, const UStr& s2);
00306 friend UStr operator&(const UStr&, int);
00307 friend UStr operator&(int, const UStr&);
00308 friend UStr operator&(const UStr&, double);
00309 friend UStr operator&(double, const UStr&);
00310 friend UStr operator&(const UStr&, const UInteger&);
00311 friend UStr operator&(const UInteger&, const UStr&);
00312 friend UStr operator&(const UStr&, const UDouble&);
00313 friend UStr operator&(const UDouble&, const UStr&);
00319
00320
00321 virtual bool insert(int to_pos, char);
00322 virtual bool insert(int to_pos, const char* from,
00323 int from_pos = 0, unsigned int from_nbchars = npos);
00324 virtual bool insert(int to_pos, const UStr& from,
00325 int from_pos = 0, unsigned int from_nbchars = npos);
00326 virtual bool insert(int to_pos, const std::string& from,
00327 int from_pos = 0, unsigned int from_nbchars = npos);
00335
00336
00337 virtual void remove(int pos, unsigned int nbchars);
00344
00345
00346 virtual void replace(int pos, unsigned int nbchars, const UStr&);
00347 virtual void replace(int pos, unsigned int nbchars, const char*);
00348 virtual void replace(int pos, unsigned int nbchars, const std::string&);
00350
00351
00352
00353
00354 virtual UStr basename(bool with_suffix = true) const;
00360 virtual UStr dirname(bool with_final_slash = true) const;
00366 virtual UStr suffix() const;
00368
00369 virtual UStr expand() const;
00371
00372
00373
00374
00375 virtual int read(const UStr& pathname);
00381 virtual int write(const UStr& pathname);
00387 static int readLines(const UStr& filename, std::vector<UStr*>& lines,
00388 bool trim_beginning = true, bool trim_end = true);
00395
00396
00397
00398 virtual void changed(bool update = true);
00404 virtual void update();
00406
00407
00408
00409
00410 #ifndef NO_DOC
00411 UStr(const char*, UMask bmodes);
00412
00413 virtual class UStr* strCast() {return this;}
00414 virtual void syncVals(char*,int);
00415
00416 virtual void addingTo(UChild *self, UGroup *parent);
00417 virtual void removingFrom(UChild* self, UGroup *parent);
00419
00420 virtual bool checkFormat(int pos, int newchar);
00421 virtual bool checkFormat(int pos, const char*);
00423
00424 virtual void initImpl(const char*, int len);
00425 virtual void setImpl(const char*, int len);
00426 virtual void setImplNoCopy(char *_s, int _len);
00427 virtual bool insertImpl(int to_pos, char newchar, bool upd);
00428 virtual bool insertImpl(int to_pos, const char* s2, int from_pos,
00429 unsigned int from_len, bool upd);
00430 virtual bool replaceImpl(int pos, unsigned int nbchars,
00431 const char* s2, bool upd);
00432
00433
00434 virtual void getSize(UContext&, int& w, int& h) const;
00435
00436
00437 virtual void getSize(UContext&, int& w, int& h,
00438 int available_width, int offset,
00439 int& sublen, int& change_line) const;
00440
00441
00442 virtual void paint(UGraph&, UContext&, const URegion&) const;
00443
00444
00445 virtual void paint(UGraph&, UContext&, const URegion&,
00446 int offset, int cellen) const;
00447 private:
00448 char* s;
00449 int len;
00450 #endif
00451 };
00452
00453
00457 struct UCstr {
00458 static char* dup(const char* s);
00460
00461 static char* dup(const char* s1, const char* s2);
00463
00464 static char* dup(const char* s1, char sep, const char* s2);
00466
00467 static int strcasecmp(const char* s1, const char* s2);
00469
00470 static int strncasecmp(const char* s1, const char* s2, unsigned int n);
00472
00473
00474
00475 static const char* suffix(const char *pathname);
00481 static void parsePath(char* path_name, int path_len,
00482 char*& dir_name, int& dir_len,
00483 char*& base_name, int& base_len,
00484 char*& suffix, int& suffix_len);
00488 };
00489 }
00490 #endif
00491
00492