Form normalized paths from absolutes with ObjC

This commit is contained in:
Roza 2020-12-14 11:42:31 -05:00
parent 9b9f4658dd
commit 8231f52d6f

View file

@ -7,7 +7,8 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "filesystemImpl.h" #import "filesystemImpl.h"
#import "exception.h" #import "util/exception.h"
#import "util/debugwriter.h"
#define PATHTONS(str) [NSFileManager.defaultManager stringWithFileSystemRepresentation:str length:strlen(str)] #define PATHTONS(str) [NSFileManager.defaultManager stringWithFileSystemRepresentation:str length:strlen(str)]
@ -40,13 +41,12 @@ std::string filesystemImpl::getCurrentDirectory() {
} }
std::string filesystemImpl::normalizePath(const char *path, bool preferred, bool absolute) { std::string filesystemImpl::normalizePath(const char *path, bool preferred, bool absolute) {
NSString *nspath = PATHTONS(path); NSString *nspath = [NSURL fileURLWithPath: PATHTONS(path)].URLByStandardizingPath.path;
if (!nspath.isAbsolutePath && absolute) { NSString *pwd = [NSString stringWithFormat:@"%@/", NSFileManager.defaultManager.currentDirectoryPath];
nspath = [NSURL fileURLWithPath: nspath].URLByStandardizingPath.path; if (!absolute) {
} nspath = [nspath stringByReplacingOccurrencesOfString:pwd withString:@""];
else {
nspath = nspath.stringByStandardizingPath;
} }
Debug() << nspath.UTF8String;
return std::string(NSTOPATH(nspath)); return std::string(NSTOPATH(nspath));
} }