(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
program isq(input,output);

type
   idstr = packed array [1..4] of char;
   datarec = record
                id : [key(0)] idstr;
                val : integer;
             end;

var
   db : file of datarec;

procedure test_write_one(id : idstr; val : integer);

var
   buf : datarec;

begin
   buf.id := id;
   buf.val := val;
   db^ := buf;
   put(db);
end;

procedure test_write;

begin
   open(db, 'pas.isq', new, organization := indexed, access_method := keyed);
   test_write_one('CCCC', 3);
   test_write_one('BBBB', 2);
   test_write_one('AAAA', 1);
   close(db);
end;

procedure test_read_sequential;

var
   buf : datarec;

begin
   open(db, 'pas.isq', old, organization := indexed, access_method := sequential);
   reset(db);
   while not(eof(db)) do begin
      buf := db^;
      writeln(buf.id, ' -> ', buf.val);
      get(db);
   end;
   close(db);
end;

procedure test_read_random_one(id : idstr);

var
   buf : datarec;

begin
   findk(db, 0, id);
   buf := db^;
   writeln(buf.id, ' -> ', buf.val);
end;

procedure test_read_random;

begin
   open(db, 'pas.isq', old, organization := indexed, access_method := keyed);
   test_read_random_one('CCCC');
   test_read_random_one('BBBB');
   test_read_random_one('AAAA');
   close(db);
end;

begin
   writeln('Pascal');
   test_write;
   test_read_sequential;
   test_read_random;
end.
