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
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.
29 #include "pcp_error.h"
30 #include "pcp_alloc.h"
31 #include "pcp_string_buffer.h"
34 // Set size of BUFFER to SIZE.
36 PcpStringBuffer::setSize(int size
)
41 // Get size of BUFFER.
43 PcpStringBuffer::getSize()
48 // Get length of string in BUFFER.
50 PcpStringBuffer::getLength()
52 return this->size
- 1;
55 // Set capacity of BUFFER to CAPACITY.
57 PcpStringBuffer::setCapacity(int capacity
)
59 this->capacity
= capacity
;
62 // Get capacity of BUFFER.
64 PcpStringBuffer::getCapacity()
66 return this->capacity
;
69 // Set internal buffer of BUFFER to RAW_BUFFER.
71 PcpStringBuffer::setBuffer(char* rawBuffer
)
73 this->buffer
= rawBuffer
;
76 // Get internal buffer of BUFFER.
78 PcpStringBuffer::getBuffer()
83 // Convert BUFFER to string.
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
95 PcpStringBuffer::ensureCapacity(int capacity
)
97 int oldCapacity
= this->getCapacity();
98 if(oldCapacity
< capacity
)
101 int size
= this->getSize();
102 int newCapacity
= oldCapacity
* 2 > capacity
? oldCapacity
* 2
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
];
113 this->setBuffer(newBuffer
);
114 this->setCapacity(newCapacity
);
118 // Create a new string buffer.
119 PcpStringBuffer::PcpStringBuffer()
121 this->setCapacity(0);
123 this->ensureCapacity(16);
125 this->getBuffer()[0] = '\0';
128 // Append STRING to BUFFER.
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();
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.
150 PcpStringBuffer::newline()
155 // Append CHARACTER to BUFFER.
157 PcpStringBuffer::appendChar(char character
)
160 sprintf(tempbuf
, "%c", character
);
161 this->append(tempbuf
);
164 // Append integer VALUE to BUFFER.
166 PcpStringBuffer::appendInt(int value
)
169 sprintf(tempbuf
, "%d", value
);
170 this->append(tempbuf
);
173 // Append integer PTR to BUFFER.
175 PcpStringBuffer::appendPointer(void* ptr
)
178 sprintf(tempbuf
, "%p", ptr
);
179 this->append(tempbuf
);
182 // Append BUFFER2 to BUFFER.
184 PcpStringBuffer::appendBuffer(PcpStringBuffer
* buffer2
)
186 this->append(buffer2
->toString());