C++: Assertion with Information

I find it useful to include the comment or information about an assertion in the assert() expression itself.

Code like:

// Check for invalid oxygen level
assert(oxyLevel < OXYGEN_MAX);

can easily be rewritten as:

assert((oxyLevel < OXYGEN_MAX) && "Invalid oxygen levels!!!");

This works because a C string constant is always true (non-zero).

I like the second version since it folds the comment information into the assert() expression. Also, when the assertion fails, Visual Studio pops up a dialog where you can see the entire assert() expression (which includes your informative string):

Visual C++: Read and Write INI Files

I find it useful to keep the settings for my application separated from the code in a initialization (INI) file. The application code reads these settings at runtime and uses them for initialization of its objects. This separation means that I can try the application with different settings without having to recompile the code. (C++ code takes ages to compile!) And the INI file is human readable and writable easily using any text editor.

A simple INI file:

;------------------------------------------------
; Foobar Settings (foobar.ini)
;------------------------------------------------

; Database settings
[DB_SETTINGS]
USER_NUM_MAX = 256  ; Maximum number of users

; Operation settings
[OP_SETTINGS]
CRITICAL_SIZE = 100000  ; Maximum memory
;------------------------------------------------

The Initialization (INI) file format is simple. Anything to the right of a semicolon ; is a comment. Sections are named within a pair of square brackets []. Key-value pairs are written as key=value.

To read an integer or string value use GetPrivateProfileInt() or GetPrivateProfileString(). To write a string value back to a key use WritePrivateProfileString(). (There is no function to write back an integer, convert it to a string.)

#include <Windows.h>

int userNumMax = GetPrivateProfileInt("DB_SETTINGS", "USER_NUM_MAX", 0, "foobar.ini");
WritePrivateProfileString("DB_SETTINGS", "USER_NUM_MAX", "99", "foobar.ini");

For other functions related to INI files look for those with the prefix GetPrivateProfile and WritePrivateProfile in Registry Functions. The functions with prefix GetProfile are meant for win.ini, which should be not be useful to anyone!

Windows 7: Hide Desktop Icons

In older versions of Windows I had to remove all the icons and folders from the desktop to keep it clean (which is how I like it). But, with Windows 7 you can keep your icons and folders on the desktop, but just hide them so they are not visible. You can always access them by going to the Desktop folder inside Windows Explorer.

To turn off the desktop icons: Right-click on the desktop and de-select ViewShow desktop icons.

Google IME: Kannada

Google has released its IME for Indian languages! And ಹೌದು (yes), Google Kannada IME is available! :-)

I am using it as my primary Kannada IME and it is pretty impressive. I was using the Baraha IME for Kannada transliteration until now. It is too early to comment on how Google Kannada IME handles the esoteric characters or words of the language or on its performance. But, it is definitely a good user experience, right out of the door. Google Pinyin IME has been available for years now and is a big hit. Surely their experience in that has served them well for other (easier) languages. (A Chinese language IME is any day far harder than any of the Indian languages.)

One major plus with the Google IME is that it shows word suggestions as you type. It also looks like Google IME maintains a user cache where I assume it will store the user picks of words. That should help the IME suggestions get better with time. Also to be noted is that this is yet another area where Google has squarely beaten Microsoft. All the NT flavours of Windows have always had complete Unicode support from the kernel up, but their Kannada IME was never a good experience. They stagnated development on their IMEs and look who got their goat! All in all, Google IME is a refreshing new experience to type in ಕನ್ನಡ (Kannada)! :-)

Python: Checking Type of Variable

isinstance() seems to be the preferred way to check the type of a Python variable. It checks if the variable (object) is an instance of the class object being checked against.

# Variables of different types
i = 1
f = 0.1
s = "Hell"
l = [0, 1, 2]
d = {0:"Zero", 1:"One"}
t = (0, 1, 2)
b = True
n = None

All of the following return True:

isinstance(i, int)
isinstance(f, float)
isinstance(s, str)
isinstance(l, list)
isinstance(d, dict)
isinstance(t, tuple)

True and False are an instance of bool, so:

isinstance(b, bool)

What about None? For that, there is always:

n is None

Windows: Shutdown or Restart from Command-Line

While connected to a computer over RDP, the Shutdown or Restart options are not available in the Start menu.

To shutdown from command-line:

$ shutdown /s /t 0

To restart from command-line:

$ shutdown /r /t 0

PS: Curious to know why Shutdown/Restart are not available over RDP? See “Logoff” and “Shutdown” Are Missing from the Start Menu When You Use Remote Desktop.

Windows Explorer: Why the Refresh?

Bharat was wondering why Windows Explorer needed a Refresh option. Certainly, a modern file viewer is aware of the changes to the files it is displaying without requiring a manual poke?! After all, Apple is so confident of the auto-refresh of Finder on OS X that it does not have the Refresh option! :-)

Not being able to figure out the reason, I asked Raymond Chen. Raymond, with his fantastic The Old New Thing blog has become the wise man on top of the Windows history mountain. Raymond replied pretty quickly:

Probably for similar reasons to the ones that cause people to write OSX extensions, eg. http://lifehacker.com/252956/download-of-the-day-refresh-finder-mac.

Not all network drives broadcast updates — I imagine that would be a performance nightmare in some cases — so you sometimes might need to refresh manually.

Windows (at least since Vista — it’s been too long since I used XP to remember) does do auto-refresh, which works most of the time and about as well as the one introduced in (IIRC) OSX 10.4 in my experience.

Windows: Cleartype on RDP

Cleartype is not turned on by default over Remote Desktop Protocol (RDP). Thus, if you connect to a computer using Remote Desktop Connection (RDC) you will notice aliased fonts, quite ugly in my opinion without Cleartype enabled.

If you are connecting to a Vista or Windows 7 computer, enable ExperienceFont smoothing in the RDC dialog.

If you are connecting to a Windows XP computer, a bit more work is needed. Apply the following Registry entries on that computer:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations]
"AllowFontAntiAlias"=dword:00000001
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp]
"AllowFontAntiAlias"=dword:00000001

Or just download the CleartypeOnRDP.reg file and execute it.