views:

280

answers:

6

The problem is that, well, it's C++. The way I've created them makes it such that they've always been run via a terminal/console window and wait for user input or else simply take a sample input and run with that. The output has also always been to the terminal screen or sometimes to a file. I'm not quite sure how I could take all of that and integrate it with a website while leaving the source code as it is, if that's at all possible. I guess what I'm trying to aim for is to have whatever website I use behave like a terminal window that will accept user input and then send it off to run the C++ program in question and return with the output (whatever it may be), all with minimal modification to the source code. Either that or else set up a more automated kind of page where a user can just click 'Go' and the program will run using a sample input.

When it comes to web I consider myself intermediate with HTML, CSS, PHP & MySQL, and a beginner with Javascript, so if this can be accomplished using those languages, that would be fantastic. If not, don't be afraid to show me something new though.

+1  A: 

have you considered using cgi ... its 19th century technology which lets webserver execute programs written in C/C++ to run and generate output

I do not know much about it ... but I used it for some school projects

jsshah
Perhaps you mean 20th century? :)
Greg Hewgill
19th century? phew...
Mike
I'm pretty sure he meant 19th.
NTDLS
As far as i remember CGI was developed in 90' of 20th century :)
Mike
I meant 19th century ... somewhere before computers were invented ;-)
jsshah
A: 

Show it all off with Screencasts. I use Camtasia Studio, but there are a ton of them out there: http://en.wikipedia.org/wiki/Screencast

Camtasia will even generate all of the HTML and Flash you need to upload to your web server. Buy a nice USB microphone, and write a script of what you're going to say and show.

lod3n
+2  A: 

The easiest interaction model to bring to the web is an application that takes its input up front and produces its output on stdout. In this situation, as the unknown poster mentioned, you could use CGI. But due to the nature of CGI, this will only work (in the simplest sense) if all the information is collected from the user in one page, sent to the application and the results returned in one page. This is because each invocation of a page using CGI spawns a new indepdent process to serve the request. (There are other more efficient solutions now, such as FastCGI which keeps a pool of processes around.) If your application is interactive, in that it collects some information, presents some results, prints some options, collects some more user input, then produces more results, it will need to be adapted.

Here is about the simplest possible CGI program in C++:

#include <iostream>
int main(int argc, char* argv[])
{
    std::cout << "Content-type: text/plain\n" << std::endl;
    std::cout << "Hello, CGI World!" << std::endl;
}

All it does is return the content type followed by a blank line, then the actual content with the usual boring greeting.

To accept user input, you would write a form in HTML, and the POST target would be your application. It will be passed a string containing the parameters of the request, in the usual HTTP style:

foo.cgi?QTY=123&N=41&DESC=Simple+Junk

You would then need to parse the query string (which is passed to the program via the QUERY_STRING environment variable) to gather the input fields from the form to pass to your application. Beware, as parsing parameter strings is the source of a great number of security exploits. It would definitely be worthwhile finding a CGI library for C++ (a Google search reveals many) that does the parsing for you. The query data can be obtained with:

const char* data = getenv("QUERY_STRING");

So at a minimum, you would need to change your application to accept its input from a query string of name=value pairs. You don't even need to generate HTML if you don't want to; simply return the content type as text/plain to begin with. Then you can improve it later with HTML (and change the content type accordingly).

There are other more sophisticated solutions, including entire web frameworks such as Wt. But that would involve considerable changes to your apps, which you said you wished to avoid.

gavinb
And I just found quite a good CGI tutorial here, with links to C++ CGI libraries: http://www.geekdaily.net/2007/08/06/c-a-basic-cgi-tutorial/
gavinb
+1  A: 

Almost off-topic, but you might want to take a look at Wt.

StackedCrooked
A: 

What is the purpose of showing off your projects? Do you wish to impress your friends or employers?

It doesn't seem feasible to emulate or port your C++ console apps through a web interface. I suppose you could write a bridge between a server side script and your C++ binary which passes the user input through to your app, then returns the result through the web interface. Bear in mind this would be a huge task for you to undertake.

Ruby have a compiler on their website which demonstrates this can be done.

However no one on the web would expect to run your C++ apps in a web browser. Also I think that anyone who is interested in running a C++ app would be totally comfortable with downloading a C++ binary that you made and running it (apart from the security risk) but when you think about it we download apps and run them all the time, whilst trusting the source.

I have a portfolio website which I created for the purpose of letting employers see my work. Take a look, it will give you an idea of another way you can do things.

Basically I provide the binaries for download, videos, screenshots and links. Things that the user can use to see my work quickly if they don't have time (or an appropriate computer) to run my projects on.

Good luck

Brock Woolf
A: 

I have no experience with this (other than hearing a guy on BART talk about implementing his server-side code all in C), but you might consider taking a look at SWIG (http://www.swig.org/). It allows you to wrap C++ so that you can access C++ code when using languages such as PHP.

davidg