~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to src/utilfuns/swbuf.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Glassey
  • Date: 2004-01-15 15:50:07 UTC
  • Revision ID: james.westby@ubuntu.com-20040115155007-n9mz4x0zxrs1isd3
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
*  swbuf.cpp  - code for SWBuf used as a transport and utility for data buffers
 
3
*
 
4
* $Id: swbuf.cpp,v 1.14 2003/08/13 03:56:14 scribe Exp $
 
5
*
 
6
* Copyright 2003 CrossWire Bible Society (http://www.crosswire.org)
 
7
*       CrossWire Bible Society
 
8
*       P. O. Box 2528
 
9
*       Tempe, AZ  85280-2528
 
10
*
 
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.
 
14
*
 
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.
 
19
*
 
20
*/
 
21
 
 
22
#include <swbuf.h>
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdarg.h>
 
26
#include <stdio.h>
 
27
 
 
28
SWORD_NAMESPACE_START
 
29
 
 
30
char *SWBuf::nullStr = "";
 
31
char SWBuf::junkBuf[JUNKBUFSIZE];
 
32
 
 
33
/******************************************************************************
 
34
* SWBuf Constructor - Creates an empty SWBuf object or an SWBuf initialized
 
35
*               to a value from a const char *
 
36
*
 
37
*/
 
38
SWBuf::SWBuf(const char *initVal, unsigned long initSize) {
 
39
        init(initSize);
 
40
        set(initVal);
 
41
}
 
42
 
 
43
/******************************************************************************
 
44
* SWBuf Constructor - Creates an SWBuf initialized
 
45
*               to a value from another SWBuf
 
46
*
 
47
*/
 
48
SWBuf::SWBuf(const SWBuf &other, unsigned long initSize) {
 
49
        init(initSize);
 
50
        set(other);
 
51
}
 
52
 
 
53
/******************************************************************************
 
54
* SWBuf Constructor - Creates an SWBuf initialized
 
55
*               to a value from a char
 
56
*
 
57
*/
 
58
SWBuf::SWBuf(char initVal, unsigned long initSize) {
 
59
        init(initSize);
 
60
 
 
61
        allocSize = 15;
 
62
        buf = (char *)calloc(allocSize, 1);
 
63
        *buf = initVal;
 
64
        end = buf+1;
 
65
        endAlloc = buf + allocSize-1;
 
66
}
 
67
 
 
68
/*
 
69
SWBuf::SWBuf(unsigned long initSize) {
 
70
        init(initSize);
 
71
        set((const char *)0);
 
72
}
 
73
*/
 
74
 
 
75
 
 
76
void SWBuf::init(unsigned long initSize) {
 
77
        fillByte = ' ';
 
78
        allocSize = 0;
 
79
        endAlloc = 0;
 
80
        buf = 0;
 
81
        end = 0;
 
82
        if (initSize)
 
83
                assureSize(initSize);
 
84
}
 
85
 
 
86
/******************************************************************************
 
87
* SWBuf Destructor - Cleans up instance of SWBuf
 
88
*/
 
89
SWBuf::~SWBuf() {
 
90
        if (buf)
 
91
                free(buf);
 
92
}
 
93
 
 
94
/******************************************************************************
 
95
* SWBuf::set - sets this buf to a new value
 
96
*/
 
97
void SWBuf::set(const char *newVal) {
 
98
        if (newVal) {
 
99
                unsigned long len = strlen(newVal) + 1;
 
100
                assureSize(len);
 
101
                memcpy(buf, newVal, len);
 
102
                end = buf + (len - 1);
 
103
        }
 
104
        else {
 
105
                assureSize(1);
 
106
                end = buf;
 
107
                *end = 0;
 
108
        }
 
109
}
 
110
 
 
111
 
 
112
/******************************************************************************
 
113
* SWBuf::set - sets this buf to a new value
 
114
*/
 
115
void SWBuf::set(const SWBuf &newVal) {
 
116
        unsigned long len = newVal.length() + 1;
 
117
        assureSize(len);
 
118
        memcpy(buf, newVal.c_str(), len);
 
119
        end = buf + (len-1);
 
120
}
 
121
 
 
122
 
 
123
/******************************************************************************
 
124
* SWBuf::append - appends a value to the current value of this SWBuf
 
125
*/
 
126
void SWBuf::append(const char *str, long max) {
 
127
        unsigned long len = (max > -1) ? max : strlen(str);
 
128
        assureMore(++len);
 
129
        memcpy(end, str, len-1);
 
130
        end += (len-1);
 
131
        *end = 0;
 
132
}
 
133
 
 
134
 
 
135
/******************************************************************************
 
136
* SWBuf::setSize - Size this buffer to a specific length
 
137
*/
 
138
void SWBuf::setSize(unsigned long len) {
 
139
        assureSize(len+1);
 
140
        if ((end - buf) < len)
 
141
                memset(end, fillByte, len - (end-buf));
 
142
        end = buf + len;
 
143
        *end = 0;
 
144
}
 
145
 
 
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.
 
150
*/
 
151
void SWBuf::appendFormatted(const char *format, ...) {
 
152
        va_list argptr;
 
153
 
 
154
        va_start(argptr, format);
 
155
        int len = vsprintf(junkBuf, format, argptr)+1;
 
156
        va_end(argptr);
 
157
        assureMore(len);
 
158
        va_start(argptr, format);
 
159
        end += vsprintf(end, format, argptr);
 
160
        va_end(argptr);
 
161
}
 
162
 
 
163
SWORD_NAMESPACE_END