Browse Source

* fall back to UTF-8 for darwin in unixcp.GetSystemCodePage if no LC_*/LANG
environment variables are set
* if no LC_*/LANG environment variables are set, let cwstring fall back to
unixcp.GetSystemCodePage
o this is mainly to avoid problems on OS X, where under certain
circumstances these variables are not set for GUI programs and
as a result we set the system code page to ASCII

git-svn-id: trunk@29257 -

Jonas Maebe 10 years ago
parent
commit
63bd65942b
2 changed files with 32 additions and 10 deletions
  1. 31 9
      rtl/unix/cwstring.pp
  2. 1 1
      rtl/unix/unixcp.pp

+ 31 - 9
rtl/unix/cwstring.pp

@@ -993,6 +993,18 @@ begin
   ansi2pchar(temp,str,result);
 end;
 
+
+function envvarset(const varname: pchar): boolean;
+var
+  varval: pchar;
+begin
+  varval:=fpgetenv(varname);
+  result:=
+    assigned(varval) and
+    (varval[0]<>#0);
+end;
+
+
 function GetStandardCodePage(const stdcp: TStandardCodePageEnum): TSystemCodePage;
 var
   langinfo: pchar;
@@ -1004,15 +1016,25 @@ begin
       exit;
     end;
 {$endif}
-  langinfo:=nl_langinfo(CODESET);
-  { there's a bug in the Mac OS X 10.5 libc (based on FreeBSD's)
-    that causes it to return an empty string of UTF-8 locales
-    -> patch up (and in general, UTF-8 is a good default on
-    Unix platforms) }
-  if not assigned(langinfo) or
-     (langinfo^=#0) then
-    langinfo:='UTF-8';
-  Result := GetCodepageByName(ansistring(langinfo));
+  { if none of the relevant LC_* environment variables are set, fall back to
+    UTF-8 (this happens under some versions of OS X for GUI applications, which
+    otherwise get CP_ASCII) }
+  if envvarset('LC_ALL') or
+     envvarset('LC_CTYPE') or
+     envvarset('LANG') then
+    begin
+      langinfo:=nl_langinfo(CODESET);
+      { there's a bug in the Mac OS X 10.5 libc (based on FreeBSD's)
+        that causes it to return an empty string of UTF-8 locales
+        -> patch up (and in general, UTF-8 is a good default on
+        Unix platforms) }
+      if not assigned(langinfo) or
+         (langinfo^=#0) then
+        langinfo:='UTF-8';
+      Result:=GetCodepageByName(ansistring(langinfo));
+    end
+  else
+    Result:=unixcp.GetSystemCodepage;
 end;
 
 {$ifdef FPC_HAS_CPSTRING}

+ 1 - 1
rtl/unix/unixcp.pp

@@ -714,7 +714,7 @@ var
 begin
   // Get one of non-empty environment variables in the next order:
   // LC_ALL, LC_CTYPE, LANG. Default is UTF-8 or ASCII.
-{$ifdef linux}
+{$if defined(linux) or defined(darwin)}
   Result:=CP_UTF8;
 {$else}
   Result:=CP_ASCII;