/* This program appends records to an existing vdata, using myfile.hdf 
 * created by wvset.c. 
 * Note: if the new records are to be appended at the end of the current last 
 * record and if the vdata is not appendable, VSseek ( calls Hseek in turn) 
 * will return  FAIL when trying to seek to the end of the vdata. 
 * A work around is: VSseek to n_recs-1 record, read in the last one to 
 * move the ptr  to the end of the vdata, then write new records.
 * Add error check when necessary, using 
 *     if (ret == FAIL) printf("<function> failed \n");
*/

#include  "hdf.h"
#include  <string.h>

main() {
  int32 vgid, vsid, f, vgref, vdref;
  int32 tags[10], refs[10];
  float32 pxy[100][2];
  float32 tmp[100];
  int16 mesh[100][3];
  int i, ret;
  char vgname[100], vsname[100], vflds[100];
  int32 ret32, n_recs, interlace, vsize;
  uint8 inbuf[100];

  for(i=0;i<100;i++) {
    tmp[i]=i*0.2;
    pxy[i][0]=i*0.01+200.0;
    pxy[i][1]=i*0.01+100.0;
    mesh[i][0]=2*i+1;
    mesh[i][1]=2*i+2;
    mesh[i][2]=2*i+3;
  }
 
  for (i=0; i<10; i++)  
      tags[i] = refs[i] = 0;

  f=Hopen("myfile.hdf",DFACC_ALL,0);
  Vstart(f);
   
    vgref = Vgetid(f, -1);  /* get the first vg */ 
/*    printf("vgref=%d\n", vgref);      */
    vgid= Vattach(f,vgref,"w");
/*     printf("vgid=%d\n", vgid);   */
    Vgetname(vgid,vgname);
    printf("vgroup name=%s\n", vgname);
    if (strncmp(vgname, "mypoints",10) != 0) {
        printf("Wrong vgroup. name=%s\n", vgname);
        exit (1);
    }

    ret32 = Vgettagrefs(vgid, tags, refs, 10);
/*    printf("  tags\t  refs\n");
    for (i=0; i<10; i++)
        printf("\t%d\t%d\n", tags[i], refs[i]);
*/
    vsid = VSattach(f,refs[0],"w");
    VSgetname(vsid, vsname);
    if (strncmp(vsname, "vdata1", 10) != 0) {
        printf("Wrong vdata. name=%s\n", vsname);
        exit (1);
    }
    ret = VSinquire(vsid, &n_recs, &interlace, vflds, &vsize, vsname);
/*    A work-around :
    Seek for n_recs-1 record, read the last one and throw it away */
    ret = VSseek(vsid, n_recs-1);
/*    printf("VSseek returns %d\n", ret);    */
    ret = VSread(vsid, inbuf, 1, FULL_INTERLACE);
    ret = VSwrite(vsid,(uint8 *) mesh,10,FULL_INTERLACE);
    VSdetach(vsid);

    vsid = VSattach(f,refs[1],"w");
    VSgetname(vsid, vsname);
    if (strncmp(vsname, "vdata2", 10) != 0) {
        printf("Wrong vdata. name=%s\n", vsname);
        exit (1);
    }

      ret = VSinquire(vsid, &n_recs, &interlace, vflds, &vsize, vsname);
      ret = VSseek(vsid, n_recs-1);
/*      printf("VSseek returns %d\n", ret);    */
      ret = VSread(vsid, inbuf, 1, FULL_INTERLACE);
      ret = VSwrite(vsid,(uint8 *) pxy,10,FULL_INTERLACE);
      VSdetach(vsid);

      vsid = VSattach(f,refs[2],"w");
      VSgetname(vsid, vsname);
      if (strncmp(vsname, "vdata3",10 ) != 0) {
        printf("Wrong vdata. name=%s\n", vsname);
        exit (1);
      }
      ret = VSinquire(vsid, &n_recs, &interlace, vflds, &vsize, vsname);
      ret = VSseek(vsid, n_recs-1);
/*      printf("VSseek returns %d\n", ret);   */
      ret = VSread(vsid, inbuf, 1, FULL_INTERLACE);
      ret = VSwrite(vsid,(uint8 *) tmp,10,FULL_INTERLACE);
      VSdetach(vsid);

    Vdetach(vgid);

  Vend(f);
  Hclose(f);

}


