I also do what Chebmaster does. Define my own StdString type which is currently UnicodeString, and convert in abstract platform routines. In your case your own type would be ansistring due to the Allegro library.
I also do what Chebmaster does. Define my own StdString type which is currently UnicodeString, and convert in abstract platform routines. In your case your own type would be ansistring due to the Allegro library.
Existence is pain
Thanks for the advices.
Actually I defined two types for Allegro.pas yet: AL_STR which is ANSISTRING, and AL_STRptr which is PCHAR (or PANSICHAR depending the compiler). That solves part of the problem.
It is using Delphi's RTL where I have problems. For example, to draw the score on screen I may use this:
This works perfect in FPC but shows a warning in Delphi. Note that it actually renders the text (except in a few Allegro functions) but the warning is pretty annoying. I know I can avoid it using conversion functions as I've explained above but they aren't needed by FPC (actually they'll not work!).Code:al_draw_text (aFont, aColor, aXpos, aYpos, 0, Format ('SCORE: %d', [aScore]));
I didn't know about $X and $V arguments. Anyway I did some testing and I didn't find they helps.
The test I did was:
Compiled with {$H-}, and also changing the "Long strings by default" to false, but it still shows the warning.Code:procedure TForm1.Button1Click (Sender: TObject); VAR lText: ANSISTRING; begin INC (fNum); lText := 'Test #%d'; lText := Format (lText, [fNum]); Memo1.Lines.Add (lText) end;
I think I should add conditional compilation in the examples (they're only a few that conficts) or write different examples for FPC and Delphi for such cases.
No signature provided yet.
You can add your own functions which do conversion when Delphi is used, but just return the string as is when FPC is used, via conditional compilation. They do the required thing within them, and just use them consistently instead of Delphi specific functions. Define them in a shared unit. I do this for different platforms. I don't use Delphi, but I have seen other code which does something similar in order to support both Delphi and FPC.
Existence is pain
I think you have something there. Thanks. I think I'll do it that way.
No signature provided yet.
Using your example in modern Delphi versions will always show a warning regardless of what string compiler directives you use in your code. Why? That is because the Format function is always returning default Delphi string type which is WideString, And since you are then assigning it to AnsiString you get a warning.
So you either need to create your own version of Format function that will be returning AnsiString result or disable of using of LongString as default string type at Project Options->Building->Delphi Compiler->Compiling (check Syntax options section).
http://docwiki.embarcadero.com/RADSt...o/en/Compiling
Do note that this affects your entire project so if you have other code parts that are built to work with Unicode string they may stop working properly.
Thanks for your comments.
After some working, I've reduced a lot the warnings when compiling. I'm still doing testing and changes trying to reach zero warnings (if possible).
I've also added my own Format function as SilverWarrior suggested and I think I'll add functions to convert from/to numeric values too.
Once done I'll release a new beta version so you can test it. If you're curious you can see what I'm doing here.
Last edited by Ñuño Martínez; 21-02-2020 at 11:01 AM. Reason: Add link
No signature provided yet.
Sorry for the double post, but I have to say this: the solution was trivial
While testing I discovered that function StrPas (used internally to force some callings to overloaded functions in my abstraction layer unit) were in a different unit in modern Delphi (AnsisString instead of SysUtils), so I added it to be used only in Delphi. Since then warnings appeared again and I was like WTF Delphi is trolling me... But using the Find declaration command to see the actual declaration of Format I discovered AnsiString also defines it's own version using ANSISTRING instead of UNICODESTRING!
Note it doesn't avoid the need of the abstraction layer unit, but it simplifies a lot the implementation of this unit (and avoids to call an extra function internally in other units). For example:
This way there's no need of conditional compilation outside Allegro.pas.Code:USES {$IFDEF ISDELPHI2009ANDUP} { This unit implements sysutils using ANSISTRING instead of UNICODESTRING, which is the default in modern Delphi compilers. } AnsiStrings; {$ELSE} sysutils; {$ENDIF} ··· FUNCTION al_str_format (CONST Fmt: AL_STR; CONST Args : ARRAY OF CONST) : AL_STR; BEGIN Format (Fmt, Args) END;
So, now we know.
Last edited by Ñuño Martínez; 21-02-2020 at 11:58 AM.
No signature provided yet.
Wait Delphi has a special unit just for dealing with ANSI strings? I must shamefully admit that I didn't knew that
Well, you know now.
Anyway I think they should add an option to tell the compiler to define STRING as ANSISTRING, as FPC does with {$unicodestrings} or something. That would help a lot porting and maintaining code.
No signature provided yet.
Bookmarks