Revision: 12201 http://bibdesk.svn.sourceforge.net/bibdesk/?rev=12201&view=rev Author: hofman Date: 2008-01-02 12:19:21 -0800 (Wed, 02 Jan 2008)
Log Message: ----------- Convert FSRef to URL to avoid an extra conversion back and forth to the path. Modified Paths: -------------- trunk/bibdesk/BDSKLinkedFile.m Modified: trunk/bibdesk/BDSKLinkedFile.m =================================================================== --- trunk/bibdesk/BDSKLinkedFile.m 2008-01-02 20:12:31 UTC (rev 12200) +++ trunk/bibdesk/BDSKLinkedFile.m 2008-01-02 20:19:21 UTC (rev 12201) @@ -292,38 +292,37 @@ @implementation BDSKLinkedAliasFile -// guaranteed to be called with a non-nil alias +// takes possession of anAlias, even if it fails - (id)initWithAlias:(AliasHandle)anAlias relativePath:(NSString *)relPath delegate:(id)aDelegate; { - NSParameterAssert(nil == aDelegate || [aDelegate respondsToSelector:@selector(basePathForLinkedFile:)]); - NSParameterAssert(NULL != anAlias); - if (self = [super init]) { + OBASSERT(nil == aDelegate || [aDelegate respondsToSelector:@selector(basePathForLinkedFile:)]); + + if (anAlias == NULL) { + [[super init] release]; + self = nil; + } else if (self = [super init]) { fileRef = NULL; // this is updated lazily, as we don't know the base path at this point alias = anAlias; relativePath = [relPath copy]; delegate = aDelegate; + } else { + DisposeHandle((Handle)anAlias); } return self; } - (id)initWithAliasData:(NSData *)data relativePath:(NSString *)relPath delegate:(id)aDelegate; { - NSParameterAssert(nil != data); + OBASSERT(nil != data); + AliasHandle anAlias = BDSKDataToAliasHandle((CFDataRef)data); - if (anAlias == NULL) { - [[super init] release]; - self = nil; - } else { - self = [self initWithAlias:anAlias relativePath:relPath delegate:aDelegate]; - if (self == nil) - DisposeHandle((Handle)anAlias); - } - return self; + return [self initWithAlias:anAlias relativePath:relPath delegate:aDelegate]; } - (id)initWithBase64String:(NSString *)base64String delegate:(id)aDelegate; { - NSParameterAssert(nil != base64String); + OBASSERT(nil != base64String); + NSData *data = [[NSData alloc] initWithBase64String:base64String]; NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data]; [data release]; @@ -332,22 +331,17 @@ - (id)initWithPath:(NSString *)aPath delegate:(id)aDelegate; { - NSParameterAssert(nil != aPath); - NSParameterAssert(nil == aDelegate || [aDelegate respondsToSelector:@selector(basePathForLinkedFile:)]); + OBASSERT(nil != aPath); + OBASSERT(nil == aDelegate || [aDelegate respondsToSelector:@selector(basePathForLinkedFile:)]); + NSString *basePath = [aDelegate basePathForLinkedFile:self]; NSString *relPath = [basePath relativePathToFilename:aPath]; AliasHandle anAlias = BDSKPathToAliasHandle((CFStringRef)aPath, (CFStringRef)basePath); - if (anAlias != NULL) { - if ((self = [self initWithAlias:anAlias relativePath:relPath delegate:aDelegate])) { - if (basePath) - // this initalizes the FSRef and update the alias - [self fileRef]; - } else - DisposeHandle((Handle)anAlias); - } else { - [[super init] release]; - self = nil; + if (self = [self initWithAlias:anAlias relativePath:relPath delegate:aDelegate]) { + if (basePath) + // this initalizes the FSRef and update the alias + [self fileRef]; } return self; } @@ -355,12 +349,13 @@ - (id)initWithURL:(NSURL *)aURL delegate:(id)aDelegate; { OBASSERT([aURL isFileURL]); + return [self initWithPath:[aURL path] delegate:aDelegate]; } - (id)initWithURLString:(NSString *)aString; { - OBASSERT_NOT_REACHED("Attempt to initialize BDSKLocalFile with a URL string"); + OBASSERT_NOT_REACHED("Attempt to initialize BDSKLinkedAliasFile with a URL string"); return nil; } @@ -420,7 +415,8 @@ } - (void)setDelegate:(id)newDelegate { - NSParameterAssert(nil == newDelegate || [newDelegate respondsToSelector:@selector(basePathForLinkedFile:)]); + OBASSERT(nil == newDelegate || [newDelegate respondsToSelector:@selector(basePathForLinkedFile:)]); + delegate = newDelegate; } @@ -493,8 +489,16 @@ - (NSURL *)URL; { - NSString *path = [self path]; - return path ? [NSURL fileURLWithPath:path] : nil; + BOOL hadFileRef = fileRef != NULL; + CFURLRef aURL = hadFileRef ? CFURLCreateFromFSRef(NULL, fileRef) : NULL; + + if (aURL == NULL && hadFileRef) { + // fileRef was invalid, try to update it + [self setFileRef:NULL]; + if ([self fileRef] != NULL) + aURL = CFURLCreateFromFSRef(NULL, fileRef); + } + return [(NSURL *)aURL autorelease]; } - (NSURL *)displayURL; @@ -505,25 +509,10 @@ return displayURL; } -- (NSString *)path; -{ - BOOL hadFileRef = fileRef != NULL; - const FSRef *ref = [self fileRef]; - NSString *path = ref == NULL ? nil : (NSString *)BDSKFSRefToPathCopy(ref); - - if (path == nil && hadFileRef) { - // apparently fileRef is invalid, try to update it - [self setFileRef:NULL]; - if (ref = [self fileRef]) - path = (NSString *)BDSKFSRefToPathCopy(ref); - } - return [path autorelease]; -} - - (NSData *)aliasDataRelativeToPath:(NSString *)basePath; { // make sure the fileRef is valid - [self path]; + [self URL]; FSRef *fsRef = (FSRef *)[self fileRef]; FSRef baseRef; @@ -637,7 +626,7 @@ - (id)initWithBase64String:(NSString *)base64String delegate:(id)aDelegate; { - OBASSERT_NOT_REACHED("Attempt to initialize BDSKLocalURL with a base 64 string"); + OBASSERT_NOT_REACHED("Attempt to initialize BDSKLinkedURL with a base 64 string"); return nil; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit