libcommonc++  0.7
CStringBuilder.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_CStringBuilder_hxx
24 #define __ccxx_CStringBuilder_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/CString.h++>
28 
29 namespace ccxx {
30 
43 {
44  public:
45 
52  CStringBuilder(char* buf, size_t size);
53 
55  ~CStringBuilder();
56 
61  CStringBuilder& clear();
62 
71  CStringBuilder& append(const char* s, size_t length = 0);
72 
81  CStringBuilder& append(const CString& s, size_t length = 0);
82 
84  CStringBuilder& append(char c);
85 
95  CStringBuilder& append(int val, size_t width, bool zeroPad = true);
96 
107  CStringBuilder& append(uint_t val, size_t width, bool zeroPad = true);
108 
110  inline char* getPosition() const
111  { return(_pos); }
112 
117  inline size_t getRemaining() const
118  { return(_left); }
119 
121  inline bool hasRemaining() const
122  { return(_left > 0); }
123 
128  inline size_t getLength() const
129  { return(_pos - _buf); }
130 
135  inline size_t length() const
136  { return(getLength()); }
137 
139  inline char *c_str() const
140  { return(_buf); }
141 
149  bool bump(size_t delta);
150 
152  void terminate();
153 
155  inline CStringBuilder& operator+=(const char *s)
156  { return(append(s)); }
157 
159  inline CStringBuilder& operator+=(char c)
160  { return(append(c)); }
161 
162  private:
163 
165 
166  template<typename T> char* _formatValue(T value, size_t width, char padChar,
167  char* buf, size_t bufsz);
168 
169  char *_buf;
170  size_t _length;
171  char *_pos;
172  size_t _left;
173 };
174 
175 } // namespace ccxx
176 
177 #endif // __ccxx_CStringBuilder_hxx
char * c_str() const
Get a pointer to the beginning of the buffer.
Definition: CStringBuilder.h++:139
size_t length() const
Get the current length of the buffer, that is, the number of characters currently in the buffer...
Definition: CStringBuilder.h++:135
A utility class for constructing C-style strings.
Definition: CStringBuilder.h++:42
char * getPosition() const
Get a pointer to the current write position for the buffer.
Definition: CStringBuilder.h++:110
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
bool hasRemaining() const
Determine if the buffer has space remaining for additional characters.
Definition: CStringBuilder.h++:121
size_t getLength() const
Get the current length of the buffer, that is, the number of characters currently in the buffer...
Definition: CStringBuilder.h++:128
CStringBuilder & operator+=(const char *s)
Append operator.
Definition: CStringBuilder.h++:155
#define COMMONCPP_API
Definition: Common.h++:126
An implicitly shared, reference-counted container for an immutable, NUL-terminated C string...
Definition: CString.h++:39
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
CStringBuilder & operator+=(char c)
Append operator.
Definition: CStringBuilder.h++:159
Definition: AllocationMap.c++:25
size_t getRemaining() const
Get the remaining capacity of the buffer, that is, the number of characters that can still be written...
Definition: CStringBuilder.h++:117