src/sysc/utils/sc_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   sc_string.h -- Implementation of a simple string class.
00021 
00022   Original Author: Stan Y. Liao, Synopsys, Inc.
00023 
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027 
00028   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00029   changes you are making here.
00030 
00031       Name, Affiliation, Date:
00032   Description of Modification:
00033 
00034  *****************************************************************************/
00035 
00036 
00037 #ifndef SC_STRING_H
00038 #define SC_STRING_H
00039 
00040 
00041 #include "sysc/utils/sc_iostream.h"
00042 #include "sysc/utils/sc_report.h"
00043 
00044 namespace sc_dt {
00045         class sc_string_old;
00046 }
00047 
00048 #ifdef SC_USE_SC_STRING_OLD
00049         typedef sc_dt::sc_string_old sc_string;
00050 #endif
00051 #ifdef SC_USE_STD_STRING
00052         typedef ::std::string sc_string;
00053 #endif
00054 
00055 namespace sc_dt {
00056 
00057 // forward class declarations
00058 class sc_string_rep;
00059 
00060 // ----------------------------------------------------------------------------
00061 //  ENUM : sc_numrep
00062 //
00063 //  Enumeration of number representations for character string conversion.
00064 // ----------------------------------------------------------------------------
00065 
00066 enum sc_numrep
00067 {
00068     SC_NOBASE = 0,
00069     SC_BIN    = 2,
00070     SC_OCT    = 8,
00071     SC_DEC    = 10,
00072     SC_HEX    = 16,
00073     SC_BIN_US,
00074     SC_BIN_SM,
00075     SC_OCT_US,
00076     SC_OCT_SM,
00077     SC_HEX_US,
00078     SC_HEX_SM,
00079     SC_CSD
00080 };
00081 
00082 // We use typedefs for istream and ostream here to get around some finickiness
00083 // from aCC:
00084 
00085 typedef ::std::istream systemc_istream;
00086 typedef ::std::ostream systemc_ostream;
00087 
00088 const std::string to_string( sc_numrep );
00089 
00090 //------------------------------------------------------------------------------
00091 //"sc_io_base"
00092 //
00093 // This inline function returns the type of an i/o stream's base as a SystemC
00094 // base designator.
00095 //     stream_object = reference to the i/o stream whose base is to be returned.
00096 //
00097 //"sc_io_show_base"
00098 //
00099 // This inline function returns true if the base should be shown when a SystemC
00100 // value is displayed via the supplied stream operator.
00101 //     stream_object = reference to the i/o stream to return showbase value for.
00102 //------------------------------------------------------------------------------
00103 #if defined(__GNUC__)  // GNU C++ compiler.
00104     inline sc_numrep sc_io_base( systemc_ostream& stream_object, 
00105         sc_numrep def_base )
00106     {
00107 	::std::ios::fmtflags flags = 
00108             stream_object.flags() & ::std::ios::basefield;
00109         if ( flags & ::std::ios::dec ) return  SC_DEC;
00110         if ( flags & ::std::ios::hex ) return  SC_HEX;
00111         if ( flags & ::std::ios::oct ) return  SC_OCT;
00112         return def_base;
00113     }
00114     inline bool sc_io_show_base( systemc_ostream& stream_object )
00115     {
00116         return stream_object.flags() & ::std::ios::showbase;
00117     }
00118 #else   // Other
00119     inline sc_numrep sc_io_base( systemc_ostream& stream_object, 
00120         sc_numrep def_base ) 
00121     { 
00122         return SC_DEC; 
00123     }
00124     inline bool sc_io_show_base( systemc_ostream& stream_object ) 
00125     { 
00126         return false; 
00127     }
00128 #endif
00129 
00130 
00131 // ----------------------------------------------------------------------------
00132 //  CLASS : sc_string
00133 //
00134 //  String class (yet another).
00135 // ----------------------------------------------------------------------------
00136 
00137 class sc_string_old 
00138 {
00139     friend systemc_ostream& operator << (systemc_ostream& os, const sc_string_old& a);
00140     friend systemc_istream& operator >> ( systemc_istream& is, sc_string_old& a );
00141 
00142 public:
00143 
00144     //  constructors
00145 
00146     explicit sc_string_old( int size = 16 );
00147     sc_string_old( const char* s );
00148     sc_string_old( const char* s, int n ); // get first n chars from the string
00149     sc_string_old( const sc_string_old& s );
00150 
00151 
00152     // destructor
00153 
00154     ~sc_string_old();
00155 
00156 
00157     // concatenation and assignment
00158 
00159     sc_string_old& operator = ( const char* s );
00160     sc_string_old& operator = ( const sc_string_old& s );
00161 
00162     sc_string_old& operator += ( const char* s );
00163     sc_string_old& operator += ( char c );
00164     sc_string_old& operator += ( const sc_string_old& s );
00165 
00166     sc_string_old operator + ( const char* s ) const;
00167     sc_string_old operator + ( char c ) const;
00168     sc_string_old operator + ( const sc_string_old& s ) const;
00169 
00170     friend sc_string_old operator + ( const char* s, const sc_string_old& t );
00171 
00172 
00173     // returns substring [first,last]
00174 
00175     sc_string_old substr( int first, int last ) const;
00176 
00177 
00178     // string comparison operators
00179 
00180     bool operator == ( const char* s ) const;
00181     bool operator != ( const char* s ) const;
00182     bool operator <  ( const char* s ) const;
00183     bool operator <= ( const char* s ) const;
00184     bool operator >  ( const char* s ) const;
00185     bool operator >= ( const char* s ) const;
00186     bool operator == ( const sc_string_old& s ) const;
00187     bool operator != ( const sc_string_old& s ) const;
00188     bool operator <  ( const sc_string_old& s ) const;
00189     bool operator <= ( const sc_string_old& s ) const;
00190     bool operator >  ( const sc_string_old& s ) const;
00191     bool operator >= ( const sc_string_old& s ) const;
00192 
00193     //
00194     // returns length of the string (excluding trailing \0)
00195     //
00196     int length() const;
00197 
00198     //
00199     // returns c-style string
00200     //
00201     const char* c_str() const;
00202     //
00203     // returns c-style string
00204     //
00205     operator const char*() const;
00206     //
00207     // returns character at "index" position
00208     //
00209     char operator[](int index) const;
00210     //
00211     // l-value subscript
00212     //
00213     char& operator[](int index);
00214 
00215     // formatted string (see printf description)
00216     static sc_string_old to_string(const char* format, ...);
00217     //
00218     //       conveniece formatting functions for common types
00219     //       e.g. sc_string_old("a=%d, s is %s").fmt(1).fmt("string")
00220     //       should produce: a=1, s is string
00221     //       it should be safe: if less arguments specified
00222     //       it should print %specifier; extra arguments should be ignored
00223     // TODO: if the type of the argument is incompatible with format 
00224     //       specifier it should be ignored
00225     //
00226     // must have it inlined because of some compilers
00227     template<class T> sc_string_old& fmt(const T& t)
00228         {
00229             // search %
00230             int index;
00231             int last_char = length()-1;
00232             sc_string_old temp(*this);
00233             do
00234             {
00235                 index = temp.pos("%");
00236                 if(index == last_char) 
00237                     return *this;
00238                 temp = substr(index,last_char);
00239             } while(temp[0] != '%');
00240             int f_len = (int)temp.fmt_length(); // length of format field
00241             temp = to_string(substr(0,index+f_len-1).c_str(),t);
00242             return (*this) = temp + substr(index+f_len,last_char);
00243         }
00244     sc_string_old& fmt(const sc_string_old& s);
00245     //
00246     // find position of substring in this string 
00247     // returns -1 if not found
00248     //
00249     int pos(const sc_string_old& sub_string)const;
00250     //
00251     // remove "count" characters from "index"
00252     //
00253     sc_string_old& remove(unsigned index, unsigned length);
00254     //
00255     // insert "substring" before "index"
00256     //
00257     sc_string_old& insert(const sc_string_old& sub_string, unsigned index);
00258     //
00259     // returns true if the character at byte index in this string matches 
00260     // any character in the delimiters string
00261     //
00262     bool is_delimiter(const sc_string_old& str, unsigned index)const;
00263     //
00264     // returns true if string contains the character
00265     //
00266     bool contains(char c)const;
00267     //
00268     // produce upper case string from this one
00269     //
00270     sc_string_old uppercase()const;
00271     //
00272     // produce lower case string from this one
00273     //
00274     sc_string_old lowercase()const;
00275     //
00276     // legacy methods
00277     //
00278     static sc_string_old make_str(long n);
00279     void set( int index, char c );
00280     int cmp( const char* s ) const;
00281     int cmp( const sc_string_old& s ) const;
00282 
00283 
00284     void print( systemc_ostream& os = ::std::cout ) const;
00285 
00286 private:
00287 
00288     sc_string_old( sc_string_rep* r );
00289 
00290     sc_string_rep* rep;
00291 
00292     void test(int position)const;
00293     unsigned fmt_length()const;
00294 };
00295 
00296 
00297 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00298 
00299 inline
00300 systemc_ostream&
00301 operator << ( systemc_ostream& os, sc_numrep numrep )
00302 {
00303     os << to_string( numrep );
00304     return os;
00305 }
00306 
00307 
00308 inline
00309 systemc_ostream&
00310 operator << ( systemc_ostream& os, const sc_string_old& a )
00311 {
00312     a.print( os );
00313     return os;
00314 }
00315 
00316 } // namespace sc_dt
00317 
00318 #endif

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