Changeset 1896c1f for libcfa/src


Ignore:
Timestamp:
Aug 31, 2021, 1:49:09 AM (3 years ago)
Author:
Jacob Prud'homme <jafprudhomme@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, pthread-emulation, qualifiedEnum
Children:
c3c76cd
Parents:
16f9aca
git-author:
Jacob Prud'homme <jafprudhomme@…> (06/15/21 11:06:25)
git-committer:
Jacob Prud'homme <jafprudhomme@…> (08/31/21 01:49:09)
Message:

Convert to using CFA file stream I/O

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/parseconfig.cfa

    r16f9aca r1896c1f  
    1 #include <iostream>
    2 #include <fstream>
     1#include <fstream.hfa>
    32#include <limits>                                                                               // numeric_limits
    43using namespace std;
     
    76static bool comments( ifstream & in, string & name ) {
    87        for ( ;; ) {
    9                 in >> name;
    10           if ( in.fail() ) return true;
     8                in | name;
     9          if ( fail( in ) ) return true;
    1110          if ( name.substr(0,1) != "#" ) break;
    12                 in.ignore( numeric_limits<int>::max(), '\n' );  // ignore remainder of line
     11                in | nl;        // ignore remainder of line
    1312        } // for
    1413        return false;
     
    1615
    1716// Process the configuration file to set the simulation parameters.
    18 void processConfigFile( const char *configFile, ConfigParms & cparms ) {
     17void parseConfig( const char * configFile, ConfigParms & cparms ) {
    1918        enum { Parmnum = 11 };
    2019        struct {
     
    4039
    4140        try {
    42                 ifstream in( configFile );                                              // open the configuration file for input
     41                ifstream in;
     42
     43                open( in, configFile );                                                 // open the configuration file for input
    4344
    4445                for ( cnt = 0 ; cnt < Parmnum; cnt += 1 ) {             // parameter names can appear in any order
     
    4748                  if ( posn == Parmnum ) break;                                 // configuration not found ?
    4849                  if ( parms[posn].used ) break;                                // duplicate configuration ?
    49                         in >> value;
     50                        in | value;
    5051                        if ( value < 0 ) {
    51                                 cerr << "Error: file \"" << configFile << "\" parameter " << name
    52                                          << " value " << value << " must be non-negative." << endl;
    53                                 exit( EXIT_FAILURE );
     52                                close( in );
     53                                exit | "Error: file \"" | configFile | "\" parameter " | name
     54                                         | " value " | value | " must be non-negative."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
    5455                        } // if
    55                   if ( in.fail() ) break;
    56                         in.ignore( numeric_limits<int>::max(), '\n' ); // ignore remainder of line
     56                  if ( fail( in ) ) break;
     57                        in | nl; // ignore remainder of line
    5758                        numOfParm += 1;
    5859                        parms[posn].used = true;
     
    6162
    6263                if ( numOfParm != Parmnum ) {
    63                         cerr << "Error: file \"" << configFile << "\" is corrupt." << endl;
    64                         exit( EXIT_FAILURE );
     64                        close( in );
     65                        exit | "Error: file \"" | configFile | "\" is corrupt."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
    6566                } // if
    6667                if ( ! comments( in, name ) ) {                                 // ! eof ?
    67                         cerr << "Error: file \"" << configFile << "\" has extraneous data." << endl;
    68                         exit( EXIT_FAILURE );
     68                        close( in );
     69                        exit | "Error: file \"" | configFile | "\" has extraneous data."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
    6970                } // if
    70         } catch( uFile::Failure & ) {
    71                 cerr << "Error: could not open input file \"" << configFile << "\"" << endl;
    72                 exit( EXIT_FAILURE );
     71        } catch( Open_Failure * ex; ex->istream == &in ) {
     72                close( in );
     73                exit | "Error: could not open input file \"" | configFile | "\""; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
    7374        } // try
    7475
    75         if (cparms.numStops < 2) {
    76         cerr << "Error: file \"" << configFile << "\" parameter NumStops value "
    77              << cparms.numStops << " must be at least 2." << endl;
    78         exit( EXIT_FAILURE );
     76        close( in );
     77
     78        if ( cparms.numStops < 2 ) {
     79                exit | "Error: file \"" | configFile | "\" parameter NumStops value "
     80                         | cparms.numStops | " must be at least 2."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
    7981        }
    80     if (cparms.numStudents < 1) {
    81         cerr << "Error: file \"" << configFile << "\" parameter NumStudents value "
    82              << cparms.numStudents << " must be at least 1." << endl;
    83         exit( EXIT_FAILURE );
    84     }
    85     if (cparms.numCouriers < 1) {
    86         cerr << "Error: file \"" << configFile << "\" parameter NumCouriers value "
    87              << cparms.numCouriers << " must be at least 1." << endl;
    88         exit( EXIT_FAILURE );
    89     }
     82        if ( cparms.numStudents < 1 ) {
     83                exit | "Error: file \"" | configFile | "\" parameter NumStudents value "
     84                         | cparms.numStudents | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
     85        }
     86        if ( cparms.numCouriers < 1 ) {
     87                exit | "Error: file \"" | configFile | "\" parameter NumCouriers value "
     88                         | cparms.numCouriers | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
     89        }
    9090} // processConfigFile
    9191
Note: See TracChangeset for help on using the changeset viewer.