views:

472

answers:

2

Hey everyone,

Is there a system library for escaping a file system path for iPhone development? I have to read and write files where the file name is based on 3rd party values so I want to make sure the paths I'm actually writing to are nice and safe.

I would have thought there was an NS library that would do this for me, since this is the "simple" type of function every developer probably shouldn't be rewriting, but I can't find it if there is one.

Thanks!

Edit: Thanks for the reply, stringByStandardizingPath: looks good for getting a real file path for a string, but what I'm looking for would do something similar to the following:

in:  @"~/Foo/Bar"
out: @"__Foo_Bar" or @"FooBar" etc.

Basically it would strip any unsafe characters out of a file path component.

After continued research it looks like almost any unicode character besides '/' is allowed in a file path. However, file paths are usually fragile and can be manipulated by an escape sequence, etc. so I'm hoping the big brains at Apple have an API to strip a file path component of unsafe values.

+1  A: 

You can try NSString's - (NSString *)stringByStandardizingPath

pgb
A: 

I would say go with a whitelist, maybe [a-zA-Z0-9_. ], or maybe something a little more complex, and simply encode any others into hex, similar to how url's encode spaces into '%20'

cobbal