home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 8
/
amigaformatcd08.iso
/
in_the_mag
/
html_tutorial
/
mas_try9.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1996-09-03
|
3KB
|
150 lines
// (C) M.A.Smith University of Brighton
//
// Permission is granted to use this code
// provided this declaration and copyright notice remains intact.
//
// 21 October 1995
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define NO_MAP
#define SERVER "http://snowwhite.it.brighton.ac.uk/"
#define TRY_PROG "cgi-bin/mas/mas_try2"
#include "parse.h"
#include "parse.cpp"
void process_script( char [], char [] );
void process( char [] );
char* getenv_n( char [] );
inline void html( char str[] )
{
cout << str << "\n";
}
inline void html_( char str[] )
{
cout << str;
}
inline void html_( char c )
{
cout << c;
}
// Main program
//
int main()
{
char *query_str = getenv("QUERY_STRING");
Parse list( query_str == 0 ? "file=html.htm&seg=H6" : query_str );
process_script( list.get_item( "file", 1, true ),
list.get_item( "seg" ) );
return 0;
}
void process_script( char file[], char seg[] )
{
enum State { NORMAL, IN_ESC, SAVE };
html( "Content-type: text/html" );
html( "" ); html( "" );
ifstream script( file );
if ( script.fail() ) return;
script >> resetiosflags( ios::skipws );
const int MAX_MES = 200;
char c;
char mes[ MAX_MES ];
char start[] = "<TRY-ME"; const int START_LEN = strlen( start );
char end[] = "</TRY-ME>"; const int END_LEN = strlen( start );
int i, p=0, mes_len = 0;
State state_is = NORMAL;
while ( script >> c, !script.eof() )
{
switch ( state_is )
{
case NORMAL :
if ( c == start[p] ) { // Escape Sequence
p++;
if ( p >= START_LEN ) { // Recognized Escape Seq.
state_is = IN_ESC;
mes_len = 0; p = 0;
}
} else { // Not Escape Seq.
p = 0;
}
break;
case IN_ESC : // In Escape Sequence
if ( c != '>' )
{
mes[p] = c;
p++;
} else {
mes[p] = '\0';
Parse rest(mes);
if ( strcmp( rest.get_item_n( "name" ), seg ) ) {
state_is = SAVE; p = 0;
}
}
break;
case SAVE :
if ( c == end[p] ) { // End of Escape Seq
p++;
if ( p >= END_LEN ) { // End of Escape Seq
mes[ mes_len ] = '\0'; // Message to process
process( mes );
p = 0; state_is = NORMAL;
}
} else { // Process gatherd chrs
if ( mes_len < MAX_MES - 10 ) {
for ( i = 0; i< p; i++ ) {
mes[mes_len++] = end[i];
}
mes[mes_len++] = c; p = 0; // Add to gatherd chrs
}
}
break;
}
}
}
void process( char mes[] )
{
html("<HTML>");
html("<HEAD>");
html("<TITLE>Example of HTML</TITLE>");
html("</HEAD>");
html("<BODY>");
html("<P>Try out the HTML<P> ");
html("<FORM ACTION=\"" SERVER TRY_PROG "\"" );
html("<TEXTAREA NAME=\"feedback\" ROWS=10 COLS=60>" );
html( mes );
html("</TEXTAREA>");
html("<INPUT TYPE=\"submit\" NAME=\"action\" VALUE=\"Execute HTML\">" );
html("</FORM>");
html("</BODY>");
html("</HTML>");
}
char* getenv_n( char var[] )
{
char *p = getenv( var );
return p == NULL ? "[]" : p;
}