Recent

Author Topic: How can I pass array of widechar to a standard StringList  (Read 1890 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
How can I pass array of widechar to a standard StringList
« on: January 22, 2019, 06:17:38 pm »
Hi

For many reasons I have to use several arrays of widechar for a particular project. I've got to the point where I want to save the arrays of widechar to a stringlist, but having created a standard stringlist and trying to use sl.add, I get page exception errors.

How do I add what is in essence a widestring (an array of widechars) to a standard stringlist?

e.g.
Code: Pascal  [Select][+][-]
  1. function 123
  2. const
  3.   BufLen = 256
  4. var
  5.   sl : TStringList;
  6.   SomeLongString : array[0..BufLen-1] of widechar;
  7.  
  8. begin
  9. sl : TStringList.Create;
  10. SomeLongString := AnAPICall(A, B, C);  // a call that returns a null terminated string
  11. ...
  12. sl.Add(SomeLongString); // This generates page protection fault
  13. end;
  14.  

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How can I pass array of widechar to a standard StringList
« Reply #1 on: January 22, 2019, 06:32:35 pm »
If you are on Lazarus (and you do not have DisableUtf8RTL defined) then why not simply convert the WideString to UTF8?
This is lossless.

Bart

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: How can I pass array of widechar to a standard StringList
« Reply #2 on: January 22, 2019, 11:14:54 pm »
Hi

For many reasons I have to use several arrays of widechar for a particular project. I've got to the point where I want to save the arrays of widechar to a stringlist, but having created a standard stringlist and trying to use sl.add, I get page exception errors.

How do I add what is in essence a widestring (an array of widechars) to a standard stringlist?

e.g.
Code: Pascal  [Select][+][-]
  1. function 123
  2. const
  3.   BufLen = 256
  4. var
  5.   sl : TStringList;
  6.   SomeLongString : array[0..BufLen-1] of widechar;
  7.  
  8. begin
  9. sl : TStringList.Create;
  10. SomeLongString := AnAPICall(A, B, C);  // a call that returns a null terminated string
  11. ...
  12. sl.Add(SomeLongString); // This generates page protection fault
  13. end;
  14.  
I do not think that fpc has a TWideStringList class yet you should either copy the tstrginlist implementation and change the base string type to widestring or find an existing implementation eg jedi code library has one that might be compilable in fpc/lazarus.

If you need it to support linux you might have to take in to account that linux is utf8 based not utf16 like windows, OSX and the rest of the commercial OS.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5448
  • Compiler Developer
Re: How can I pass array of widechar to a standard StringList
« Reply #3 on: January 23, 2019, 09:08:30 am »
If you don't need to use the specific functions of TStringList, but only need a simple container for UnicodeStrings I'd suggest you to use one of the generic containers (either TFPGList<> in unit FGL or TList<> in unit Generics.Collections) and specialize that for UnicodeString. It might be that you'll need to add a variable of type array of WideChar like this however:

Code: Pascal  [Select][+][-]
  1. mylist.Add(StrPas(@SomeLongString[0]));

And no, specializing the generic containers with a array of WideChar won't work as you'd expect it.
Note: Same would be true if you'd use PWideChar.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: How can I pass array of widechar to a standard StringList
« Reply #4 on: January 23, 2019, 11:58:58 am »
Thanks guys. I solved this (kind of, though another question has arisen) by doing :
Code: Pascal  [Select][+][-]
  1. var
  2.   mSL : TStringList;
  3.   OutputMessage: array[0..Buflen-1] of WideChar;    
  4. ...
  5. begin
  6.   mySL := TStringList.Create;
  7.   mySL.Add(WideCharToString(@OutputMessage));    
  8.   mySL.free;
  9. end;
  10.  

 

TinyPortal © 2005-2018