Using C++ iostreams for file IO in Cocoa
Using C++ iostreams for file IO in Cocoa
- Subject: Using C++ iostreams for file IO in Cocoa
- From: Bert Torfs <email@hidden>
- Date: Wed, 1 Feb 2006 20:51:58 +0100
First remark : it will make your application harder to universalize
because of byte-order stuff.
I did not use streams, but - for historical reasons - the stream
classes of PowerPlant. My pure C++ business layer classes
(theDocument) still think they write themselves to Powerplant
streams. These stream classes read or write from the NSData objects
you get in the Cocoa persistency methods.
Opening files is done in the dataRepresentation from file method. All
the open and save dialog stuff has been done at that time. My code
looks like this
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
/*
Create an LStream object that takes its data from 'data'
*/
long theLength;
void *theData;
theLength = [data length];
theData = (void*)[data bytes];
// [data getBytes:theBuffer];
LDataStream *theInputStream = new LDataStream();
theInputStream->setBuffer(theData ,theLength);
theDocument->initFromStream(theInputStream);
etc...
LDataStream is an unchanged powerplant class.
Saving files is done in dataRepresentationOfType
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
NSMutableData* theData;
BTDataStream* theDataStream;
theData = [[NSMutableData dataWithCapacity:2000]autorelease];
theDataStream = new BTDataStream();
theDataStream->setDataObject(theData);
theDocument->DoSave(theDataStream);
return theData;
}
Here, BTDataStream is an LStream (from PowerPlant) descendant that
has its PutBytes method changed to write to the NSMutableData it has
been given in the setDataObject method..
Hope this helps,
Bert
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden