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