Goodbye, C++ , I kind of like much more another programming language now …

Yesterday I had to service a request from a tester: there’s an legacy application that’s still a money maker. It has been developed in Delphi 7 and it uses .INI files to store the application configuration. It’s a pain to maintain it, let me tell you …

There are two INI files containing exactly the same data, something like:

[Tests]

WBC=WBC

RBC=RBC

IMAGE001=SCAT_WBC

IMAGE002=DIST_PNG

FLAG001=Neutropenia

FLAG002=Neutrophilia

NRBC=NRBC

FLAG003=Anemia

So the tester wanted to split the [Tests] section into three sections, [Tests],[Images] and [Flags], and sort the lines inside the sections by key. I assume this would make it much easier for him to inspect the .INI files (I do not get it why we wants to do that, he should test the application using the black-box method). Of course I’d had to update the legacy Delphi 7 code to properly read and then write back the INI files, which I’ve immediately told his boss was not an option. They’ve accepted instead my solution, which is to implement a command line tool that reorganizes the [Tests] section in a more readable way: all “true tests” together, sorted by key, followed by all “Image*” lines sorted by key, followed by “Flag*” lines sorted by key.

I was not allowed to use C# to implement this utility, because the target systems still run an older version of .NET Framework. So, C++, back to you.

Boy, did I google a lot while writing the tool: how to read .INI files, how to parse the result of GetPrivateProfileSection (in the end I wrote my own state machine for this), how to sort a map (google told me the map is sorted by default by key 🙂

I spent a good half an hour figuring how to properly append a char to a string; first, I was calling s.append( c) and the compiler errored with a really unhelpful message. Once I’ve figured I should call s.append(c, 1) the code compiled, but it didn’t work. That’s because the C++ people are thinking backwards when it comes to the order of function parameters, I was calling s.append( c, 1 ) instead of s.append( 1, c ).

Then I was placing the final null terminator over an existing null terminator; another 10 minutes to figure it out.

Another 15 minutes to figure out a normal way to implement string.StartsWith (you should use mismatch for this).

When it was all done and refactored and ready for inspection, I’ve took a good look at the code. Even with auto keyword, iterators, algorithms, STL, the code is much less readable than the C# code. So, goodbye, good old C++ friend (pun intended:-), I’ll enjoy C# even more from now on.

Advertisements