Sunday, May 19, 2013

NSString stringByResolvingSymlinksInPath example ios


[NSString stringByResolvingSymlinksInPath]

Returns a new string made from the receiver by resolving all symbolic links and standardizing path.
- (NSString *)stringByResolvingSymlinksInPath
Return Value of [NSString stringByResolvingSymlinksInPath]
A new string made by expanding an initial tilde expression in the receiver, then resolving all symbolic links and references to current or parent directories if possible, to generate a standardized path. If the original path is absolute, all symbolic links are guaranteed to be removed; if it’s a relative path, symbolic links that can’t be resolved are left unresolved in the returned string. Returns self if an error occurs.
Discussion of [NSString stringByResolvingSymlinksInPath]
If the name of the receiving path begins with /private, the stringByResolvingSymlinksInPath method strips off the /private designator, provided the result is the name of an existing file.
Note that this method only works with file paths (not, for example, string representations of URLs).
Example of [NSString stringByResolvingSymlinksInPath]
void checkForCapsIssues(NSString* compiledPath)
{

NSArray* validFilePaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[compiledPath stringByDeletingLastPathComponent] stringByResolvingSymlinksInPath] error:nil];
NSString* lastPathComponent = [compiledPath lastPathComponent];
for (NSString* fileName in validFilePaths) {
    if([fileName isEqualToString:lastPathComponent])
    {
        return;
    }
    if([[fileName lowercaseString] isEqualToString:[lastPathComponent lowercaseString]])
    {
        NSLog(@"Warning! Caps Problem Found! %@", compiledPath);
        return;
    }
}

}
Example of [NSString stringByResolvingSymlinksInPath]
- (NSData *)bookmarkDataWithError:(NSError **)outError {
    if (outError) *outError = nil;
    NSString *path = [[self stringByResolvingSymlinksInPath]
                               stringByStandardizingPath];
    AliasHandle alias = NULL;
    FSRef itemRef;
    OSStatus status = FSPathMakeRef((const UInt8 *)[path UTF8String], &itemRef, NULL);
    if (status != noErr) {
        if (outError) {
            if (status == fnfErr) *outError = [NSError errorWithDomain:NSCocoaErrorDomain 
              code:NSFileNoSuchFileError userInfo:nil];
            else *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
              code:status userInfo:nil];
        }
        return nil;
    }
    status = FSNewAlias(NULL, &itemRef, &alias);
    if (status != noErr ) {
        if (outError)
        *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
                code:status userInfo:nil];
        return nil;
    }
    HLock((Handle)alias);
    NSData *bookmarkData =
[[[NSData dataWithBytes:*alias length:GetHandleSize((Handle)alias)] retain] autorelease];
    HUnlock((Handle)alias);
    if (alias) DisposeHandle((Handle)alias);
    return bookmarkData;
}