Ver Mensaje Individual
  #1  
Antiguo 22-02-2015
JuanOrtega JuanOrtega is offline
Miembro
NULL
 
Registrado: sep 2011
Posts: 130
Reputación: 13
JuanOrtega Va por buen camino
Funcion para extraer texto de archivos de texto

Hola tengo el siguiente codigo :

Código Delphi [-]

function read_file(const FileName: String): AnsiString;
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    SetLength(Result, Stream.Size);
    Stream.ReadBuffer(Pointer(Result)^, Stream.Size);
  finally
    Stream.Free;
  end;
end;

function cortar(archivo: String; deaca: String; hastaaca: String): String;
var text:string;
begin
  text := read_file(archivo);
  Delete(text, 1, AnsiPos(deaca, text) + Length(deaca) - 1);
  SetLength(text, AnsiPos(hastaaca, text) - 1);
  Result := text;
end;

function cut(archivo,Delimit1, Delimit2 :String) :String;
var
  Buffer      :AnsiString;
  ResLength   :Integer;
  i           :Integer;
  PosDelimit  :Integer;
begin
  Buffer := read_file(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;

Estoy tratando de mejorar mi funcion cortar() para que se parezca mas a la funcion cut() del codigo mostrado , lo que hacen es leer un archivo cualquiera y buscar en ellos texto que este entre dos etiquetas , el problema es que mi funcion cortar() me parece perfecta pero nunca obtengo el mismo resultado que la funcion cut() que parece mas compleja y no entiendo cual es el diferencia entre las dos porque cuando uso texto normal en un string las dos funciones son iguales pero cuando leo un archivo la funcion cut() es notablemente superior.

¿ como mejoro mi funcion cortar() para que haga lo mismo que cut() en los archivos ?
Responder Con Cita