メインページ   モジュール   名前空間一覧   クラス階層   アルファベット順一覧   構成   ファイル一覧   構成メンバ   ファイルメンバ   関連ページ    

TUtilities.cc

解説を見る。
00001 // ============================================================================
00002 //  $Id: TUtilities.cc,v 1.6 2004/03/07 10:30:34 goiwai Exp $
00003 //  $Name: CLDAQ-1-14-03 $
00004 //  $Log: TUtilities.cc,v $
00005 //  Revision 1.6  2004/03/07 10:30:34  goiwai
00006 //  ROOTに組みこむためのおまじないマクロを埋めこみました。
00007 //  全てにおいて完全に動作するわけではありません。
00008 //
00009 //  Revision 1.5  2004/01/29 04:25:07  goiwai
00010 //  いくつかの関数で微妙に仕様変更しました.
00011 //  あとは,substituteをconst sourceのconstをとろうかどうしようか迷ってます.
00012 //
00013 //  Revision 1.4  2003/11/26 23:42:31  goiwai
00014 //  GetBaseName(),GetDirectoryName(),で使ってた std::string::copy にはどう
00015 //  もバグ入りのようですので, strncpy でごまかしました.
00016 //
00017 //  Revision 1.3  2003/10/10 11:50:15  goiwai
00018 //  なんかコンパイルにこけたりするんでバッファ長を短くしました.なんでだろ?
00019 //
00020 //  Revision 1.2  2003/10/10 09:37:22  goiwai
00021 //  バッファサイズを変更しました.
00022 //
00023 //  Revision 1.1  2003/10/06 16:34:36  goiwai
00024 //  データの変換や正規表現等,頻繁に使用される関数群のためのラッパークラス
00025 //  です.あたかもCのライブラリ関数の如く使用できます.
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 // Tstring TUtilities::GetBaseName( const Tstring& path )
00180 // {
00181 //   Tstring copy = path;
00182 //   const Tchar* cpch = copy.c_str();
00183 //   // const Tchar* から Tchar* への変換
00184 //   Tchar* pch = const_cast<Tchar*> (cpch);
00185 //   Tstring base = basename( pch );
00186 //   return base;
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   //path.copy( copy, path.size() ); // std::string::copy has bugs.
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 // Tstring TUtilities::GetDirectoryName( const Tstring& path )
00202 // {
00203 //   Tstring copy = path;
00204 //   const Tchar* cpch = copy.c_str();
00205 //   // const Tchar* から Tchar* への変換
00206 //   Tchar* pch = const_cast<Tchar*> (cpch);
00207 //   Tstring dir = dirname( pch );
00208 //   return dir;
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   //path.copy( copy, path.size() ); // std::string::copy has bugs.
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


CLDAQ - a Class Library for DataAcQuisition (Version 1.14.3)
Go IWAI -- goiwai at users.sourceforge.jp