Until this weekend, my last C++ program (more like a hello world “application”), dated from a few years ago. This weekend, I needed to write a small utility, that had to be very lightweight and fast. So I thought, why not try to write it C++? I must say, it was a bit of a challenge being a C# .NET programmer, but I succeeded. The biggest challenge though, turned out to be understanding strings in Visual C++.

As a C# programmer – used to just simply using the “String” class whenever text is involved – I was utterly confused to find many different constructs for strings in (Visual) C++. LPTSTR, LPCSTR, CString, TCHAR[], std:string… just to name a few. String types seem to live on different islands and in villages. There is an Unicode island, an ANSI island. On both islands there are “standard” and Microsoft villages.

It is very difficult to get a clear overview of Visual C++ string landscape, even by Googling. What I found where mainly forum threads with confusing answers, and MSDN articles shedding light only on Microsoft variants like LPTSTR.
But, I found a lighthouse, a guide that clearly explains the why and what of Visual C++ strings:

Unraveling Strings in Visual C++.

I hope this will help other .NET programmers who found themselves lost in a sting of C++ islands.

CodeIgniter query log hook

August 22nd, 2011

Whenever I use an ORM, such as CodeIgniter’s Active Record implementation, I find it very important to see the actual queries generated. Mainly because the danger of using an ORM is not knowing what happens in the background, which can introduce hard to find bugs and performance problems.

Below you’ll find a CodeIgniter hook that logs all database queries to a simple text file. I found this code useful in my first CodeIgniter project (since it’s from my first CI project, I think many revisions will follow, but you’ll get the idea).

/* config/hooks.php */
$hook['post_system'][] = array(
        'class' => 'QueryLogHook',
        'function' => 'log_queries',
        'filename' => 'QueryLogHook.php',
        'filepath' => 'hooks'

/* application/hooks/QueryLogHook.php */
class QueryLogHook {

    function log_queries() {   
        $CI =& get_instance();
        $times = $CI->db->query_times;
        $dbs    = array();
        $output = NULL;    
        $queries = $CI->db->queries;

        if (count($queries) == 0)
            $output .= "no queries\n";
            foreach ($queries as $key=>$query)
                $output .= $query . "\n";
            $took = round(doubleval($times[$key]), 3);
            $output .= "===[took:{$took}]\n\n";

        if ( ! write_file(APPPATH  . "/logs/queries.log.txt", $output, 'a+'))
             log_message('debug','Unable to write query the file');


If I had a “programming quote of the week” on this blog, this would be the one for this week:

It’s harder to read code than to write it.

Very true indeed. Why?

We’re programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We’re not excited by incremental renovation: tinkering, improving, planting flower beds.

Read more: