commore  1.0.6-SNAPSHOT
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StringBuffer.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2014 Raphael David / CANTOR
3 //
4 
5 
6 #ifndef CMR_STRING_BUFFER_INCLUDED
7 #define CMR_STRING_BUFFER_INCLUDED
8 
9 #include "Commore.h"
10 #include "AString.h"
11 
12 
13 namespace commore
14 {
16 
22  {
23  public:
24  struct Page;
25  typedef Page* PPage;
26 
31  {
32  public :
34  : current_(0) {}
36  : current_(p) {}
38  : current_(sb.first_) {}
39 
40  public:
44  bool more() const { return current_ != 0; }
48  const char* current() { if (current_) { return (const char*) &current_->s_[0]; } else { return ""; } }
53  size_t next()
54  {
55  if (current_)
56  {
57  current_ = current_->next_;
58  }
59  if (current_)
60  {
61  return current_->size_;
62  }
63  return 0;
64  }
68  size_t get_size() const
69  {
70  if (current_)
71  {
72  return current_->size_;
73  }
74  return 0;
75  }
81  {
82  ConstPageIterator r = *this;
83  next();
84  return r;
85  }
90  ConstPageIterator& operator++() { next(); return *this; }
94  const char* operator*() { return current(); }
98  bool operator==(const ConstPageIterator& i) const { return i.current_ == current_; }
99  private:
100  const Page* current_;
101 
102  };
103  friend class ConstPageIterator;
104 
109  {
110  public :
111  ConstIterator() : size_(0), index_(0), current_(0) {}
112  ConstIterator(size_t t, int i, const Page* p) : size_(t), index_(i), current_(p) {}
113  ConstIterator(const StringBuffer& sb) : size_(sb.size_), index_(0), current_(sb.first_) {}
114  public:
118  bool more() const { return size_ > index_; }
122  const char& current()
123  {
124  if (current_)
125  {
126  return *(const char*)&current_->s_[index_ % PAGE_SIZE];
127  }
128  else
129  {
130  return *"";
131  }
132  }
136  size_t pos() { return index_; }
141  size_t next()
142  {
143  if (size_ > index_)
144  {
145  if ((index_ + 1) % PAGE_SIZE == 0)
146  {
147  current_ = current_->next_;
148  }
149  ++index_;
150  }
151  return index_;
152  }
158  {
159  ConstIterator r = *this;
160  next();
161  return r;
162  }
167  ConstIterator& operator++() { next(); return *this; }
171  const char& operator*() { return current(); }
175  bool operator==(const ConstIterator& i) const { return i.index_ == index_; }
176 
177  private:
178  size_t size_;
179  size_t index_;
180  const Page* current_;
181 
182  };
183  friend class ConstIterator;
184  typedef ConstIterator const_iterator; // naming consistency with other commore classes
185 
189  class Iterator
190  {
191  public :
193  : size_(0), index_(0), current_(0) {}
194  Iterator(size_t t, int i, Page* p)
195  : size_(t), index_(i), current_(p) {}
197  : size_(sb.size_), index_(0), current_(sb.first_) {}
198 
199  public:
203  bool more() const { return size_ > index_; }
207  char& current()
208  {
209  char* u = (char*) &current_->s_[0];
210 
211  int i = index_ % PAGE_SIZE;
212  return u[i];
213  }
218  size_t next()
219  {
220  if (size_ > index_)
221  {
222  if ((index_ + 1) % PAGE_SIZE == 0)
223  {
224  current_ = current_->next_;
225  }
226  ++index_;
227  }
228  return index_;
229  }
235  {
236  Iterator r = *this;
237  next();
238  return r;
239  }
245  {
246  next();
247  return *this;
248  }
252  char& operator*() { return current(); }
256  bool operator==(const Iterator& i) const { return i.index_ == index_; }
257 
258  private:
259  size_t size_;
260  size_t index_;
261  Page* current_;
262 
263  };
264  friend class Iterator;
266 
267  struct Page
268  {
269  Page() : next_(0) {}
271  size_t size_;
273  unsigned char s_[4];
274 
275  };
276 
277  //class StreamBuf;
278  //friend class StreamBuf;
279 
280  public:
281  enum { PAGE_SIZE = 128 };
282 
283  public :
287  StringBuffer(size_t page_size = PAGE_SIZE);
292  StringBuffer(const char* s, size_t page_size = PAGE_SIZE);
293  StringBuffer(const StringBuffer& s);
294  StringBuffer(const StringBuffer& s, size_t page_size);
295  StringBuffer& operator =(const StringBuffer& C)
296  {
297  clear();
298  append(C);
299  return *this;
300  }
304  StringBuffer& operator =(const char* s)
305  {
306  clear();
307  append(s);
308  return *this;
309  }
310  ~StringBuffer();
311 
312  public:
316  size_t get_size() const { return size_; }
317  size_t size() const { return size_; }
321  size_t get_page_size() const { return page_size_; }
325  void clear();
329  void splice(StringBuffer& x);
333  size_t append(char);
337  size_t append(const char*);
341  size_t append(const StringBuffer&);
346  char* append();
351  size_t add_size(size_t size);
352 
356  StringBuffer& operator +=(char c)
357  {
358  append(c);
359  return *this;
360  }
364  StringBuffer& operator +=(const char* s)
365  {
366  append(s);
367  return *this;
368  }
372  StringBuffer& operator +=(const StringBuffer& C)
373  {
374  append(C);
375  return *this;
376  }
380  StringBuffer& operator +=(const StringBuffer* C)
381  {
382  append(*C);
383  return *this;
384  }
389  {
390  append(c);
391  return *this;
392  }
396  StringBuffer& operator <<(const char* s)
397  {
398  append(s);
399  return *this;
400  }
405  {
406  append(C);
407  return *this;
408  }
413  {
414  append(*C);
415  return *this;
416  }
420  const char* c_str(char* s, size_t size) const;
424  const char* c_str(AString& s) const;
425  const char* c_str(const AString& s = AString()) const;
426 
431  {
432  return const_iterator(size_, 0, first_);
433  }
438  {
439  return iterator(size_, 0, first_);
440  }
441 
448  {
449  return begin();
450  }
451 
455  int compare(const StringBuffer&) const;
456  int compare(const char*) const;
457  bool operator<(const StringBuffer& s) const
458  {
459  return compare(s) < 0;
460  }
461  bool operator>(const StringBuffer& s) const
462  {
463  return compare(s) > 0;
464  }
465  bool operator>(const char* s) const
466  {
467  return compare(s) > 0;
468  }
469  bool operator<(const char* s) const
470  {
471  return compare(s) < 0;
472  }
473  bool operator==(const StringBuffer& s) const
474  {
475  return compare(s) == 0;
476  }
477  bool operator==(const char* s) const
478  {
479  return compare(s) == 0;
480  }
481 
485  long write(CommBuffer& buffer) const;
489  long read(const CommBuffer& buffer);
493  long write_xml(OBStream& o) const;
497  long write(OBStream& o) const;
501  long write(OBStream& o, const char* delimitor) const;
505  long read(IBStream& i);
509  long read(IBStream& i, const char* delimitor);
513  long read_xml(IBStream& i);
519  long write(const Path& file_name, bool check = false) const;
523  long read(const Path& fileName);
524 
525  private:
526  size_t page_size_;
527  size_t size_;
528  PPage first_;
529  PPage last_;
530  void add_page_();
531  PPage allocate_(size_t size);
532  void release_(PPage p);
533 
534 
535  };
536 };
537 
539 {
540  b.write(o);
541  return o;
542 }
543 #endif
size_t pos()
Definition: StringBuffer.h:136
ConstIterator operator++(int)
Definition: StringBuffer.h:157
bool operator>(const StringBuffer &s) const
Definition: StringBuffer.h:461
size_t next()
Definition: StringBuffer.h:53
Iterator iterator
Definition: StringBuffer.h:265
ConstPageIterator operator++(int)
Definition: StringBuffer.h:80
Definition: AString.h:39
Page * PPage
Definition: StringBuffer.h:24
const char * current()
Definition: StringBuffer.h:48
ConstIterator & operator++()
Definition: StringBuffer.h:167
long CMREXD write_xml(const AString &sIn, OBStream &o)
Definition: AString.cpp:27
Definition: StringBuffer.h:21
ConstIterator()
Definition: StringBuffer.h:111
ConstIterator const_iterator
Definition: StringBuffer.h:184
ConstPageIterator(const StringBuffer &sb)
Definition: StringBuffer.h:37
Definition: StringBuffer.h:189
bool read(IBStream &str, int &num)
Definition: Time.cpp:200
size_t size_
Definition: StringBuffer.h:271
iterator begin()
Definition: StringBuffer.h:437
size_t get_size() const
Definition: StringBuffer.h:316
bool operator<(const char *s) const
Definition: StringBuffer.h:469
bool more() const
Definition: StringBuffer.h:44
const char & operator*()
Definition: StringBuffer.h:171
const char * operator*()
Definition: StringBuffer.h:94
ConstIterator(const StringBuffer &sb)
Definition: StringBuffer.h:113
PPage next_
Definition: StringBuffer.h:272
size_t next()
Definition: StringBuffer.h:141
#define CMREXD
Definition: Compiler.h:22
const_iterator cbegin() const
Definition: StringBuffer.h:447
Iterator(size_t t, int i, Page *p)
Definition: StringBuffer.h:194
bool operator<(const StringBuffer &s) const
Definition: StringBuffer.h:457
StringBuffer * PStringBuffer
Definition: StringBuffer.h:15
const_iterator begin() const
Definition: StringBuffer.h:430
commore::OBStream & operator<<(commore::OBStream &o, const commore::StringBuffer &b)
Definition: StringBuffer.h:538
size_t get_page_size() const
Definition: StringBuffer.h:321
Page()
Definition: StringBuffer.h:269
Iterator operator++(int)
Definition: StringBuffer.h:234
ConstPageIterator & operator++()
Definition: StringBuffer.h:90
Definition: Path.h:19
long write(CommBuffer &buffer) const
Definition: StringBuffer.cpp:559
Iterator & operator++()
Definition: StringBuffer.h:244
ConstIterator(size_t t, int i, const Page *p)
Definition: StringBuffer.h:112
Iterator()
Definition: StringBuffer.h:192
Definition: CommBuffer.h:28
bool operator>(const char *s) const
Definition: StringBuffer.h:465
long CMREXD read_xml(AString &sOut, IBStream &i)
Definition: AString.cpp:65
size_t size() const
Definition: StringBuffer.h:317
size_t next()
Definition: StringBuffer.h:218
char & operator*()
Definition: StringBuffer.h:252
bool more() const
Definition: StringBuffer.h:118
Definition: StringBuffer.h:267
bool operator==(const StringBuffer &s) const
Definition: StringBuffer.h:473
Definition: IOBStream.h:166
Iterator(StringBuffer &sb)
Definition: StringBuffer.h:196
bool operator==(const ConstPageIterator &i) const
Definition: StringBuffer.h:98
ConstPageIterator()
Definition: StringBuffer.h:33
Definition: IOBStream.h:29
Definition: StringBuffer.h:30
bool more() const
Definition: StringBuffer.h:203
size_t get_size() const
Definition: StringBuffer.h:68
bool operator==(const Iterator &i) const
Definition: StringBuffer.h:256
char & current()
Definition: StringBuffer.h:207
const char & current()
Definition: StringBuffer.h:122
bool operator==(const char *s) const
Definition: StringBuffer.h:477
ConstPageIterator(const Page *p)
Definition: StringBuffer.h:35
Definition: StringBuffer.h:108
bool operator==(const ConstIterator &i) const
Definition: StringBuffer.h:175
PStringBuffer cont_
Definition: StringBuffer.h:270