Browse files

-[NSString ks_stringByIncrementingPath]

  • Loading branch information...
1 parent 2ed22e5 commit b86bdd31a4dc22adbf5f8f072c71aad01e8ed30b @mikeabdullah mikeabdullah committed May 26, 2011
Showing with 73 additions and 0 deletions.
  1. +12 −0 KSPathUtilities.h
  2. +61 −0 KSPathUtilities.m
View
12 KSPathUtilities.h
@@ -32,14 +32,26 @@
// Standardizes the paths and tests equality ignoring case
- (BOOL)ks_isEqualToPath:(NSString *)aPath;
+
+#pragma mark Path Suffix
+
+// Given a path "foo/bar.png", adjusts it to be "foo/bar-2.png". Calling -ks_stringByIncrementingPath on that string will then give "foo/bar-3.png" and so on
+- (NSString *)ks_stringByIncrementingPath;
+
// like -stringByAppendingString: but inserts the suffix string in front of path extension if there is one. e.g. [@"foo.png" ks_stringWithPathSuffix:@"-2"] = @"foo-2.png"
- (NSString *)ks_stringWithPathSuffix:(NSString *)aString;
+
+#pragma mark Comparing Paths
+
// Will preserve any trailing slashes that are part of self
- (NSString *)ks_pathRelativeToDirectory:(NSString *)otherPath;
- (BOOL)ks_isSubpathOfPath:(NSString *)aPath; // Does aPath contain self?
+
+#pragma mark POSIX Paths
+
// NSString has built-in methods for standardizing a path, but they consult the filesystem for symlinks. This method only looks at the path itself
- (NSString *)ks_standardizedPOSIXPath;
View
61 KSPathUtilities.m
@@ -27,6 +27,21 @@
#import "KSPathUtilities.h"
+@interface KSIncrementedPath : NSString
+{
+ @private
+ NSString *_storage;
+ NSString *_basePath;
+ NSUInteger _suffix;
+}
+
+- (id)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix;
+@end
+
+
+#pragma mark -
+
+
@implementation NSString (KSPathUtilities)
- (BOOL)ks_isEqualToPath:(NSString *)aPath;
@@ -38,6 +53,8 @@ - (BOOL)ks_isEqualToPath:(NSString *)aPath;
return result;
}
+#pragma mark Path Suffix
+
- (NSString *)ks_stringWithPathSuffix:(NSString *)aString;
{
NSString *result = self;
@@ -58,6 +75,11 @@ - (NSString *)ks_stringWithPathSuffix:(NSString *)aString;
return result;
}
+- (NSString *)ks_stringByIncrementingPath;
+{
+ return [[[KSIncrementedPath alloc] initWithBasePath:self suffix:2] autorelease];
+}
+
- (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath
{
if ([dirPath isAbsolutePath])
@@ -205,3 +227,42 @@ - (BOOL)ks_isEqualToPOSIXPath:(NSString *)otherPath
}
@end
+
+
+#pragma mark -
+
+
+@implementation KSIncrementedPath
+
+- (id)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix;
+{
+ NSParameterAssert(suffix >= 2);
+
+ self = [self init];
+
+ _basePath = [basePath copy];
+ _suffix = suffix;
+ _storage = [[basePath ks_stringWithPathSuffix:[NSString stringWithFormat:@"-%u", suffix]] copy];
+
+ return self;
+}
+
+- (void)dealloc;
+{
+ [_basePath release];
+ [_storage release];
+ [super dealloc];
+}
+
+- (NSString *)ks_stringByIncrementingPath;
+{
+ return [[[[self class] alloc] initWithBasePath:_basePath suffix:(_suffix + 1)] autorelease];
+}
+
+#pragma mark NSString Primitives
+
+- (NSUInteger)length; { return [_storage length]; }
+- (unichar)characterAtIndex:(NSUInteger)index; { return [_storage characterAtIndex:index]; }
+
+@end
+

0 comments on commit b86bdd3

Please sign in to comment.