00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TMacroFileParser.hh"
00017
00018 TMacroFileParser::TMacroFileParser()
00019 : theMacroFileName(), theCommentStringList(), theMacroFileStream()
00020 {
00021 theCommentStringList.clear();
00022 theCommentStringList.push_back( Tsharp );
00023 theCommentStringList.push_back( Twslash );
00024 }
00025
00026 TMacroFileParser::TMacroFileParser( const Tstring& input )
00027 : theMacroFileName( input ), theCommentStringList(), theMacroFileStream()
00028 {
00029 theCommentStringList.clear();
00030 theCommentStringList.push_back( Tsharp );
00031 theCommentStringList.push_back( Twslash );
00032 Open();
00033 }
00034
00035 TMacroFileParser::TMacroFileParser( const Tstring& input, const TstringList& comment )
00036 : theMacroFileName( input ), theCommentStringList( comment ),
00037 theMacroFileStream()
00038 {
00039 Open();
00040 }
00041
00042 TMacroFileParser::TMacroFileParser( const TMacroFileParser& right )
00043 : theMacroFileName( right.theMacroFileName ),
00044 theCommentStringList( right.theCommentStringList )
00045 {
00046 Open();
00047 }
00048
00049 TMacroFileParser::~TMacroFileParser()
00050 {
00051 Close();
00052 }
00053
00054 Tvoid TMacroFileParser::Open()
00055 {
00056 if ( IsOpen() ) {
00057 Close();
00058 }
00059 if ( !theMacroFileName.empty() ) {
00060 theMacroFileStream.open( theMacroFileName.c_str() );
00061 checkFileStream();
00062 }
00063 return;
00064 }
00065
00066 Tvoid TMacroFileParser::Open( const Tstring& filename )
00067 {
00068 theMacroFileName = filename;
00069 Open();
00070 return;
00071 }
00072
00073 Tbool TMacroFileParser::IsOpen()
00074 {
00075 if ( theMacroFileStream.is_open() == Ttrue ) {
00076 return Ttrue;
00077 } else {
00078 return Tfalse;
00079 }
00080 }
00081
00082 Tbool TMacroFileParser::IsFail() const
00083 {
00084 if ( theMacroFileStream.fail() == Ttrue ) {
00085 return Ttrue;
00086 } else {
00087 return Tfalse;
00088 }
00089 }
00090
00091 Tbool TMacroFileParser::IsGood() const
00092 {
00093 if ( theMacroFileStream.good() == Ttrue ) {
00094 return Ttrue;
00095 } else {
00096 return Tfalse;
00097 }
00098 }
00099
00100 Tbool TMacroFileParser::IsEndOfFile() const
00101 {
00102 if ( theMacroFileStream.eof() == Ttrue ) {
00103 return Ttrue;
00104 } else {
00105 return Tfalse;
00106 }
00107 }
00108
00109 Tbool TMacroFileParser::IsSuccess()
00110 {
00111 if ( IsOpen() && !IsFail() && !IsEndOfFile() ) {
00112 return Ttrue;
00113 } else {
00114 return Tfalse;
00115 }
00116 }
00117
00118 Tbool TMacroFileParser::IsExecutable( const TstringList& readbuf ) const
00119 {
00120 if ( readbuf.empty() ) {
00121 return Tfalse;
00122 } else {
00123 return Ttrue;
00124 }
00125 }
00126
00127 Tvoid TMacroFileParser::Close()
00128 {
00129 if ( IsOpen() ) {
00130 theMacroFileStream.close();
00131 }
00132 return;
00133 }
00134
00135 TstringList TMacroFileParser::ReadLine()
00136 {
00137 static const Tsize_t bufflen = 1024;
00138 static Tchar linebuf[ bufflen ];
00139
00140 TstringList listbuf;
00141 listbuf.clear();
00142 Tstring strbuf;
00143 theMacroFileStream.getline( linebuf, bufflen );
00144 strbuf = linebuf;
00145 if ( !( strbuf.empty() ) ) {
00146 eraseComment( strbuf );
00147 }
00148 listbuf = divideLine( strbuf );
00149
00150 return listbuf;
00151 }
00152
00153 Tstring TMacroFileParser::GetCommand( const TstringList& readbuf ) const
00154 {
00155 Tstring command;
00156 if ( !readbuf.empty() ) {
00157 command = readbuf[ 0 ];
00158 }
00159 return command;
00160 }
00161
00162 TstringList TMacroFileParser::GetArguments( const TstringList& readbuf ) const
00163 {
00164 TstringList arguments = readbuf;
00165
00166 if ( arguments.size() > 0 ) {
00167 arguments.erase( arguments.begin() );
00168 } else {
00169 arguments.clear();
00170 }
00171
00172 return arguments;
00173 }
00174
00175 Tvoid TMacroFileParser::checkFileStream()
00176 {
00177 if ( !( IsGood() ) || !( IsOpen() ) ) {
00178 Tstring head = "TMacroFileParser::checkFileStream: ";
00179 Tcerr << head << "fail to open a file " << theMacroFileName << Tendl;
00180 }
00181 return;
00182 }
00183
00184 const Tstring& TMacroFileParser::eraseComment( Tstring& readline ) const
00185 {
00186 for ( Tsize_t i = 0; i < theCommentStringList.size(); i ++ ) {
00187 Tsize_t pos = readline.find( theCommentStringList[ i ] );
00188 Tsize_t bufsize = readline.size();
00189 if ( pos >= 0 && pos <= bufsize - theCommentStringList[ i ].size() ) {
00190 readline.erase( pos, bufsize - pos );
00191 }
00192 }
00193
00194 while ( !( readline.empty() ) && readline.rfind( Tspace ) == readline.size() - 1 ) {
00195 readline.erase( readline.end() - 1 );
00196 }
00197
00198 while ( !( readline.empty() ) && readline.find( Tspace ) == 0 ) {
00199 readline.erase( readline.begin() );
00200 }
00201
00202 return readline;
00203 }
00204
00205 TstringList TMacroFileParser::divideLine( Tstring& readline ) const
00206 {
00207 Tsize_t begin = 0;
00208 Tsize_t end = 0;
00209 Tstring strbuf;
00210 TstringList listbuf;
00211 listbuf.clear();
00212
00213 if ( !( readline.empty() ) ) {
00214 Tsize_t lastpos = readline.rfind( Tspace ) + 1;
00215 do {
00216 begin = readline.find_first_not_of( Tspace, end );
00217 end = readline.find( Tspace, begin );
00218 if ( end > readline.size() ) {
00219 end = readline.size();
00220 }
00221 strbuf = readline.substr( begin, end - begin );
00222 listbuf.push_back( strbuf );
00223 } while ( begin != lastpos );
00224 }
00225
00226 return listbuf;
00227 }
00228
00229 #ifdef __CLDAQ_ROOT_DLL
00230 ClassImp(TMacroFileParser)
00231 #endif