// -*- Mode: C++ -*- // // Copyright (C) Peter A. Buhr 1996 // // String.cc -- Pretty print a FORTRAN program; Test program in file StringTest // // Author : Peter A. Buhr // Created On : Fri Sep 20 22:16:01 1996 // Last Modified By : Peter A. Buhr // Last Modified On : Fri Oct 8 07:52:07 2021 // Update Count : 197 // #include #include #include #include using namespace std; // Redefine string operations to properly handle boundary case. string::size_type Find_first_not_of( string text, string mask ) { string::size_type p = text.find_first_not_of( mask ); // find position of 1st mask character if ( p == string::npos ) p = text.length(); // any characters left, reset ? return p; } // Find_first_not_of string::size_type Find_last_not_of( string text, string mask ) { string::size_type p = text.find_last_not_of( mask ); // find position of last mask character if ( p == string::npos ) p = text.length(); // any characters left, reset ? return p; } // Find_last_not_of string line, label, text, seqno; string indent; void PrintLine() { static string blanks( 66, ' ' ); text = indent + text; if ( text.length() > 66 ) { cout << line << endl << "--- ERROR --- FORTRAN TEXT > 66 CHARACTERS AFTER INDENTATION" << endl; } else { cout << label + text + blanks.substr( 0, 66 - text.length() ) + seqno << endl; } // if } // PrintLine enum InDentType { InDent, OutDent, OutInDent, NoDent }; int main( int argc, char *argv[] ) { map< string, InDentType > KeyWord; char comment = 'C'; string word; string identifier = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string indentval = "| "; indent = ""; KeyWord[ "IF" ] = InDent; KeyWord[ "ELSE" ] = OutInDent; KeyWord[ "ENDIF" ] = OutDent; KeyWord[ "WHILE" ] = InDent; KeyWord[ "ENDWHILE" ] = OutDent; KeyWord[ "DO" ] = InDent; KeyWord[ "CONTINUE" ] = OutDent; cin >> noskipws; // turn off white space skipping or leading whitespace is removed for ( ;; ) { int i; getline( cin, line ); // read in line, up to but not including newline if ( cin.fail() ) break; if ( line[0] == comment ) { cout << comment << endl; for ( ;; ) { // scan off comments cout << line << endl; getline( cin, line ); if ( cin.fail() ) { // end with comment ? cout << comment << endl; goto finish; } // exit if ( line[0] != comment ) break; } // for cout << comment << endl; } // if label = line.substr( 0, 6 ); // label & cont., cols 1-6 text = line.substr( 6, 66 ); // Fortran text, cols 7-72 seqno = line.substr( 72, 8 ); // sequence no., cols 73-80 text = text.substr( Find_first_not_of( text, " " ) ); // remove leading blanks text = text.substr( 0, Find_last_not_of( text, " " ) + 1 ); // remove trailing blanks word = text.substr( 0, Find_first_not_of( text, identifier ) ); // extract 1st word on line // Determine if the 1st word denotes a control structure. map< string, InDentType >::iterator posn = KeyWord.find( word ); switch ( posn == KeyWord.end() ? NoDent : posn->second ) { case InDent: PrintLine(); indent += indentval; break; case OutDent: indent = indent.replace( 0, indentval.length(), "" ); PrintLine(); break; case OutInDent: indent = indent.replace( 0, indentval.length(), "" ); PrintLine(); indent += indentval; break; case NoDent: PrintLine(); break; } // switch } // for finish: ; } // main