3
\$\begingroup\$

I've just finished this function and wanted to know if anyone know another way to do the same:

function findtext(archivo,Delimit1, Delimit2 :String) :String;
var
  Buffer      :AnsiString;
  ResLength   :Integer;
  i           :Integer;
  PosDelimit  :Integer;
begin
  Buffer := read_file_z(archivo);
  if Pos(Delimit1, Buffer) > Pos(Delimit2, Buffer) then
    PosDelimit := Length(Buffer)-(Pos(Delimit1, Buffer)+Length(Delimit1))
  else PosDelimit := Length(Buffer)-(Pos(Delimit2, Buffer)+Length(Delimit2));
  Buffer := Copy(Buffer, (Length(Buffer)-PosDelimit), Length(Buffer));
  ResLength := Pos(Delimit2, Buffer)-(Pos(Delimit1, Buffer)+Length(Delimit1));
  for i := 0 to (Reslength-1) do
    Result := Result+Buffer[Pos(Delimit1, Buffer)+(Length(Delimit1)+i)];
end;

The auxiliary function read_file_z is this:

function read_file_z(const FileName: String): AnsiString;
var
  F: File;
  DefaultFileMode: Byte;
begin
  DefaultFileMode := FileMode;
  try
    FileMode := 0;
    AssignFile(F, FileName);
    {$I-}
    Reset(F, 1);
    {$I+}
    if IoResult=0 then
      try
        SetLength(Result,FileSize(F));
        if Length(Result)>0 then begin
          {$I-}
          BlockRead(F,Result[1],LENGTH(Result));
          {$I+}
          if IoResult<>0 then Result:='';
        end;
      finally
        CloseFile(F);
      end;
  finally
    FileMode := DefaultFileMode;
  end;
end;

The function finds text between two tags. An example would [hi]hi world[hi] and the function will respond "hi world".

Example:

findtext('test.exe','[hi]','[hi]');

The way seek to do the function is the "uses" default.

What alternatives do I have to make the function "findtext"?

\$\endgroup\$
6
  • \$\begingroup\$ Could you give me an example of your file? An does the start and end tag have to be in the same line? \$\endgroup\$ Commented Feb 23, 2015 at 13:52
  • \$\begingroup\$ I can not is a binary application that I use as an agenda login , data is saved in the bottom of the file , the eof \$\endgroup\$
    – Matt Olsen
    Commented Feb 23, 2015 at 13:54
  • \$\begingroup\$ So data is stored inside the exe file as a Resource? \$\endgroup\$ Commented Feb 23, 2015 at 13:55
  • \$\begingroup\$ no , is plain text. \$\endgroup\$
    – Matt Olsen
    Commented Feb 23, 2015 at 13:56
  • \$\begingroup\$ So you want the text between Delimit1 and Delimit2 ? \$\endgroup\$ Commented Feb 23, 2015 at 14:03

1 Answer 1

4
\$\begingroup\$

You code is extremly hard to read. But I belive this is what you are looking for:

function FindText(FileName: TFilename; const Delimit1, Delimit2: string): string;
var
  Buffer: TStringList;
  PD1, PD2: Integer;
begin
  Buffer := TStringList.Create;
  try
    Buffer.LoadFromFile(FileName);

    PD1 := Pos(Delimit1, Buffer.Text) + Length(Delimit1);
    PD2 := Pos(Delimit2, Buffer.Text);

    Result := Copy(Buffer.Text, PD1, PD2 - PD1);
  finally
    FreeAndNil(Buffer);
  end;
end;
\$\endgroup\$
2
  • \$\begingroup\$ is perfect , there is no way to do without classes because I wanted to know if I could do with the "uses" that are by default \$\endgroup\$
    – Matt Olsen
    Commented Feb 23, 2015 at 14:10
  • 2
    \$\begingroup\$ Why have you made the changes the way you did? On Code Review, we prefer if code is reviewed, not just replaced. \$\endgroup\$
    – Pimgd
    Commented Feb 23, 2015 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.