Skip to content

Commit 88a8066

Browse files
authoredMar 11, 2023
Merge pull request #109 from suve/add-iconv-functions
- Add iconv functions - Adds dependency from Strings/AnsiStrings unit (should be adressed later, see issue #118)
2 parents f6e7ace + 4ed1838 commit 88a8066

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
 

‎units/sdl2.pas

+32
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ interface
204204

205205
implementation
206206

207+
(*
208+
* We need an strlen() implementation for some operations on C-strings.
209+
* FPC ships one in the Strings unit; Delphi has one in the AnsiStrings unit.
210+
* Since FPC defines "DELPHI" when building in Delphi-compatibility mode,
211+
* check if "FPC" is defined to determine which compiler is used.
212+
*)
213+
uses
214+
{$IFDEF FPC}
215+
Strings
216+
{$ELSE}
217+
AnsiStrings
218+
{$ENDIF}
219+
;
220+
207221
// Macros from "sdl_version.h"
208222
procedure SDL_VERSION(out x: TSDL_Version);
209223
begin
@@ -428,6 +442,24 @@ function SDL_SHAPEMODEALPHA(mode: TWindowShapeMode): Boolean;
428442
Result := (mode = ShapeModeDefault) or (mode = ShapeModeBinarizeAlpha) or (mode = ShapeModeReverseBinarizeAlpha);
429443
end;
430444

445+
// from "sdl_stdinc.h"
446+
447+
// Note: We're using FPC's Strings.strlen() here, not SDL_strlen().
448+
function SDL_iconv_utf8_locale(Const str: PAnsiChar): PAnsiChar; cdecl;
449+
begin
450+
Result := SDL_iconv_string('', 'UTF-8', str, strlen(str)+1)
451+
end;
452+
453+
function SDL_iconv_utf8_ucs2(Const str: PAnsiChar): pcUint16; cdecl;
454+
begin
455+
Result := pcUint16(SDL_iconv_string('UCS-2-INTERNAL', 'UTF-8', str, strlen(str)+1))
456+
end;
457+
458+
function SDL_iconv_utf8_ucs4(Const str: PAnsiChar): pcUint32; cdecl;
459+
begin
460+
Result := pcUint32(SDL_iconv_string('UCS-4-INTERNAL', 'UTF-8', str, strlen(str)+1))
461+
end;
462+
431463
//from "sdl_video.h"
432464

433465
function SDL_WINDOWPOS_UNDEFINED_DISPLAY(X: Variant): Variant;

‎units/sdlstdinc.inc

+34
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,37 @@ function SDL_realloc(mem: Pointer; size: csize_t): Pointer; cdecl;
137137
*)
138138
procedure SDL_free(mem: Pointer); cdecl;
139139
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_free' {$ENDIF} {$ENDIF};
140+
141+
(**
142+
* This function converts a string between encodings in one pass, returning a
143+
* string that must be freed with SDL_free(), or NIL on error.
144+
*
145+
* \since This function is available since SDL 2.0.0.
146+
*)
147+
function SDL_iconv_string(Const tocode, fromcode, inbuf: PAnsiChar; inbytesleft: csize_t): PAnsiChar; cdecl;
148+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_iconv_string' {$ENDIF} {$ENDIF};
149+
150+
// These are macros in the original C headers, we will reimplement them as simple Pascal functions.
151+
function SDL_iconv_utf8_locale(Const str: PAnsiChar): PAnsiChar; cdecl;
152+
function SDL_iconv_utf8_ucs2(Const str: PAnsiChar): pcUint16; cdecl;
153+
function SDL_iconv_utf8_ucs4(Const str: PAnsiChar): pcUint32; cdecl;
154+
155+
(* The SDL implementation of iconv() returns these error codes *)
156+
const
157+
SDL_ICONV_ERROR = csize_t(-1);
158+
SDL_ICONV_E2BIG = csize_t(-2);
159+
SDL_ICONV_EILSEQ = csize_t(-3);
160+
SDL_ICONV_EINVAL = csize_t(-4);
161+
162+
type
163+
TSDL_iconv = record end;
164+
PSDL_iconv = ^TSDL_iconv;
165+
166+
function SDL_iconv_open(Const tocode, fromcode: PAnsiChar): PSDL_iconv; cdecl;
167+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_iconv_open' {$ENDIF} {$ENDIF};
168+
169+
function SDL_iconv_close(cd: PSDL_iconv): cint; cdecl;
170+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_iconv_close' {$ENDIF} {$ENDIF};
171+
172+
function SDL_iconv(cd: PSDL_iconv; Const inbuf: PPAnsiChar; inbytesleft: pcsize_t; outbuf: PPAnsiChar; outbytesleft: pcsize_t): csize_t; cdecl;
173+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_iconv' {$ENDIF} {$ENDIF};

0 commit comments

Comments
 (0)
Please sign in to comment.