Fix DealII type problems.
[official-gcc/Ramakrishna.git] / libpcp / pcp_string_buffer.cc
blob496d5d49682ce8356b4493665f472c610010d3af
1 // Copyright (C) 2009 Free Software Foundation, Inc.
2 // Contributed by Jan Sjodin <jan.sjodin@amd.com>.
4 // This file is part of the Polyhedral Compilation Package Library (libpcp).
6 // Libpcp is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation; either version 2.1 of the License, or
9 // (at your option) any later version.
11 // Libpcp is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with libpcp; see the file COPYING.LIB. If not, write to the
18 // Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 // MA 02110-1301, USA.
21 // As a special exception, if you link this library with other files, some
22 // of which are compiled with GCC, to produce an executable, this library
23 // does not by itself cause the resulting executable to be covered by the
24 // GNU General Public License. This exception does not however invalidate
25 // any other reasons why the executable file might be covered by the GNU
26 // General Public License.
28 // PCP String Buffer
29 #include "pcp_error.h"
30 #include "pcp_alloc.h"
31 #include "pcp_string_buffer.h"
32 #include <string.h>
34 // Set size of BUFFER to SIZE.
35 void
36 PcpStringBuffer::setSize(int size)
38 this->size = size;
41 // Get size of BUFFER.
42 int
43 PcpStringBuffer::getSize()
45 return this->size;
48 // Get length of string in BUFFER.
49 int
50 PcpStringBuffer::getLength()
52 return this->size - 1;
55 // Set capacity of BUFFER to CAPACITY.
56 void
57 PcpStringBuffer::setCapacity(int capacity)
59 this->capacity = capacity;
62 // Get capacity of BUFFER.
63 int
64 PcpStringBuffer::getCapacity()
66 return this->capacity;
69 // Set internal buffer of BUFFER to RAW_BUFFER.
70 void
71 PcpStringBuffer::setBuffer(char* rawBuffer)
73 this->buffer = rawBuffer;
76 // Get internal buffer of BUFFER.
77 char*
78 PcpStringBuffer::getBuffer()
80 return this->buffer;
83 // Convert BUFFER to string.
84 const char*
85 PcpStringBuffer::toString()
87 return this->getBuffer();
90 // Ensure that the internal buffer of BUFFER capacity is CAPACIY. If
91 // the current capacity is not enough the internal buffer is replaced
92 // with a bigger buffer and the contents are copied to the new
93 // buffer.
94 void
95 PcpStringBuffer::ensureCapacity(int capacity)
97 int oldCapacity = this->getCapacity();
98 if(oldCapacity < capacity)
100 int i;
101 int size = this->getSize();
102 int newCapacity = oldCapacity * 2 > capacity ? oldCapacity * 2
103 : capacity;
104 char* newBuffer =(char*) PCP_NEWVEC(int, newCapacity);
105 char* oldBuffer = this->getBuffer();
107 for(i = 0; i < size; i++)
109 newBuffer[i] = oldBuffer[i];
112 // FREE: oldBuffer.
113 this->setBuffer(newBuffer);
114 this->setCapacity(newCapacity);
118 // Create a new string buffer.
119 PcpStringBuffer::PcpStringBuffer()
121 this->setCapacity(0);
122 this->setSize(0);
123 this->ensureCapacity(16);
124 this->setSize(1);
125 this->getBuffer()[0] = '\0';
128 // Append STRING to BUFFER.
129 void
130 PcpStringBuffer::append(const char* string)
132 int oldSize = this->getSize();
133 int length = strlen(string);
134 int newSize = oldSize + length;
135 int startIndex = this->getLength();
136 int i;
138 this->ensureCapacity(newSize);
139 char* rawBuffer = this->getBuffer();
140 for(i = 0; i < length; i++)
142 rawBuffer[startIndex + i] = string[i];
144 rawBuffer[newSize - 1] = '\0';
145 this->setSize(newSize);
148 // Append newline to BUFFER.
149 void
150 PcpStringBuffer::newline()
152 this->append("\n");
155 // Append CHARACTER to BUFFER.
156 void
157 PcpStringBuffer::appendChar(char character)
159 char tempbuf[2];
160 sprintf(tempbuf, "%c", character);
161 this->append(tempbuf);
164 // Append integer VALUE to BUFFER.
165 void
166 PcpStringBuffer::appendInt(int value)
168 char tempbuf[64];
169 sprintf(tempbuf, "%d", value);
170 this->append(tempbuf);
173 // Append integer PTR to BUFFER.
174 void
175 PcpStringBuffer::appendPointer(void* ptr)
177 char tempbuf[64];
178 sprintf(tempbuf, "%p", ptr);
179 this->append(tempbuf);
182 // Append BUFFER2 to BUFFER.
183 void
184 PcpStringBuffer::appendBuffer(PcpStringBuffer* buffer2)
186 this->append(buffer2->toString());