Sane(r) way to get character-encoding of CLI?
Posted
by danyowdee
on Super User
See other posts from Super User
or by danyowdee
Published on 2010-06-18T13:15:31Z
Indexed on
2010/06/18
13:24 UTC
Read the original article
Hit count: 356
Hi all!
I was writing a CLI-Tool for Mac OS X (10.5+) that has to deal with command-line arguments which are very likely to contain non-ASCII characters.
For further processing, I convert these arguments using +[NSString stringWithCString:encoding:].
My problem is, that I couldn't find good information on how to determine the character-encoding used by the shell in which said cli-tool is running in.
What I came up with as a solution is the following:
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *ianaName = [[environment objectForKey:@"LANG"] pathExtension];
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(
CFStringConvertIANACharSetNameToEncoding( (CFStringRef)ianaName ) );
NSString *someArgument = [NSString stringWithCString:argv[someIndex] encoding:encoding];
I find that a little crude, however -- which makes me think that I missed out something obvious...but what?
Is there a saner/cleaner way of achieving essentially the same?
Thanks in advance
D
© Super User or respective owner