00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
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 
00066 class scfx_string;
00067 
00068 
00069 
00070 
00071 
00072 
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 
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 } 
00226 
00227 
00228 #endif
00229 
00230