(* CVT_REC.PAS -- extract chains from a GEOS files
** Copyright (c) 1995,1996 Jochen Metzinger
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*)

PROGRAM cvt_rec;

USES global, dump, geos, errors, cvt;

VAR
 chain: INTEGER;
 do_dump: BOOLEAN;
 dir: DirRec;
 info: InfoRec;

PROCEDURE usage;
BEGIN
 WriteLn('CVT_REC  Version 0.3  Output a chain of a GEOS file');
 WriteLn('Copyright (c) 1995,1996 Jochen Metzinger ');
 WriteLn;
 WriteLn(short_usage);
 WriteLn;
 WriteLn('  filename    GEOS file');
 WriteLn('  number      chain number (0-126)');
 WriteLn('    I           number for info record');
 WriteLn('    D           number for directory record');
 WriteLn('    V           number for `VLIR'' record');
 WriteLn('    /D          output an dump');
 HALT(1);
END; (* usage *)

PROCEDURE Init;
 VAR cvt_name: STRING;
  i, j: WORD; par: STRING;
  code: INTEGER;
BEGIN
 short_usage := 'CVT_REC [/?] filename number [options]';
 cvt_name := ''; chain := -42; do_dump := FALSE;
 IF ParamCount = 0 THEN
  usage;
 FOR i := 1 TO ParamCount DO BEGIN
  par := ParamStr(i);
  IF (par[1] = '/') OR (par[1] = '-') THEN
   BEGIN
    IF Length(par) = 1 THEN
     FATAL('unknown option '+par);
    FOR j := 2 TO Length(par) DO
     CASE UpCase(par[j]) OF
      '?', 'H': usage;
      'D': do_dump := TRUE;
      ELSE FATAL('unknown option '+par);
     END; (* case *)
   END
  ELSE IF cvt_name = '' THEN
   cvt_name := AddExt(par, CVT_EXT)
  ELSE IF chain = -42 THEN
   CASE UpCase(par[1]) OF
    'I': chain := -1;
    'D': chain := -2;
    'V': chain := -3;
    ELSE BEGIN
     Val(par, chain, code);
     IF code <> 0 THEN
      FATAL('no number');
     IF (chain < 0) OR (126 < chain) THEN
      FATAL('number out of range');
    END
   END (* case *)
  ELSE
   FATAL('too many arguments');
 END; (* for *)
 IF (cvt_name = '') OR (chain = -42) THEN usage;
 cvt_open(cvt_name);
 short_usage := '';
END; (* init *)

PROCEDURE do_output(VAR mem; length: WORD);
 VAR i: WORD;
  str: ARRAY [1..$FFFF] OF CHAR ABSOLUTE mem;
BEGIN
 IF do_dump
  THEN DumpVar(mem, length, 0)
  ELSE FOR i := 1 TO length DO Write(str[i]);
END; (* do_output *)

BEGIN (* cvt_rec *)
 Init;
 IF chain >= 0 THEN
  BEGIN (* a normal chain *)
   cvt_chain(chain);
   IF NOT cvt_eof THEN do_output(cvt_buffer^, cvt_size(chain));
  END
 ELSE IF chain = -1 THEN
  BEGIN (* info sector *)
   cvt_info(info);
   do_output(info, SizeOf(info));
  END
 ELSE IF chain = -2 THEN
  BEGIN (* dir entry *)
   cvt_dir(dir);
   do_output(dir, SizeOf(dir));
  END
 ELSE IF chain = -3 THEN
  BEGIN (* vlir *)
   cvt_vlir(info);
   do_output(info, SizeOf(info));
  END;
 cvt_close;
END. (* cvt_rec *)
