The Y utility

The windows command prompt (aka: DOS window, DOS shell) offers a limited set of text manipulation utilities, especailly when compared to the wealth of tools offered in Unix/Linux systems. The Y utility (below) tries to partially fill this gap: It reads its input from different sources (existing files, stdin) and combines it into a single document that is printed to stdout.

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;


void doFile(istream& in)
{   
   while(in)
   {
      string line;
      getline(in, line);
      if(!in)
         break;
      cout << line << endl;
   }
}

void usage(const char* prg)
{
   cerr << prg << ": Text merging utility" << endl;
   cerr << "Command line options (may recur in any order)" << endl;
   cerr << "         <file>     Print contents of <file> to stdout" << endl;
   cerr << "         -i         Print contents of stdin to stdout" << endl;
   cerr << "         -s<string> Print <string> to stdout" << endl;
   exit(-1);
}

int main(int argc, const char* argv[])
{
   for(int i = 1; i < argc; ++i)
   {
      string curr = argv[i];      
      if(curr.find("-s") == 0)
      {
         cout << curr.substr(2) << endl;
         continue;
      }
      
      if(curr == "-i")
      {
         doFile(cin);
         continue;
      }
      
      if(curr.find("-") == 0)
         usage(argv[0]); // Program stops
      
      ifstream in(argv[i]);
      doFile(in);
   }
   
   return 0;
}