如何将经典的HFS路径转换为POSIX路径 [英] How to convert a classic HFS path into a POSIX path

查看:106
本文介绍了如何将经典的HFS路径转换为POSIX路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取仍使用HFS样式路径的旧文件,例如VolumeName:Folder:File.

I am reading old files that still use HFS style paths, such as VolumeName:Folder:File.

我需要将它们转换为POSIX路径.

I need to convert them into POSIX paths.

我不喜欢执行字符串替换,因为它有点棘手,也不想为此任务调用AppleScript或Shell操作.

I do not like to do string replacement as it is a bit tricky, nor do I want to invoke AppleScript or Shell operations for this task.

是否有框架功能来完成此任务?弃用不是问题.

Is there a framework function to accomplish this? Deprecation is not an issue.

顺便说一句,这是反操作的解决方案.

BTW, here's a solution for the inverse operation.

推荐答案

在Obj-C和Swift中作为NSString / String的类别/扩展名的解决方案.不能使用kCFURLHFSPathStyle样式的方式与链接问题中的方式相同.

A solution in Obj-C and Swift as category / extension of NSString / String. The unavailable kCFURLHFSPathStyle style is circumvented in the same way as in the linked question.

Objective-C

@implementation NSString (POSIX_HFS)

    - (NSString *)POSIXPathFromHFSPath
    {
        NSString *posixPath = nil;
        CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)self, 1, [self hasSuffix:@":"]); // kCFURLHFSPathStyle
        if (fileURL)    {
            posixPath = [(__bridge NSURL*)fileURL path];
            CFRelease(fileURL);
        }

        return posixPath;
    }

@end

快速

extension String {

    func posixPathFromHFSPath() -> String?
    {
        guard let fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
                                                          self as CFString?,
                                                          CFURLPathStyle(rawValue:1)!,
                                                          self.hasSuffix(":")) else { return nil }
        return (fileURL as URL).path
    }
}

这篇关于如何将经典的HFS路径转换为POSIX路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆