Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iconv functions #109

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions units/sdl2.pas
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ interface

implementation

(*
* We need an strlen() implementation for some operations on C-strings.
* FPC ships one in the Strings unit; Delphi has one in the AnsiStrings unit.
* Since FPC defines "DELPHI" when building in Delphi-compatibility mode,
* check if "FPC" is defined to determine which compiler is used.
*)
uses
{$IFDEF FPC}
Strings
{$ELSE}
AnsiStrings
{$ENDIF}
;

// Macros from "sdl_version.h"
procedure SDL_VERSION(out x: TSDL_Version);
begin
Expand Down Expand Up @@ -427,6 +441,24 @@ function SDL_SHAPEMODEALPHA(mode: TWindowShapeMode): Boolean;
Result := (mode = ShapeModeDefault) or (mode = ShapeModeBinarizeAlpha) or (mode = ShapeModeReverseBinarizeAlpha);
end;

// from "sdl_stdinc.h"

// Note: We're using FPC's Strings.strlen() here, not SDL_strlen().
function SDL_iconv_utf8_locale(Const str: PAnsiChar): PAnsiChar; cdecl;
begin
Result := SDL_iconv_string('', 'UTF-8', str, strlen(str)+1)
end;

function SDL_iconv_utf8_ucs2(Const str: PAnsiChar): pcUint16; cdecl;
begin
Result := pcUint16(SDL_iconv_string('UCS-2-INTERNAL', 'UTF-8', str, strlen(str)+1))
end;

function SDL_iconv_utf8_ucs4(Const str: PAnsiChar): pcUint32; cdecl;
begin
Result := pcUint32(SDL_iconv_string('UCS-4-INTERNAL', 'UTF-8', str, strlen(str)+1))
end;

//from "sdl_video.h"

function SDL_WINDOWPOS_UNDEFINED_DISPLAY(X: Variant): Variant;
Expand Down
34 changes: 34 additions & 0 deletions units/sdlstdinc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,37 @@ function SDL_realloc(mem: Pointer; size: csize_t): Pointer; cdecl;
*)
procedure SDL_free(mem: Pointer); cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_free' {$ENDIF} {$ENDIF};

(**
* This function converts a string between encodings in one pass, returning a
* string that must be freed with SDL_free(), or NIL on error.
*
* \since This function is available since SDL 2.0.0.
*)
function SDL_iconv_string(Const tocode, fromcode, inbuf: PAnsiChar; inbytesleft: csize_t): PAnsiChar; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_iconv_string' {$ENDIF} {$ENDIF};

// These are macros in the original C headers, we will reimplement them as simple Pascal functions.
function SDL_iconv_utf8_locale(Const str: PAnsiChar): PAnsiChar; cdecl;
function SDL_iconv_utf8_ucs2(Const str: PAnsiChar): pcUint16; cdecl;
function SDL_iconv_utf8_ucs4(Const str: PAnsiChar): pcUint32; cdecl;

(* The SDL implementation of iconv() returns these error codes *)
const
SDL_ICONV_ERROR = csize_t(-1);
SDL_ICONV_E2BIG = csize_t(-2);
SDL_ICONV_EILSEQ = csize_t(-3);
SDL_ICONV_EINVAL = csize_t(-4);

type
TSDL_iconv = record end;
PSDL_iconv = ^TSDL_iconv;

function SDL_iconv_open(Const tocode, fromcode: PAnsiChar): PSDL_iconv; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_iconv_open' {$ENDIF} {$ENDIF};

function SDL_iconv_close(cd: PSDL_iconv): cint; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_iconv_close' {$ENDIF} {$ENDIF};

function SDL_iconv(cd: PSDL_iconv; Const inbuf: PPAnsiChar; inbytesleft: pcsize_t; outbuf: PPAnsiChar; outbytesleft: pcsize_t): csize_t; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_iconv' {$ENDIF} {$ENDIF};