// Copyright (c) 1999-2000 David Muse
// See the COPYING file for more information

#ifndef PAGE_H
#define PAGE_H

#include <inputoutput.h>
#include <cgi.h>
#include <pathnames.h>

// The page class provides functions to parse a page and process the events
// generated by it.

class	page : virtual public inputoutput {
	public:
			page();
		virtual	~page();

			void	initPage (cgi *cgiptr, pathnames *pathsptr);

			// page processing functions
			void	rewriteBase(char *protocol,
					char *hostname, char *path, char *file);
			void	rewriteBase(char *base);
				// provokes the parsing routines to
				// replace the value of the href attribute
				// in the <base> tag (should there be one) 
				// with the provided base
			void	dontRewriteBase();
				// turns off base-rewriting behavior (default)
			void	rewriteFrames(char *extension);
				// Provokes the parsing routines to
				// replace the value of the src attribute
				// in all <frame> tags with the corresponding
				// cgi, should that cgi exist. This allows a
				// frameset page to reference html files for
				// development purposes.  The cgi is assumed
				// to have the same name as the html file but
				// a different extension as defined by 
				// "extension".
			void	dontRewriteFrames();
				// turns off frame-rewriting behavior (default)

			int	sendPage(char *path, char *filename);
				// sends page /"path"/"filename" to the browser

			int	parsePage(strstream *container, char *path, 
						char *filename, ...);
				// Page /"path"/"filename" is parsed.
				//
				// A variable in the page is denoted as
				// $(variable) and replaced with either a value
				// specified in the method call, the named
				// environment variable, or the named form 
				// entry passed from the previous page.
				//
				// To specify replacement values for variables,
				// use the method as follows:
				//
				// parsePage(container,path,filename,
				//	variable_name_array1,
				//		replacement_value_array1,
				//	variable_name_array2,
				//		replacement_value_array2,
				//	NULL,
				//	variable_name1,
				//		replacement_value1,
				//	variable_name2,
				//		replacement_value2,
				//	NULL);
				// 
				// Any number of array and individual value
				// replacements may be specified.
				//
				// In addition, for each 
				// <!-- start segment -->
				// ...
				// <!-- end segment -->
				// in the page, handleSegment() is called
				// and passed the html between the comments
				// and the "segment" name.
				//
				// handleSegment() is a protected, virtual
				// function which is expected to be 
				// overriden and contain a block of condition 
				// statements to handle each possible segment.
				//
				// The resulting, parsed page is returned in
				// "container".  If container is NULL, then
				// the result is written directly to the
				// browser.

			void	parseSegment(strstream *container,
						char *string, ...);
				// This function serves the same purpose as 
				// parsePage, but instead of parsing a file,
				// it parses a string passed into "string" and
				// returns the result in "container".  If
				// container is NULL, then the result is
				// written to the browser directly.

		virtual	int	handleSegment(strstream *container,
						char *name, char *segment);
				// this function should be overridden
				// in the derived class

	private:
		#include <private/page.h>

};

#endif