/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <string.h>

#include <starlet.h>
#include <rms.h>

#define FNM "c.isq"

struct datarec
{
   char id[4];
   int val;
};

void test_write_one(struct RAB *rabptr, struct datarec *buf)
{
   long stat;
   rabptr->rab$l_kbf = buf->id;
   rabptr->rab$b_ksz = 4;
   rabptr->rab$l_rbf = (char *)buf;
   rabptr->rab$w_rsz = sizeof(struct datarec);
   stat = sys$put(rabptr, 0, 0);
}

void test_write()
{
   long stat;
   struct FAB fab;
   struct RAB rab;
   struct XABKEY xab;
   struct datarec buf;
   fab = cc$rms_fab;
   fab.fab$l_fna = (char *)FNM;
   fab.fab$b_fns = strlen(FNM);
   fab.fab$b_org = FAB$C_IDX;
   fab.fab$b_rfm = FAB$C_FIX;
   fab.fab$w_mrs = 8;
   fab.fab$b_fac = FAB$M_PUT;
   fab.fab$l_xab = (char *)&xab;
   xab = cc$rms_xabkey;
   xab.xab$b_dtp = XAB$C_STG;
   xab.xab$w_pos0 = 0;
   xab.xab$b_ref = 0;
   xab.xab$b_siz0 = 4;
   stat = sys$create(&fab, 0, 0);
   rab = cc$rms_rab;
   rab.rab$l_fab = &fab;
   rab.rab$b_rac = RAB$C_KEY;
   stat = sys$connect(&rab, 0 ,0);
   memcpy(buf.id, "CCCC", 4);
   buf.val = 3;
   test_write_one(&rab, &buf);
   memcpy(buf.id, "BBBB", 4);
   buf.val = 2;
   test_write_one(&rab, &buf);
   memcpy(buf.id, "AAAA", 4);
   buf.val = 1;
   test_write_one(&rab, &buf);
   stat = sys$disconnect(&rab, 0, 0);
   stat = sys$close(&fab, 0, 0);
}

void test_read_sequential()
{
   long stat;
   struct FAB fab;
   struct RAB rab;
   struct datarec buf;
   fab = cc$rms_fab;
   fab.fab$l_fna = (char *)FNM;
   fab.fab$b_fns = strlen(FNM);
   fab.fab$b_fac = FAB$M_GET;
   stat = sys$open(&fab, 0, 0);
   rab = cc$rms_rab;
   rab.rab$l_fab = &fab;
   rab.rab$b_rac = RAB$C_SEQ;
   stat = sys$connect(&rab, 0 ,0);
   for(;;)
   {
      rab.rab$l_ubf = (char *)&buf;
      rab.rab$w_usz = sizeof(struct datarec);
      stat = sys$get(&rab, 0, 0);
      if(stat == RMS$_EOF) break;
      printf("%4s -> %d\n", buf.id, buf.val);
   }
   stat = sys$disconnect(&rab, 0, 0);
   stat = sys$close(&fab, 0, 0);
}

void test_read_random_one(struct RAB *rabptr, char *id)
{
   long stat;
   struct datarec buf;
   rabptr->rab$l_kbf = id;
   rabptr->rab$b_ksz = 4;
   rabptr->rab$l_ubf = (char *)&buf;
   rabptr->rab$w_usz = sizeof(struct datarec);
   stat = sys$get(rabptr, 0, 0);
   printf("%4s -> %d\n", buf.id, buf.val);
}

void test_read_random()
{
   long stat;
   struct FAB fab;
   struct RAB rab;
   fab = cc$rms_fab;
   fab.fab$l_fna = (char *)FNM;
   fab.fab$b_fns = strlen(FNM);
   fab.fab$b_fac = FAB$M_GET;
   stat = sys$open(&fab, 0, 0);
   rab = cc$rms_rab;
   rab.rab$l_fab = &fab;
   rab.rab$b_rac = RAB$C_KEY;
   stat = sys$connect(&rab, 0 ,0);
   test_read_random_one(&rab, "CCCC");
   test_read_random_one(&rab, "BBBB");
   test_read_random_one(&rab, "AAAA");
   stat = sys$disconnect(&rab, 0, 0);
   stat = sys$close(&fab, 0, 0);
}

int main(int argc, char *argv[])
{
   printf("RMS (C)\n");
   test_write();
   test_read_sequential();
   test_read_random();
   return 0;
}
