home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / pascal / rehack / text / rhprint.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-23  |  3.8 KB  |  222 lines

  1. //---------------------------------------------------------------------------
  2. //
  3. //    File:        RHPRINT.CPP
  4. //    Path:        ...\REHACK\TEXT\RHPRINT.CPP
  5. //    Version:        1.00
  6. //    Author:        Bob Provencher/GD SL
  7. //    CIS Id:        71621,2632
  8. //    Created On:    6/11/93
  9. //    Modified On:    6/11/93
  10. //    Description:    Simple utility for printing Rehack text files. 
  11. //                Sample usage: RHPRINT RHDES.TXT LPT1
  12. //    Tabs:        5
  13. //
  14. //---------------------------------------------------------------------------
  15.  
  16. #include <iostream.h>
  17. #include <iomanip.h>
  18. #include <fstream.h>
  19.  
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. #define LINES_PER_PAGE    58        // desired lines per page printing
  25.  
  26. const char new_page = 0x0C;        // eject page character
  27.  
  28. char* input;                    // copy of pointer to input file name
  29.  
  30. char  line[ 80 ];                // line read in
  31. char title[ 80 ];                // current section title
  32. char  junk[ 80 ];                // work area
  33.  
  34. int lines = 0;                    // lines printed on page
  35. int page_num = 1;                // current page number
  36. int section;                    // section number
  37.  
  38. //
  39. //    Print a header
  40. //
  41.  
  42. ostream& header( ostream& os )
  43. {
  44.  
  45.     //
  46.     //    Print "Section header, continued"
  47.     //
  48.  
  49.     if ( strlen( title ) )
  50.     {
  51.         os << '\n' << title << ", continued" << endl << endl;
  52.         lines = 3;
  53.     }
  54.  
  55.     return os;
  56.  
  57. }
  58.  
  59. //
  60. //    Print a footer
  61. //
  62.  
  63. ostream& footer( ostream& os )
  64. {
  65.  
  66.     while( lines < LINES_PER_PAGE - 1 )
  67.     {
  68.         os << endl;
  69.         lines++;
  70.     }
  71.  
  72.     //
  73.     //    need to change this to find the real path of the file
  74.     //
  75.  
  76.     sprintf( line, "...\\REHACK\\TEXT\\%s", input );
  77.     os.setf( ios::left, ios::adjustfield );
  78.     os << setw( 40 ) << line;
  79.  
  80.     sprintf( line, "Page %d%c", page_num, new_page );
  81.     os.setf( ios::right, ios::adjustfield );
  82.     os << setw( 38 ) << line;
  83.  
  84.     page_num++;
  85.     lines = 0;
  86.     return os;
  87.  
  88. }
  89.  
  90. //
  91. //    Main program entry point
  92. //
  93.  
  94. int main( int argc, char* argv[] )
  95. {
  96.  
  97.     if ( argc != 3 )
  98.     {
  99.         cerr << "Usage: PAGE INFILE.EXT OUTFILE.EXT\n";
  100.         return 1;
  101.     }
  102.  
  103.     if ( strcmp( argv[ 1 ], argv[ 2 ] ) == 0 )
  104.     {
  105.         cerr << "Input and output files cannot be the same.\n";
  106.         return 2;
  107.     }
  108.  
  109.     ifstream  in( argv[ 1 ] );
  110.  
  111.     if ( !in )
  112.     {
  113.         cout << "Error opening " << argv[ 1 ] << endl;
  114.         return 3;
  115.     }
  116.  
  117.     ofstream out( argv[ 2 ] );
  118.  
  119.     if ( !out )
  120.     {
  121.         cout << "error opening " << argv[ 2 ] << endl;
  122.         return 4;
  123.     }
  124.  
  125.     //
  126.     //    translate input file name to uppercase for the footer
  127.     //
  128.  
  129.     strupr( argv[ 1 ] );
  130.  
  131.     input = argv[ 1 ];
  132.  
  133.     title[ 0 ] = 0;
  134.  
  135.     //
  136.     //    if the open was ok, and we've read a line ok....
  137.     //
  138.  
  139.     if ( in.getline( line, 80 ).good() )
  140.     {
  141.  
  142.         //
  143.         //    ... loop while we still have lines
  144.         //
  145.  
  146.         do
  147.         {
  148.  
  149.             //
  150.             //    if we haven't printed on this page yet, and it's not the
  151.             //    first page, print the header
  152.             //
  153.  
  154.             if ( lines == 0 && page_num > 1 )
  155.                 header( out );
  156.  
  157.  
  158.             //
  159.             //    check if the current line is a section header
  160.             //    If it is, save the section header
  161.             //
  162.  
  163.             int rc = sscanf( line, "%d. %s\n", §ion, &junk );
  164.  
  165.             if ( rc > 0 )
  166.                 strcpy( title, line );
  167.  
  168.             //
  169.             //    copy the input line to the output file stream
  170.             //
  171.  
  172.             out << line << endl;
  173.             lines++;
  174.  
  175.             //
  176.             //    if three lines from bottom of page, print the footer
  177.             //
  178.  
  179.             if ( lines > LINES_PER_PAGE - 3 )
  180.                 footer( out );
  181.  
  182.         }
  183.         while( in.getline( line, 80 ).good() && out.good() );
  184.  
  185.     }
  186.  
  187.     //
  188.     //    if out had a problem writing
  189.     //
  190.  
  191.     if ( out.bad() )
  192.     {
  193.         cerr << "Error writing to " << argv[ 2 ] << endl;
  194.         return 5;
  195.     }
  196.     else if ( !in.eof() )            // or not eof on in
  197.     {
  198.         cerr << "Error reading from " << argv[ 1 ] << endl;
  199.         return 6;
  200.     }
  201.     else if ( lines )                // both ok, print footer
  202.         footer( out );
  203.  
  204.     out.close();
  205.     in.close();
  206.  
  207.     if ( out.bad() )
  208.     {
  209.         cerr << "Error closing " << argv[ 2 ] << endl;
  210.         return 7;
  211.     }
  212.  
  213.     if ( in.bad() )
  214.     {
  215.         cerr << "Error closing " << argv[ 1 ] << endl;
  216.         return 8;
  217.     }
  218.  
  219.     return 0;
  220.  
  221. }
  222.