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

TUserInterface.cc

解説を見る。
00001 // =====================================================================
00002 //  $Id: TUserInterface.cc,v 1.8 2004/03/07 10:30:31 goiwai Exp $
00003 //  $Name: CLDAQ-1-14-03 $
00004 //  $Log: TUserInterface.cc,v $
00005 //  Revision 1.8  2004/03/07 10:30:31  goiwai
00006 //  ROOTに組みこむためのおまじないマクロを埋めこみました。
00007 //  全てにおいて完全に動作するわけではありません。
00008 //
00009 //  Revision 1.7  2003/10/06 17:02:40  goiwai
00010 //  *** empty log message ***
00011 //
00012 //  Revision 1.6  2003/07/30 16:18:52  goiwai
00013 //  ファイルにコミットログをつけることにしました.
00014 //
00015 // =====================================================================
00016 #include "TUserInterface.hh"
00017 #include "TCommand.hh"
00018 
00019 //static const Tchar* _cup = "cup";
00020 static Tchar* _clear = "clear";
00021 static Tchar* _cols = "cols";
00022 static Tchar* _lines = "lines";
00023 
00024 
00025 TUserInterface::TUserInterface( const Tstring& history )
00026   : theCommandTable(),
00027     theHistoryFileStream(),
00028     theCurrentWorkingDirectory( Tslash ), 
00029     theCommandHistory()
00030 {
00031   if ( isexist( history ) ) {
00032     // 同名のファイルがあればアペンドモードかつ,そのファイルをヒストリ追加
00033     theHistoryFileStream.open( history.c_str(), Tout|Tapp );
00034     if ( !( theHistoryFileStream.good() ) ) {
00035       Tcerr << "TUserInterface::TUserInterface: fail to open a file ";
00036       Tcerr << history << "." << Tendl;
00037     }
00038 
00039     // ヒストリ追加
00040     Tifstream ifs( history.c_str() );
00041     static const Tsize_t buflen = 1024;
00042     static Tchar buf[ buflen ];
00043     Tstring strbuf;
00044     while ( ifs.getline( buf, buflen ) && !ifs.eof() ) {
00045       if ( ifs.fail() || !ifs.good() ) {
00046         break;
00047       }
00048       strbuf = buf;
00049       if ( !strbuf.empty() ) {
00050         theCommandHistory.push_back( strbuf );
00051       }
00052     }
00053     ifs.close();
00054 
00055   } else {
00056     theHistoryFileStream.open( history.c_str(), Tout );
00057     if ( !( theHistoryFileStream.good() ) ) {
00058       Tcerr << "TUserInterface::TUserInterface: fail to open a file ";
00059       Tcerr << history << "." << Tendl;
00060     }
00061   }
00062 
00063   theCommandTable.Clear();
00064 
00065   ClearScreen();
00066 }
00067 
00068 TUserInterface::~TUserInterface()
00069 {
00070   ClearCommands();
00071   if ( theHistoryFileStream.is_open() ) {
00072     theHistoryFileStream.close();
00073   }
00074 }
00075 
00076 Tint TUserInterface::AddCommand( TCommand* command )
00077 {
00078   if ( theCommandTable.AlreadyExist( command ) ) {
00079     Tcerr << "TUserInterface::AddCommand: already exist command identified as " << command -> GetCommandName() << "." << Tendl;
00080   } else {
00081     theCommandTable.AddCommand( command );
00082   }
00083   return theCommandTable.GetSize();
00084 }
00085 
00086 Tint TUserInterface::RemoveCommand( Tint index )
00087 {
00088   static const Tstring head = "TUserInterface::RemoveCommand: ";
00089   if ( index < 0 || index >= theCommandTable.GetSize() ) {
00090     Tcerr << head << "invalid index " << index << "." << Tendl;
00091     return theCommandTable.GetSize();
00092   }
00093   Tstring fullname = theCommandTable[ index ].GetFullName();
00094   if ( ! theCommandTable[ index ].IsAliasedCommand() ) {
00095     delete ( theCommandTable[ index ].GetCommand() );
00096   }
00097   Tcout << head << fullname << " was deleted." << Tendl;
00098   theCommandTable.RemoveCommand( index );
00099 
00100   return theCommandTable.GetSize();
00101 }
00102 
00103 Tvoid TUserInterface::ClearCommands()
00104 {
00105   static const Tstring head = "TUserInterface::ClearCommands: ";
00106   for ( Tint i = 0; i < theCommandTable.GetSize(); i ++ ) {
00107     if ( theCommandTable[ i ].GetCommand() ) {
00108       Tstring fullname = theCommandTable[ i ].GetFullName();
00109       if ( ! theCommandTable[ i ].IsAliasedCommand() ) {
00110         delete ( theCommandTable[ i ].GetCommand() );
00111       }
00112       Tcout << head << fullname << " was deleted." << Tendl;
00113     }
00114   }
00115   theCommandTable.Clear();
00116   return;
00117 }
00118 
00119 TCommand* TUserInterface::FindCommand( const Tstring& fullname )
00120 {
00121   return theCommandTable.FindCommand( fullname );
00122 }
00123 
00124 TCommand* TUserInterface::FindCommand( const Tstring& name, const Tstring& path )
00125 {
00126   return theCommandTable.FindCommand( name, path );
00127 }
00128 
00129 TCommand* TUserInterface::GetCommand( Tint index )
00130 {
00131   if ( index < 0 || index >= theCommandTable.GetSize() ) {
00132     Tcerr << "TUserInterface::GetCommand: invalid index" << Tendl;
00133     return 0;
00134   }
00135   return theCommandTable[ index ].GetCommand();
00136 }
00137 
00138 TCommand* TUserInterface::GetCommand( const Tstring& fullname )
00139 {
00140   return FindCommand( fullname );
00141 }
00142 
00143 TCommand* TUserInterface::GetCommand( const Tstring& name, const Tstring& path )
00144 {
00145   return FindCommand( name, path );
00146 }
00147 
00148 Tvoid TUserInterface::ExecuteCommand( const Tstring& command, const TstringList& arguments )
00149 {
00150   Tstring abspath = ModifyPath( command );
00151   TCommand* com = FindCommand( abspath );
00152   if ( com ) {
00153     com -> Execute( arguments );
00154   }
00155   return;
00156 }
00157 
00158 Tvoid TUserInterface::ExecuteCommand( const Tstring& command )
00159 {
00160   TstringList args;
00161   args.clear();
00162   ExecuteCommand( command, args );
00163   return;
00164 }
00165 
00166 Tvoid TUserInterface::NotFoundCommand( const Tstring& commandname ) const
00167 {
00168   // should be overwritten in derived class.
00169   Tcout << commandname << ": Command not found." << Tendl;
00170   return;
00171 }
00172 
00173 Tvoid TUserInterface::ClearScreen() const
00174 {
00175   setupterm( 0, 1, 0 );
00176   //DEBUG//putp( tigetstr( _cup ) );
00177   putp( tparm( tigetstr( _clear ), 0, 0 ) );
00178   return;
00179 }
00180 
00181 Tint TUserInterface::GetNumberOfColumns() const
00182 {
00183   setupterm( 0, 1, 0 );
00184   Tint ncol = tigetnum( _cols );
00185   return ncol;
00186 }
00187 
00188 Tint TUserInterface::GetNumberOfLines() const
00189 {
00190   setupterm( 0, 1, 0 );
00191   Tint nlines = tigetnum( _lines );
00192   return nlines;
00193 }
00194 
00195 Tint TUserInterface::GetCurrentWorkingDirectoryLevel() const
00196 {
00197   Tint nslash = 0;
00198   for ( Tsize_t i = 0; i < theCurrentWorkingDirectory.size(); i ++ ) {
00199     if ( theCurrentWorkingDirectory[ i ] == '/' ) {
00200       nslash ++;
00201     }
00202   }
00203   Tint level = nslash - 1;
00204   if ( level < 0 ) {
00205     Tcerr << "TUserInterface::GetCurrentWorkingDirectoryLevel: unexpected level." << Tendl;
00206   }
00207   return level;
00208 }
00209 
00210 Tvoid TUserInterface::SetCurrentWorkingDirectory( const Tstring& directory )
00211 {
00212   Tstring path = ModifyPath( directory );
00213 
00214   if ( theCommandTable.AlreadyExistDirectory( path ) ) {
00215     theCurrentWorkingDirectory = path;
00216   } else {
00217     Tcerr << directory << ": No such file or directory." << Tendl;
00218   }
00219   return;
00220 }
00221 
00222 Tstring TUserInterface::ModifyPath( const Tstring& path ) const
00223 {
00224   Tstring temppath = path;
00225   Tstring newpath = theCurrentWorkingDirectory;
00226 
00227   // if global command, nothig to do.
00228   for ( Tint i = 0; i < theCommandTable.GetSize(); i ++ ) {
00229     if ( theCommandTable[ i ].IsBuiltinCommand() ) {
00230       if ( theCommandTable[ i ].GetName() == path ) {
00231         return path;
00232       }
00233     }
00234   }
00235 
00236 
00237   // erase continuas slash
00238   for ( Tsize_t i = 0; i < temppath.size() - 1; i ++ ) {
00239     if ( temppath[ i ] == '/' && temppath[ i + 1 ] == '/' ) {
00240       temppath.erase( i, 1 );
00241       i --;
00242     }
00243   }
00244 
00245   if ( temppath == "." || temppath == "./" ) {
00246     return newpath;
00247   }
00248 
00249 
00250   if ( temppath.size() > 0 ) {
00251     if ( temppath[ 0 ] == '/' ) {
00252       // full path is given
00253       newpath = temppath;
00254     } else if ( temppath[ 0 ] != '.' ) {
00255       // add current prefix
00256       if ( newpath == "/" ) {
00257         newpath += temppath;
00258       } else {
00259         newpath = newpath + '/' + temppath;
00260       }
00261     } else if ( temppath.substr( 0, 2 ) == "./" ) {
00262       // add current prefix
00263       if ( newpath == "/" ) {
00264         newpath += temppath.substr( 2, temppath.size() - 2 );
00265       } else {
00266         newpath = newpath + '/' + temppath.substr( 2, temppath.size() - 2 );
00267       }
00268     } else {
00269       // swim up with ".."
00270       while ( 1 ) {
00271         if ( temppath.substr( 0, 2 ) == ".." ) {
00272           if ( newpath != "/" ) { 
00273             newpath = newpath.substr( 0, newpath.rfind( '/' ) + 1 );
00274           }
00275           if ( temppath == ".." || temppath == "../" ) {
00276             break;
00277           }
00278           temppath = temppath.substr( 3, temppath.size() - 3 );
00279         } else {
00280           newpath += temppath;
00281           break;
00282         }
00283       }
00284     }
00285   }
00286 
00287   if ( newpath != "/" && newpath[ newpath.size() - 1 ] == '/' ) {
00288     newpath.erase( newpath.size() - 1, 1 );
00289   }
00290 
00291   if ( newpath[ newpath.size() - 1 ] == '*' ) {
00292     newpath.erase( newpath.size() - 1, 1 );
00293   }
00294 
00295   if ( newpath[ newpath.size() - 1 ] == '@' ) {
00296     newpath.erase( newpath.size() - 1, 1 );
00297   }
00298 
00299   return newpath;
00300 }
00301 
00302 #ifdef __CLDAQ_ROOT_DLL
00303     ClassImp(TUserInterface)
00304 #endif


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