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 #include "TUtilities.hh"
00029 #include "TRegularExpression.hh"
00030
00031 static const Tsize_t _buflen = 127;
00032
00033 Tstring TUtilities::ConvertIntegerToString( Tint i, Tint digits )
00034 {
00035 static Tchar buf[ _buflen + 1 ];
00036 Tostrstream os( buf, _buflen );
00037
00038 if ( i >= 0 ) {
00039 os << setfill( '0' ) << setiosflags( Tios::right ) << setw( digits );
00040 os << i << Tends;
00041 Tstring s = os.str();
00042 return s;
00043 } else {
00044 os << i << Tends;
00045 Tstring s = os.str();
00046 Tint nzero = digits - s.size();
00047 if ( nzero > 0 ) {
00048 s.insert( 1, nzero, '0' );
00049 }
00050 return s;
00051 }
00052 }
00053
00054 Tstring TUtilities::ConvertLongToString( Tlong l, Tint digits )
00055 {
00056 static Tchar buf[ _buflen + 1 ];
00057 Tostrstream os( buf, _buflen );
00058
00059 if ( l >= 0 ) {
00060 os << setfill( '0' ) << setiosflags( Tios::right ) << setw( digits );
00061 os << l << Tends;
00062 Tstring s = os.str();
00063 return s;
00064 } else {
00065 os << l << Tends;
00066 Tstring s = os.str();
00067 Tint nzero = digits - s.size();
00068 if ( nzero > 0 ) {
00069 s.insert( 1, nzero, '0' );
00070 }
00071 return s;
00072 }
00073 }
00074
00075 Tstring TUtilities::ConvertUnsignedLongToString( TUlong ul, Tint digits )
00076 {
00077 static Tchar buf[ _buflen + 1 ];
00078 Tostrstream os( buf, _buflen );
00079
00080 if ( ul >= 0 ) {
00081 os << setfill( '0' ) << setiosflags( Tios::right ) << setw( digits );
00082 os << ul << Tends;
00083 Tstring s = os.str();
00084 return s;
00085 } else {
00086 os << ul << Tends;
00087 Tstring s = os.str();
00088 Tint nzero = digits - s.size();
00089 if ( nzero > 0 ) {
00090 s.insert( 1, nzero, '0' );
00091 }
00092 return s;
00093 }
00094 }
00095
00096 Tstring TUtilities::ConvertDoubleToString( Tdouble d, Tint precision )
00097 {
00098 static Tchar buf[ _buflen + 1 ];
00099 Tostrstream os( buf, _buflen );
00100
00101 os << setprecision( precision ) << d << Tends;
00102 Tstring s = os.str();
00103 return s;
00104 }
00105
00106 Tstring TUtilities::ConvertFloatToString( Tfloat f, Tint precision )
00107 {
00108 return ConvertDoubleToString( (Tdouble)f, precision );
00109 }
00110
00111 Tbool TUtilities::FileExist( const Tstring& filename, Tint mode )
00112 {
00113 if ( access( filename.c_str(), mode ) == 0 ) {
00114 return Ttrue;
00115 } else {
00116 return Tfalse;
00117 }
00118 }
00119
00120 Tint TUtilities::ConvertStringToInteger( const Tstring& nptr, Tchar** endptr, Tint base )
00121 {
00122 return (Tint)strtol( nptr.c_str(), endptr, base );
00123 }
00124
00125 Tdouble TUtilities::ConvertStringToDouble( const Tstring& nptr, Tchar** endptr )
00126 {
00127 return strtod( nptr.c_str(), endptr );
00128 }
00129
00130 Tvoid TUtilities::ShowBitPattern( Tint bit )
00131 {
00132 Tint nbit = Tsizeof( bit ) * 8;
00133 for ( Tint i = nbit; i > 0; i -- ) {
00134 Tcout << ( ( bit >> i - 1 ) & 0x01 );
00135 }
00136 Tcout << Tendl;
00137 return;
00138 }
00139
00140 TstringList TUtilities::Split( const Tstring& source, const Tstring& pattern, Tint pos )
00141 {
00142 TRegularExpression regex( pattern );
00143 return regex.Split( source, pos );
00144 }
00145
00146 TintList TUtilities::Index( const Tstring& source, const Tstring& pattern, Tint pos )
00147 {
00148 TRegularExpression regex( pattern );
00149 return regex.Indexes( source, pos );
00150 }
00151
00152 TintList TUtilities::Size( const Tstring& source, const Tstring& pattern, Tint pos )
00153 {
00154 TRegularExpression regex( pattern );
00155 return regex.Sizes( source, pos );
00156 }
00157
00158 Tstring TUtilities::Substitute( const Tstring& source, const Tstring& pattern, const Tstring& substr, Tint pos )
00159 {
00160 TRegularExpression regex( pattern );
00161 return regex.Substitute( source, substr, pos );
00162 }
00163
00164 Tstring TUtilities::SubstituteAll( const Tstring& source, const Tstring& pattern, const Tstring& substr, Tint pos )
00165 {
00166 TRegularExpression regex( pattern );
00167 return regex.SubstituteAll( source, substr, pos );
00168 }
00169
00170 Tbool TUtilities::IsMatch( const Tstring& source, const Tstring& pattern, Tbool igcase, Tint pos )
00171 {
00172 TRegularExpression regex( pattern );
00173 if ( igcase == Ttrue ) {
00174 regex.IgnoreCase();
00175 }
00176 return regex.IsMatch( source, pos );
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 Tstring TUtilities::GetBaseName( const Tstring& path )
00190 {
00191 const Tsize_t len = path.size() + 1;
00192 Tchar* copy = new Tchar[ len ];
00193
00194 strncpy( copy, path.c_str(), len );
00195 Tchar* b = basename( copy );
00196 Tstring base( b );
00197 delete [] copy;
00198 return base;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 Tstring TUtilities::GetDirectoryName( const Tstring& path )
00212 {
00213 const Tsize_t len = path.size() + 1;
00214 Tchar* copy = new Tchar[ len ];
00215
00216 strncpy( copy, path.c_str(), len );
00217 Tchar* d = dirname( copy );
00218 Tstring dir( d );
00219 delete [] copy;
00220 return dir;
00221 }
00222
00223 Tstring TUtilities::GetSubMatch( Tint index, const Tstring& source, const Tstring& pattern, Tint pos )
00224 {
00225 TRegularExpression regex( pattern );
00226 return regex.GetSubMatch( index, source, pos );
00227 }
00228
00229 TstringList TUtilities::GetSubMatch( const Tstring& source, const Tstring& pattern, Tint pos )
00230 {
00231 TRegularExpression regex( pattern );
00232 return regex.GetSubMatch( source, pos );
00233 }
00234
00235 #ifdef __CLDAQ_ROOT_DLL
00236 ClassImp(TUtilities)
00237 #endif