Hi all,
I didn't find anything out in the net so I'm asking in here (and please
correct me if the problem is already referred to by someone)...
I have problems concerning the FileDialog and filenames
containing special characters, such as the german Umlaute. When I try to
open such files the path and filename returned contain false characters
and some question marks when used as a Java String (eg. with
System.out.println).
Looking at the sources (I'm refering to SWT 3.4) the code reading the
Filenames from the OS is (like):
...
char [] buff= new char [length];
OS.CFStringGetCharacters (str, range, buff);
result = new String (buff);
...
According to Apple's documentation shipped with xCode, the function
CFStringGetCharacters will return UTF-8 Chars, with sould be o.k. for
Java, nevertheless an 'Ä' in a Filename get's an 'A?' in Java.
I'm temporarily using the following Function and replaced the above code
with a call to it to fix this, but I think I remember it did fork a view
weeks ago (using an older release ?!).
public static String CFStringToString(int str) {
String result = null;
int length = OS.CFStringGetLength (str);
CFRange range = new CFRange ();
range.length = length;
int[] usedBufLen = { 0 };
byte[] buffer = new byte[1];
if (OS.CFStringGetBytes(str, range,
OS.kCFStringEncodingMacRoman, (byte) 0, false, null, 0, usedBufLen) > 0) {
int maxLen = usedBufLen[0];
buffer = new byte[maxLen];
OS.CFStringGetBytes(str, range,
OS.kCFStringEncodingMacRoman, (byte) 0, false, buffer, maxLen, usedBufLen);
try {
result = new String (buffer, "MacRoman");
}
catch (Exception e) {
//None of interest, just mark an Error
result = null;
}
}
//The above didn't work? Try something else...
if (result == null) {
char [] buff= new char [length];
OS.CFStringGetCharacters (str, range, buff);
result = new String (buff);
}
return result;
}
Is there a bug in SWT, so that FileDialog cannot work in some
circumstances, or am I doing something wrong (such as that I would have
to tell the Java VM to use the right encoding, or converting the
returned strings first)?
Thanks,
Alexander Hofmann.