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

Contents of /trunk/Src/StringBuffer.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 441 - (show annotations)
Sat Apr 14 14:11:03 2012 UTC (12 years, 10 months ago) by twoaday
File size: 5102 byte(s)
2012-04-14  Timo Schulz  <twoaday@gmx.net>

        * wptBalloonPop.cpp (window_proc): Removed unused variable.
        * wptCardDlg.cpp (do_askpin): Corrected generation of PIN
        info text. Thanks to Grzesiek.
			

1 /* 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 (void)
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 (void)
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 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
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 StringBuffer& StringBuffer::operator +(char *val)
200 {
201 addCharacters (val, strlen(val));
202 return *this;
203 }
204
205
206 StringBuffer& StringBuffer::operator +=(int val)
207 {
208 add (val);
209 return *this;
210 }
211
212
213 StringBuffer& StringBuffer::operator +=(unsigned long val)
214 {
215 add (val);
216 return *this;
217 }
218
219
220 StringBuffer& StringBuffer::operator +=(const char *val)
221 {
222 addCharacters (val, strlen (val));
223 return *this;
224 }
225
226
227 StringBuffer& StringBuffer::operator +=(char *val)
228 {
229 addCharacters (val, strlen (val));
230 return *this;
231 }
232
233
234 StringBuffer& StringBuffer::operator +=(char val)
235 {
236 add (val);
237 return *this;
238 }
239
240
241 StringBuffer& StringBuffer::operator=(const char *val)
242 {
243 reset ();
244 addCharacters (val, strlen (val));
245 return *this;
246 }
247
248 StringBuffer& StringBuffer::operator=(char *val)
249 {
250 reset ();
251 addCharacters (val, strlen (val));
252 return *this;
253 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26