George V. Reilly

Decrementing Loops

The canonical for-loop in C and C++ is written thus, counting up i = 0, i = 1, ..., i = N-1:

for (int i=0; i < N; i++) {
    // loop body
}

(In C, you have to declare int i before the for-loop.)

Let's unpack that for-loop into an equivalent while-loop:

int i = 0;
while (i < N) {
    // loop body
    i = i + 1
}

In other words, we initialize i to zero. Then, before every execution of either loop, we check i < N. If i is still within bounds, we execute the loop body. Then we postin­cre­ment continue.

Sample ASP Components: now at Github

From October 1996 to May 1997, I wrote a number of sample components for the then-new Active Server Pages (Classic ASP). I worked for Mi­cro­Crafts, a consulting company in Redmond, WA; the samples were written for Mi­crosoft­'s Internet In­for­ma­tion Server (IIS) team. Most of the components used Mi­crosoft­'s new Active Template Library (ATL), a C++ library for COM.

This work had two important con­se­quences for me: Microsoft recruited me to join the IIS de­vel­op­ment team to work on improving ASP per­for­mance for IIS 3, and Wrox Press invited me to write Beginning ATL COM Pro­gram­ming I was originally supposed to be the sole author of the book, but I was a slow writer and I was continue.

Multilingual

This week, I have written code in C#, C++, Managed C++, C, WiX, NAnt, Ac­tion­Script, VBScript, JScript, cmd batch, NMake, HTML, XSLT, and Ruby. And I will probably get some Python in before the weekend is over. <boggle/>

NUnit calling CppUnit

Over the last few days, I've been adapting an existing native C++ library so that it can be called from managed code. I had written a large number of unit tests with CppUnit and I wanted to be able to call the tests from NUnit.

I suppose that I could have written a new CppUnit TestRunner so that I could call it from NUnit. Instead, I took the cheap-n-dirty route, playing with #define and include paths. It took less time to get working than it did to write this blog post.

Here's the original native CppUnit test code

//-------------------------------
// native\FooTest.h
//-------------------------------

#include <cppunit/extensions/HelperMacros.h>

class FooTest : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE( FooTest 
continue.

0xFF...FF / 10 == 0x19...99

A few weeks ago, I wrote a C++ routine to parse decimal numbers using the overflow detection principles of SafeInt. I couldn't find anything in the libraries that actually did a good job of checking for overflow.

Briefly, to see if unsigned values A+B overflow, check if (A > MAX_UINT - B). Similarly, A*B will overflow if (A > MAX_UINT / B).

// Convert a string to an unsigned. Returns 'true' iff conversion is legitimate.
bool
StringToUnsigned(
    const string& str,
    unsigned&     rUint)
{
    rUint = 0;

    if (str.empty())
        
continue.

Assert(Result.ToString() == "Expected")

I'm writing some C++ code at the moment, after months of C#. I'm trying to be very Test First, writing Red tests, then making them turn Green.

I'm also using CppUnit for the first time. It's not as easy as NUnit. You can't just declare your test method with an attribute, you have to declare the test method in a header file, place it inside a macro, and then have the test im­ple­men­ta­tion in a .cpp-file. And there's no nunit-gui. I'm using a post-build step to run the tests, which makes it fairly pain free.

There was one internal method that I didn't have an explicit test for, although I had tests continue.

Compiling Vim with Visual C++ 2003 Toolkit

I've been trying to make Vim 7 compile with the Microsoft Visual C++ 2003 Toolkit, as a favor to Bram Moolenaar, the primary author of Vim. He wants to be able to use the free compiler as the primary build tool for the Win32 version of Vim.

Oh. My. God.

The VC2003 toolkit may include a full optimizing compiler, but it's certainly far from a complete system for building Windows binaries.

First, I discovered that it came only with the C library headers, but not the Windows headers. That was easily rectified. Download the Platform SDK. Just the Windows Core SDK subset. This also got me nmake.

At this point, I was able to continue.

Printf Tricks

It may be old-fashioned, but I still find printf (and sprintf and _vsnprintf) incredibly useful, both for printing debug output and for generating formatted strings.

Here are a few lesser-known formats that I use again and again. See MSDN for the full reference.

%04x - 4-digit hex number with leading zeroes

A quick review of some of the basics.

%x prints an int in hexa­dec­i­mal.

%4x prints a hex int, right-justified to 4 places. If it's less than 4 digits, it's preceded by spaces. If it's more than 4 digits, you get the full number.

%04x prints a hex int, right-justified to 4 places. If it's less than 4 digits, it's preceded continue.