Results 1 to 10 of 16

Thread: UNICODESTRING vs ANSISTRING

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Question UNICODESTRING vs ANSISTRING

    This is more asking for opinions than an actual question.

    Working with Allegro.pas I've found a problem when compiling with Delphi: default STRING is UNICODESTRING instead of ANSISTRING. Since Allegro is written in C it expects ANSISTRING, so when compiling it returns a bunch of warnings because "implicit cast with potential data loss", where sometimes "potential" is "actual" and things don't work as expected.

    I know I can deal with it using UTF8Encode and UTF8Decode/UTF8toString to convert strings, but that means an overhead that will make examples more complex and executables slower. Free Pascal STRING default is ANSISTRING so it doesn't has such problem.

    A workaround would be to OVERLOAD all (or most) Allegro.pas FUNCTIONs and PROCEDUREs that deal with strings. For example, I've test next and it works:

    Code:
    UNIT al5nativedlg;
    
    INTERFACE
    
    ...
    
      FUNCTION al_show_native_message_box (
        display: ALLEGRO_DISPLAYptr;
        CONST title, heading, str, buttons: AL_STR;
        flags: AL_INT
      ): AL_INT; INLINE; OVERLOAD;
      FUNCTION al_show_native_message_box (
        display: ALLEGRO_DISPLAYptr;
        CONST title, heading, str, buttons: UNICODESTRING;
        flags: AL_INT
      ): AL_INT; INLINE; OVERLOAD;
    
    ...
    
      FUNCTION _al_show_native_message_box (
        display: ALLEGRO_DISPLAYptr; CONST title, heading, str, buttons: AL_STR;
        flags: AL_INT
      ): AL_INT; CDECL;
      EXTERNAL ALLEGRO_NATIVE_DLG_LIB_NAME NAME 'al_show_native_message_box';
    
    IMPLEMENTATION
    
      FUNCTION al_show_native_message_box (
        display: ALLEGRO_DISPLAYptr; CONST title, heading, str, buttons: AL_STR;
        flags: AL_INT
      ): AL_INT;
      VAR
        ButtonsPtr: AL_STRptr;
      BEGIN
        IF buttons <> '' THEN
          ButtonsPtr := AL_STRptr (buttons)
        ELSE
          ButtonsPtr := NIL;
        al_show_native_message_box := _al_show_native_message_box (
           display, AL_STRptr (Title), AL_STRptr (Heading), AL_STRptr (Str), ButtonsPtr, flags
        )
      END;
    
      FUNCTION al_show_native_message_box (
        display: ALLEGRO_DISPLAYptr; CONST title, heading, str, buttons: UNICODESTRING;
        flags: AL_INT
      ): AL_INT;
      BEGIN
        RESULT := al_show_native_message_box (
          display,
          UTF8Encode (title),
          UTF8Encode (heading),
          UTF8Encode (str),
          UTF8Encode (buttons),
          flags
        )
      END;
    
    END.
    As I've said, it works even with Free Pascal, but the problem is that there are a lot of FUNCTIONs and PROCEDUREs that should be overloaded. Also Delphi will generate slower executables.

    The other option is to add a bunch of compilation directives (as {$IFDEF DCC}...{$ENDIF}) to the examples where strings are used, but that means that some examples will not be so clean for beginners.

    Also I can keep the examples as if there are no differences and add a warning to the Delphi documentation about this issue and how to fix it, but again beginners (and people don't read documentation).

    So what do you think is the best option? Do you have another solution?
    Last edited by Ñuño Martínez; 12-02-2020 at 07:59 PM.
    No signature provided yet.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •