// // // // // // // // // // // // // //---------------------------------------------------------------------------- // // // prfxtst.cc - C++ Test File for a Specialized IOstream //

C++ Test File for a Specialized IOstream

//

Reading/Writing files with a per line prefix

//
//

    // <!-*-C++-*- file: prfxtst.cc --->
    // <!-------------------------------------------------------------------------->
    // <! Copyright (C) 1995 Dietmar Kuehl >
    // <!>
    // <! This file is free software; you can redistribute it and/or modify >
    // <! it under the terms of the GNU General Public License as published by >
    // <! the Free Software Foundation; either version 2 of the License, or >
    // <! (at your option) any later version. >
    // <!>
    // <! This program is distributed in the hope that it will be useful, >
    // <! but WITHOUT ANY WARRANTY; without even the implied warranty of >
    // <! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the >
    // <! GNU General Public License for more details. >
    //----------------------------------------------------------------------------
    // <HTML>
    // <HEAD>
    // <TITLE> prfxtst.cc - C++ Test File for a Specialized IOstream </TITLE>
    // <H1>C++ Test File for a Specialized IOstream</H1>
    // <H2>Reading/Writing files with a per line prefix</H2>
    // <HR>
    // </HEAD>
See prfxstream.h for the declarations and prfxstream.cc of [io]prfxstream.

This application is a little bit fancy: It uses the name of the program to determine whether a the processing should be applied to the input or the output: If the program name starts with 'i' it is assumed that the program should read its standard input to strip off prefixes from these lines. The result is send to the standard ouptut. If the program name starts with 'o' it is assumed that the program should read its standard input and preceed every line with a prefix. The result is again send to the standard output.
The prefix which is to be stripped off or to be prepended is the argument to the program. If either the program name doesn't start with 'o' or 'i' or there is no argument given, the program complains about this fact.

    #include "prfxstream.h"

    int	main(int ac, char **av)
    {
      if (ac != 2)
      {
        cerr << "Usage: " << av[0] << " <prefix>" << endl;
        exit(1);
      }

      if (av[0][0] == 'o')
      {
    // </PRE>
    // The following is the line creating a stream which prefixes every line
    // written with a given prefix.  The output is actually send to the
    // streambuf indicated by the first argument.
    // <PRE>
        oprfxstream	out(cout.rdbuf(), av[1]);
        char	c;

        while (cin)
          if (cin.get(c))
    	out.put(c);
      }
      else if (av[0][0] == 'i')
      {
    // </PRE>
    // The following is the line creating a stream which strips off a given
    // prefix from every line read. The input is actually read from the
    // streambuf indicated by the first argument.
    // <PRE>
        iprfxstream	in(cin.rdbuf(), av[1]);
        char	c;

        while (in)
          if (in.get(c))
    	cout.put(c);
      }
      else
      {
        cerr << "Programname doesn't start with 'i' or 'o'!" << endl;
        exit(1);
      }

      return 0;
    }
    // </PRE>
    //----------------------------------------------------------------------------
    // <HR>
    // Please send comments, suggestions, problem reports, bug fixes etc. to 
    // <BR>
    // <A HREF="http://www.informatik.uni-konstanz.de/~kuehl"><I>Dietmar Kühl</I></A>:
    // <A HREF="mailto:dietmar.kuehl@claas-solutions.de">Dietmar.Kuehl@claas-solutions.de</A>
    // </BODY>
    // </HTML>
    

Please send comments, suggestions, problem reports, bug fixes etc. to
This file is converted from C++ source to HTML using c++2html.