PDA

View Full Version : C++ for CGI scripts



sean
01-02-2003, 01:15 PM
All my previous CGI scripts have been written in C, and I'm told that it can be done in any language that writes to STDIO. C++ writes to SDTIO, but I've never seen it used in scripts. I think OOP could be very useful in my next script, and Java applets are out of the question. What's the situation with browser's handling C++, anybody know?

Eibro
01-02-2003, 02:16 PM
Originally posted by Sean
All my previous CGI scripts have been written in C, and I'm told that it can be done in any language that writes to STDIO. C++ writes to SDTIO, but I've never seen it used in scripts. I think OOP could be very useful in my next script, and Java applets are out of the question. What's the situation with browser's handling C++, anybody know?
Why wouldn't it work? Its compiled just as C is. A processor doesn't differentiate between a program written in C, and one in C++.

ammar
01-02-2003, 03:41 PM
May I ask: How can you write CGI scripts in C?

Prelude
01-03-2003, 10:53 AM
>How can you write CGI scripts in C?
It's surprisingly simple, here is a CGI program that takes a message and creates a simple web page:


#include <stdio.h>
#include <string.h>

#define OPEN "<HTML><BODY>"
#define CLOSE "</BODY></HTML>"

void parseMsg ( char *buf, char *s )
{
strcpy ( buf, s ); /* Insert a more suitable parse routine here */
}

int main ( int argc, char **argv )
{
char msg[BUFSIZ];

if ( argc > 1 ) {
parseMsg ( msg, argv[1] );

printf ( "Content-type: text/html\n\n" );
printf ( "%s\n%s\n%s\n", OPEN, msg, CLOSE );
}

return 0;
}

Or the C++ equivalent:


#include <iostream>
#include <string>

const char OPEN[] = "<HTML><BODY>";
const char CLOSE[] = "</BODY></HTML>";

void parseMsg ( std::string& buf, char *s )
{
buf = s; /* Insert a more suitable parse routine here */
}

int main ( int argc, char **argv )
{
std::string msg;

if ( argc > 1 ) {
parseMsg ( msg, argv[1] );

std::cout<<"Content-type: text/html\n\n";
std::cout<< OPEN <<'\n'<< msg <<'\n'<< CLOSE <<std::endl;
}
}

-Prelude