1/*
2 * Legal Notice
3 *
4 * This document and associated source code (the "Work") is a part of a
5 * benchmark specification maintained by the TPC.
6 *
7 * The TPC reserves all right, title, and interest to the Work as provided
8 * under U.S. and international laws, including without limitation all patent
9 * and trademark rights therein.
10 *
11 * No Warranty
12 *
13 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
14 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
15 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
16 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
17 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
18 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
19 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
20 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
21 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
22 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
23 * WITH REGARD TO THE WORK.
24 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
25 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
26 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
27 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
28 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
29 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
30 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
31 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * Contributors:
34 * Gradient Systems
35 */
36#include "config.h"
37#include "porting.h"
38#include <stdio.h>
39#include <assert.h>
40#ifndef USE_STDLIB_H
41#include <malloc.h>
42#endif
43#include "StringBuffer.h"
44
45/*
46 * Routine: InitBuffer
47 * Purpose:
48 * Algorithm:
49 * Data Structures:
50 *
51 * Params:
52 * Returns:
53 * Called By:
54 * Calls:
55 * Assumptions:
56 * Side Effects:
57 * TODO: None
58 */
59StringBuffer_t *InitBuffer(int nSize, int nIncrement) {
60 StringBuffer_t *pBuf;
61
62 pBuf = (StringBuffer_t *)malloc(sizeof(struct STRING_BUFFER_T));
63 MALLOC_CHECK(pBuf);
64 if (pBuf == NULL)
65 return (NULL);
66 memset((void *)pBuf, 0, sizeof(struct STRING_BUFFER_T));
67
68 pBuf->pText = (char *)malloc(sizeof(char) * nSize);
69 MALLOC_CHECK(pBuf->pText);
70 if (pBuf->pText == NULL)
71 return (NULL);
72 memset((void *)pBuf->pText, 0, sizeof(char) * nSize);
73
74 pBuf->nIncrement = nIncrement;
75 pBuf->nBytesAllocated = nSize;
76 pBuf->nFlags = SB_INIT;
77
78 return (pBuf);
79}
80
81/*
82 * Routine: AddBuffer
83 * Purpose:
84 * Algorithm:
85 * Data Structures:
86 *
87 * Params:
88 * Returns:
89 * Called By:
90 * Calls:
91 * Assumptions:
92 * Side Effects:
93 * TODO: None
94 */
95int AddBuffer(StringBuffer_t *pBuf, char *pStr) {
96 int nRemaining = pBuf->nBytesAllocated - pBuf->nBytesUsed, nRequested = strlen(pStr);
97
98 if (!nRequested)
99 return (0);
100
101 while (nRequested >= nRemaining) {
102 pBuf->pText = (char *)realloc((void *)pBuf->pText, pBuf->nBytesAllocated + pBuf->nIncrement);
103 if (!pBuf->pText)
104 return (-1);
105 pBuf->nBytesAllocated += pBuf->nIncrement;
106 nRemaining += pBuf->nIncrement;
107 }
108
109 strcat(pBuf->pText, pStr);
110 if (pBuf->nBytesUsed == 0) /* first string adds a terminator */
111 pBuf->nBytesUsed = 1;
112 pBuf->nBytesUsed += nRequested;
113
114 return (0);
115}
116
117/*
118 * Routine: ResetStringBuffer
119 * Purpose:
120 * Algorithm:
121 * Data Structures:
122 *
123 * Params:
124 * Returns:
125 * Called By:
126 * Calls:
127 * Assumptions:
128 * Side Effects:
129 * TODO: None
130 */
131int ResetBuffer(StringBuffer_t *pBuf) {
132 pBuf->nBytesUsed = 0;
133 if (pBuf->nBytesAllocated)
134 pBuf->pText[0] = '\0';
135
136 return (0);
137}
138
139/*
140 * Routine: GetBuffer
141 * Purpose:
142 * Algorithm:
143 * Data Structures:
144 *
145 * Params:
146 * Returns:
147 * Called By:
148 * Calls:
149 * Assumptions:
150 * Side Effects:
151 * TODO: None
152 */
153char *GetBuffer(StringBuffer_t *pBuf) {
154 return (pBuf->pText);
155}
156
157/*
158 * Routine: FreeBuffer
159 * Purpose:
160 * Algorithm:
161 * Data Structures:
162 *
163 * Params:
164 * Returns:
165 * Called By:
166 * Calls:
167 * Assumptions:
168 * Side Effects:
169 * TODO: None
170 */
171void FreeBuffer(StringBuffer_t *pBuf) {
172 if (!pBuf)
173 return;
174 if (pBuf->pText)
175 free((void *)pBuf->pText);
176 free((void *)pBuf);
177
178 return;
179}
180