home *** CD-ROM | disk | FTP | other *** search
- //---------------------------------------------------------------------------
- //
- // File: RHPRINT.CPP
- // Path: ...\REHACK\TEXT\RHPRINT.CPP
- // Version: 1.00
- // Author: Bob Provencher/GD SL
- // CIS Id: 71621,2632
- // Created On: 6/11/93
- // Modified On: 6/11/93
- // Description: Simple utility for printing Rehack text files.
- // Sample usage: RHPRINT RHDES.TXT LPT1
- // Tabs: 5
- //
- //---------------------------------------------------------------------------
-
- #include <iostream.h>
- #include <iomanip.h>
- #include <fstream.h>
-
- #include <string.h>
- #include <stdio.h>
- #include <ctype.h>
-
- #define LINES_PER_PAGE 58 // desired lines per page printing
-
- const char new_page = 0x0C; // eject page character
-
- char* input; // copy of pointer to input file name
-
- char line[ 80 ]; // line read in
- char title[ 80 ]; // current section title
- char junk[ 80 ]; // work area
-
- int lines = 0; // lines printed on page
- int page_num = 1; // current page number
- int section; // section number
-
- //
- // Print a header
- //
-
- ostream& header( ostream& os )
- {
-
- //
- // Print "Section header, continued"
- //
-
- if ( strlen( title ) )
- {
- os << '\n' << title << ", continued" << endl << endl;
- lines = 3;
- }
-
- return os;
-
- }
-
- //
- // Print a footer
- //
-
- ostream& footer( ostream& os )
- {
-
- while( lines < LINES_PER_PAGE - 1 )
- {
- os << endl;
- lines++;
- }
-
- //
- // need to change this to find the real path of the file
- //
-
- sprintf( line, "...\\REHACK\\TEXT\\%s", input );
- os.setf( ios::left, ios::adjustfield );
- os << setw( 40 ) << line;
-
- sprintf( line, "Page %d%c", page_num, new_page );
- os.setf( ios::right, ios::adjustfield );
- os << setw( 38 ) << line;
-
- page_num++;
- lines = 0;
- return os;
-
- }
-
- //
- // Main program entry point
- //
-
- int main( int argc, char* argv[] )
- {
-
- if ( argc != 3 )
- {
- cerr << "Usage: PAGE INFILE.EXT OUTFILE.EXT\n";
- return 1;
- }
-
- if ( strcmp( argv[ 1 ], argv[ 2 ] ) == 0 )
- {
- cerr << "Input and output files cannot be the same.\n";
- return 2;
- }
-
- ifstream in( argv[ 1 ] );
-
- if ( !in )
- {
- cout << "Error opening " << argv[ 1 ] << endl;
- return 3;
- }
-
- ofstream out( argv[ 2 ] );
-
- if ( !out )
- {
- cout << "error opening " << argv[ 2 ] << endl;
- return 4;
- }
-
- //
- // translate input file name to uppercase for the footer
- //
-
- strupr( argv[ 1 ] );
-
- input = argv[ 1 ];
-
- title[ 0 ] = 0;
-
- //
- // if the open was ok, and we've read a line ok....
- //
-
- if ( in.getline( line, 80 ).good() )
- {
-
- //
- // ... loop while we still have lines
- //
-
- do
- {
-
- //
- // if we haven't printed on this page yet, and it's not the
- // first page, print the header
- //
-
- if ( lines == 0 && page_num > 1 )
- header( out );
-
-
- //
- // check if the current line is a section header
- // If it is, save the section header
- //
-
- int rc = sscanf( line, "%d. %s\n", §ion, &junk );
-
- if ( rc > 0 )
- strcpy( title, line );
-
- //
- // copy the input line to the output file stream
- //
-
- out << line << endl;
- lines++;
-
- //
- // if three lines from bottom of page, print the footer
- //
-
- if ( lines > LINES_PER_PAGE - 3 )
- footer( out );
-
- }
- while( in.getline( line, 80 ).good() && out.good() );
-
- }
-
- //
- // if out had a problem writing
- //
-
- if ( out.bad() )
- {
- cerr << "Error writing to " << argv[ 2 ] << endl;
- return 5;
- }
- else if ( !in.eof() ) // or not eof on in
- {
- cerr << "Error reading from " << argv[ 1 ] << endl;
- return 6;
- }
- else if ( lines ) // both ok, print footer
- footer( out );
-
- out.close();
- in.close();
-
- if ( out.bad() )
- {
- cerr << "Error closing " << argv[ 2 ] << endl;
- return 7;
- }
-
- if ( in.bad() )
- {
- cerr << "Error closing " << argv[ 1 ] << endl;
- return 8;
- }
-
- return 0;
-
- }
-