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

Generated on Wed Jan 21 15:32:10 2009 for SystemC by  doxygen 1.5.5