Files
fpc-msgbase/examples/example_read.pas
Ken Johnson c68a225ad9 Tests + examples: 11 tests passing, full 6-target build matrix
- ma.api: add per-instance FOpCS critical section to serialise Do*
  calls (fixes racing writers that dropped 8/100 messages)
- .uni adapters: momCreate pre-creates empty format files
- example_read/example_write/example_tosser
- tests: test_read (samples), test_roundtrip (all 5 storage formats),
  test_lock (4 threads/100 msgs), test_batch (5 pkts*10 msgs/3 threads)
- run_tests.sh: single-command test runner
- build.sh: per-target binutils (i386-linux, i386-freebsd12, i386-emx)
2026-04-15 08:29:37 -07:00

132 lines
3.2 KiB
ObjectPascal

{
example_read.pas - open a message base via the unified API,
walk every message, dump a one-line summary.
usage: example_read <format> <path>
format : hudson | jam | squish | msg | pcboard | ezycom |
goldbase | auto
path : directory or basename the backend expects
Example:
example_read hudson /home/ken/fidonet/msg/hudson
}
program example_read;
{$mode objfpc}{$H+}
uses
SysUtils,
ma.types, ma.events, ma.api,
ma.fmt.hudson, ma.fmt.hudson.uni,
ma.fmt.jam, ma.fmt.jam.uni,
ma.fmt.squish, ma.fmt.squish.uni,
ma.fmt.msg, ma.fmt.msg.uni,
ma.fmt.pkt, ma.fmt.pkt.uni,
ma.fmt.pcboard, ma.fmt.pcboard.uni,
ma.fmt.ezycom, ma.fmt.ezycom.uni,
ma.fmt.goldbase, ma.fmt.goldbase.uni;
type
TLogger = class
procedure OnLog(Level: TMsgEventType;
const Source, Msg: AnsiString);
end;
procedure TLogger.OnLog(Level: TMsgEventType;
const Source, Msg: AnsiString);
begin
WriteLn('[', EventTypeToStr(Level), '] ', Source, ': ', Msg);
end;
var
gLogger: TLogger;
function ParseFormat(const S: string; out AFormat: TMsgBaseFormat;
out AAuto: boolean): boolean;
begin
AAuto := False;
Result := True;
case LowerCase(S) of
'hudson': AFormat := mbfHudson;
'jam': AFormat := mbfJam;
'squish': AFormat := mbfSquish;
'msg': AFormat := mbfMsg;
'pcboard': AFormat := mbfPCBoard;
'ezycom': AFormat := mbfEzyCom;
'goldbase': AFormat := mbfGoldBase;
'wildcat': AFormat := mbfWildcat;
'auto': AAuto := True;
else
Result := False;
end;
end;
procedure Usage;
begin
WriteLn('Usage: example_read <format> <path>');
WriteLn(' formats: hudson jam squish msg pcboard ezycom goldbase wildcat auto');
Halt(2);
end;
var
fmtName: string;
path: string;
fmt: TMsgBaseFormat;
auto: boolean;
base: TMessageBase;
msg: TUniMessage;
i, n: longint;
begin
if ParamCount < 2 then Usage;
fmtName := ParamStr(1);
path := ParamStr(2);
if not ParseFormat(fmtName, fmt, auto) then Usage;
try
if auto then
base := MessageBaseOpenAuto(path, momReadOnly)
else
base := MessageBaseOpen(fmt, path, momReadOnly);
except
on E: Exception do begin
WriteLn('error: ', E.Message);
Halt(1);
end;
end;
if base = nil then begin
WriteLn('error: could not detect format at ', path);
Halt(1);
end;
gLogger := TLogger.Create;
base.Events.OnLog := @gLogger.OnLog;
if not base.Open then begin
WriteLn('error: Open returned False (base files missing?)');
base.Free;
Halt(1);
end;
try
n := base.MessageCount;
WriteLn('format: ', MSG_BASE_FORMAT_NAME[base.Format],
' path: ', base.BasePath,
' messages: ', n);
for i := 0 to n - 1 do
if base.ReadMessage(i, msg) then
WriteLn(Format('%5d %-20s -> %-20s %s',
[msg.MsgNum,
Copy(msg.WhoFrom, 1, 20),
Copy(msg.WhoTo, 1, 20),
Copy(msg.Subject, 1, 40)]));
finally
base.Close;
base.Free;
gLogger.Free;
end;
end.