/[winpt]/trunk/Src/StringBuffer.cpp
ViewVC logotype

Annotation of /trunk/Src/StringBuffer.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (hide annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 4867 byte(s)


1 twoaday 326 /* StringBuffer.cpp - A simple string buffer implementation.
2     Copyright (C) 2006, 2007 Timo Schulz
3     Released under the GNU General Public License, Version 2. */
4    
5     #include <string.h>
6     #include <stdio.h>
7    
8     #include "StringBuffer.h"
9    
10     /* Add the converted data at the end of the internal buffer.
11     Allocate more memory if needed, plus some extra bytes. */
12     void StringBuffer::addCharacters (const char *buffer, int bufsize)
13     {
14     if (size+bufsize > alloced_size) {
15     char *save = private_contents;
16    
17     alloced_size += bufsize+DEFSIZE+1;
18     private_contents = new char[alloced_size+1];
19     memset (private_contents, 0, alloced_size+1);
20     if (save != NULL) {
21     strcpy (private_contents, save);
22     delete []save;
23     }
24     }
25     strcat (private_contents, buffer);
26     size += bufsize;
27     }
28    
29    
30     /* Create an empty buffer object. */
31 twoaday 328 StringBuffer::StringBuffer (void)
32 twoaday 326 {
33     alloced_size = 0;
34     size = 0;
35     private_contents = NULL;
36     }
37    
38    
39     /* Create an empty buffer object with an inital size. */
40     StringBuffer::StringBuffer (int bufsize)
41     {
42     alloced_size = bufsize+DEFSIZE+1;
43     private_contents = new char[alloced_size+1];
44     memset (private_contents, 0, alloced_size+1);
45     size = 0;
46     }
47    
48    
49     /* Create a buffer object with the given contents. */
50     StringBuffer::StringBuffer (const char *contents)
51     {
52     alloced_size = strlen (contents)+1+DEFSIZE;
53     private_contents = new char[alloced_size+1];
54     memset (private_contents, 0, alloced_size+1);
55     strcpy (private_contents, contents);
56     size = strlen (contents);
57     }
58    
59    
60     /* Destructor which fres the used memory. */
61 twoaday 328 StringBuffer::~StringBuffer (void)
62 twoaday 326 {
63     if (private_contents != NULL)
64     delete []private_contents;
65     private_contents = NULL;
66     size = 0;
67     alloced_size = 0;
68     }
69    
70    
71     /* Return a copy of the buffer which must be freed after use. */
72     char* StringBuffer::getBufferCopy (void)
73     {
74     char *p = new char[strlen (private_contents)+1];
75     strcpy (p, private_contents);
76     return p;
77     }
78    
79    
80     /* Return a constant pointer to the 'char*' data of the buffer. */
81     const char *StringBuffer::getBuffer (void)
82     {
83     return private_contents;
84     }
85    
86    
87     /* Overwrite buffer contents in a safe manner. */
88     void StringBuffer::wipeContents (void)
89     {
90     volatile char *ptr = (volatile char*)private_contents;
91     int len = getSize ();
92    
93     while (len-- > 0) {
94     *ptr = '\0';
95     *ptr++;
96     }
97     }
98    
99    
100     /* Return the size of the underlying string. */
101     int StringBuffer::getSize (void)
102     {
103     return size;
104     }
105    
106     /* Reset the buffer contents. */
107     void StringBuffer::reset (void)
108     {
109     memset (private_contents, 0, alloced_size);
110     size = 0;
111     }
112    
113    
114     /* Add an integer to the buffer. */
115     void StringBuffer::add (int val)
116     {
117     char tmp[32];
118    
119     _snprintf (tmp, 31, "%d", val);
120     addCharacters (tmp, strlen (tmp));
121     }
122    
123 twoaday 328 void StringBuffer::add(const char* val, size_t len)
124     {
125     if (len > strlen(val))
126     len = strlen(val);
127     addCharacters(val, len);
128     }
129 twoaday 326
130     /* Add a single character to the buffer. */
131     void StringBuffer::add (char val)
132     {
133     char tmp[4];
134    
135     _snprintf (tmp, 3, "%c", val);
136     addCharacters (tmp, strlen (tmp));
137     }
138    
139    
140     /* Add a long-integer to the buffer. */
141     void StringBuffer::add (long val)
142     {
143     char tmp[32];
144    
145     _snprintf (tmp, 31, "%d", val);
146     addCharacters (tmp, strlen (tmp));
147     }
148    
149    
150     /* Add a string to the buffer. */
151     void StringBuffer::add (const char *val)
152     {
153     addCharacters (val, strlen (val));
154     }
155    
156    
157     /* Add an unsigned integer to the buffer. */
158     void StringBuffer::add (unsigned long val)
159     {
160     char tmp[32];
161    
162     _snprintf (tmp, 31, "%lu", val);
163     addCharacters (tmp, strlen (tmp));
164     }
165    
166    
167     /* Add an unsigned integer in hex format to the buffer. */
168     void StringBuffer::addHex (unsigned long val)
169     {
170     char tmp[32];
171    
172     _snprintf (tmp, 31, "%08lx", val);
173     addCharacters (tmp, strlen (tmp));
174     }
175    
176    
177     /* Overloaded operators to provide an easier interface
178     for manipulating the underlying buffer. */
179     StringBuffer& StringBuffer::operator +(int val)
180     {
181     add (val);
182     return *this;
183     }
184    
185    
186     StringBuffer& StringBuffer::operator +(unsigned long val)
187     {
188     add (val);
189     return *this;
190     }
191    
192    
193     StringBuffer& StringBuffer::operator +(const char *val)
194     {
195     addCharacters (val, strlen (val));
196     return *this;
197     }
198    
199    
200 twoaday 328
201 twoaday 326 StringBuffer& StringBuffer::operator +=(int val)
202     {
203     add (val);
204     return *this;
205     }
206    
207    
208     StringBuffer& StringBuffer::operator +=(unsigned long val)
209     {
210     add (val);
211     return *this;
212     }
213    
214    
215     StringBuffer& StringBuffer::operator +=(const char *val)
216     {
217     addCharacters (val, strlen (val));
218     return *this;
219     }
220    
221    
222 twoaday 328 StringBuffer& StringBuffer::operator +=(char *val)
223     {
224     addCharacters (val, strlen (val));
225     return *this;
226     }
227    
228    
229 twoaday 326 StringBuffer& StringBuffer::operator +=(char val)
230     {
231     add (val);
232     return *this;
233     }
234    
235    
236     StringBuffer& StringBuffer::operator=(const char *val)
237     {
238     reset ();
239     addCharacters (val, strlen (val));
240     return *this;
241     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26