Skip to content

Commit

Permalink
Updated to modern Obj-C.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulkite committed Aug 13, 2016
1 parent 0b3162b commit 25dd73a
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 294 deletions.
22 changes: 11 additions & 11 deletions KSFileWrapperExtensions.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ @implementation NSFileWrapper (KSFileWrapperExtensions)
- (NSString *)addFileWrapper:(NSFileWrapper *)wrapper subdirectory:(NSString *)subpath;
{
// Create any directories required by the subpath
NSArray *components = [subpath pathComponents];
NSArray *components = subpath.pathComponents;
NSFileWrapper *parentWrapper = self;

NSUInteger i, count = [components count];
NSUInteger i, count = components.count;
for (i = 0; i < count; i++)
{
NSString *aComponent = [components objectAtIndex:i];
NSFileWrapper *aWrapper = [[parentWrapper fileWrappers] objectForKey:aComponent];
NSString *aComponent = components[i];
NSFileWrapper *aWrapper = parentWrapper.fileWrappers[aComponent];
if (!aWrapper)
{
aWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
[aWrapper setPreferredFilename:aComponent];
aWrapper.preferredFilename = aComponent;
[parentWrapper addFileWrapper:aWrapper];

#if ! __has_feature(objc_arc)
Expand All @@ -62,13 +62,13 @@ - (NSString *)addFileWrapper:(NSFileWrapper *)wrapper subdirectory:(NSString *)s
- (void)ks_removeAllVisibleFileWrappers;
{
// Leopard had a bug where -fileWrappers returns its own backing store, which means it will mutate while enumerating
NSDictionary *wrappers = [[self fileWrappers] copy];
NSDictionary *wrappers = [self.fileWrappers copy];

for (NSString *aFilename in wrappers)
{
if (![aFilename hasPrefix:@"."])
{
NSFileWrapper *aWrapper = [wrappers objectForKey:aFilename];
NSFileWrapper *aWrapper = wrappers[aFilename];
[self removeFileWrapper:aWrapper];
}
}
Expand All @@ -89,7 +89,7 @@ - (NSFileWrapper *)ks_symbolicLinkDestinationFileWrapper;
}
#endif

NSFileWrapper *result = [[NSFileWrapper alloc] initWithURL:[self symbolicLinkDestinationURL]
NSFileWrapper *result = [[NSFileWrapper alloc] initWithURL:self.symbolicLinkDestinationURL
options:0
error:NULL];
return [result autorelease];
Expand All @@ -104,7 +104,7 @@ - (BOOL)ks_writeToURL:(NSURL *)URL options:(NSFileWrapperWritingOptions)options

- (BOOL)ks_writeToURL:(NSURL *)URL options:(NSFileWrapperWritingOptions)options originalParentDirectoryURL:(NSURL *)originalParentDirectory copyIfLinkingFails:(BOOL)fallbackToCopy error:(NSError **)outError;
{
NSString *filename = [self filename];
NSString *filename = self.filename;

// The NSFileWrapper docs state:
//
Expand All @@ -115,10 +115,10 @@ - (BOOL)ks_writeToURL:(NSURL *)URL options:(NSFileWrapperWritingOptions)options
// Does NSFileWrapper then sacrifice a little possible efficiency by only doing hardlinking if the filenames match too? i.e. that the filename being written to is the same as the source? If so, that would avoid this fasle positive. On the downside it would make adjusting filenames for existing files inside the package impossible to do efficiently, but that's pronbably not a big deal
// I haven't devised a proper test of NSFileWrapper for this yet; just going to go ahead and do the filename check for now

NSURL *originalURL = ([filename isEqualToString:[URL lastPathComponent]] ? [originalParentDirectory URLByAppendingPathComponent:filename] : nil);
NSURL *originalURL = ([filename isEqualToString:URL.lastPathComponent] ? [originalParentDirectory URLByAppendingPathComponent:filename] : nil);

// NSFileWrapper won't create hardlinks when writing an individual file, so we try to do so ourselves when reasonable, for performance reasons
if ([self isRegularFile])
if (self.regularFile)
{
// If the file is already inside a doc, we favour hardlinking for performance
if ([self matchesContentsOfURL:originalURL])
Expand Down
2 changes: 1 addition & 1 deletion KSMailtoURLs.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ extern NSString *KSURLMailtoHeaderBody;
+ (NSURL *)ks_mailtoURLWithEmailAddress:(NSString *)address headerLines:(NSDictionary *)headers;

// Nil if its not a mailto URL or has no header lines
- (NSDictionary *)ks_mailHeaderLines;
@property (nonatomic, readonly, copy) NSDictionary *ks_mailHeaderLines;

@end
6 changes: 3 additions & 3 deletions KSMailtoURLs.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ + (NSURL *)ks_mailtoURLWithEmailAddress:(NSString *)address headerLines:(NSDicti
if (headers)
{
NSString *query = [self ks_queryWithParameters:headers];
if ([query length])
if (query.length)
{
string = [string stringByAppendingFormat:@"?%@", query];
}
Expand All @@ -64,9 +64,9 @@ + (NSURL *)ks_mailtoURLWithEmailAddress:(NSString *)address headerLines:(NSDicti

- (NSDictionary *)ks_mailHeaderLines;
{
if (![[self scheme] isEqualToString:KSURLMailtoScheme]) return nil;
if (![self.scheme isEqualToString:KSURLMailtoScheme]) return nil;

NSString *urlString = [self absoluteString];
NSString *urlString = self.absoluteString;
NSRange queryIndicatorRange = [urlString rangeOfString:@"?"];

if (queryIndicatorRange.location != NSNotFound)
Expand Down
4 changes: 2 additions & 2 deletions KSPathUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

// 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
// More at http://www.mikeabdullah.net/incrementing-paths.html
- (NSString *)ks_stringByIncrementingPath;
@property (nonatomic, readonly, copy) 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;
Expand All @@ -62,7 +62,7 @@
#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;
@property (nonatomic, readonly, copy) NSString *ks_standardizedPOSIXPath;

// Like -isEqualToString: but ignores trailing slashes
- (BOOL)ks_isEqualToPOSIXPath:(NSString *)otherPath;
Expand Down
41 changes: 20 additions & 21 deletions KSPathUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ @interface KSIncrementedPath : NSString
NSUInteger _suffix;
}

- (id)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix;
- (instancetype)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix;
@end


Expand All @@ -49,7 +49,7 @@ + (NSString *)ks_stringWithPath:(NSString *)path relativeToDirectory:(NSString *
{
NSParameterAssert(path);

if (!directory || [path isAbsolutePath]) return path;
if (!directory || path.absolutePath) return path;

NSString *result = [directory stringByAppendingPathComponent:path];
return result;
Expand All @@ -59,11 +59,10 @@ + (NSString *)ks_stringWithPath:(NSString *)path relativeToDirectory:(NSString *

- (NSString *)ks_stringWithPathSuffix:(NSString *)aString;
{
NSString *extension = [self pathExtension];
if ([extension length])
NSString *extension = self.pathExtension;
if (extension.length)
{
NSString *result = [[[self
stringByDeletingPathExtension]
NSString *result = [[self.stringByDeletingPathExtension
stringByAppendingString:aString]
stringByAppendingPathExtension:extension];
return result;
Expand Down Expand Up @@ -154,8 +153,8 @@ - (void)ks_enumeratePathComponentsInRange:(NSRange)range

- (BOOL)ks_isEqualToPath:(NSString *)aPath;
{
NSString *myPath = [self stringByStandardizingPath];
aPath = [aPath stringByStandardizingPath];
NSString *myPath = self.stringByStandardizingPath;
aPath = aPath.stringByStandardizingPath;

BOOL result = ([myPath caseInsensitiveCompare:aPath] == NSOrderedSame);
return result;
Expand Down Expand Up @@ -193,14 +192,14 @@ - (BOOL)ks_isSubpathOfPath:(NSString *)aPath

- (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath
{
if ([dirPath isAbsolutePath])
if (dirPath.absolutePath)
{
if (![self isAbsolutePath]) return self; // job's already done for us!
if (!self.absolutePath) return self; // job's already done for us!
}
else
{
// An absolute path relative to a relative path is always going to be self
if ([self isAbsolutePath]) return self;
if (self.absolutePath) return self;

// But comparing two relative paths is a bit of an edge case. Internally, pretend they're absolute
dirPath = (dirPath ? [@"/" stringByAppendingString:dirPath] : @"/");
Expand All @@ -209,7 +208,7 @@ - (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath


// Determine the common ancestor directory containing both paths
__block NSRange mySearchRange = NSMakeRange(1, [self length] - 1);
__block NSRange mySearchRange = NSMakeRange(1, self.length - 1);
NSMutableString *result = [NSMutableString string];


Expand All @@ -220,7 +219,7 @@ - (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath
}
else
{
__block NSRange dirSearchRange = NSMakeRange(1, [dirPath length] - 1);
__block NSRange dirSearchRange = NSMakeRange(1, dirPath.length - 1);

[self ks_enumeratePathComponentsInRange:mySearchRange options:0 usingBlock:^(NSString *myComponent, NSRange myRange, NSRange enclosingRange, BOOL *stopOuter) {

Expand Down Expand Up @@ -254,7 +253,7 @@ - (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath

if (range.length == 2) NSAssert([dirPath compare:@".." options:NSLiteralSearch range:range] != NSOrderedSame, @".. unsupported: %@", dirPath);

if ([result length]) [result appendString:@"/"];
if (result.length) [result appendString:@"/"];
[result appendString:@".."];
}];
}
Expand All @@ -268,15 +267,15 @@ - (NSString *)ks_pathRelativeToDirectory:(NSString *)dirPath
pathRelativeToCommonDir = [pathRelativeToCommonDir substringFromIndex:1];
}

if ([pathRelativeToCommonDir length])
if (pathRelativeToCommonDir.length)
{
if ([result length]) [result appendString:@"/"];
if (result.length) [result appendString:@"/"];
[result appendString:pathRelativeToCommonDir];
}


// Were the paths found to be equal?
if ([result length] == 0)
if (result.length == 0)
{
[result appendString:@"."];
[result appendString:[self substringWithRange:mySearchRange]]; // match original's oddities
Expand All @@ -295,9 +294,9 @@ - (NSString *)ks_standardizedPOSIXPath
{
NSString *result = self;

while ([result length] > 1 && [result hasSuffix:@"/"]) // Stops @"/" being altered
while (result.length > 1 && [result hasSuffix:@"/"]) // Stops @"/" being altered
{
result = [result substringToIndex:([result length] - 1)];
result = [result substringToIndex:(result.length - 1)];
}

return result;
Expand All @@ -320,7 +319,7 @@ - (BOOL)ks_isEqualToPOSIXPath:(NSString *)otherPath

@implementation KSIncrementedPath

- (id)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix;
- (instancetype)initWithBasePath:(NSString *)basePath suffix:(NSUInteger)suffix;
{
NSParameterAssert(suffix >= 2);

Expand All @@ -340,7 +339,7 @@ - (NSString *)ks_stringByIncrementingPath;

#pragma mark NSString Primitives

- (NSUInteger)length; { return [_storage length]; }
- (NSUInteger)length; { return _storage.length; }
- (unichar)characterAtIndex:(NSUInteger)index; { return [_storage characterAtIndex:index]; }

@end
Expand Down
10 changes: 5 additions & 5 deletions KSURLComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
@param url The URL whose components you want.
@param resolve Whether to resolve relative URLs before retrieving components
*/
- (id)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve __attribute((nonnull));
- (instancetype)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve __attribute((nonnull));

/**
Initializes and returns a newly created `KSURLComponents` with the components of a URL.
Expand All @@ -67,21 +67,21 @@
@param url The URL whose components you want.
@param resolve Whether to resolve relative URLs before retrieving components
*/
+ (id)componentsWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve __attribute((nonnull));
+ (instancetype)componentsWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve __attribute((nonnull));

/**
Initialize a `KSURLComponents` with a URL string.
If the URLString is malformed, `nil` is returned.
*/
- (id)initWithString:(NSString *)URLString;
- (instancetype)initWithString:(NSString *)URLString;

/**
Initializes and returns a newly created `KSURLComponents` with a URL string.
If the URLString is malformed, `nil` is returned.
*/
+ (id)componentsWithString:(NSString *)URLString;
+ (instancetype)componentsWithString:(NSString *)URLString;

/**
Returns a URL created from the `KSURLComponents`.
Expand All @@ -92,7 +92,7 @@
and has a path component, the path component must not start with `//`. If those
requirements are not met, `nil` is returned.
*/
- (NSURL *)URL;
@property (nonatomic, readonly, copy) NSURL *URL;

/**
Returns a URL created from the `KSURLComponents` relative to a base URL.
Expand Down
16 changes: 8 additions & 8 deletions KSURLComponents.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ @implementation KSURLComponents

#pragma mark Lifecycle

- (id)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve;
- (instancetype)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve;
{
if (!url) [NSException raise:NSInvalidArgumentException format:@"-[KSURLComponents %@] requires a non-nil URL", NSStringFromSelector(_cmd)];

if (resolve) url = [url absoluteURL];
if (resolve) url = url.absoluteURL;
CFStringRef urlString = CFURLGetString((CFURLRef)url);
BOOL fudgedParsing = NO;

Expand Down Expand Up @@ -178,12 +178,12 @@ - (id)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve;
return self;
}

+ (id)componentsWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve;
+ (instancetype)componentsWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve;
{
return [[[self alloc] initWithURL:url resolvingAgainstBaseURL:resolve] autorelease];
}

- (id)initWithString:(NSString *)URLString;
- (instancetype)initWithString:(NSString *)URLString;
{
NSURL *url = [[NSURL alloc] initWithString:URLString];
if (!url)
Expand All @@ -198,7 +198,7 @@ - (id)initWithString:(NSString *)URLString;
return self;
}

+ (id)componentsWithString:(NSString *)URLString;
+ (instancetype)componentsWithString:(NSString *)URLString;
{
return [[[self alloc] initWithString:URLString] autorelease];
}
Expand Down Expand Up @@ -236,7 +236,7 @@ - (NSURL *)URLRelativeToURL:(NSURL *)baseURL;

// If the KSURLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string.
if (hasAuthorityComponent &&
!(path.length == 0 || [path isAbsolutePath]))
!(path.length == 0 || path.absolutePath))
{
return nil;
}
Expand Down Expand Up @@ -507,7 +507,7 @@ - (BOOL)isEqual:(id)object;
if (myHost != otherHost && ![myHost isEqualToString:otherHost]) return NO;

NSNumber *myPort = self.port;
NSNumber *otherPort = [(KSURLComponents *)object port];
NSNumber *otherPort = ((KSURLComponents *)object).port;
if (myPort != otherPort && ![myPort isEqualToNumber:otherPort]) return NO;

NSString *myPath = self.percentEncodedPath;
Expand Down Expand Up @@ -553,7 +553,7 @@ - (id)copyWithZone:(NSZone *)zone;

- (NSString *)description;
{
return [[super description] stringByAppendingFormat:
return [super.description stringByAppendingFormat:
@" {scheme = %@, user = %@, password = %@, host = %@, port = %@, path = %@, query = %@, fragment = %@}",
self.scheme,
self.percentEncodedUser,
Expand Down
Loading

0 comments on commit 25dd73a

Please sign in to comment.