Lazarus Pascal
Lazarus Pascal
Implement Preprocessor #include with StringBuilder
Demonstrates how to implement #include with a Chilkat StringBuilder.Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
sbSrc: TStringBuilder;
filePath: string;
sbIncludeFile: TStringBuilder;
begin
// First build a string that has a preprocessor include
sbSrc := TStringBuilder.Create;
sbSrc.Append('1' + #13#10 + '2' + #13#10 + '3' + #13#10);
sbSrc.Append('#include <qa_data/txt/helloWorld.txt>' + #13#10);
sbSrc.Append('4' + #13#10 + '5' + #13#10);
WriteLn(sbSrc.GetAsString());
// sbSrc contains:
// 1
// 2
// 3
// #include <qa_data/txt/helloWorld.txt>
// 4
// 5
// The qa_data/txt/helloWorld.txt file contains "Hello World!"
filePath := sbSrc.GetAfterBetween('#include','<','>');
if (sbSrc.LastMethodSuccess <> True) then
begin
WriteLn('No #include''s found.');
Exit;
end;
WriteLn('filePath: ' + filePath);
// Load the contents of the filePath
sbIncludeFile := TStringBuilder.Create;
sbIncludeFile.LoadFile(filePath,'utf-8');
// Replace the first occurrence of #include <...> line with the contents of the include file.
sbSrc.ReplaceAllBetween('#include','>',sbIncludeFile.GetAsString(),True);
WriteLn(sbSrc.GetAsString());
// sbSrce now contains:
// 1
// 2
// 3
// Hello World!
// 4
// 5
sbSrc.Free;
sbIncludeFile.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.