Sunday, April 26, 2009

Using libcurl for simple http

In "Accelerated C++" the authors develop a function (find_urls) to extract URLs from a string. I tested it by downloading the home page of news.bbc.co.uk, saving it to disk and then using it as input to find_urls. The output was a list of all the URLs in the page. That's good, I thought but wouldn't it be nice if we could query the page live off the website without having to download it first?
So I went in search of a C/C++ library that would act as an http client. I came across libcurl - apparently a popular C library that does a lot more that http. A quick perusal of the docs reminded me how complex C APIs can be. Although there are examples on the libcurl site I decided to do what any programmer in 2009 now does - search StackOverflow to see if someone else has done it! And indeed they had. One of the answers contained a link to a blog site called Luckyspin.org, where the blogger had kindly put up a working example of how to download a webpage into a string using libcurl.
So, after a quick download of the libcurl development package and the Luckyspin sample I was off and running. Initially I tried to get libcurl to compile statically but couldn't get that to work, so I used the dll version. Unfortunately this adds about a meg of dlls that you need to lug around with you. But anyway, now I can do
libcurl_test http://new.bbc.co.uk | find_urls
Notice that the Luckyspin example did exactly what I wanted - it stored the web page in a string and then output it to stdout, meaning I could just pipe it into my find_urls program. I love it when I find stuff that works with no modification!

Sunday, April 19, 2009

Cheating at Bookworm Adventures

Bookworm Adventures is a word game where you have to make words out of a grid containing 16 random  letters. The longer the word, the bigger the damage you do to the current baddie. In essence, it's an anagram game - you need to jumble the letters up to make a legal word. I thought it would be fun as a first C++ project to create a program that outputs the longest legal word using the grid letters as input. Of course, me and my 7-year old son would only use this as a last resort (maybe).
This sort of thing can be done in 20 lines of Ruby. And probably any other scripting language. I guarantee my C++ version will be much longer! Still, it's a useful exercise because it will involve I/O streams and STL containers and algorithms. It's also small enough to be done in an evening (hopefully)!

Wednesday, April 15, 2009

There really aren't enough C++ blogs

Well there isn't, is there? So I decided to start one. Who the hell am I anyway? Well, I'm just your bog standard average Mere Mortal who has dabbled in programming over the past 20 years. By dabbled I mean I am not a professional programmer, although I do work as a Systems Engineer. I have used C, C++, Java and C# in various situations. I wouldn't call myself an expert in any of them.
The purpose of this blog is to document my progress in learning modern C++ with the help of two books - Accelerated C++ by Andrew
Koenig and Programming -- Principles and Practice Using C++ by the man who started it all, Bjarne Stroustrup. 
I am hoping others may find the journey interesting and maybe even useful - comments are welcome!