[F-Script-talk] Re: F-script and binary data file
Brought to you by:
pmougin
From: Philippe M. <pm...@ac...> - 2005-05-17 23:02:50
|
Le 17 mai 05 =E0 07:37, Nicholas Crosbie a =E9crit : > I'm new to both F-Script and Cocoa. How (using > F-Script) would I map the contents of a BINARY data > file to objects. My BINARY file has HEADER, TEXT, > DATA, and ANALYSIS segments; the HEADER segment includes > ASCII-encoded integers indicating the byte offsets for > the start and end of each segment. > > Any help would be GREATLY appreciated. A typical way to handle this in Cocoa is to use the NSData class (see =20= http://developer.apple.com/documentation/Cocoa/Conceptual/BinaryData/=20 index.html) Below is an example that shows how you can get at the bytes of a =20 binary file from F-Script: I have a text file (which, after all, is also a binary data file) on =20 my desktop named "test.txt". Here is how I can read it from F-Script using NSData: "Load the content of my file into an NSData object" data :=3D NSData = dataWithContentsOfFile:'/Users/pmougin/Desktop/test.txt'. "Allocate a buffer large enough to contain all the bytes of my data" buf :=3D FSPointer malloc:data length. "Put my data in the buffer" data getBytes:buf. "Cast the pointer to my buffer to an unsigned char * (see the =20 Objective-C manual for an explanation about run-time type encoding)" buf setType:'C'. "I can now get at the bytes in my buffer" buf at:0 The last instruction return a number with a value of 72, which is the =20= ASCII code for the letter "H", which is expected because my file =20 start with the word "Hello". To get the value of the 2th byte you do =20 "buf at:1", which in my case returns 101, the ASCII code for the =20 letter "e", etc. When you are done with the buffer, don't forget to send it a free =20 message (i.e. "buf free") in order to dispose it. Alternatively you =20 can do "buf setFreeWhenDone:YES" after creating the buffer (or =20 later). The system will then take care of disposing the buffer when =20 you are done with it. Best, Philippe Mougin= |