1 /*-------------------------------------------------------------------------
2 * drawElements Memory Pool Library
3 * --------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Memory pool management.
22 *//*--------------------------------------------------------------------*/
24 #include "dePoolStringBuilder.h"
30 typedef struct StringBlock_s
33 struct StringBlock_s* next;
36 struct dePoolStringBuilder_s
40 StringBlock* blockListHead;
41 StringBlock* blockListTail;
44 dePoolStringBuilder* dePoolStringBuilder_create (deMemPool* pool)
46 dePoolStringBuilder* builder = DE_POOL_NEW(pool, dePoolStringBuilder);
52 builder->blockListHead = DE_NULL;
53 builder->blockListTail = DE_NULL;
58 deBool dePoolStringBuilder_appendString (dePoolStringBuilder* builder, const char* str)
60 StringBlock* block = DE_POOL_NEW(builder->pool, StringBlock);
61 size_t len = strlen(str);
62 char* blockStr = (char*)deMemPool_alloc(builder->pool, len + 1);
64 if (!block || !blockStr)
67 /* Initialize block. */
75 block->str = blockStr;
76 block->next = DE_NULL;
79 /* Add block to list. */
80 if (builder->blockListTail)
81 builder->blockListTail->next = block;
83 builder->blockListHead = block;
85 builder->blockListTail = block;
87 builder->length += (int)len;
92 deBool dePoolStringBuilder_appendFormat (dePoolStringBuilder* builder, const char* format, ...)
98 va_start(args, format);
99 vsnprintf(buf, DE_LENGTH_OF_ARRAY(buf), format, args);
100 ok = dePoolStringBuilder_appendString(builder, buf);
106 /* \todo [2009-09-05 petri] Other appends? printf style? */
108 int dePoolStringBuilder_getLength (dePoolStringBuilder* builder)
110 return builder->length;
113 char* dePoolStringBuilder_dupToString (dePoolStringBuilder* builder)
115 return dePoolStringBuilder_dupToPool(builder, builder->pool);
118 char* dePoolStringBuilder_dupToPool (dePoolStringBuilder* builder, deMemPool* pool)
120 char* resultStr = (char*)deMemPool_alloc(pool, (size_t)builder->length + 1);
124 StringBlock* block = builder->blockListHead;
125 char* dstPtr = resultStr;
129 const char* p = block->str;
137 DE_ASSERT((int)strlen(resultStr) == builder->length);