function UTF8ToSys(const s: string): string; begin {$IFDEF UTF8_RTL} Result:=s; {$ELSE} if NeedRTLAnsi and (not IsASCII(s)) then Result:=UTF8ToAnsi(s) else Result:=s; {$ENDIF} end;
{$ifdef WinCe} function UTF8ToWinCP(const s: string): string; inline; begin Result := Utf8ToSys(s); end; {$else} function UTF8ToWinCP(const s: string): string; // result has codepage CP_ACP var src: UnicodeString; len: LongInt; begin Result:=s; if IsASCII(Result) then begin {$ifdef FPC_HAS_CPSTRING} // prevent codepage conversion magic SetCodePage(RawByteString(Result), CP_ACP, False); {$endif} exit; end; src:=UTF8Decode(s); if src='' then exit; len:=WideCharToMultiByte(CP_ACP,0,PUnicodeChar(src),length(src),nil,0,nil,nil); SetLength(Result,len); if len>0 then begin WideCharToMultiByte(CP_ACP,0,PUnicodeChar(src),length(src),@Result[1],length(Result),nil,nil); {$ifdef FPC_HAS_CPSTRING} // prevent codepage conversion magic SetCodePage(RawByteString(Result), CP_ACP, False); {$endif} end; end; {$endif not wince}
{$ifdef WinCe} function UTF8ToConsole(const s: string): string; // converts UTF8 to console string (used by Write, WriteLn) begin Result := UTF8ToSys(s); end; {$else} function UTF8ToConsole(const s: string): string; // converts UTF8 to console string (used by Write, WriteLn) var Dst: PChar; begin {$ifndef NO_CP_RTL} Result := UTF8ToWinCP(s); {$else NO_CP_RTL} Result := UTF8ToSys(s); // Kept for compatibility {$endif NO_CP_RTL} Dst := AllocMem((Length(Result) + 1) * SizeOf(Char)); if CharToOEM(PChar(Result), Dst) then Result := StrPas(Dst); FreeMem(Dst); {$ifndef NO_CP_RTL} SetCodePage(RawByteString(Result), CP_OEMCP, False); {$endif NO_CP_RTL} end; {$endif not WinCE}