00001 /***************************************************************************** 00002 00003 The following code is derived, directly or indirectly, from the SystemC 00004 source code Copyright (c) 1996-2006 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 // $Log: scfx_string.h,v $ 00037 // Revision 1.1.1.1 2006/12/15 20:31:36 acg 00038 // SystemC 2.2 00039 // 00040 // Revision 1.2 2006/01/03 23:18:34 acg 00041 // Changed copyright to include 2006. 00042 // 00043 // Revision 1.1.1.1 2005/12/19 23:16:43 acg 00044 // First check in of SystemC 2.1 into its own archive. 00045 // 00046 // Revision 1.9 2005/09/15 23:02:03 acg 00047 // Added std:: prefix to appropriate methods and types to get around 00048 // issues with the Edison Front End. 00049 // 00050 // Revision 1.8 2005/06/07 17:27:02 acg 00051 // Fixed bug in scfx_string::operator += where an array reference was used 00052 // rather than the [] operator. This meant that the buffer may have been 00053 // accessed beyond its allocated storage. 00054 // 00055 00056 #ifndef SCFX_STRING_H 00057 #define SCFX_STRING_H 00058 00059 #include <cstdio> 00060 00061 00062 namespace sc_dt 00063 { 00064 00065 // classes defined in this module 00066 class scfx_string; 00067 00068 00069 // ---------------------------------------------------------------------------- 00070 // CLASS : scfx_string 00071 // 00072 // Simple string class for internal use. 00073 // ---------------------------------------------------------------------------- 00074 00075 class scfx_string 00076 { 00077 void resize( std::size_t ); 00078 00079 public: 00080 00081 scfx_string(); 00082 00083 ~scfx_string(); 00084 00085 int length() const; 00086 00087 void clear(); 00088 00089 char& operator [] ( int ); 00090 00091 void append( int ); 00092 void discard( int ); 00093 void remove( int ); 00094 00095 void operator += ( char ); 00096 void operator += ( const char* ); 00097 00098 operator const char* (); 00099 00100 private: 00101 00102 std::size_t m_len; 00103 std::size_t m_alloc; 00104 char* m_buffer; 00105 }; 00106 00107 00108 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 00109 00110 inline 00111 void 00112 scfx_string::resize( std::size_t i ) 00113 { 00114 do { 00115 m_alloc *= 2; 00116 } while( i >= m_alloc ); 00117 00118 char* temp = new char[m_alloc]; 00119 00120 for( int j = 0; j < (int) m_len; ++ j ) { 00121 temp[j] = m_buffer[j]; 00122 } 00123 temp[m_len] = 0; 00124 00125 delete [] m_buffer; 00126 m_buffer = temp; 00127 } 00128 00129 00130 inline 00131 scfx_string::scfx_string() 00132 : m_len( 0 ), m_alloc( BUFSIZ ), m_buffer( new char[m_alloc] ) 00133 { 00134 m_buffer[m_len] = 0; 00135 } 00136 00137 00138 inline 00139 scfx_string::~scfx_string() 00140 { 00141 delete [] m_buffer; 00142 } 00143 00144 00145 inline 00146 int 00147 scfx_string::length() const 00148 { 00149 return m_len; 00150 } 00151 00152 00153 inline 00154 void 00155 scfx_string::clear() 00156 { 00157 m_len = 0; 00158 m_buffer[m_len] = 0; 00159 } 00160 00161 00162 inline 00163 char& 00164 scfx_string::operator [] ( int i ) 00165 { 00166 if( i >= (int) m_alloc ) { 00167 resize( i ); 00168 } 00169 return m_buffer[i]; 00170 } 00171 00172 00173 inline 00174 void 00175 scfx_string::append( int n ) 00176 { 00177 m_len += n; 00178 m_buffer[m_len] = 0; 00179 } 00180 00181 inline 00182 void 00183 scfx_string::discard( int n ) 00184 { 00185 m_len -= n; 00186 m_buffer[m_len] = 0; 00187 } 00188 00189 inline 00190 void 00191 scfx_string::remove( int i ) 00192 { 00193 for( int j = i + 1; j < (int) m_len; ++ j ) 00194 m_buffer[j - 1] = m_buffer[j]; 00195 -- m_len; 00196 m_buffer[m_len] = 0; 00197 } 00198 00199 00200 inline 00201 void 00202 scfx_string::operator += ( char c ) 00203 { 00204 this->operator [] ( m_len ) = c; 00205 m_len ++; 00206 this->operator [] ( m_len ) = 0; 00207 } 00208 00209 inline 00210 void 00211 scfx_string::operator += ( const char* s ) 00212 { 00213 while( *s ) 00214 (*this) += *s ++; 00215 } 00216 00217 00218 inline 00219 scfx_string::operator const char*() 00220 { 00221 m_buffer[m_len] = 0; 00222 return m_buffer; 00223 } 00224 00225 } // namespace sc_dt 00226 00227 00228 #endif 00229 00230 // Taf!