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

Annotation of /trunk/Generic/StringBuffer.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 281 - (hide annotations)
Mon Jan 15 22:04:00 2007 UTC (18 years, 1 month ago) by twoaday
File size: 4374 byte(s)


1 twoaday 281 /* 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     /* Return the size of the underlying string. */
88     int StringBuffer::getSize (void)
89     {
90     return size;
91     }
92    
93     /* Reset the buffer contents. */
94     void StringBuffer::reset (void)
95     {
96     memset (private_contents, 0, alloced_size);
97     size = 0;
98     }
99    
100    
101     /* Add an integer to the buffer. */
102     void StringBuffer::add (int val)
103     {
104     char tmp[32];
105    
106     _snprintf (tmp, 31, "%d", val);
107     addCharacters (tmp, strlen (tmp));
108     }
109    
110    
111     /* Add a single character to the buffer. */
112     void StringBuffer::add (char val)
113     {
114     char tmp[4];
115    
116     _snprintf (tmp, 3, "%c", val);
117     addCharacters (tmp, strlen (tmp));
118     }
119    
120    
121     /* Add a long-integer to the buffer. */
122     void StringBuffer::add (long val)
123     {
124     char tmp[32];
125    
126     _snprintf (tmp, 31, "%d", val);
127     addCharacters (tmp, strlen (tmp));
128     }
129    
130    
131     /* Add a string to the buffer. */
132     void StringBuffer::add (const char *val)
133     {
134     addCharacters (val, strlen (val));
135     }
136    
137    
138     /* Add an unsigned integer to the buffer. */
139     void StringBuffer::add (unsigned long val)
140     {
141     char tmp[32];
142    
143     _snprintf (tmp, 31, "%lu", val);
144     addCharacters (tmp, strlen (tmp));
145     }
146    
147    
148     /* Add an unsigned integer in hex format to the buffer. */
149     void StringBuffer::addHex (unsigned long val)
150     {
151     char tmp[32];
152    
153     _snprintf (tmp, 31, "%08lx", val);
154     addCharacters (tmp, strlen (tmp));
155     }
156    
157    
158     /* Overloaded operators to provide an easier interface
159     for manipulating the underlying buffer. */
160     StringBuffer& StringBuffer::operator +(int val)
161     {
162     add (val);
163     return *this;
164     }
165    
166    
167     StringBuffer& StringBuffer::operator +(unsigned long val)
168     {
169     add (val);
170     return *this;
171     }
172    
173    
174     StringBuffer& StringBuffer::operator +(const char *val)
175     {
176     addCharacters (val, strlen (val));
177     return *this;
178     }
179    
180    
181     StringBuffer& StringBuffer::operator +=(int val)
182     {
183     add (val);
184     return *this;
185     }
186    
187    
188     StringBuffer& StringBuffer::operator +=(unsigned long val)
189     {
190     add (val);
191     return *this;
192     }
193    
194    
195     StringBuffer& StringBuffer::operator +=(const char *val)
196     {
197     addCharacters (val, strlen (val));
198     return *this;
199     }
200    
201    
202     StringBuffer& StringBuffer::operator +=(char val)
203     {
204     add (val);
205     return *this;
206     }
207    
208    
209     StringBuffer& StringBuffer::operator=(const char *val)
210     {
211     reset ();
212     addCharacters (val, strlen (val));
213     return *this;
214     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26