00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TCommandTable.hh"
00017 #include "TCommand.hh"
00018
00019 TCommandTable::TCommandTable()
00020 : Tvector<TCommandSpecified>()
00021 {;}
00022
00023 TCommandTable::~TCommandTable()
00024 {;}
00025
00026 TCommand* TCommandTable::FindCommand( const Tstring& fullname ) const
00027 {
00028 for ( Tsize_t i = 0; i < size(); i ++ ) {
00029 if ( (*this)[ i ].GetFullName() == fullname ) {
00030 return (*this)[ i ].GetCommand();
00031 }
00032 }
00033 return 0;
00034 }
00035
00036 TCommand* TCommandTable::FindCommand( const Tstring& name, const Tstring& path ) const
00037 {
00038 for ( Tsize_t i = 0; i < size(); i ++ ) {
00039 if ( (*this)[ i ].GetName() == name && (*this)[ i ].GetAbsolutePath() == path ) {
00040 return (*this)[ i ].GetCommand();
00041 }
00042 }
00043 return 0;
00044 }
00045
00046 Tbool TCommandTable::AlreadyExist( TCommand* command ) const
00047 {
00048 Tstring fullname = command -> GetCommandName();
00049 return AlreadyExist( fullname );
00050 }
00051
00052 Tbool TCommandTable::AlreadyExist( const Tstring& fullname ) const
00053 {
00054 if ( FindCommand( fullname ) ) {
00055 return Ttrue;
00056 } else {
00057 return Tfalse;
00058 }
00059 }
00060
00061 Tbool TCommandTable::AlreadyExist( const Tstring& name, const Tstring& path ) const
00062 {
00063 if ( FindCommand( name, path ) ) {
00064 return Ttrue;
00065 } else {
00066 return Tfalse;
00067 }
00068 }
00069
00070 Tbool TCommandTable::AlreadyExistDirectory( const Tstring& path ) const
00071 {
00072 if ( path == "/" ) {
00073 return Ttrue;
00074 }
00075
00076 Tstring newpath;
00077 Tstring name;
00078
00079 Tstring strbuf = path;
00080
00081 Tsize_t pos = strbuf.rfind( '/' );
00082 if ( pos == 0 ) {
00083 newpath = strbuf[ 0 ];
00084 name = strbuf.substr( 1, strbuf.size() - 1 );
00085 } else if ( pos > 0 && pos < strbuf.size() ) {
00086 newpath = strbuf.substr( 0, pos );
00087 name = strbuf.substr( pos + 1, strbuf.size() - pos );
00088 } else {
00089 return Tfalse;
00090 }
00091
00092 return AlreadyExistDirectory( name, newpath );
00093 }
00094
00095 Tbool TCommandTable::AlreadyExistDirectory( const Tstring& name, const Tstring& path ) const
00096 {
00097 TstringList dirlist = GetDirectoryList( path );
00098 Tstring dir;
00099 if ( name[ name.size() - 1 ] != '/' ) {
00100 dir = name + "/";
00101 }
00102 for ( Tsize_t i = 0; i < dirlist.size(); i ++ ) {
00103 if ( dirlist[ i ] == dir ) {
00104 return Ttrue;
00105 }
00106 }
00107 return Tfalse;
00108 }
00109
00110 Tvoid TCommandTable::AddCommand( TCommand* command )
00111 {
00112 push_back( convert( command ) );
00113 return;
00114 }
00115
00116 TCommandSpecified TCommandTable::convert( TCommand* command ) const
00117 {
00118 Tstring fullname = command -> GetCommandName();
00119
00120 Tint nslash = 0;
00121 for ( Tsize_t i = 0; i < fullname.size(); i ++ ) {
00122 if ( fullname[ i ] == '/' ) {
00123 nslash ++;
00124 }
00125 }
00126
00127 Tint depth;
00128 Tbool flag;
00129 Tstring name;
00130 Tstring path;
00131 if ( nslash > 0 ) {
00132 flag = Tfalse;
00133 depth = nslash - 1;
00134 Tsize_t slashpos = fullname.rfind( '/' );
00135 path = fullname.substr( 0, slashpos );
00136 name = fullname.substr( slashpos + 1, fullname.size() - slashpos - 1 );
00137 } else {
00138 flag = Ttrue;
00139 depth = TCommandSpecified::tBuiltinDepth;
00140 name = fullname;
00141 path = "";
00142 }
00143
00144 return TCommandSpecified( name, fullname, path, depth, flag, command );
00145 }
00146
00147 TstringList TCommandTable::GetCommandList( const Tstring& path ) const
00148 {
00149 TstringList strlist;
00150 Tstring strbuf;
00151
00152
00153 for ( Tsize_t i = 0; i < size(); i ++ ) {
00154 if ( (*this)[ i ].GetAbsolutePath() == path ) {
00155 strbuf = (*this)[ i ].GetName();
00156 if ( (*this)[ i ].IsAliasedCommand() ) {
00157 strbuf += "@";
00158 } else {
00159 strbuf += "*";
00160 }
00161 strlist.push_back( strbuf );
00162 }
00163 }
00164
00165 return strlist;
00166 }
00167
00168 TstringList TCommandTable::GetDirectoryList( const Tstring& path ) const
00169 {
00170 TstringList strlist;
00171 Tstring strbuf;
00172 TstringList unique;
00173
00174
00175 for ( Tsize_t i = 0; i < size(); i ++ ) {
00176 Tbool hasalready = Tfalse;
00177 for ( Tsize_t j = 0; j < unique.size(); j ++ ) {
00178 if ( unique[ j ] == (*this)[ i ].GetAbsolutePath() ) {
00179 hasalready = Ttrue;
00180 break;
00181 }
00182 }
00183 if ( hasalready == Tfalse ) {
00184 unique.push_back( (*this)[ i ].GetAbsolutePath() );
00185 }
00186 }
00187
00188
00189
00190
00191 for ( Tsize_t i = 0; i < unique.size(); i ++ ) {
00192 strbuf = unique[ i ];
00193 if ( strbuf.substr( 0, path.size() ) == path ) {
00194
00195 if ( path != "/" && strbuf.size() > path.size() && strbuf[ path.size() ] != '/' ) {
00196 continue;
00197 }
00198
00199 strbuf.erase( 0, path.size() );
00200
00201 if ( strbuf != "/" && strbuf[ 0 ] == '/' ) {
00202 strbuf.erase( 0, 1 );
00203 }
00204
00205 Tsize_t pos = strbuf.find( '/' );
00206 if ( pos < strbuf.size() ) {
00207 strbuf.erase( pos, strbuf.size() - pos );
00208 }
00209
00210 if ( ! strbuf.empty() ) {
00211 strbuf += "/";
00212 strlist.push_back( strbuf );
00213 }
00214
00215 }
00216 }
00217
00218
00219
00220 TstringList newstrlist;
00221 for ( Tsize_t i = 0; i < strlist.size(); i ++ ) {
00222 Tbool hasalready = Tfalse;
00223 for ( Tsize_t j = 0; j < strlist.size(); j ++ ) {
00224 if ( i != j && strlist[ i ] == strlist[ j ] ) {
00225 hasalready = Ttrue;
00226 break;
00227 }
00228 }
00229 if ( hasalready == Ttrue ) {
00230 strlist.erase( strlist.begin() + i );
00231 }
00232 }
00233
00234 return strlist;
00235 }
00236
00237 TstringList TCommandTable::Sort( const Tstring& path ) const
00238 {
00239 TstringList comlist = GetCommandList( path );
00240 TstringList dirlist = GetDirectoryList( path );
00241 for ( Tsize_t i = 0; i < dirlist.size(); i ++ ) {
00242 comlist.push_back( dirlist[ i ] );
00243 }
00244 return Sort( comlist );
00245 }
00246
00247 TstringList TCommandTable::Sort( const TstringList& sort ) const
00248 {
00249 TstringList sorted = sort;
00250 Tsize_t sortlen = sorted.size();
00251 if ( sortlen > 1 ) {
00252 Tbool sortcomplete = Tfalse;
00253 Tstring strbuf;
00254 do {
00255 for ( Tsize_t i = 0; i < sortlen - 1; i ++ ) {
00256 if ( sorted[ i ] > sorted[ i + 1 ] ) {
00257 strbuf = sorted[ i + 1 ];
00258 sorted[ i + 1 ] = sorted[ i ];
00259 sorted[ i ] = strbuf;
00260 }
00261 Tbool result = Ttrue;
00262 for ( Tsize_t j = 0; j < sortlen - 1; j ++ ) {
00263 result &= ( sorted[ j ] < sorted[ j + 1 ] );
00264 }
00265 sortcomplete = result;
00266 }
00267 } while ( sortcomplete != Ttrue );
00268 }
00269
00270 return sorted;
00271 }
00272
00273 Tvoid TCommandTable::List( Tint column, const Tstring& path ) const
00274 {
00275 TstringList comlist = GetCommandList( path );
00276 TstringList dirlist = GetDirectoryList( path );
00277 for ( Tsize_t i = 0; i < dirlist.size(); i ++ ) {
00278 comlist.push_back( dirlist[ i ] );
00279 }
00280 List( column, comlist );
00281 return;
00282 }
00283
00284 Tvoid TCommandTable::List( Tint column, const TstringList& strlist ) const
00285 {
00286 Tsize_t maxlen = 0;
00287 TintList lenlist;
00288 TintList lastlenlist;
00289 Tint totallen = 0;
00290
00291 Tint ncol = 0;
00292 Tint nrow = 0;
00293
00294 for ( Tsize_t ncolid = strlist.size(); ncolid > 0; ncolid -- ) {
00295 lastlenlist = lenlist;
00296 lenlist.clear();
00297 totallen = 0;
00298 for ( Tsize_t i = 0; i < strlist.size(); i ++ ) {
00299 Tsize_t collen = strlist[ i ].size();
00300 if ( maxlen < collen ) {
00301 maxlen = collen;
00302 }
00303
00304 if ( strlist.size() % ncolid == 0 ) {
00305 Tint tmp = strlist.size() / ncolid;
00306 if ( ( i + 1 ) % tmp == 0 ) {
00307 lenlist.push_back( maxlen );
00308 maxlen = 0;
00309 }
00310 } else {
00311 Tint tmp = strlist.size() / ncolid + 1;
00312 if ( ( i + 1 ) % tmp == 0 ) {
00313 lenlist.push_back( maxlen );
00314 maxlen = 0;
00315 } else if ( i == strlist.size() - 1 ) {
00316 lenlist.push_back( maxlen );
00317 maxlen = 0;
00318 }
00319 }
00320 }
00321
00322
00323 for ( Tsize_t i = 0; i < lenlist.size(); i ++ ) {
00324 totallen += lenlist[ i ];
00325 if ( i != lenlist.size() - 1 ) {
00326 totallen += 2;
00327 }
00328 }
00329
00330 if ( totallen < column ) {
00331 ncol = ncolid;
00332 break;
00333 }
00334
00335 }
00336
00337 for ( Tsize_t i = 0; i < lenlist.size(); i ++ ) {
00338 if ( i != lenlist.size() - 1 ) {
00339 lenlist[ i ] += 2;
00340 }
00341 }
00342
00343
00344 ncol = lenlist.size();
00345 if ( strlist.size() % ncol == 0 ) {
00346 nrow = strlist.size() / ncol;
00347 } else {
00348 nrow = strlist.size() / ncol;
00349 nrow += 1;
00350 }
00351
00352
00353 for ( Tint row = 0; row < nrow; row ++ ) {
00354 for ( Tint col = 0; col < ncol; col ++ ) {
00355 Tint comid = nrow * col;
00356 comid += row;
00357 if ( comid >= (Tint)strlist.size() ) {
00358 break;
00359 }
00360 const Tchar* cbuf = strlist[ comid ].c_str();
00361 Tcout.setf( Tios::left );
00362 Tcout << setw( lenlist[ col ] ) << cbuf;
00363 }
00364 Tcout << Tendl;
00365 }
00366
00367 }
00368
00369 Tvoid TCommandTable::RemoveCommand( Tint index )
00370 {
00371 erase( begin() + index );
00372 return;
00373 }
00374
00375 Tint TCommandTable::GetSize() const
00376 {
00377 return (Tint)size();
00378 }
00379
00380 Tvoid TCommandTable::Clear()
00381 {
00382 clear();
00383 return;
00384 }
00385
00386 TCommandSpecified TCommandTable::GetCommandSpecified( Tint index ) const
00387 {
00388 TCommandSpecified spec;
00389 static const Tstring head = "TCommandTable::GetCommandSpecified: ";
00390 if ( index < 0 || index >= (Tint)size() ) {
00391 Tcerr << head << "invalid index " << index << "." << Tendl;
00392 return spec;
00393 }
00394 spec = ( *this )[ index ];
00395 return spec;
00396 }
00397
00398 TCommandSpecified TCommandTable::GetCommandSpecified( const Tstring& fullname ) const
00399 {
00400 TCommandSpecified spec;
00401 TCommand* command = FindCommand( fullname );
00402 if ( command ) {
00403 return convert( command );
00404 }
00405 return spec;
00406 }
00407
00408 #ifdef __CLDAQ_ROOT_DLL
00409 ClassImp(TCommandTable)
00410 #endif