Merge pull request #201 from Fokka-Engineering/new-nsurl-methods

New NSURL initializers (macOS 10.11 initFileURLWithPath:relativeToURL:) and cleanup
This commit is contained in:
rfm 2021-08-18 13:54:36 +01:00 committed by GitHub
commit 2adc5c5959
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 48 deletions

View file

@ -97,6 +97,17 @@ GS_EXPORT_CLASS
#if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST)
+ (instancetype) fileURLWithPath: (NSString*)aPath isDirectory: (BOOL)isDir;
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_11, GS_API_LATEST)
+ (instancetype) fileURLWithPath: (NSString *)aPath
isDirectory: (BOOL)isDir
relativeToURL: (NSURL *)baseURL;
/** Create and return a file URL with the supplied path, relative to a base URL.
*/
+ (instancetype) fileURLWithPath:(NSString *)aPath relativeToURL:(NSURL *)baseURL;
#endif
/**
* Create and return a URL with the supplied string, which should
* be a string (containing percent escape codes where necessary)
@ -145,6 +156,33 @@ GS_EXPORT_CLASS
isDirectory: (BOOL)isDir;
#endif
#if OS_API_VERSION(MAC_OS_X_VERSION_10_11, GS_API_LATEST)
/**
* Initialise as a file URL with the specified path (which must
* be a valid path on the local filesystem) relative to the base URL.<br />
* Raises NSInvalidArgumentException if aPath is nil.<br />
* Converts relative paths to absolute ones.<br />
* Appends a trailing slash to the path when necessary if it
* specifies a directory.<br />
* Calls -initWithScheme:host:path:
*/
- (instancetype) initFileURLWithPath: (NSString *)aPath
relativeToURL: (NSURL *)baseURL;
/**
* Initialise as a file URL with the specified path (which must
* be a valid path on the local filesystem) relative to the base URL.<br />
* Raises NSInvalidArgumentException if aPath is nil.<br />
* Converts relative paths to absolute ones.<br />
* Appends a trailing slash to the path when necessary if it
* specifies a directory.<br />
* Calls -initWithScheme:host:path:
*/
- (instancetype) initFileURLWithPath: (NSString *)aPath
isDirectory: (BOOL)isDir
relativeToURL: (NSURL *)baseURL;
#endif
/**
* Initialise by building a URL string from the supplied parameters
* and calling -initWithString:relativeToURL:<br />

View file

@ -587,6 +587,21 @@ static NSUInteger urlAlign;
isDirectory: isDir]);
}
+ (id) fileURLWithPath: (NSString *)aPath
isDirectory: (BOOL)isDir
relativeToURL: (NSURL *)baseURL
{
return AUTORELEASE([[NSURL alloc] initFileURLWithPath: aPath
isDirectory: isDir
relativeToURL: baseURL]);
}
+ (id)fileURLWithPath: (NSString *)aPath relativeToURL: (NSURL *)baseURL
{
return AUTORELEASE([[NSURL alloc] initFileURLWithPath: aPath
relativeToURL: baseURL]);
}
+ (id) fileURLWithPathComponents: (NSArray*)components
{
return [self fileURLWithPath: [NSString pathWithComponents: components]];
@ -624,71 +639,63 @@ static NSUInteger urlAlign;
return nil;
}
- (id) initFileURLWithPath: (NSString*)aPath
- (id) initFileURLWithPath: (NSString *)aPath
{
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL flag = NO;
if (nil == aPath)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@ %@] nil string parameter",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
}
if ([aPath isAbsolutePath] == NO)
{
aPath = [[mgr currentDirectoryPath]
stringByAppendingPathComponent: aPath];
}
if ([mgr fileExistsAtPath: aPath isDirectory: &flag] == YES)
{
if ([aPath isAbsolutePath] == NO)
{
aPath = [aPath stringByStandardizingPath];
}
if (flag == YES && [aPath hasSuffix: @"/"] == NO)
{
aPath = [aPath stringByAppendingString: @"/"];
}
}
self = [self initWithScheme: NSURLFileScheme
host: @""
path: aPath];
return self;
/* isDirectory flag will be overwritten if a directory exists. */
return [self initFileURLWithPath: aPath isDirectory: NO relativeToURL: nil];
}
- (id) initFileURLWithPath: (NSString*)aPath isDirectory: (BOOL)isDir
- (id) initFileURLWithPath: (NSString *)aPath isDirectory: (BOOL)isDir
{
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL flag = NO;
return [self initFileURLWithPath: aPath isDirectory: isDir relativeToURL: nil];
}
- (id) initFileURLWithPath: (NSString *)aPath relativeToURL: (NSURL *)baseURL
{
/* isDirectory flag will be overwritten if a directory exists. */
return [self initFileURLWithPath: aPath isDirectory: NO relativeToURL: baseURL];
}
- (id) initFileURLWithPath: (NSString *)aPath
isDirectory: (BOOL)isDir
relativeToURL: (NSURL *)baseURL
{
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL flag = NO;
if (nil == aPath)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@ %@] nil string parameter",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
[NSException
raise:NSInvalidArgumentException
format:@"[%@ %@] nil string parameter", NSStringFromClass([self class]),
NSStringFromSelector(_cmd)];
}
if ([aPath isAbsolutePath] == NO)
{
aPath = [[mgr currentDirectoryPath]
stringByAppendingPathComponent: aPath];
if (baseURL)
{
/* Append aPath to baseURL */
aPath = [[baseURL relativePath] stringByAppendingPathComponent:aPath];
}
else
{
aPath =
[[mgr currentDirectoryPath] stringByAppendingPathComponent:aPath];
}
}
if ([mgr fileExistsAtPath: aPath isDirectory: &flag] == YES)
if ([mgr fileExistsAtPath:aPath isDirectory:&flag] == YES)
{
if ([aPath isAbsolutePath] == NO)
{
aPath = [aPath stringByStandardizingPath];
}
{
aPath = [aPath stringByStandardizingPath];
}
isDir = flag;
}
if (isDir == YES && [aPath hasSuffix: @"/"] == NO)
if (isDir == YES && [aPath hasSuffix:@"/"] == NO)
{
aPath = [aPath stringByAppendingString: @"/"];
aPath = [aPath stringByAppendingString:@"/"];
}
self = [self initWithScheme: NSURLFileScheme
host: @""
path: aPath];
return self;
return [self initWithScheme: NSURLFileScheme host: @"" path: aPath];
}
- (id) initWithScheme: (NSString*)aScheme