Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/copyfile.cfa

    r29d618e r3f654ec  
    88//
    99// Author           : Peter A. Buhr
    10 // Created On       : Fri Jun 19 13:44:05 2020
     10// Created On       : Tue Jul 16 16:47:22 2019
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Aug 15 15:00:48 2020
    13 // Update Count     : 6
     12// Last Modified On : Wed Jul 17 18:04:44 2019
     13// Update Count     : 26
    1414//
    1515
    1616#include <fstream.hfa>
    17 #include <exception.hfa>
     17#include <stdlib.hfa>                                                                   // new/delete
    1818
    1919int main( int argc, char * argv[] ) {
    20         ifstream in  = stdin;                                                           // copy default files
    21         ofstream out = stdout;
     20        ifstream * in  = &stdin;                                                        // default files
     21        ofstream * out = &stdout;
     22        try {
     23                choose ( argc ) {
     24                  case 2, 3:
     25                          in = new( (const char *)argv[1] );            // open input file first as output creates file
     26                          if ( argc == 3 ) out = new( (const char *)argv[2] ); // only open output if input opens as output created if nonexistent
     27                  case 1: ;                                     // use default files
     28                  default:
     29                          exit | "Usage [ input-file (default stdin) [ output-file (default stdout) ] ]";
     30                } // choose
    2231
    23         try {
    24                 choose ( argc ) {                                                               // terminate if command-line errors
    25                   case 2, 3:
    26                         open( in, argv[1] );                                            // open input file first as output creates file
    27                         if ( argc == 3 ) open( out, argv[2] );          // do not create output unless input opens
    28                   case 1: ;                                                                             // use default files
    29                   default:                                                                              // wrong number of options
    30                         exit | "Usage" | argv[0] | "[ input-file (default stdin) [ output-file (default stdout) ] ]";
    31                 } // choose
    32         } catch( Open_Failure * ex ; ex->istream == &in ) {
    33                 exit | "Unable to open input file" | argv[1];
    34         } catch( Open_Failure * ex ; ex->ostream == &out ) {
    35                 close( in );                                                                    // optional
    36                 exit | "Unable to open output file" | argv[2];
     32                char ch;
     33                *out | nlOff;                                                                   // turn off auto newline
     34                *in  | nlOn;                                                                    // turn on reading newline
     35
     36                for () {                                                                                // read all characters
     37                        *in | ch;
     38                  if ( eof( *in ) ) break;                                              // eof ?
     39                        *out | ch;
     40                } // for
     41        } finally {
     42                if ( in  != &stdin  ) delete( in );                             // close file, do not delete stdin!
     43                if ( out != &stdout ) delete( out );                    // close file, do not delete stdout!
    3744        } // try
    38 
    39         out | nlOff;                                                                            // turn off auto newline
    40         in  | nlOn;                                                                                     // turn on reading newline
    41 
    42         char ch;
    43         for () {                                                                                        // read all characters
    44                 in | ch;
    45           if ( eof( in ) ) break;                                                       // eof ?
    46                 out | ch;
    47         } // for
    4845} // main
    4946
Note: See TracChangeset for help on using the changeset viewer.