| 1 |
/* $Id$ */ |
| 2 |
/* |
| 3 |
* Copyright (c) 1990-1996 Sam Leffler |
| 4 |
* Copyright (c) 1991-1996 Silicon Graphics, Inc. |
| 5 |
* HylaFAX is a trademark of Silicon Graphics |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 8 |
* its documentation for any purpose is hereby granted without fee, provided |
| 9 |
* that (i) the above copyright notices and this permission notice appear in |
| 10 |
* all copies of the software and related documentation, and (ii) the names of |
| 11 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 12 |
* publicity relating to the software without the specific, prior written |
| 13 |
* permission of Sam Leffler and Silicon Graphics. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 16 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 17 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
* |
| 19 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 20 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 21 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 22 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 23 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 24 |
* OF THIS SOFTWARE. |
| 25 |
*/ |
| 26 |
#include "StackBuffer.h" |
| 27 |
#include "Str.h" |
| 28 |
#include <stdlib.h> |
| 29 |
|
| 30 |
fxStackBuffer::fxStackBuffer(u_int grow) |
| 31 |
{ |
| 32 |
base = next = buf; |
| 33 |
end = &buf[sizeof(buf)]; |
| 34 |
amountToGrowBy = grow ? grow : 500; |
| 35 |
} |
| 36 |
|
| 37 |
fxStackBuffer::~fxStackBuffer() |
| 38 |
{ |
| 39 |
if (base != buf) free(base); |
| 40 |
} |
| 41 |
|
| 42 |
fxStackBuffer::fxStackBuffer(const fxStackBuffer& other) |
| 43 |
{ |
| 44 |
u_int size = other.end - other.base; |
| 45 |
u_int len = other.getLength(); |
| 46 |
if (size > sizeof(buf)) { |
| 47 |
base = (char*) malloc(size); |
| 48 |
} else { |
| 49 |
base = &buf[0]; |
| 50 |
} |
| 51 |
end = base + size; |
| 52 |
next = base + len; |
| 53 |
memcpy(base, other.base, len); |
| 54 |
} |
| 55 |
|
| 56 |
void |
| 57 |
fxStackBuffer::addc(char c) |
| 58 |
{ |
| 59 |
if (next >= end) { |
| 60 |
grow(amountToGrowBy); |
| 61 |
} |
| 62 |
*next++ = c; |
| 63 |
} |
| 64 |
|
| 65 |
void |
| 66 |
fxStackBuffer::grow(u_int amount) |
| 67 |
{ |
| 68 |
// insure an acceptable amount of growth |
| 69 |
if (amount < amountToGrowBy) amount = amountToGrowBy; |
| 70 |
|
| 71 |
// move data into larger piece of memory |
| 72 |
u_int size = end - base; |
| 73 |
u_int len = getLength(); |
| 74 |
u_int newSize = size + amount; |
| 75 |
if (base == buf) { |
| 76 |
base = (char*) malloc(newSize); |
| 77 |
memcpy(base, buf, sizeof(buf)); |
| 78 |
} else { |
| 79 |
base = (char*) realloc(base, newSize); |
| 80 |
} |
| 81 |
|
| 82 |
// update position pointers |
| 83 |
end = base + newSize; |
| 84 |
next = base + len; |
| 85 |
} |
| 86 |
|
| 87 |
void |
| 88 |
fxStackBuffer::put(char const* c, u_int len) |
| 89 |
{ |
| 90 |
u_int remainingSpace = end - next; |
| 91 |
if (len > remainingSpace) { |
| 92 |
grow(len - remainingSpace); |
| 93 |
} |
| 94 |
memcpy(next, c, len); |
| 95 |
next += len; |
| 96 |
} |
| 97 |
|
| 98 |
void |
| 99 |
fxStackBuffer::put(const fxStr& s) |
| 100 |
{ |
| 101 |
put(s, s.length()); |
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
fxStackBuffer::vput(const char* fmt, va_list ap) |
| 106 |
{ |
| 107 |
put(fxStr::vformat(fmt, ap)); |
| 108 |
} |
| 109 |
|
| 110 |
void |
| 111 |
fxStackBuffer::fput(const char* fmt ...) |
| 112 |
{ |
| 113 |
va_list ap; |
| 114 |
va_start(ap, fmt); |
| 115 |
vput(fmt, ap); |
| 116 |
va_end(ap); |
| 117 |
} |
| 118 |
|
| 119 |
fxStackBuffer& fxStackBuffer::operator=(const fxStackBuffer& other) |
| 120 |
{ |
| 121 |
if (&other != this) { |
| 122 |
u_int size = other.end - other.base; |
| 123 |
u_int len = other.getLength(); |
| 124 |
if (base != buf) free(base); |
| 125 |
base = (size > sizeof(buf)) ? (char*) malloc(size) : &buf[0]; |
| 126 |
end = base + size; |
| 127 |
next = base + len; |
| 128 |
memcpy(base, other.base, len); |
| 129 |
} |
| 130 |
return *this; |
| 131 |
} |