SystemC 3.0.0
Accellera SystemC proof-of-concept library
scfx_string.h
Go to the documentation of this file.
1/*****************************************************************************
2
3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4 more contributor license agreements. See the NOTICE file distributed
5 with this work for additional information regarding copyright ownership.
6 Accellera licenses this file to you under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with the
8 License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22 scfx_string.h -
23
24 Original Author: Robert Graulich, Synopsys, Inc.
25 Martin Janssen, Synopsys, Inc.
26
27 *****************************************************************************/
28
29/*****************************************************************************
30
31 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
32 changes you are making here.
33
34 Name, Affiliation, Date:
35 Description of Modification:
36
37 *****************************************************************************/
38// $Log: scfx_string.h,v $
39// Revision 1.1.1.1 2006/12/15 20:20:04 acg
40// SystemC 2.3
41//
42// Revision 1.2 2006/01/03 23:18:34 acg
43// Changed copyright to include 2006.
44//
45// Revision 1.1.1.1 2005/12/19 23:16:43 acg
46// First check in of SystemC 2.1 into its own archive.
47//
48// Revision 1.9 2005/09/15 23:02:03 acg
49// Added std:: prefix to appropriate methods and types to get around
50// issues with the Edison Front End.
51//
52// Revision 1.8 2005/06/07 17:27:02 acg
53// Fixed bug in scfx_string::operator += where an array reference was used
54// rather than the [] operator. This meant that the buffer may have been
55// accessed beyond its allocated storage.
56//
57
58#ifndef SCFX_STRING_H
59#define SCFX_STRING_H
60
61#include <cstdio>
62
63
64namespace sc_dt
65{
66
67// classes defined in this module
68class scfx_string;
69
70
71// ----------------------------------------------------------------------------
72// CLASS : scfx_string
73//
74// Simple string class for internal use.
75// ----------------------------------------------------------------------------
76
78{
79 void resize( std::size_t );
80
81public:
82
84
86
87 int length() const;
88
89 void clear();
90
91 char& operator [] ( int );
92
93 void append( int );
94 void discard( int );
95 void remove( int );
96
97 void operator += ( char );
98 void operator += ( const char* );
99
100 operator const char* ();
101
102private:
103
104 std::size_t m_len;
105 std::size_t m_alloc;
106 char* m_buffer;
107};
108
109
110// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
111
112inline
113void
114scfx_string::resize( std::size_t i )
115{
116 do {
117 m_alloc *= 2;
118 } while( i >= m_alloc );
119
120 char* temp = new char[m_alloc];
121
122 for( int j = 0; j < (int) m_len; ++ j ) {
123 temp[j] = m_buffer[j];
124 }
125 temp[m_len] = 0;
126
127 delete [] m_buffer;
128 m_buffer = temp;
129}
130
131
132inline
134: m_len( 0 ), m_alloc( BUFSIZ ), m_buffer( new char[m_alloc] )
135{
136 m_buffer[m_len] = 0;
137}
138
139
140inline
142{
143 delete [] m_buffer;
144}
145
146
147inline
148int
150{
151 return m_len;
152}
153
154
155inline
156void
158{
159 m_len = 0;
160 m_buffer[m_len] = 0;
161}
162
163
164inline
165char&
167{
168 if( i >= (int) m_alloc ) {
169 resize( i );
170 }
171 return m_buffer[i];
172}
173
174
175inline
176void
178{
179 m_len += n;
180 m_buffer[m_len] = 0;
181}
182
183inline
184void
186{
187 m_len -= n;
188 m_buffer[m_len] = 0;
189}
190
191inline
192void
194{
195 for( int j = i + 1; j < (int) m_len; ++ j )
196 m_buffer[j - 1] = m_buffer[j];
197 -- m_len;
198 m_buffer[m_len] = 0;
199}
200
201
202inline
203void
205{
206 this->operator [] ( m_len ) = c;
207 m_len ++;
208 this->operator [] ( m_len ) = 0;
209}
210
211inline
212void
214{
215 while( *s )
216 (*this) += *s ++;
217}
218
219
220inline
221scfx_string::operator const char*()
222{
223 m_buffer[m_len] = 0;
224 return m_buffer;
225}
226
227} // namespace sc_dt
228
229
230#endif
231
232// Taf!
#define SC_API
Definition: sc_cmnhdr.h:148
char & operator[](int)
Definition: scfx_string.h:166
void operator+=(char)
Definition: scfx_string.h:204
int length() const
Definition: scfx_string.h:149