ubit
ustr.hpp
1 /************************************************************************
2  *
3  * ustr.hpp: active strings (can have callbacks)
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 | Eric Lecolinet | TELECOM ParisTech | http://www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 
17 #ifndef _ustr_hpp_
18 #define _ustr_hpp_ 1
19 #include <ubit/udata.hpp>
20 namespace ubit {
21 
72  class UStr: public UData {
73  public:
74  UCLASS(UStr)
75  static const unsigned int npos = static_cast<unsigned int>(-1);
76 
77  virtual int getNodeType() const {return TEXT_NODE;}
78  virtual const UStr& getNodeName() const;
79  virtual UStr getNodeValue() const {return *this;}
80  virtual const UStr& getData() const {return *this;}
81 
82  virtual UStr* toStr() {return this;}
83  virtual const UStr* toStr() const {return this;}
84 
85  virtual UStr& onChange(UCall& c);
90  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
91 
92  UStr();
94 
95  UStr(const char*);
97 
98  UStr(const UStr&);
100 
101  UStr(const std::string&);
103 
104  virtual ~UStr();
105 
106  int length() const {return len;}
108 
109  bool empty() const {return (len == 0);}
111 
112  bool isEmpty() const {return (len == 0);}
114 
115  virtual UStr& clear();
117 
118  virtual UStr& operator=(const char*);
119  virtual UStr& operator=(const UStr&);
120  virtual UStr& operator=(const std::string&);
121 
122  UStr& setInt(int); // NB: a numeral, such as 111 is an int
123  UStr& setLong(long);
124  UStr& setFloat(float);
125  UStr& setDouble(double); // NB: un numeral 1.1 est double
126 
127  int toInt() const;
128  long toLong() const;
129  float toFloat() const;
130  double toDouble() const;
131 
132  int scan(const char* format, ...) const;
133 
134  bool scanValue(float& val, UStr& unit) const;
135  bool scanValue(float& val, const char*& unit) const;
136 
137  const char* c_str() const {return s;}
139 
140  char at(int pos) const;
146  char operator[](int pos) const;
148 
149  char setCharAt(int pos, char c, bool update = true);
156  friend std::ostream& operator<<(std::ostream&, const UStr&);
158 
159  friend std::istream& operator>>(std::istream&, UStr&);
161 
162  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
163  // comparisons
164 
165  bool operator==(const char* s2) const {return equals(s2);}
166  bool operator==(const UStr& s2) const {return equals(s2);}
167  bool operator==(const std::string& s2) const {return equals(s2);}
168 
169  bool operator!=(const char* s2) const {return !equals(s2);}
170  bool operator!=(const UStr& s2) const {return !equals(s2);}
171  bool operator!=(const std::string& s2) const {return !equals(s2);}
172 
173  virtual bool equals(const char*, bool ignore_case =false) const;
175 
176  virtual bool equals(const UStr&, bool ignore_case =false) const;
178 
179  virtual bool equals(const std::string&, bool ignore_case =false) const;
181 
182  virtual int compare(const char*, bool ignore_case =false) const;
184 
185  virtual int compare(const UStr&, bool ignore_case =false) const;
187 
188  virtual int compare(const std::string&, bool ignore_case =false) const;
190 
191  virtual int compareN(const char*, unsigned int n, bool ignore_case =false) const;
193 
194  virtual int compareN(const UStr&, unsigned int n, bool ignore_case =false) const;
196 
197  virtual int compareN(const std::string&, unsigned int n, bool ignore_case=false) const;
199 
200  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
201 
202  virtual int find(char s, int pos = 0) const;
204 
205  virtual int find(const char* s, int pos = 0) const;
207 
208  virtual int find(const UStr& s, int pos = 0) const;
210 
211  virtual int find(const std::string& s, int pos = 0) const;
213 
214  virtual int rfind(char s) const;
216 
217  UStr substring(int from_pos=0, unsigned int nbchars = npos) const;
219 
220  std::string toString(int pos=0, unsigned int nbchars = npos) const;
226  bool toString(std::string& to, int pos=0, unsigned int nbchars = npos)const;
232  UStr toUpper() const;
234 
235  UStr toLower() const;
237 
238  void upper();
240 
241  void lower();
243 
244  void capitalize();
246 
247  UStr split(int pos, bool delete_char_at_pos = false);
253  void trim(bool trim_beginning = true, bool trim_end = true);
255 
256  int tokenize(std::vector<UStr*>& tokens, const UStr& separators,
257  bool trim_begin = true, bool trim_end = true) const;
271  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
272 
273  virtual void append(char);
274  virtual void append(const char*);
275  virtual void append(const char*, unsigned int nbchars);
276  virtual void append(const UStr&);
277  virtual void append(const UStr&, unsigned int nbchars);
278  virtual void append(const std::string&);
279  virtual void append(const std::string&, unsigned int nbchars);
280  virtual void append(int);
281  virtual void append(double);
282 
283  UStr& operator&=(const char c) {append(c); return *this;}
284  UStr& operator&=(const char* s2) {append(s2); return *this;}
285  UStr& operator&=(const UStr& s2) {append(s2); return *this;}
286  UStr& operator&=(const std::string& s2) {append(s2); return *this;}
287  UStr& operator&=(int i) {append(i); return *this;}
288  UStr& operator&=(double d) {append(d); return *this;}
294  friend UStr operator&(const UStr& s1, const UStr& s2);
295  friend UStr operator&(const UStr& s1, const char* s2);
296  friend UStr operator&(const char* s1, const UStr& s2);
297  friend UStr operator&(const UStr& s1, const std::string& s2);
298  friend UStr operator&(const std::string& s1, const UStr& s2);
299  friend UStr operator&(const UStr&, int);
300  friend UStr operator&(int, const UStr&);
301  friend UStr operator&(const UStr&, double);
302  friend UStr operator&(double, const UStr&);
308  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
309 
310  virtual bool insert(int to_pos, char);
311  virtual bool insert(int to_pos, const char* from,
312  int from_pos = 0, unsigned int from_nbchars = npos);
313  virtual bool insert(int to_pos, const UStr& from,
314  int from_pos = 0, unsigned int from_nbchars = npos);
315  virtual bool insert(int to_pos, const std::string& from,
316  int from_pos = 0, unsigned int from_nbchars = npos);
324  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
325 
326  virtual void remove(int pos, unsigned int nbchars);
333  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
334 
335  virtual void replace(int pos, unsigned int nbchars, const UStr&);
336  virtual void replace(int pos, unsigned int nbchars, const char*);
337  virtual void replace(int pos, unsigned int nbchars, const std::string&);
339 
340  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
341  // filenames.
342 
343  virtual UStr basename(bool with_suffix = true) const;
349  virtual UStr dirname(bool with_final_slash = true) const;
355  virtual UStr suffix() const;
357 
358  virtual UStr expand() const;
360 
361  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
362  // I/O.
363 
364  virtual int read(const UStr& pathname);
370  virtual int write(const UStr& pathname);
376  static int readLines(const UStr& filename, std::vector<UStr*>& lines,
377  bool trim_beginning = true, bool trim_end = true);
384  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
385  // impl.
386 
387  virtual void changed(bool update = true);
393  virtual void update();
395 
396  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
397  // implementation
398 
399 #ifndef NO_DOC
400  UStr(const char*, UConst);
402 
403  virtual void syncVals(char*,int);
404 
405  virtual void addingTo(UChild&, UElem& parent);
406  virtual void removingFrom(UChild&, UElem& parent);
408 
409  virtual bool checkFormat(int pos, int newchar);
410  virtual bool checkFormat(int pos, const char*);
412 
413  virtual void initImpl(const char*, int len);
414  virtual void setImpl(const char*, int len);
415  virtual void setImplNoCopy(char *_s, int _len);
416  virtual bool insertImpl(int to_pos, char newchar, bool upd);
417  virtual bool insertImpl(int to_pos, const char* s2, int from_pos,
418  unsigned int from_len, bool upd);
419  virtual bool replaceImpl(int pos, unsigned int nbchars,
420  const char* s2, bool upd);
421 
422  // prototype for non-warped text (UView)
423  virtual void getSize(UUpdateContext&, UDimension&) const;
424 
425  // prototype for warped text (UFlowview)
426  virtual void getSize(UUpdateContext&, UDimension&, float available_width,
427  int offset, int& str_sublen, int& change_line) const;
428 
429  // prototype for non-warped text (UView)
430  virtual void paint(UGraph&, UUpdateContext&, const URect&) const;
431 
432  // prototype for warped text (UFlowview)
433  virtual void paint(UGraph&, UUpdateContext&, const URect&, int offset, int cellen) const;
434  private:
435  char* s;
436  int len;
437 #endif
438  };
439 
440 
441  std::ostream& operator<<(std::ostream&, const UStr&);
443 
444  std::istream& operator>>(std::istream&, UStr&);
446 
447  inline UStr& ustr() {return *new UStr();}
449 
450  inline UStr& ustr(const char* s) {return *new UStr(s);}
452 
453  inline UStr& ustr(const UStr& s) {return *new UStr(s);}
455 
456  inline UStr& ustr(const std::string& s) {return *new UStr(s);}
458 
459 
460  /* === useful functions =============================== ======== ======= */
464  struct UCstr {
465  static char* dup(const char* s);
467 
468  static char* dup(const char* s1, const char* s2);
470 
471  static char* dup(const char* s1, char sep, const char* s2);
473 
474  static bool equals(const char* s1, const char* s2, bool ignore_case =false);
476 
477  static int compare(const char* s1, const char* s2, bool ignore_case =false);
479 
480  static int compareN(const char* s1, const char* s2, unsigned int n, bool ignore_case =false);
482 
483  static const char* suffix(const char *pathname);
489  static void parsePath(char* path_name, int path_len,
490  char*& dir_name, int& dir_len,
491  char*& base_name, int& base_len,
492  char*& suffix, int& suffix_len);
496  };
497 
498 }
499 #endif
virtual void changed(bool update=true)
[impl] called when object&#39;s content is changed.
Definition: ustr.cpp:444
useful functions for C strings.
Definition: ustr.hpp:464
2D Dimension.
Definition: ugeom.hpp:55
char at(int pos) const
returns the character at this position.
Definition: ustr.cpp:467
Definition: uobject.hpp:282
bool isEmpty() const
returns true if the string is empty.
Definition: ustr.hpp:112
class for drawing on widgets.
Definition: ugraph.hpp:44
2D Rectangle.
Definition: ugeom.hpp:165
virtual UStr & clear()
clears the string&#39;s content.
Definition: ustr.cpp:186
UStr toLower() const
returns a lowercase copy.
Definition: ustr.cpp:389
virtual bool equals(const char *, bool ignore_case=false) const
checks if string contents are the same, ignores case if last argument is true.
virtual int find(char s, int pos=0) const
search for the first occurrence of &#39;s&#39; starting at &#39;pos&#39;, return -1 if not found. ...
virtual UStr * toStr()
dynamic cast: returns this object if it derives from UStr and null otherwise.
Definition: ustr.hpp:82
virtual UStr suffix() const
returns the suffix of a pathname.
Definition: ustr.cpp:1047
virtual const UStr * toStr() const
dynamic cast: returns this object if it derives from UStr and null otherwise.
Definition: ustr.hpp:83
virtual int write(const UStr &pathname)
writes the content of this UStr in this file.
Definition: ustr.cpp:1141
friend std::istream & operator>>(std::istream &, UStr &)
reads the string from an input stream (eg. cin >> str).
virtual int compareN(const char *, unsigned int n, bool ignore_case=false) const
compare the N first chars of strings, ignores case if last argument is true.
Base class for Viewable Objects.
Definition: udata.hpp:31
virtual int rfind(char s) const
search for the last occurrence of &#39;s&#39;, return -1 if not found.
Definition: ustr.cpp:895
virtual void removingFrom(UChild &, UElem &parent)
NOTE that this function require a specific destructor.
Definition: ustr.cpp:128
int tokenize(std::vector< UStr *> &tokens, const UStr &separators, bool trim_begin=true, bool trim_end=true) const
parses this UStr and stores the tokens separated by a separator in the vector.
Definition: ustr.cpp:814
lightweight general purpose container.
Definition: uelem.hpp:44
virtual void addingTo(UChild &, UElem &parent)
called when this object is added to a parent.
Definition: ustr.cpp:111
friend std::ostream & operator<<(std::ostream &, const UStr &)
prints the string on an output stream (e.g. cout << str).
std::string toString(int pos=0, unsigned int nbchars=npos) const
UStr to std::string conversion.
static int readLines(const UStr &filename, std::vector< UStr *> &lines, bool trim_beginning=true, bool trim_end=true)
reads this file and store its lines as a UStr vector.
Definition: ustr.cpp:1121
virtual UStr basename(bool with_suffix=true) const
returns the base name of a pathname (omitting the directory).
Definition: ustr.cpp:1017
base class of callback objects for firing functions or methods.
Definition: ucall.hpp:144
UStr toUpper() const
returns a upercase copy.
Definition: ustr.cpp:382
char operator[](int pos) const
same as at(int).
Definition: ustr.cpp:461
UStr()
creates a new empty string;
Definition: ustr.cpp:59
int length() const
returns the char count of the string.
Definition: ustr.hpp:106
void trim(bool trim_beginning=true, bool trim_end=true)
removes white spaces, tabs and newlines at the beginning and/or the end of this UStr.
Definition: ustr.cpp:784
virtual void update()
updates grahics.
Definition: ustr.cpp:437
Definition: uupdatecontext.hpp:32
Definition: uhardfont.hpp:31
void capitalize()
capitalizes this string.
Definition: ustr.cpp:406
char setCharAt(int pos, char c, bool update=true)
changes the character at offset &#39;pos&#39;.
Definition: ustr.cpp:473
UStr substring(int from_pos=0, unsigned int nbchars=npos) const
returns a substring.
Definition: ustr.cpp:776
const char * c_str() const
returns an unmutable C string.
Definition: ustr.hpp:137
virtual UStr & onChange(UCall &c)
adds a callback that is fired when the string is modified.
Definition: ustr.cpp:49
virtual int getNodeType() const
returns the XML node type.
Definition: ustr.hpp:77
void lower()
converts to lowercase characters.
Definition: ustr.cpp:401
virtual bool replaceImpl(int pos, unsigned int nbchars, const char *s2, bool upd)
Definition: ustr.cpp:697
UStr split(int pos, bool delete_char_at_pos=false)
splits this UStr removes chars after &#39;pos&#39; in this UStr and returns the remaining chars in another US...
Definition: ustr.cpp:849
bool empty() const
returns true if the string is empty.
Definition: ustr.hpp:109
virtual int compare(const char *, bool ignore_case=false) const
compare strings lexicographically, ignores case if last argument is true.
virtual int read(const UStr &pathname)
reads this file and loads its content into this UStr.
Definition: ustr.cpp:1073
Ubit String.
Definition: ustr.hpp:72
virtual const UStr & getNodeName() const
return the XML node name.
Definition: ustr.cpp:47
virtual UStr expand() const
expands a pathname that contains a ~ into an absolute path.
Definition: ustr.cpp:1058
void upper()
converts to uppercase characters.
Definition: ustr.cpp:396
virtual UStr dirname(bool with_final_slash=true) const
returns the directory of a pathname (omitting the file name).
Definition: ustr.cpp:1031
[impl] Internal implementation of a child node.
Definition: uchild.hpp:23
virtual UStr getNodeValue() const
return the XML node value.
Definition: ustr.hpp:79