Ver Mensaje Individual
  #1  
Antiguo 11-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
Archivos de Texto con INCLUDE

Hola todos, quisiera por favor el mejor algoritmo Delphi para leer un archivo de texto que a su vez (mediante un INCLUDE) pueda llamar a otros...

Gráficamente esto podría ser algo como:

Archivo1.txt
line1
line2
INCLUDE "Archivo2.txt"
line6
INCLUDE "Archivo4.txt"
line9
<EOF>

Archivo2.txt
line3
line4
INCLUDE "Archivo3.txt"
<EOF>

Archivo3.txt
line5
<EOF>

Archivo4.txt
line7
line8
<EOF>

Donde <EOF> es el fin de cada archivo. Finalmente el propósito de leer todos los archivos es para conseguir un nuevo archivo final de la forma:
ArchivoFinal.txt
line1
line2
....
line9

Obviamente cada line contiene texto variable.

----------------------------------------
Mi idea es la siguiente:

Código Delphi [-]
var
  file1 : TextFile;
  nIncludeLevel: Integer = 0;
  filename: array[0..6] of string; { 7 levels for INCLUDE }

implementation

procedure TFormMain.FileOpen1Accept(Sender: TObject);
var
  s0: String; 
  n: Integer;
begin
  { default nIncludeLevel is zero }
  filename[nIncludeLevel] := FileOpen1.Dialog.FileName;
  AssignFile(file1, filename[nIncludeLevel]);
  Reset(file1);

  { CountLines }
  n := 0;
  while (not Eof(file1)) do begin
    Readln(file1,s0);
    if 'INCLUDE'=GetFirstWord then begin { GetFirstWord returns 'INCLUDE' and s0->"NameFile" }
      s0 := ExtractFilePath(filename[nIncludeLevel]) + AnsiDequotedStr(s0,'"'); { s0: 'Archivo2.txt' }
      Inc(nIncludeLevel); { 1 }
      filename[nIncludeLevel] := s0;
      AssignFile(file1, filename[nIncludeLevel]);
      Reset(file1);
    end
    else { salvar lineN };
    if (Eof(file1)) and (nIncludeLevel<>0) then begin
      CloseFile(file1);
      Dec(nIncludeLevel);
      { aqui se apaga mi cerebro :-( }
      // AssignFile(file1, filename[nIncludeLevel]);
      // Reset(file1);
    end;
    Inc(n);
  end;
  CloseFile(file1);
  Reset(file1);
end;


Gracias por sus respuestas.

- Gaak -
__________________
L'Gaak dice

Última edición por GaaK fecha: 11-06-2008 a las 00:19:33. Razón: syntax
Responder Con Cita