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 __SYSTEMC_EXT_DT_FX_SCFX_STRING_HH__
59#define __SYSTEMC_EXT_DT_FX_SCFX_STRING_HH__
60
61#include <cstdio>
62
63namespace sc_dt
64{
65
66// classes defined in this module
67class scfx_string;
68
69
70// ----------------------------------------------------------------------------
71//  CLASS : scfx_string
72//
73//  Simple string class for internal use.
74// ----------------------------------------------------------------------------
75
76class scfx_string
77{
78    void resize(std::size_t);
79
80  public:
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    std::size_t m_len;
102    std::size_t m_alloc;
103    char *m_buffer;
104};
105
106
107// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
108
109inline void
110scfx_string::resize(std::size_t i)
111{
112    do {
113        m_alloc *= 2;
114    } while (i >= m_alloc);
115
116    char *temp = new char[m_alloc];
117
118    for (int j = 0; j < (int) m_len; ++j) {
119        temp[j] = m_buffer[j];
120    }
121    temp[m_len] = 0;
122
123    delete [] m_buffer;
124    m_buffer = temp;
125}
126
127inline scfx_string::scfx_string() :
128        m_len(0), m_alloc(BUFSIZ), m_buffer(new char[m_alloc])
129{
130    m_buffer[m_len] = 0;
131}
132
133inline scfx_string::~scfx_string() { delete [] m_buffer; }
134
135inline int scfx_string::length() const { return m_len; }
136
137inline void
138scfx_string::clear()
139{
140    m_len = 0;
141    m_buffer[m_len] = 0;
142}
143
144inline char &
145scfx_string::operator [] (int i)
146{
147    if (i >= (int)m_alloc) {
148        resize(i);
149    }
150    return m_buffer[i];
151}
152
153inline void
154scfx_string::append(int n)
155{
156    m_len += n;
157    m_buffer[m_len] = 0;
158}
159
160inline void
161scfx_string::discard(int n)
162{
163    m_len -= n;
164    m_buffer[m_len] = 0;
165}
166
167inline void
168scfx_string::remove(int i)
169{
170    for (int j = i + 1; j < (int)m_len; ++j)
171        m_buffer[j - 1] = m_buffer[j];
172    --m_len;
173    m_buffer[m_len] = 0;
174}
175
176inline void
177scfx_string::operator += (char c)
178{
179    this->operator [] (m_len) = c;
180    m_len++;
181    this->operator [] (m_len) = 0;
182}
183
184inline void
185scfx_string::operator += (const char *s)
186{
187    while (*s)
188        (*this) += *s++;
189}
190
191inline scfx_string::operator const char * ()
192{
193    m_buffer[m_len] = 0;
194    return m_buffer;
195}
196
197} // namespace sc_dt
198
199#endif // __SYSTEMC_EXT_DT_FX_SCFX_STRING_HH__
200