Sunday, January 22, 2012

Sending Nulls to stdout in REALBasic

(notes from the future: This is a historical repost of an article that I wrote on October 3rd, 2007. )

While writing a CGI application in REALbasic I noticed that I could not return an image. Something in the binary data was causing the data to truncate and get corrupted. Some investigation revealed that if the string you’re printing for output contained null characters, and pretty much any binary data does, it would hang up and never make it through. The solution was to link directly to the system frameworks on the Mac and write to stdout directly, not through RB’s print statement. This sounds straightforward but I have more experience with C on regular unix than the mac and it turns out you can’t just link in LibC in this way, you have to find the Cocoa/Carbon framework file. The declare in REALbasic to do this is:

soft declare function write Lib "System.Framework" (FileNo as integer, TheData as Ptr, BytesToWrite as integer) as integer 

Which you could pass a memory block to like this:

i = write( 1, m, m.size)

Potential problems are my assumption that the file handle for stdout will always be 1, but so far it hasn’t been wrong for anything I’ve used this for. 

(notes from the future: It’s possible that this bug has long since been fixed in RB as 2007 was a long time ago.  The bugs database has changed a couple of times since then and I can’t find any record of my original report of this so I don’t know. Just keeping the documentation alive in case anybody else should have similar problems.)

No comments:

Post a Comment