Recent

Author Topic: list of null terminated strings terminated with two nulls  (Read 6141 times)

acp693

  • Jr. Member
  • **
  • Posts: 73
list of null terminated strings terminated with two nulls
« on: February 24, 2013, 06:12:57 pm »
Hi,

I'm calling an openal function ALCGETSTRING, which returns a list of device specifiers each one terminated by a null, the whole list is terminated by two nulls.


My question: how can I convert this list into an array of strings from which I can populate a combobox?

Any help greatly appreciated.

Albert

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: list of null terminated strings terminated with two nulls
« Reply #1 on: February 24, 2013, 06:25:34 pm »
I don't know any easy way, so I guess you'll need to do it manually, using functions from Strings unit. Here's an example:
Code: [Select]
uses
  { order is important, StrPas from Strings only converts to
     ShortString, while SysUtils one converts to AnsiString }
  Strings,
  SysUtils;
function PCharsToStrings(P: PChar): TStrings;
var
  T: PChar;
begin
  Result := TStringList.Create;
  while Assigned(P) do begin
    T := StrScan(P,#0);
    Result.Add(StrPas(P));
    P := T + 1;
  end;
end;
You can use it in a combobox like this:
Code: [Select]
Devices := PCharsToStrings(DeviceList);
ComboBox1.Items.Assign(Devices);
Devices.Free; // don't forget this
WARNING: untested code, I just compile it with my brain :P

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: list of null terminated strings terminated with two nulls
« Reply #2 on: February 24, 2013, 06:33:43 pm »
Wow, thanks for the quick reply, I'll try this out.

Best regards

Albert

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: list of null terminated strings terminated with two nulls
« Reply #3 on: February 24, 2013, 07:15:08 pm »
I just tried that code, it causes a SigSev crash, any idea what it might be? I'm really lost with this.

Thanks again

Albert

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: list of null terminated strings terminated with two nulls
« Reply #4 on: February 24, 2013, 07:44:11 pm »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: list of null terminated strings terminated with two nulls
« Reply #5 on: February 24, 2013, 07:57:19 pm »
Quote
I just tried that code, it causes a SigSev crash, any idea what it might be? I'm really lost with this.
Sorry, I miss the null check, just change the while condition:
Code: [Select]
while Assigned(P) and (P^ <> #0) do beginIt's rather overkill though, if you need a little faster execution, move the Assigned(P) check before the while loop. You define yourself what to return when nil is passed (as P). Either TStrings with no elements, or nil. Personally, I would go for the first to make the caller code consistent.

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: list of null terminated strings terminated with two nulls
« Reply #6 on: February 24, 2013, 09:10:47 pm »
Thanks for the replys, I didn't get those functions to work, so I wrote my own very cluncky function. The compiler doesn't like the bit where I try to copy a specific part of my pchar to a stringlist. I suppose I just don't understand pchars. Any ideas?  thanks a lot.  Also, how can I build such a pchar list, terminated with two nulls? for testing? I don't have a computer with more than one soundcard so I won't get a list with more than one device listed. Just want to be certain it works.
  Albert

Code: [Select]
pc:= 'one';
  nullfound:= false;
  count:=0;
  arraycount:=0;
  plen:= strlen(pc);
  firstchar:=true ;
  list.Create;

     while (pc^ <> #0) and not nullfound do begin  // {#0 is ASCII 0, the null character}
       if (pc^ <> #0) then begin
           if firstchar then  begin
              lower:= count;
              firstchar:= false;
           end;
           count:=count+1;
           inc(pc);
           nullfound:=false;
       end
       else begin
          nullfound:= true;
          higher:= count;
          list.Add(StrPas(pc[lower:higher]); //////////////////////// goes wrong here//////////////////////////
          count:=count+1;
          inc(pc);
          firstchar:= true;
       end;
     end;         
« Last Edit: February 24, 2013, 09:14:24 pm by acp693 »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: list of null terminated strings terminated with two nulls
« Reply #7 on: February 25, 2013, 01:10:58 am »
Code: [Select]
pc[lower:higher]
Are you trying to cut certain part of string?
Code: [Select]
copy('Hello world!', 2, 4) // returns 'ello'

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: list of null terminated strings terminated with two nulls
« Reply #8 on: February 25, 2013, 10:08:00 am »
Leledumbo, Thank you very much, your code works perfectly, I made a stupid mistake which prevented it working.

I think I understand how your code works now; strscan returns a pointer to the first occurence of a null, this then gets copied to the stringlist, and the line P:= T+1 reassigns P to the point after where the first null was found? thus throwing away the bit already copied?

What I don't understand is, why does the line Result.Add(StrPas(P)); not copy the whole of P to the string? is it because strpas just copies the characters upto where the first null is found?

Many thanks to everybody else for your help and suggestions.

Albert

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: list of null terminated strings terminated with two nulls
« Reply #9 on: February 25, 2013, 11:25:40 am »
What I don't understand is, why does the line Result.Add(StrPas(P)); not copy the whole of P to the string? is it because strpas just copies the characters upto where the first null is found?

Yes it converts a null terminated string to a pascal string.
A null terminated string is a string terminated with a #0  ;)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: list of null terminated strings terminated with two nulls
« Reply #10 on: February 25, 2013, 02:26:28 pm »
Quote
strscan returns a pointer to the first occurence of a null, this then gets copied to the stringlist
First sentence correct, second is not. The returned pointer is just a placeholder for the pointer to null character to advance to next pchar.
Quote
and the line P:= T+1 reassigns P to the point after where the first null was found? thus throwing away the bit already copied?
In a better words: it moves P to point to the first character in the next pchar.

Theo already answered your StrPas question.

Now that my head is a bit clearer, I can give you a more optimized version:
Code: [Select]
function PCharsToStrings(P: PChar): TStrings;
begin
  Result := TStringList.Create;
  if Assigned(P) then begin
    while P^ <> #0 do begin
      Result.Add(StrPas(P));
      P := StrScan(P,#0) + 1;
    end;
  end
end;

 

TinyPortal © 2005-2018