src/sysc/datatypes/fx/scfx_string.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2005 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 2.4 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 /*****************************************************************************
00019 
00020   scfx_string.h - 
00021 
00022   Original Author: Robert Graulich, Synopsys, Inc.
00023                    Martin Janssen,  Synopsys, Inc.
00024 
00025  *****************************************************************************/
00026 
00027 /*****************************************************************************
00028 
00029   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00030   changes you are making here.
00031 
00032       Name, Affiliation, Date:
00033   Description of Modification:
00034 
00035  *****************************************************************************/
00036 
00037 #ifndef SCFX_STRING_H
00038 #define SCFX_STRING_H
00039 
00040 #include <cstdio>
00041 
00042 
00043 namespace sc_dt
00044 {
00045 
00046 // classes defined in this module
00047 class scfx_string;
00048 
00049 
00050 // ----------------------------------------------------------------------------
00051 //  CLASS : scfx_string
00052 //
00053 //  Simple string class for internal use.
00054 // ----------------------------------------------------------------------------
00055 
00056 class scfx_string
00057 {
00058     void resize( size_t );
00059 
00060 public:
00061 
00062     scfx_string();
00063 
00064     ~scfx_string();
00065 
00066     int length() const;
00067 
00068     void clear();
00069 
00070     char& operator [] ( int );
00071 
00072     void append( int );
00073     void discard( int );
00074     void remove( int );
00075 
00076     void operator += ( char );
00077     void operator += ( const char* );
00078 
00079     operator const char* ();
00080 
00081 private:
00082 
00083     size_t m_len;
00084     size_t m_alloc;
00085     char*  m_buffer;
00086 };
00087 
00088 
00089 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00090 
00091 inline
00092 void
00093 scfx_string::resize( size_t i )
00094 {
00095     do {
00096         m_alloc *= 2;
00097     } while( i >= m_alloc );
00098 
00099     char* temp = new char[m_alloc];
00100 
00101     for( int j = 0; j < (int) m_len; ++ j ) {
00102         temp[j] = m_buffer[j];
00103     }
00104     temp[m_len] = 0;
00105 
00106     delete [] m_buffer;
00107     m_buffer = temp;
00108 }
00109 
00110 
00111 inline
00112 scfx_string::scfx_string()
00113 : m_len( 0 ), m_alloc( BUFSIZ ), m_buffer( new char[m_alloc] )
00114 {
00115     m_buffer[m_len] = 0;
00116 }
00117 
00118 
00119 inline
00120 scfx_string::~scfx_string()
00121 {
00122     delete [] m_buffer;
00123 }
00124 
00125 
00126 inline
00127 int
00128 scfx_string::length() const
00129 {
00130     return m_len;
00131 }
00132 
00133 
00134 inline
00135 void
00136 scfx_string::clear()
00137 {
00138     m_len = 0;
00139     m_buffer[m_len] = 0;
00140 }
00141 
00142 
00143 inline
00144 char&
00145 scfx_string::operator [] ( int i )
00146 {
00147     if( i >= (int) m_alloc ) {
00148         resize( i );
00149     }
00150     return m_buffer[i];
00151 }
00152 
00153 
00154 inline
00155 void
00156 scfx_string::append( int n )
00157 {
00158     m_len += n;
00159     m_buffer[m_len] = 0;
00160 }
00161 
00162 inline
00163 void
00164 scfx_string::discard( int n )
00165 {
00166     m_len -= n;
00167     m_buffer[m_len] = 0;
00168 }
00169 
00170 inline
00171 void
00172 scfx_string::remove( int i )
00173 {
00174     for( int j = i + 1; j < (int) m_len; ++ j )
00175         m_buffer[j - 1] = m_buffer[j];
00176     -- m_len;
00177     m_buffer[m_len] = 0;
00178 }
00179 
00180 
00181 inline
00182 void
00183 scfx_string::operator += ( char c )
00184 {
00185     this->operator [] ( m_len ) = c;
00186     m_len ++;
00187     this->operator [] ( m_len ) = 0;
00188 }
00189 
00190 inline
00191 void
00192 scfx_string::operator += ( const char* s )
00193 {
00194     while( *s )
00195         (*this) += *s ++;
00196 }
00197 
00198 
00199 inline
00200 scfx_string::operator const char*()
00201 {
00202     m_buffer[m_len] = 0;
00203     return m_buffer;
00204 }
00205 
00206 } // namespace sc_dt
00207 
00208 
00209 #endif
00210 
00211 // Taf!

Generated on Wed Apr 25 13:53:26 2007 for SystemC by  doxygen 1.5.1