1
/******************************************************************************
2
* swbuf.cpp - code for SWBuf used as a transport and utility for data buffers
4
* $Id: swbuf.cpp,v 1.14 2003/08/13 03:56:14 scribe Exp $
6
* Copyright 2003 CrossWire Bible Society (http://www.crosswire.org)
7
* CrossWire Bible Society
11
* This program is free software; you can redistribute it and/or modify it
12
* under the terms of the GNU General Public License as published by the
13
* Free Software Foundation version 2.
15
* This program is distributed in the hope that it will be useful, but
16
* WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* General Public License for more details.
30
char *SWBuf::nullStr = "";
31
char SWBuf::junkBuf[JUNKBUFSIZE];
33
/******************************************************************************
34
* SWBuf Constructor - Creates an empty SWBuf object or an SWBuf initialized
35
* to a value from a const char *
38
SWBuf::SWBuf(const char *initVal, unsigned long initSize) {
43
/******************************************************************************
44
* SWBuf Constructor - Creates an SWBuf initialized
45
* to a value from another SWBuf
48
SWBuf::SWBuf(const SWBuf &other, unsigned long initSize) {
53
/******************************************************************************
54
* SWBuf Constructor - Creates an SWBuf initialized
55
* to a value from a char
58
SWBuf::SWBuf(char initVal, unsigned long initSize) {
62
buf = (char *)calloc(allocSize, 1);
65
endAlloc = buf + allocSize-1;
69
SWBuf::SWBuf(unsigned long initSize) {
76
void SWBuf::init(unsigned long initSize) {
86
/******************************************************************************
87
* SWBuf Destructor - Cleans up instance of SWBuf
94
/******************************************************************************
95
* SWBuf::set - sets this buf to a new value
97
void SWBuf::set(const char *newVal) {
99
unsigned long len = strlen(newVal) + 1;
101
memcpy(buf, newVal, len);
102
end = buf + (len - 1);
112
/******************************************************************************
113
* SWBuf::set - sets this buf to a new value
115
void SWBuf::set(const SWBuf &newVal) {
116
unsigned long len = newVal.length() + 1;
118
memcpy(buf, newVal.c_str(), len);
123
/******************************************************************************
124
* SWBuf::append - appends a value to the current value of this SWBuf
126
void SWBuf::append(const char *str, long max) {
127
unsigned long len = (max > -1) ? max : strlen(str);
129
memcpy(end, str, len-1);
135
/******************************************************************************
136
* SWBuf::setSize - Size this buffer to a specific length
138
void SWBuf::setSize(unsigned long len) {
140
if ((end - buf) < len)
141
memset(end, fillByte, len - (end-buf));
146
/******************************************************************************
147
* SWBuf::appendFormatted - appends formatted strings to the current value of this SWBuf
148
* WARNING: This function can only write at most
149
* JUNKBUFSIZE to the string per call.
151
void SWBuf::appendFormatted(const char *format, ...) {
154
va_start(argptr, format);
155
int len = vsprintf(junkBuf, format, argptr)+1;
158
va_start(argptr, format);
159
end += vsprintf(end, format, argptr);