FROM : Matthew Fahrenbacher
DATE : Sat Nov 09 23:11:01 2002
I was working on a project where I can distribute files at certain
times, but I'm running into a little snag. It's painfully easy to use
NSData to archive a files data and keep its attributes in an
NSDictionary to be applied later. The trick is to be able to maintain
the resource fork data as well. Here's my attempt at doing it, and I
was wondering if anyone could help point out what I'm doing wrong (I'm
not very good with the Carbon APIs).
- (NSData *)loadResourceDataForFile:(NSString *)source_path
{
//try to load resource data
Handle handle;
CFURLRef urlRef;
SInt16 refNum;
Str255 str;
BOOL isDir;
NSData *resourceData = nil;
[[NSFileManager defaultManager] fileExistsAtPath:source_path
isDirectory:&isDir];
//will this work if I don't escape out spaces? i.e.
@"/Documents/Hello World"
urlRef = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
(CFStringRef) source_path,
kCFURLPOSIXPathStyle,
isDir);
if (urlRef != NULL)
{
FSRef fsRef;
if (CFURLGetFSRef(urlRef, &fsRef))
{
FSSpec spec;
if(FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL,
&spec, NULL) == noErr)
{
if((refNum = FSpOpenResFile(&spec,fsRdPerm)) == -1)
return nil;
//load the resource data into memory
//**
SetResLoad(TRUE);
LoadResource(handle);
if(ResError() != noErr || handle == NULL)
return nil;
//we've got data!
BlockMove(*handle, str, GetHandleSize(handle));
//is this the right length?
resourceData = [[[NSData alloc] initWithBytes:str
length:256] autorelease];
//DisposeHandle(handle);
CloseResFile(refNum);
//**
}
}
CFRelease(urlRef);
}
return resourceData;
}
Everything is mostly the same for applying the resource fork data
except for between the stars (and the method signature)
- (void)applyResourceData:(NSData *)resourceData forFile:(NSString
*)source_path
//**
const char *bytes = [resourceData bytes];
handle = NewHandle( bytes[0]+1 );
if (handle != NULL)
{
BlockMove( bytes, (* handle), bytes[0]+1 );
WriteResource(handle);
DisposeHandle(handle);
}
//**
Thanks a lot,
Matt Fahrenbacher
DATE : Sat Nov 09 23:11:01 2002
I was working on a project where I can distribute files at certain
times, but I'm running into a little snag. It's painfully easy to use
NSData to archive a files data and keep its attributes in an
NSDictionary to be applied later. The trick is to be able to maintain
the resource fork data as well. Here's my attempt at doing it, and I
was wondering if anyone could help point out what I'm doing wrong (I'm
not very good with the Carbon APIs).
- (NSData *)loadResourceDataForFile:(NSString *)source_path
{
//try to load resource data
Handle handle;
CFURLRef urlRef;
SInt16 refNum;
Str255 str;
BOOL isDir;
NSData *resourceData = nil;
[[NSFileManager defaultManager] fileExistsAtPath:source_path
isDirectory:&isDir];
//will this work if I don't escape out spaces? i.e.
@"/Documents/Hello World"
urlRef = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
(CFStringRef) source_path,
kCFURLPOSIXPathStyle,
isDir);
if (urlRef != NULL)
{
FSRef fsRef;
if (CFURLGetFSRef(urlRef, &fsRef))
{
FSSpec spec;
if(FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL,
&spec, NULL) == noErr)
{
if((refNum = FSpOpenResFile(&spec,fsRdPerm)) == -1)
return nil;
//load the resource data into memory
//**
SetResLoad(TRUE);
LoadResource(handle);
if(ResError() != noErr || handle == NULL)
return nil;
//we've got data!
BlockMove(*handle, str, GetHandleSize(handle));
//is this the right length?
resourceData = [[[NSData alloc] initWithBytes:str
length:256] autorelease];
//DisposeHandle(handle);
CloseResFile(refNum);
//**
}
}
CFRelease(urlRef);
}
return resourceData;
}
Everything is mostly the same for applying the resource fork data
except for between the stars (and the method signature)
- (void)applyResourceData:(NSData *)resourceData forFile:(NSString
*)source_path
//**
const char *bytes = [resourceData bytes];
handle = NewHandle( bytes[0]+1 );
if (handle != NULL)
{
BlockMove( bytes, (* handle), bytes[0]+1 );
WriteResource(handle);
DisposeHandle(handle);
}
//**
Thanks a lot,
Matt Fahrenbacher






Cocoa mail archive

