2

I'm trying to embed some text read from a file into an Inno installer. Ideally this will happen in the pre-processor (ISPP) so the text cannot be modified. I am able to read the text but cannot get any newlines to display:

#define FileHandle
#define FileLine
#define ReadmeText ""

#for {FileHandle = FileOpen("README.txt"); FileHandle && !FileEof(FileHandle); FileLine = FileRead(FileHandle)} ReadmeText = ReadmeText + FileLine

#if FileHandle
   #expr FileClose(FileHandle)
#endif

I've attempted to insert special characters when appending each line, but none appear to work in the pre-processor (e.g. '\n', '%n', '#13').

I'm adding the text to a TMemo field, currently using:

Memo.Text := '{#ReadmeText}';

If I could parse the file into an array of lines, it might be possible to use the Memo.Lines.AddLine() function, but I'm not sure if it is possible to create a variable sized array in the pre-processor and then to use it in the Pascal scripting.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
james
  • 77
  • 8

1 Answers1

3

I think this could do the trick:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
DefaultGroupName=My Program

[Code]

procedure InitializeWizard;
var
  Memo: TNewMemo;
  Form: TSetupForm;
begin
  Form := CreateCustomForm;
  try  
    Form.Position := poScreenCenter;
    Memo := TNewMemo.Create(Form);
    Memo.Parent := Form;
    Memo.Align := alClient;
    #define FileLine
    #define FileHandle
    #sub ProcessFileLine
      #emit '    Memo.Lines.Add(''' + FileLine + ''');'
    #endsub
    #for {FileHandle = FileOpen("Readme.txt"); \
      FileHandle && !FileEof(FileHandle); FileLine = FileRead(FileHandle)} \
      ProcessFileLine
    #if FileHandle
      #expr FileClose(FileHandle)
    #endif
    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

#expr SaveToFile("c:\PreprocessedScript.iss")

Or this way you can create and fill an array:

#define FilesSource "Readme.txt"
#define FileLine
#define FileIndex
#define FileCount
#define FileHandle
#dim FileList[65536]
#sub ProcessFileLine
  #expr FileList[FileCount] = FileLine
  #expr FileCount = ++FileCount
#endsub
#for {FileHandle = FileOpen(FilesSource); \
  FileHandle && !FileEof(FileHandle); \
  FileLine = FileRead(FileHandle)} \
  ProcessFileLine
#if FileHandle
  #expr FileClose(FileHandle)
#endif

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
DefaultGroupName=My Program

[Code]

procedure InitializeWizard;
var
  Memo: TNewMemo;
  Form: TSetupForm;
begin
  Form := CreateCustomForm;
  try  
    Form.Position := poScreenCenter;
    Memo := TNewMemo.Create(Form);
    Memo.Parent := Form;
    Memo.Align := alClient;
    #sub AddFileItemCode
      #emit '    Memo.Lines.Add(''' + FileList[FileIndex] + ''');'
    #endsub
    #for {FileIndex = 0; FileIndex < FileCount; FileIndex++} AddFileItemCode
    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

#expr SaveToFile("c:\PreprocessedScript.iss")
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
TLama
  • 75,147
  • 17
  • 214
  • 392
  • Thank you very much. This is exactly what I needed. I couldn't quite get the step between the pre-processor array, and adding the lines to the Memo box. – james Sep 11 '13 at 15:58