Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: String Encodings and POSIX Commands



Over the past 3 months, I have designed an app for Darwin/Mac OS X using
the POSIX API.  I slapped a cocoa interface on it and it's been ported
to most Unixes.  Recently I ran into the problem of STRING ENCODING
CONVERSION and have a couple of questions related:

a.  My understanding of Darwin is that the File System uses Unicode
16byte strings, correct?

The BSD-level interface to the file system uses canonically decomposed UTF-8.


b.  It is my understanding then that the commands open() and chdir() etc
are all expecting (or at least can handle) Unicode-16 strings under
Darwin?

See above.

c.  I am receiving string data over a network encoded in Windows Latin 1
(ANSI codepage 1252) and I need to be sure that it is converted to the
local File System encoding.

- On OSX/Darwin, I have been using CFString functions to read the server
strings as kCFStringEncodingWindowsLatin1, and write them to local as
kCFStringEncodingUTF8.  Is this OK?  (vice versa going from Local to
server).
(when I tried using kCFStringEncodingUnicode it balked at my straight
ascii strings I sent the conversion function, and when I tried using
kCFStringEncodingASCII things didn't' convert right.)  See CFString.h
for definitions.

If you are working in Cocoa, the appropriate thing to do is to create an NSString and use -[NSString fileSystemRepresentation], or -[NSString getFileSystemRepresentation:maxLength:], or -[NSFileManager fileSystemRepresentationWithPath:] to obtain something you can pass to BSD APIs like open(). (These APIs all do the same thing, just with slightly different interfaces.) If you have a CFString, you can use it as an NSString with just a cast from CFStringRef to NSString *, and vice versa; the two are interchangeable. For the reverse conversion, use -[NSFileManager stringWithFileSystemRepresentation:length:].


For those who may be working purely in CoreFoundation, my suggestion at present for this conversion would be (a) create a CFString using kCFStringEncodingWindowsLatin1, as you have been doing; (b) create a CFURL using CFURLCreateWithFileSystemPath(); (c) use CFURLGetFileSystemRepresentation() to get something you can pass to open(). Then release the intermediate objects. For the reverse conversion, use CFURLCreateFromFileSystemRepresentation() and CFURLCopyFileSystemPath().

Douglas Davidson
_______________________________________________
unix-porting mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/unix-porting
Do not post admin requests to the list. They will be ignored.





Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.