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

Annotation of /trunk/Src/StringBuffer.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 326 - (hide annotations)
Fri Sep 25 15:53:21 2009 UTC (15 years, 5 months ago) by twoaday
File size: 4612 byte(s)
Begin update.

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     StringBuffer::StringBuffer ()
32     {
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     StringBuffer::~StringBuffer ()
62     {
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    
124     /* Add a single character to the buffer. */
125     void StringBuffer::add (char val)
126     {
127     char tmp[4];
128    
129     _snprintf (tmp, 3, "%c", val);
130     addCharacters (tmp, strlen (tmp));
131     }
132    
133    
134     /* Add a long-integer to the buffer. */
135     void StringBuffer::add (long val)
136     {
137     char tmp[32];
138    
139     _snprintf (tmp, 31, "%d", val);
140     addCharacters (tmp, strlen (tmp));
141     }
142    
143    
144     /* Add a string to the buffer. */
145     void StringBuffer::add (const char *val)
146     {
147     addCharacters (val, strlen (val));
148     }
149    
150    
151     /* Add an unsigned integer to the buffer. */
152     void StringBuffer::add (unsigned long val)
153     {
154     char tmp[32];
155    
156     _snprintf (tmp, 31, "%lu", val);
157     addCharacters (tmp, strlen (tmp));
158     }
159    
160    
161     /* Add an unsigned integer in hex format to the buffer. */
162     void StringBuffer::addHex (unsigned long val)
163     {
164     char tmp[32];
165    
166     _snprintf (tmp, 31, "%08lx", val);
167     addCharacters (tmp, strlen (tmp));
168     }
169    
170    
171     /* Overloaded operators to provide an easier interface
172     for manipulating the underlying buffer. */
173     StringBuffer& StringBuffer::operator +(int val)
174     {
175     add (val);
176     return *this;
177     }
178    
179    
180     StringBuffer& StringBuffer::operator +(unsigned long val)
181     {
182     add (val);
183     return *this;
184     }
185    
186    
187     StringBuffer& StringBuffer::operator +(const char *val)
188     {
189     addCharacters (val, strlen (val));
190     return *this;
191     }
192    
193    
194     StringBuffer& StringBuffer::operator +=(int val)
195     {
196     add (val);
197     return *this;
198     }
199    
200    
201     StringBuffer& StringBuffer::operator +=(unsigned long val)
202     {
203     add (val);
204     return *this;
205     }
206    
207    
208     StringBuffer& StringBuffer::operator +=(const char *val)
209     {
210     addCharacters (val, strlen (val));
211     return *this;
212     }
213    
214    
215     StringBuffer& StringBuffer::operator +=(char val)
216     {
217     add (val);
218     return *this;
219     }
220    
221    
222     StringBuffer& StringBuffer::operator=(const char *val)
223     {
224     reset ();
225     addCharacters (val, strlen (val));
226     return *this;
227     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26