Wednesday, March 10, 2010

C++ and Web Development

In my first few years as a young programmer, I thought the whole programming world revolved around C and C++. To best honest, they still are a very popular choice for a programming language. In terms of web development, it may not be, but I've been playing with the idea of using C++ to develop my next website. How can this be done you ask?

Well, there is a C++ class library that will definitely help you get started. GNU has release a library call cgicc. After checking it out, and creating a fairly bare-bones website, It definitely opened up. It offers functionality which will make dealing with HTTP headers and tags much easier, and speed up development time. The class offers functions to create any HTML tag you like. At first this sounded pretty cool, but after actually developing a pretty simple HTML page, I found out that it was pretty frustrating and tedious. Naturally, I'm (and I'm sure you are as well) used to developing HTML by just writing code in a flat file. So, I went searching, and found another C++ library that will let me do this. It's called ctemplate, and it's brought to you by Google Code. With this class, you can write all your HTML in a file and then make some simple function calls within your codebase to make use of it. It's like most templating systems, in that you can set variables, create sections (and conditionals), loops and all the like so that developing with typical structures is seamless and efficient.

Now to the good stuff...

Here is a sample file to get started:


main.cpp:

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

#include <ctemplate/template.h>

using namespace std;

int main(int argc, char **argv) {
string content = "";

ctemplate::TemplateDictionary dict("example");
ctemplate::Template* tpl = ctemplate::Template::GetTemplate("main.tpl", ctemplate::DO_NOT_STRIP);

// If user is logged in
if(1 == 1) {
dict.ShowSection("USER");
dict.SetValue("USERNAME","John Doe");
}
else {
dict.ShowSection("NOUSER");
}

tpl->Expand(&content, &dict);

cout << content;

return 0;
}

Here is the corresponding template file (main.tpl):

<div id="content">

<div id="header">
{{#USER}}
<div id="userinfo">{{USERNAME}} | <a href="/logout.html">logout</a></div>
<ul>
<li><a href="/projects.ehtml">Projects</a></li>
</ul>
{{/USER}}
{{#NOUSER}}
<ul><li> </li></ul>
{{/NOUSER}}
</div>

</div>

Making the ShowSection call will show the HTML between {{#USER}} and {{/USER}}. Normal variables are just like {{USERNAME}} or {{VARIABLE}}.

Although this is a fairly trivial application, I think it shows that developing with C++ doesn't have to be brain surgery or pain-staking. With these tools one could easily develop a lightly dynamic website with ease. Now, if you want to get into database use, sessions, and all kinds of other stuff, then one has to put a little more elbow grease and crank out some classes. Although, I wouldn't be surprised if there were some already out there.

1 comment: