Filepath lastPathComponent from HFS or POSIX path

96 Views Asked by At

I am on OSX, not iOS, Objective-C

I receive external input like this and i need to get the file.

Case A (posix path): "path/to/afile.extension"
Case B (HFS path): "path:to:afile.extension"

In Case A i can get the file with

[path lastPathComponent];

In Case B i can get it via

[[path componentsSeparatedByString:@":"] lastObject];

Unfortunately i don't know if the input is of type A or B. What would be the best way to identify if the delivered path is a posix path or a HFS path?

1

There are 1 best solutions below

2
On BEST ANSWER

What would be the best way to identify if the delivered path is a posix path or a HFS path?

Off-the-top-of-my-head: I don't think you can do this trivially based on the string as the POSIX path separator, '/', is a valid in a HFS file name, and vice-versa. E.g. the POSIX path fragment:

... Desktop/a:colon.txt

and the HFS path fragment:

... Desktop:a/colon.txt

refer to the same file.

What you could do instead is check if the path exists using file manager (NSFileManager) calls - these take HFS paths - and the access(2) system call - which takes a POSIX path. If only one of these works you know the type of path you have, if both work you've got some unusually named disks and files and the path is ambiguous! (And if neither work the path is invalid interpreted either way.)

You can also do checks by creating a file NSURL from the path and if successful then calling NSURL methods to check for existence.

Update

Your comment states that the files do not exist so checking for existence will obviously fail. So think about examining the path to work it out, only producing an error for ones you cannot figure out. E.g.:

  • If a path contains only '/' or ':' delimiters you can determine POSIX or HFS
  • A full HFS path always starts with the volume name followed by a colon. Determine the mounted volume names and check those against the path you have, if there is a match for one you have a HFS path
  • Etc.

For checking for characters in a string, volume names, etc. start with the NSString, NSFileManager and NSURL documentation. Once you've built up your series of tests if you have any problems etc. ask a new question describing your tests, showing your code, etc. and someone will undoubtedly help you.

HTH