Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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"?

share|improve this question
    
Could you give me an example of your file? An does the start and end tag have to be in the same line? –  Jens Borrisholt Feb 23 at 13:52
    
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 –  NickFuryy Feb 23 at 13:54
    
So data is stored inside the exe file as a Resource? –  Jens Borrisholt Feb 23 at 13:55
    
no , is plain text. –  NickFuryy Feb 23 at 13:56
    
So you want the text between Delimit1 and Delimit2 ? –  Jens Borrisholt Feb 23 at 14:03

1 Answer 1

up vote 2 down vote accepted

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;
share|improve this answer
    
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 –  NickFuryy Feb 23 at 14:10
1  
Why have you made the changes the way you did? On Code Review, we prefer if code is reviewed, not just replaced. –  Pimgd Feb 23 at 14:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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