Programming Challenges:110507 The Stern-Brocot Number System

Last night I was bored, and decided to tackle a Programming Challenges problem: 110507 The Stern-Brocot Number System seemed easy, and in the end it was.

However it took two attempts, due to messing up the termination logic. ie run till m and n are both 1

I first had:

    while (m != 1 && n != 1)

instead of:

    while (m != 1 || n != 1)

maybe I should have used

    while (!(m == 1 && n == 1))

Gosh I dislike double negatives to loop logic.

GetTextMetricsW causing memory corruption

I have been converting our ANSI product to UNICODE, and today during testing I was getting “Run-Time Check Failure #2 – Stack around the variable ‘tm’ was corrupted.” error messages.

Turned out it was this code:

	TEXTMETRICW tm;
	GetTextMetricsW( hDC, &tm ) ;

I isolated the code into a test function, which then became two functions:

void TestFuncA(HDC hDC)
{
	TEXTMETRICA tm;
	void* p = &tm;
	memset(&tm, -1, sizeof(tm) );
	GetTextMetricsA( hDC, &tm ) ;
}

void TestFuncW(HDC hDC)
{
	TEXTMETRICW tm;
	void* p = &tm;
	memset(&tm, 0, sizeof(tm) );
	GetTextMetricsW( hDC, &tm ) ;
}

For TestFuncW the sizeof(tm) is 57 bytes, yet GetTextMetricsW is over writing 60, as seen by the Memory debugging window, whereas for TestFuncA sizeof(tm) is only 53 bytes, and GetTextMetricsA overwrites only 53 bytes.

I then created a brand new Win32 Windows C++ application, and placed my test code into this, and strangely enough, for TestFuncW the sizeof(tm) is 60 bytes, yet GetTextMetricsW is over writing 60, and for TestFuncA sizeof(tm) is only 56 bytes, and GetTextMetricsA overwrites only 53 bytes.

Hazarding a guess, “that it is the fact the original product as Struct Member Alignment (same as #pragma pack() ) set to 1 Byte (/Zp1)”, I changed this from the default to 1, and tada!

Now my new project crashes also.

Lesson for others, avoid Struct Member Alignment across full projects.

Lesson for me, I now have weeks of work to unravel the effects/impact of why it was set to 1 in the first place (old legacy code and lazy developers)

Why my file operations fail on the Mac

My first thought for why things fails is the the Environment.SpecialFolder don’t map, and thus I found this page talking about it

So I ran the code on my Windows XP box and got:

Desktop                 C:\Documents and Settings\spilgrim\Desktop
Programs                C:\Documents and Settings\spilgrim\Start Menu\Programs
Personal                C:\Documents and Settings\spilgrim\My Documents
Personal                C:\Documents and Settings\spilgrim\My Documents
Favorites               C:\Documents and Settings\spilgrim\Favorites
Startup                 C:\Documents and Settings\spilgrim\Start Menu\Programs\Startup
Recent                  C:\Documents and Settings\spilgrim\Recent
SendTo                  C:\Documents and Settings\spilgrim\SendTo
StartMenu               C:\Documents and Settings\spilgrim\Start Menu
MyMusic                 C:\Documents and Settings\spilgrim\My Documents\My Music
DesktopDirectory        C:\Documents and Settings\spilgrim\Desktop
MyComputer
Templates               C:\Documents and Settings\spilgrim\Templates
ApplicationData         C:\Documents and Settings\spilgrim\Application Data
LocalApplicationData    C:\Documents and Settings\spilgrim\Local Settings\Application Data
InternetCache           C:\Documents and Settings\spilgrim\Local Settings\Temporary Internet Files
Cookies                 C:\Documents and Settings\spilgrim\Cookies
History                 C:\Documents and Settings\spilgrim\Local Settings\History
CommonApplicationData   C:\Documents and Settings\All Users\Application Data
System                  C:\WINDOWS\system32
ProgramFiles            C:\Program Files
MyPictures              C:\Documents and Settings\spilgrim\My Documents\My Pictures
CommonProgramFiles      C:\Program Files\Common Files

and on my MacBook I get

Desktop                 /Users/parents/Desktop
Programs
Personal                /Users/parents
Favorites
Startup
Recent
SendTo
StartMenu
MyMusic                 /Users/parents/Music
DesktopDirectory        /Users/parents/Desktop
MyComputer
Templates
ApplicationData         /Users/parents/.config
LocalApplicationData    /Users/parents/.local/share
InternetCache
Cookies
History
CommonApplicationData   /usr/share
System
ProgramFiles
MyPictures              /Users/parents/Pictures
CommonProgramFiles

One question raised on that page is why are there two Personal values, and it’s because MyDocuments is the second Enumeration, but .ToString() only names the first in order of an enum set.

So the mystery deepens as I’m using Environment.SpecialFolder.Personal, and therefore it should work, but it seems to be failing to create the data directory, time to explore more…

After some a bit more playing around I found the file “SAVE/player.cha” in my Mac folder structure, and realised I was doing string concatenation with DOS slashes, etc. hard coded all over the place, so I refactored the code, and now it saves/loads files fine.

I then created a Mac .app bundle, so you can now just download form Google Code the Mac OS X version of the game, un-tar/gzip it, and as long as you have Mono installed, tada, instant game action. Albeit slow…

Only problem to solve is that the save files are case sensitive, in how they are searched and managed, and I’ve not got a standard schema in place. I may have to build some wrapper code to manage this, so odd DOS/Windows casing works fine on Mac/Linux systems.

Also I’ll have to make a Linux tar ball for those Linux/Mono people to test play with.

Sony Walkman NWZ-E345 and MacBook

We purchased a Sony Walkman NWZ-E345 for Michaela’s welcome to the USA present.

We had some concern once we got home because it only mentions support for Windows XP and Windows Vista on the packaging, and nobody on Amazon had mentioned Mac support.

But never fear it works perfectly with our MacBook, it just appears as an USB device, and you just drag’n’drop like an USB hard drive, and when you disconnect (after ejecting via Finder), it rescans the library.

Michaela really likes it, problem solved (the problems was bad music choice in the car).

Curse of the Azure Bonds port working on Mac OS X

Just tried running the game under Mono on my MacBook, and it like works!

Except for

  • Saved games/player file IO does not work. I think this is due to the new Vista locations, thus I’ll need a new pick a location code, in which case I could leave Windows XP working in the old location…
  • It runs really slow in combat, ie you can see the icon redraws
  • There is no sound
  • The right click context menu do not seem to work
  • You have to lunch it from the terminal prompt