Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Problem mit StrPos/AnsiStrPos (https://www.delphipraxis.net/58211-problem-mit-strpos-ansistrpos.html)

DevilsCamp 3. Dez 2005 09:20


Problem mit StrPos/AnsiStrPos
 
ich habe folgenden Code:

Delphi-Quellcode:
Procedure ReadSection(const Section: String; List: TStrings);
var
  p : PChar;
  f : PChar;
  g : PChar;
  s : String;
begin
  List.Clear;
  f := FStream.Memory;
  f[FStream.Size] := #0;
  p := StrPos(f, PChar(Format('[%s]', [Section])+#13#10));

  if (p<>nil) then
  begin
    f := StrPos(p, PChar(#13#10+'['));

    if (f=nil) then
      f := p+StrLen(p)-2;

    try
      GetMem(g, StrLen(p)+2);
      StrLCopy(g, p, f-p);

      p := StrPos(g, PChar(']'+#13#10))+3;
      f := p;
      p := StrPos(f, PChar('='));

      while (p<>nil) do
      begin
        SetString(s, f, p-f);
        List.Add(s);
        f := StrPos(p, Pchar(#13#10))+2;
        p := StrPos(f, PChar('='));
      end;

    finally
      FreeMem(g);
    end;
  end; // if (p<>nil)

end;
Dieser Code funktioniert so wie er soll, bis zu den Zeilen
Delphi-Quellcode:
p := StrPos(g, PChar(']'+#13#10))+3;
f := p;
p := StrPos(f, PChar('='));
Da bekomme ich dann eine "Access Violation" bei
Delphi-Quellcode:
p := StrPos(f, PChar('='));
die sich völlig meines Verständnisses entzieht.

Bei debuggen sehe ich auch, dass vor dieser Zeile f WIRKLICH gleich p ist und auch Text vorhanden ist (f und p sind NIChT nil und die Länge ist > 0)

Vielleicht kann mir einer helfen




PS:
Ich habe es auch schon AnsiStrPos statt StrPos versucht.

DevilsCamp 3. Dez 2005 09:32

Re: Problem mit StrPos/AnsiStrPos
 
Ich habe folgendes Herausgefunden:

Wenn ich
Delphi-Quellcode:
p := StrPos(f, PChar('='));
benutze, dann bekomme ich die Access Violation.

Benutze ich aber:
Delphi-Quellcode:
s := '=';
p := StrPos(f, PChar(s));
oder
Delphi-Quellcode:
p := StrPos(f, PChar('aa')); // oder irgendeinen anderen String mit MINDESTENS 2 Zeichen
dann kommt sie nicht


<edit>

Habe 'ne möglichkeit gefunden:
Delphi-Quellcode:
p := StrPos(f, '='#0);
</edit>

himitsu 3. Dez 2005 12:02

Re: Problem mit StrPos/AnsiStrPos
 
Und so ging es nicht? (die CompilerMagic sollte es möglich machen)
Delphi-Quellcode:
p := StrPos(f, '=');
Die #0 ist ja sonst auch mit im String drin, auch wenn man diese nicht sieht ^^

DevilsCamp 3. Dez 2005 12:54

Re: Problem mit StrPos/AnsiStrPos
 
:wall: :wall: :wall:

Auf die einfachste Lösung kommt man meistens nie selber :D


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:03 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz