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

.code { background:#f5f8fa; background-repeat:no-repeat; border: solid #5C7B90; border-width: 1px 1px 1px 20px; color: #000000; font: 13px 'Courier New', Courier, monospace; line-height: 16px; margin: 10px 0 10px 10px; max-height: 200px; min-height: 16px; overflow: auto; padding: 28px 10px 10px; width: 90%; } .code:hover { background-repeat:no-repeat; }