(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
[inherit('pbdbdir:pbdb')]
program big(input, output);

var
   buf : packed array [1..10000000] of chr;
   buflen : integer;

(*
Note: these IO routines are neither typical Pascal nor typical VMS,
      but they are comptible with the way the C code works.
*)

procedure load(fnm : pstr);

var
   f : text;
   c : char;

begin
   open(f, fnm, old);
   reset(f);
   while not eof(f) do begin
      if eoln(f) then begin
         buflen := buflen + 1;
         buf[buflen] := chr(10);
         readln(f);
      end else begin
         read(f, c);
         bufen := buflen + 1;
         buf[buflen] := c;
      end;
   end;
   close(f);
end;

procedure store(fnm : pstr);

var
   f : text;
   i : integer;

begin
   open(f, fnm, new, record_format := 'stmlf');
   rewrite(f);
   for i := 1 to buflen do begin
      write(f, buf[i]);
   end;
   close(f);
end;

var
   db : db_ptr;
   stat, dummy : integer;
   wdummy : word;

begin
   (* open database *)
   db := db_open('big.db');
   (* put *)
   load('4300-0.txt');
   db_sk_put(db, 'copy1', buf, buflen);
   db_sk_put(db, 'copy2', buf, buflen);
   (* get *)
   buflen := 0;
   db_sk_get(db, 'copy1', buf, size(buf), buflen);
   store('copy1.txt');
   buflen := 0;
   db_sk_get(db, 'copy2', buf, size(buf), buflen);
   store('copy2.txt');
   (* close database *)
   db_close(db);
end.
