modules/up/src/util/buffer.cc
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- append
1 // $Id: buffer.cc,v 1.1.1.1 2000/03/10 16:32:16 engin Exp $
2 //
3 // Copyright (c) 1994 by the University of Southern California
4 // All rights reserved.
5 //
6 // Permission to use, copy, modify, and distribute this software and its
7 // documentation in source and binary forms for lawful non-commercial
8 // purposes and without fee is hereby granted, provided that the above
9 // copyright notice appear in all copies and that both the copyright
10 // notice and this permission notice appear in supporting documentation,
11 // and that any documentation, advertising materials, and other materials
12 // related to such distribution and use acknowledge that the software was
13 // developed by the University of Southern California, Information
14 // Sciences Institute. The name of the USC may not be used to endorse or
15 // promote products derived from this software without specific prior
16 // written permission.
17 //
18 // THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY
19 // REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY
20 // PURPOSE. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
21 // IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
23 // TITLE, AND NON-INFRINGEMENT.
24 //
25 // IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
26 // SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT, TORT,
27 // OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH, THE USE
28 // OR PERFORMANCE OF THIS SOFTWARE.
29 //
30 // Questions concerning this software should be directed to
31 // ratoolset@isi.edu.
32 //
33 // Author(s): WeeSan Lee <wlee@ISI.EDU>
34
35 #include "buffer.hh"
36
37 StringBuffer &StringBuffer::append(char *pzcText, unsigned int uiLen)
/* [<][>][^][v][top][bottom][index][help] */
38 {
39 // Check if the buffer is big enuf to hold the whole thing
40 if (ulEnd + uiLen > capacity())
41 {
42 // Calculate the new length
43 unsigned long ulNewLen = length() + uiLen;
44 // Check if the new text and old text would fit in the half of
45 // the same buffer after being reformed
46 if ((length() + ulNewLen) <= (capacity() >> 1))
47 {
48 // Yes, reform the same old buffer
49 strcpy(pzcBuffer, pzcBuffer + ulBegin);
50 // Update some pointers
51 ulEnd = length(); // Have to do this first before assigning ulBegin
52 ulBegin = 0;
53 }
54 else // No, allocate a new large buffer to replace the old one
55 {
56 // Calculate the new capacity
57 unsigned long ulNewCapacity = capacity() + PROBE_BUFFER_SIZE;
58 // If the new capacity is still too small,
59 // make use of the new length + PROBE_BUFFER_SIZE
60 if (ulNewCapacity < ulNewLen)
61 ulNewCapacity = ulNewLen + PROBE_BUFFER_SIZE;
62 // Update the old capacity
63 ulCapacity = ulNewCapacity;
64 // Allocate a new buffer
65 char *pzcTempBuffer = new char [ulNewCapacity + 1];
66 assert(pzcTempBuffer != NULL);
67 // Very expensive copy
68 // Could do strcpy because pzcBuffer + ulEnd is always NULL
69 strcpy(pzcTempBuffer, pzcBuffer + ulBegin);
70 // Update some pointers
71 ulEnd = length(); // Have to do this first before assigning ulBegin
72 ulBegin = 0;
73 // Switch buffers
74 delete []pzcBuffer;
75 pzcBuffer = pzcTempBuffer;
76 }
77 }
78 // Whew! The buffer is big enuf
79 strncpy(pzcBuffer + ulEnd, pzcText, uiLen);
80 ulEnd += uiLen;
81 pzcBuffer[ulEnd] = 0;
82 return *this;
83 }