Programming Assignment 5a - Demand Output

Computer Algorithms I, Fall 2003


Due Date

This assignment is due by 5:00 p.m. on Thursday, 4 December.

See the assignment turn-in page (last modified on 3 November 2003) for instructions on turning in your assignment.

The Problem

Modify your code so it no longer prints word statistics after every page, but instead prints them in response to a URI of the form

http://pagelog/filename

If the /filename or filename portion of the host is missing, output should go to std-out (which corresponds to std::cout); otherwise output should be written to the file with the given name (which does not include the initial slash). The new file should replace an old file of the same name. filename may begin with a slash, as in, for example, the URI

http://pagelog//tmp/words.out

In such cases, the filename without the initial slash is, to continue the example, /tmp/words.out, which creates the file words.out in the directory /tmp. Files that are not absolute are written relative to the directory in which cmd(page-logger) is running.

There are three filenames your code should recognize and treat specially:

  1. http://pagelog/std::cout - Output should be written to std-out (which corresponds to std::cout). This is the default behavior in the absence of a filename.

  2. http://pagelog/std::cerr - Output should be written to std-error (which corresponds to std::cerr).

  3. http://pagelog/html - Output should be written to (that is, stored in) a storage block allocated within mitm() and returned via the resource parameter in mitm(); the output should not be null terminated. The size field of the resource contains the output's length in characters. The information in the block will be sent back to the requesting browser as an HTML page; the code calling mitm() will free the storage block.

    You might want to review Section 3.4 of your text for help on implementing this form of output; you can also look at Chapter 19 in Deitel and Deitel.

The special filenames must appear exactly as given above; the URI http://pagelog/cout creates a file called cout in the directory in which the page-logger is running and writes the word statistics to it.

If an error occurs when writing a file, your code should pass back an informative error message using the same approach as described for HTML output above. The error message will be sent back to the requesting browser as an HTML page. If there were no errors during the file write, or if the resource is unused, your code should set the size field of the resource to zero.

To accommodate the ability to pass information back out via the resource parameter, the prototype for mitm() has been changed from

void mitm(const std::string &, const resource &)

to

void mitm(const std::string &, resource &)

Make sure you update your code accordingly. Remember that it is only when mitm() is called with a pagelog URI that your code uses the resource parameter to send information (if any) back to the mitm() caller. If your code does not send any information back to the caller via the resource parameter in response to a pagelog URI, your code should should set the size field of the resource parameter to zero. When called with a pagelog URI, your mitm() code should assume nothing about the initial contents of the resource parameter.

The behavior described in the previous paragraph only occurs when mitm() is called with a pagelog URI. When mitm() is called with a non-pagelog URI, the resource parameter is used as an input parameter to mitm(), as it has for all previous assignments.

You can find os-page-logger, my solution to this assignment, in the assignment directory; os is one of linux or solaris. Remember, the objective of this assignment is to correctly generate a reverse index of words in HTML documents; it is not to faithfully reproduce the behavior of my solution. If my solution's wrong and you copy the error, you're going to lose points.


This page last modified on 5 December 2003.