views:

161

answers:

1

I am using libxml2 in my iPhone app. I have an NSString that holds the pathname to an XML file. The pathname may include non-ASCII characters. I want to get a C string representation of the NSString for to pass to xmlReadFile(). It appears that cStringUsingEncoding gives me the representation I seek. I am not clear on which encoding to use.

I wonder if there is a "default" encoding in iPhone OS that I can use here and ensure that I can roundtrip non-ASCII pathnames.

+1  A: 

Use NSString's fileSystemRepresentation. If the string contains characters that are not representable in the file system's encoding then this method will raise an exception.

To convert back, use NSFileManager's stringWithFileSystemRepresentation:length:

dreamlax
I notice that the documentation you link to says, "This method operates by replacing the abstract path and extension separator characters (‘/’ and ‘.’ respectively) with their equivalents for the operating system." I notice that it doesn't mention non-ASCII characters.
iter
@iter: That would probably be because file systems do not usually define special meanings for non-ASCII characters, so they don't get any special treatment in that regard. What did you expect it to say about them?
Chuck
On my Mac, I just tried it using an NSString containing `テストファイル.txt`, and using the result of `fileSystemRepresentation` in a call to `fopen` worked a treat. Why are you skeptical about this method?
dreamlax
I'm not skeptical so much as trying to learn iPhone OS conventions for locale handling. On other operating systems, my worry is that that my program uses one convention for encoding characters to bytes (say UTF8), while another program uses another convention (say Shift-JIS) and what I write looks like gibberish in the other program. Most OSs I am familiar with have "system" encoding in an environment variable or equivalent so all programs can use the same one. I guess it isn't as relevant on the phone, since only my program can access my files and I just need to be consistent with myself.
iter