diff --git a/lcl/fileutil.pas b/lcl/fileutil.pas index f01bcfff35..44973b8b5c 100644 --- a/lcl/fileutil.pas +++ b/lcl/fileutil.pas @@ -187,6 +187,7 @@ function NeedRTLAnsi: boolean;// true if system encoding is not UTF-8 procedure SetNeedRTLAnsi(NewValue: boolean); function UTF8ToSys(const s: string): string;// as UTF8ToAnsi but more independent of widestringmanager function SysToUTF8(const s: string): string;// as AnsiToUTF8 but more independent of widestringmanager +function ConsoleToUTF8(const s: string): string;// converts OEM encoded string to UTF8 (used with some Windows specific functions) function UTF8ToConsole(const s: string): string;// converts UTF8 string to console encoding (used by Write, WriteLn) // file operations diff --git a/lcl/include/fileutil.inc b/lcl/include/fileutil.inc index d16307fea9..859e1a8d56 100644 --- a/lcl/include/fileutil.inc +++ b/lcl/include/fileutil.inc @@ -82,6 +82,23 @@ begin Result := s; end; +function ConsoleToUTF8(const s: string): string;// converts UTF8 string to console encoding (used by Write, WriteLn) +{$ifdef MSWindows} +var + Dst: PChar; +{$endif} +begin + {$ifdef MSWindows} + Dst := AllocMem((Length(s) + 1) * SizeOf(Char)); + if OemToChar(PChar(s), Dst) then + Result := StrPas(Dst) + else + Result := s; + FreeMem(Dst); + {$endif} + Result := SysToUTF8(Result); +end; + function UTF8ToConsole(const s: string): string; {$ifdef MSWindows} var @@ -244,12 +261,18 @@ end; function GetEnvironmentStringUTF8(Index: Integer): String; begin - Result:=SysToUTF8(SysUtils.GetEnvironmentString(Index)); + // on Windows SysUtils.GetEnvironmentString returns OEM encoded string + // so ConsoleToUTF8 function should be used! + // RTL issue: http://bugs.freepascal.org/view.php?id=15233 + Result:=ConsoleToUTF8(SysUtils.GetEnvironmentString(Index)); end; function GetEnvironmentVariableUTF8(const EnvVar: String): String; begin - Result:=SysToUTF8(SysUtils.GetEnvironmentVariable(UTF8ToSys(EnvVar))); + // on Windows SysUtils.GetEnvironmentString returns OEM encoded string + // so ConsoleToUTF8 function should be used! + // RTL issue: http://bugs.freepascal.org/view.php?id=15233 + Result:=ConsoleToUTF8(SysUtils.GetEnvironmentVariable(UTF8ToSys(EnvVar))); end; function GetAppConfigDirUTF8(Global: Boolean): string;