1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdarg.h>
#include "ts/ink_platform.h"
#include "ts/ink_memory.h"
#include "ts/TextBuffer.h"
/****************************************************************************
*
* TextBuffer.cc - A self-expanding buffer, primarly meant for strings
*
*
*
****************************************************************************/
textBuffer::textBuffer(int size)
{
bufferStart = NULL;
nextAdd = NULL;
currentSize = spaceLeft = 0;
if (size > 0) {
// Insitute a minimum size
if (size < 1024) {
size = 1024;
}
bufferStart = (char *)ats_malloc(size);
nextAdd = bufferStart;
currentSize = size;
spaceLeft = size - 1; // Leave room for a terminator;
nextAdd[0] = '\0';
}
}
textBuffer::~textBuffer()
{
ats_free(bufferStart);
}
char *
textBuffer::release()
{
char *ret = bufferStart;
bufferStart = nextAdd = NULL;
currentSize = spaceLeft = 0;
return ret;
}
// void textBuffer::reUse()
//
// Sets the text buffer for reuse by repositioning the
// ptrs to beginning of buffer. The buffer space is
// reused
void
textBuffer::reUse()
{
if (bufferStart != NULL) {
nextAdd = bufferStart;
spaceLeft = currentSize - 1;
nextAdd[0] = '\0';
}
}
// int textBuffer::copyFrom(void*,int num_bytes)
//
//
// Copy N bytes (determined by num_bytes) on to the
// end of the buffer.
//
// Returns the number of bytes copies or
// -1 if there was insufficient memory
int
textBuffer::copyFrom(const void *source, unsigned num_bytes)
{
// Get more space if necessary
if (spaceLeft < num_bytes) {
if (enlargeBuffer(num_bytes) == -1) {
return -1;
}
}
memcpy(nextAdd, source, num_bytes);
spaceLeft -= num_bytes;
nextAdd += num_bytes;
nextAdd[0] = '\0';
return num_bytes;
}
// textBuffer::enlargeBuffer(int n)
//
// Enlarge the buffer so at least at N
// bytes are free in the buffer.
//
// Always enlarges by a power of two.
//
// Returns -1 if insufficient memory,
// zero otherwise
int
textBuffer::enlargeBuffer(unsigned N)
{
unsigned addedSize = 0;
unsigned newSize = (currentSize ? currentSize : 1) * 2;
char *newSpace;
if (spaceLeft < N) {
while ((newSize - currentSize) < N) {
newSize *= 2;
}
addedSize = newSize - currentSize;
newSpace = (char *)ats_realloc(bufferStart, newSize);
if (newSpace != NULL) {
nextAdd = newSpace + (unsigned)(nextAdd - bufferStart);
bufferStart = newSpace;
spaceLeft += addedSize;
currentSize = newSize;
} else {
// Out of Memory, Sigh
return -1;
}
}
return 0;
}
// int textBuffer::rawReadFromFile
//
// - Issues a single read command on the file descriptor or handle
// passed in and reads in raw data (not assumed to be text, no
// string terminators added).
// - Cannot read from file descriptor on win32 because the win32
// read() function replaces CR-LF with LF if the file is not
// opened in binary mode.
int
textBuffer::rawReadFromFile(int fd)
{
int readSize;
// Check to see if we have got a resonable amount of space left in our
// buffer, if not try to get somemore
if (spaceLeft < 4096) {
if (enlargeBuffer(4096) == -1) {
return -1;
}
}
readSize = read(fd, nextAdd, spaceLeft - 1);
if (readSize == 0) { // EOF
return 0;
} else if (readSize < 0) {
// Error on read
return readSize;
} else {
nextAdd = nextAdd + readSize;
spaceLeft -= readSize;
return readSize;
}
}
// Read the entire contents of the given file descriptor.
void
textBuffer::slurp(int fd)
{
int nbytes;
do {
nbytes = readFromFD(fd);
} while (nbytes > 0);
}
// int textBuffer::readFromFD(int fd)
//
// Issues a single read command on the file
// descritor passed in. Attempts to read a minimum of
// 512 bytes from file descriptor passed.
int
textBuffer::readFromFD(int fd)
{
int readSize;
// Check to see if we have got a resonable amount of space left in our
// buffer, if not try to get somemore
if (spaceLeft < 512) {
if (enlargeBuffer(512) == -1) {
return -1;
}
}
readSize = read(fd, nextAdd, spaceLeft - 1);
if (readSize == 0) {
// Socket is empty so we are done
return 0;
} else if (readSize < 0) {
// Error on read
return readSize;
} else {
nextAdd = nextAdd + readSize;
nextAdd[0] = '\0';
spaceLeft -= readSize + 1;
return readSize;
}
}
char *
textBuffer::bufPtr()
{
return bufferStart;
}
void
textBuffer::format(const char *fmt, ...)
{
va_list ap;
bool done = false;
do {
int num;
va_start(ap, fmt);
num = vsnprintf(this->nextAdd, this->spaceLeft, fmt, ap);
va_end(ap);
if ((unsigned)num < this->spaceLeft) {
// We had enough space to format including the NUL. Since the returned character
// count does not include the NUL, we can just increment and the next format will
// overwrite the previous NUL.
this->spaceLeft -= num;
this->nextAdd += num;
done = true;
} else {
if (enlargeBuffer(num + 1) == -1) {
return;
}
}
} while (!done);
}
void
textBuffer::chomp()
{
while ((nextAdd > bufferStart) && (nextAdd[-1] == '\n')) {
--nextAdd;
++spaceLeft;
*nextAdd = '\0';
}
}
|