Source for main.cpp for Part 4

This document shows the contents of main.cpp if you are starting the tutorial in Part 4. Developing Classes in the IDE.

#include <fstream.h>

int main(int argc, char* argv[]) {
   // Declare a Request object; this reads the request into fields of object
   Request Req;
   // Output is plain text unless later shown otherwise
   IBoolean HtmlOut=false;
   // Initialize the Result string to contain output
   IString Result="";
   // For now, require that key value be "secret"
   if (Req.GetKey()!="secret")
      Result="Sorry, you aren't authorized";
   else {
      // Handle request for file.  Display if found.
      if (Req.GetFilename()=="")
         Result="Sorry, you didn't specify a file";
      else {
         ifstream InFile(Req.GetFilepath());
         if (!InFile) Result="Sorry, file not found";
         else {
            // IString::lineFrom(ifstream,EOF) reads entire file
            Result=IString::lineFrom(InFile,EOF);
            // If the file contains "<html>" or "<HTML>" set HtmlOut to true
            if (Result.indexOf("<html>")>0 || Result.indexOf("<HTML>")>0)
               HtmlOut=true;
            else if (Result=="") Result="Sorry, file was empty";
         }
      }
   }
   // Write the Content-Type header of the appropriate type (html/plain)
   cout << "Content-Type: text/";
   if (HtmlOut) cout << "html";
   else         cout << "plain";
   // Write the result. The two \n's are a required part of the header.
   cout << "\n\n" << Result << flush;
   return 0;
}