(file) Return to Buffer.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 martin 1.12 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.13 //
  3 martin 1.12 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.13 //
 10 martin 1.12 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.13 //
 17 martin 1.12 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.13 //
 20 martin 1.12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.13 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.12 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.13 //
 28 martin 1.12 //////////////////////////////////////////////////////////////////////////
 29 mike   1.2  //
 30             //%/////////////////////////////////////////////////////////////////////////////
 31             
 32             #ifndef Pegasus_Buffer_h
 33             #define Pegasus_Buffer_h
 34             
 35             #include <Pegasus/Common/Config.h>
 36             #include <Pegasus/Common/Linkage.h>
 37             #include <cstring>
 38             
 39             PEGASUS_NAMESPACE_BEGIN
 40             
 41             struct BufferRep
 42             {
 43 kumpf  1.10     Uint32 size;
 44                 Uint32 cap;
 45 mike   1.2      char data[1];
 46             };
 47             
 48             class PEGASUS_COMMON_LINKAGE Buffer
 49             {
 50             public:
 51             
 52 marek  1.11     Buffer(Uint32 minCap=2048);
 53 mike   1.2  
 54                 Buffer(const Buffer& x);
 55             
 56 marek  1.11     Buffer(const char* data, Uint32 size, Uint32 minCap=2048);
 57 mike   1.2  
 58                 ~Buffer();
 59             
 60                 Buffer& operator=(const Buffer& x);
 61             
 62                 void swap(Buffer& x);
 63             
 64 kumpf  1.10     Uint32 size() const;
 65 mike   1.2  
 66 kumpf  1.10     Uint32 capacity() const;
 67 mike   1.2  
 68 kumpf  1.5      /**
 69                     Returns a pointer to a character buffer with the Buffer contents.
 70                     The character buffer is null-terminated even if the Buffer contents
 71                     do not include a null termination character.
 72                  */
 73 mike   1.2      const char* getData() const;
 74             
 75 marek  1.16     /**
 76                     Returns a pointer to the character buffer with the Buffer contents.
 77                     This function does NOT append a null character.
 78                     Resulting data should NOT be used with C string processing functions.
 79                 */
 80                 char* getContentPtr();
 81             
 82 kumpf  1.10     char get(Uint32 i) const;
 83 mike   1.2  
 84 kumpf  1.10     void set(Uint32 i, char x);
 85 mike   1.2  
 86 kumpf  1.10     const char& operator[](Uint32 i) const;
 87 mike   1.2  
 88 kumpf  1.10     void reserveCapacity(Uint32 cap);
 89 mike   1.2  
 90 kumpf  1.10     void grow(Uint32 size, char x = '\0');
 91 mike   1.2  
 92                 void append(char x);
 93             
 94 thilo.boehm 1.15     void append_unchecked(char x);
 95                  
 96 kumpf       1.10     void append(const char* data, Uint32 size);
 97 mike        1.2  
 98 thilo.boehm 1.15     void append_unchecked(const char* data, Uint32 size);
 99                  
100                      void append(char c1, char c2);
101                  
102                      void append(char c1, char c2, char c3);
103                  
104 mike        1.2      void append(char c1, char c2, char c3, char c4);
105                  
106                      void append(
107 kumpf       1.9          char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8);
108 mike        1.2  
109 kumpf       1.10     void insert(Uint32 pos, const char* data, Uint32 size);
110 mike        1.2  
111 karl        1.16.4.1     /* inserts size bytes of data at position pos, but overwrites overlay
112                             bytes of the original Buffer data.
113                             This function is used in HTTPConnection.
114                             ATTENTION: Function does NOT check if enough capacity is available in
115                                       the Buffer and expects the caller to take care of that upfront.
116                          */
117                          void insertWithOverlay(
118                              Uint32 pos,
119                              const char* data,
120                              Uint32 size,
121                              Uint32 overlay);
122                      
123 kumpf       1.10         void remove(Uint32 pos, Uint32 size);
124 mike        1.2      
125 kumpf       1.10         void remove(Uint32 pos);
126 mike        1.2      
127                          void clear();
128                      
129                      private:
130                      
131 kumpf       1.10         void _reserve_aux(Uint32 cap);
132 mike        1.2      
133                          void _append_char_aux();
134                      
135                          BufferRep* _rep;
136                          static BufferRep _empty_rep;
137 marek       1.11         Uint32 _minCap;
138 mike        1.2      };
139                      
140 marek       1.11     inline Buffer::Buffer(Uint32 minCap) : _rep(&_empty_rep), _minCap(minCap)
141 kumpf       1.14     {
142 mike        1.2      }
143                      
144                      inline Buffer::~Buffer()
145                      {
146 gs.keenan   1.6          if (_rep->cap != 0)
147 kumpf       1.9              free(_rep);
148 mike        1.2      }
149                      
150                      inline void Buffer::swap(Buffer& x)
151                      {
152                          BufferRep* tmp = _rep;
153                          _rep = x._rep;
154                          x._rep = tmp;
155                      }
156                      
157 kumpf       1.10     inline Uint32 Buffer::size() const
158 mike        1.2      {
159                          return _rep->size;
160                      }
161                      
162 kumpf       1.10     inline Uint32 Buffer::capacity() const
163 mike        1.2      {
164                          return _rep->cap;
165                      }
166                      
167                      inline const char* Buffer::getData() const
168                      {
169 gs.keenan   1.6          if (_rep->cap == 0)
170 kumpf       1.5          {
171                              const_cast<Buffer*>(this)->_append_char_aux();
172                          }
173                      
174                          _rep->data[_rep->size] = '\0';
175                      
176 mike        1.2          return _rep->data;
177                      }
178                      
179 marek       1.16     inline char* Buffer::getContentPtr()
180                      {
181                          return _rep->data;
182                      }
183                      
184 kumpf       1.10     inline char Buffer::get(Uint32 i) const
185 mike        1.2      {
186                          return _rep->data[i];
187                      }
188                      
189 kumpf       1.10     inline void Buffer::set(Uint32 i, char x)
190 mike        1.2      {
191                          _rep->data[i] = x;
192                      }
193                      
194 kumpf       1.10     inline const char& Buffer::operator[](Uint32 i) const
195 mike        1.2      {
196                          return _rep->data[i];
197                      }
198                      
199 kumpf       1.10     inline void Buffer::reserveCapacity(Uint32 cap)
200 mike        1.2      {
201                          if (cap > _rep->cap)
202 kumpf       1.9              _reserve_aux(cap);
203 mike        1.2      }
204                      
205 kumpf       1.10     inline void Buffer::grow(Uint32 size, char x)
206 mike        1.2      {
207 kumpf       1.10         Uint32 cap = _rep->size + size;
208 mike        1.2      
209                          if (cap > _rep->cap)
210 kumpf       1.9              _reserve_aux(cap);
211 mike        1.2      
212                          memset(_rep->data + _rep->size, x, size);
213                          _rep->size += size;
214                      }
215                      
216                      inline void Buffer::append(char x)
217                      {
218                          if (_rep->size == _rep->cap)
219 kumpf       1.9              _append_char_aux();
220 mike        1.2      
221                          _rep->data[_rep->size++] = x;
222                      }
223                      
224 thilo.boehm 1.15     inline void Buffer::append_unchecked(char x)
225                      {
226                          _rep->data[_rep->size++] = x;
227                      }
228                      
229 kumpf       1.10     inline void Buffer::append(const char* data, Uint32 size)
230 mike        1.2      {
231 kumpf       1.10         Uint32 cap = _rep->size + size;
232 mike        1.2      
233                          if (cap > _rep->cap)
234 kumpf       1.9              _reserve_aux(cap);
235 mike        1.2      
236                          memcpy(_rep->data + _rep->size, data, size);
237                          _rep->size += size;
238                      }
239                      
240 thilo.boehm 1.15     inline void Buffer::append_unchecked(const char* data, Uint32 size)
241                      {
242                          memcpy(_rep->data + _rep->size, data, size);
243                          _rep->size += size;
244                      }
245                      
246 mike        1.2      inline void Buffer::clear()
247                      {
248 gs.keenan   1.6          if (_rep->cap != 0)
249 kumpf       1.9              _rep->size = 0;
250 mike        1.2      }
251                      
252 kumpf       1.10     inline void Buffer::remove(Uint32 pos)
253 mike        1.2      {
254                          remove(pos, 1);
255                      }
256                      
257 thilo.boehm 1.15     inline void Buffer::append(char c1, char c2)
258                      {
259                          Uint32 cap = _rep->size + 2;
260                      
261                          if (cap > _rep->cap)
262                              _reserve_aux(cap);
263                      
264                          char* p = _rep->data + _rep->size;
265                          p[0] = c1;
266                          p[1] = c2;
267                          _rep->size += 2;
268                      }
269                      
270                      inline void Buffer::append(char c1, char c2, char c3)
271                      {
272                          Uint32 cap = _rep->size + 3;
273                      
274                          if (cap > _rep->cap)
275                              _reserve_aux(cap);
276                      
277                          char* p = _rep->data + _rep->size;
278 thilo.boehm 1.15         p[0] = c1;
279                          p[1] = c2;
280                          p[2] = c3;
281                          _rep->size += 3;
282                      }
283                      
284 mike        1.2      inline void Buffer::append(char c1, char c2, char c3, char c4)
285                      {
286 kumpf       1.10         Uint32 cap = _rep->size + 4;
287 mike        1.2      
288                          if (cap > _rep->cap)
289 kumpf       1.9              _reserve_aux(cap);
290 mike        1.2      
291                          char* p = _rep->data + _rep->size;
292                          p[0] = c1;
293                          p[1] = c2;
294                          p[2] = c3;
295                          p[3] = c4;
296                          _rep->size += 4;
297                      }
298                      
299                      inline void Buffer::append(
300                          char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8)
301                      {
302 kumpf       1.10         Uint32 cap = _rep->size + 8;
303 mike        1.2      
304                          if (cap > _rep->cap)
305 kumpf       1.9              _reserve_aux(cap);
306 mike        1.2      
307                          char* p = _rep->data + _rep->size;
308                          p[0] = c1;
309                          p[1] = c2;
310                          p[2] = c3;
311                          p[3] = c4;
312                          p[4] = c5;
313                          p[5] = c6;
314                          p[6] = c7;
315                          p[7] = c8;
316                          _rep->size += 8;
317                      }
318                      
319                      inline bool operator==(const Buffer& x, const Buffer& y)
320                      {
321                          return memcmp(x.getData(), y.getData(), x.size()) == 0;
322                      }
323                      
324                      PEGASUS_NAMESPACE_END
325                      
326                      #endif /* Pegasus_Buffer_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2