1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
RelativePathUtilities.m
Implementations of extensions to the NSString class to provide
relative path handling.
Authors: Matt Rice <ratmice@yahoo.com>
Saso Kiselkov <diablos@manga.sk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#import "RelativePathUtilities.h"
#import <Foundation/NSArray.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSException.h>
@implementation NSString (RelativePathUtilities)
/**
* Builds a relative path from the path represented by the
* receiver to the path in `otherPath'.
*
* @return The resulting relative path.
*/
- (NSString *) stringByConstructingRelativePathTo: (NSString *) otherPath
{
NSString *prefix = [self commonPrefixWithString: otherPath
options: NSLiteralSearch];
NSRange rng1 = [otherPath rangeOfString: prefix];
NSRange rng2 = [self rangeOfString: prefix];
NSString *newPath = @"";
NSString *tmp1;
NSString *tmp2;
NSArray *arr2;
int i, c;
tmp1 = [otherPath substringFromIndex: rng1.location + rng1.length];
tmp2 = [self substringFromIndex: rng2.location + rng2.length];
arr2 = [tmp2 pathComponents];
for (i = 0, c = [arr2 count]; i < c; i++)
{
newPath = [newPath stringByAppendingPathComponent: @".."];
}
if ([tmp1 isAbsolutePath])
{
tmp1 = [@"." stringByAppendingString: tmp1];
}
return [newPath stringByAppendingPathComponent: tmp1];
}
/**
* Builds a minimalized path which results when the path
* represented by the receiver and `otherPath' are concatenated.
* For example, let the receiver be @"/foo/bar" and
* otherPath = @"../foobar". Then the returned result would be
* @"/foo/foobar". In case `otherPath' is an absolute path, this
* method simply returns `otherPath'.
*
* @return The concatenated and minimized result path.
*/
- (NSString *) stringByConcatenatingWithPath: (NSString *) otherPath
{
if ([otherPath isAbsolutePath])
{
return otherPath;
}
else
{
BOOL isAbsolute = [self isAbsolutePath];
NSMutableArray * myPathComponents = [[[self pathComponents]
mutableCopy] autorelease];
NSArray * otherPathComponents = [otherPath pathComponents];
NSEnumerator * e = [otherPathComponents objectEnumerator];
NSString * pathComponent;
while ((pathComponent = [e nextObject]) != nil)
{
if ([pathComponent isEqualToString: @".."])
{
int numberOfComponents = [myPathComponents count];
/* if we've run out of path components to pop away,
then we may safely return the rest of the relative
path given as the argument */
if (numberOfComponents == 0)
{
do {
[myPathComponents addObject: pathComponent];
} while ((pathComponent = [e nextObject]) != nil);
return [NSString pathWithComponents: myPathComponents];
}
/* trouble - we are an absolute path, but the argument
relative path wants ascend above our root. How do
we best handle this case? For now, we simply ignore
further ".." components, behaving exactly like a UNIX
system behaves when somebody tries to ascend above
the root of the VFS. */
else if (numberOfComponents == 1 && isAbsolute == YES)
{
continue;
}
else
{
[myPathComponents removeLastObject];
}
}
else
{
[myPathComponents addObject: pathComponent];
}
}
// this is to make +[NSString pathWithComponents:] construct
// an absolute path if necessary
if (isAbsolute == YES)
{
[myPathComponents replaceObjectAtIndex: 0 withObject: @""];
}
return [NSString pathWithComponents: myPathComponents];
}
}
@end
|