Ver Mensaje Individual
  #3  
Antiguo 17-06-2008
Avatar de GaaK
GaaK GaaK is offline
Miembro
 
Registrado: oct 2005
Ubicación: Trujillo - Peru
Posts: 31
Reputación: 0
GaaK Va por buen camino
Cita:
Empezado por coso Ver Mensaje
deberías usar recursividad...
Gracias, me inspiré en su idea y escribí el siguiente código (requiere: Form1, OpenDialog1, Button1):
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function LeerArchivo(namefile: String; destino: TStringList): Boolean;

var
  Form1: TForm1;
  sPath: String;

implementation

{$R *.dfm}

procedure ParseToken(tok: String; destino: TStringList);
begin
  tok := TrimLeft(tok);
  if (Pos('INCLUDE',tok)<>0) then begin
    { tok := 'INCLUDE "Archivo2.txt"' }
    Delete(tok, 1, 7); { 'INCLUDE' contains 7 chars }
    tok := TrimLeft(tok);
    { tok := '"Archivo2.txt"' }
    tok := AnsiDequotedStr(tok, '"');
    { tok := 'Archivo2.txt' }
    LeerArchivo(sPath+tok, destino);
  end
  else destino.Add(tok);
end;

function LeerArchivo(namefile: String; destino: TStringList): Boolean;
var
  st: TStringList;
  i, r: Longint;
  tok: String;
begin
  st := TStringList.Create;

  st.LoadFromFile(namefile);
  r := st.Count;
  for i:=0 to r-1 do begin
    tok := st[i];
    ParseToken(tok, destino);
  end;

  st.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  dest: TStringList;
begin
  dest := TStringList.Create;
  if OpenDialog1.Execute then begin
    sPath := ExtractFilePath(OpenDialog1.FileName);
    LeerArchivo(OpenDialog1.FileName, dest);
    dest.SaveToFile('ArchivoFinal.txt');
  end;
  dest.Free;
end;

end.
... para hacerlo funcionar, clic en el botón y elegir "Archivo1.txt" (mencionado arriba) y auto se creará "ArchivoFinal.txt" con todas las líneas de los INCLUDE.

Ahora, con esta misma idea no sería mejor hacerlo directamente con AssignFile etc etc?... Digo esto porque lo de crear StringList me parece consumo de memoria y tiempo?... (imaginando archivos de texto de tamaño medio ~200KB).

Iluminarme si me equivoco.

Gracias de antemano.

- Gaak -
__________________
L'Gaak dice
Responder Con Cita