Well, it looks like they aren't there!
As Yuji pointed out, the main encoding of file names is UTF-8, no matter what. Therefore, it was necessary to process two scenarios :
- The arguments the user enters are character for character.
- Arguments that are terminated by tabulation or the output of commands of type
ls , since they do not convert any characters.
The second case is simply covered by the assumption of UTF-8.
The first case, however, is problematic:
- On Mac OS 10.6, $ LANG contains the IANA name of the encoding type
de_DE.IANA_NAME . - Prior to Snow Leopard, this does not apply to codes other than UTF-8!
I did not test every encoding I could think of, but none of the European ones were included. Instead, $ LANG was a language ( de_DE in my case)!
Since the results of calling +[NSString stringWithCString:encoding:] with the wrong encoding are undefined , you cannot safely assume that it will return nil in this case * (if, for example, it is ASCII-only, it can work fine!).
What adds to the general mess is that $LANG not guaranteed to be around. In any case: a checkbox in the settings of Terminal.app, which allows the user not to set $LANG to everything (not to mention X11.app, which does not seem to handle any input that does not contain ASCII ...).
So what's left:
- Check for the availability of
$LANG . If it is not installed, select: 4! - Check if
$LANG contains encoding information. If it is not, Goto: 4! - Check if there is UTF-8 encoding. If it's Goto: 6, else ...
- If
argc greater than 2 and [[NSString stringWithCString: argv[0] encoding: NSUTF8StringEncoding] isEqualToString: yourForceUTFArgumentFlag] , print that you are now forcing UTF-8 and Goto 6. If not: - Suppose you don't know anything, provide a warning that your user should set the terminal encoding to UTF-8 and might consider passing
yourForceUTFArgumentFlag as the first argument and exit () . - Assume UTF-8 and do what you need ...
Sounds bad? This is because it is, but I cannot think of a way for saner to do this.
One more note: If you use UTF-8 as an encoding, stringWithCString: encoding: returns nil whenever it encounters non-ASCII characters in a C-String that is not encoded in UTF-8.)
source share