home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / UIFlow 1.0.1 / UIFlow Source / VSet2.0 / Src / vmake.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-20  |  10.5 KB  |  439 lines  |  [TEXT/????]

  1.  
  2. /*****************************************************************************
  3. *
  4. * vmake.c
  5. *
  6. *    HDF Vset utility.
  7. *
  8. *    vmake:  utility creates vsets. May be used in 3 ways:
  9. *            (1)    add a new vgroup.
  10. *            (2)    add a new vdata from ascii data.
  11. *            (3)    link vgroups and vdatas to a vgroup.
  12. *
  13. *    USAGE:
  14. *            (1)     vmake file vgname
  15. *            (2)    vmake file vsname {format} {ascii data stream}
  16. *            (3)    vmake file -l vgref v1 v2 ... vn
  17. *
  18. *
  19. *****************************************************************************
  20. *              NCSA HDF Vset release 2.1
  21. *                    May 1991
  22. *                 Jason Ng May 1991 NCSA
  23. *
  24. * NCSA HDF Vset release 2.1 source code and documentation are in the public
  25. * domain.  Specifically, we give to the public domain all rights for future
  26. * licensing of the source code, all resale rights, and all publishing rights.
  27. * We ask, but do not require, that the following message be included in all
  28. * derived works:
  29. * Portions developed at the National Center for Supercomputing Applications at
  30. * the University of Illinois at Urbana-Champaign.
  31. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  32. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  33. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  34. ******************************************************************************/
  35.  
  36. #include "vg.h"
  37.  
  38. #ifdef MAC
  39.  int show_help_msg(void);
  40.  int vgadd(char *, char *);
  41.  int vsadd(char *,char *, char *);
  42.  int vsetlink (char *, int, int*, int);
  43.  int scanit (char*, char***, int**, int**);
  44.  int getdata ( unsigned char**);  
  45.  int compact (char *,char *);
  46.  int savfld (char *,int, int);
  47.  int savtype (char *,int, int);
  48.  int separate(char *ss, char *, int*);
  49.  
  50. #endif
  51.  
  52. main(ac,av) int ac; char**av; {
  53.  
  54.   char  *hfile, *vgname, *vsname, *fmt;
  55.  
  56.   if (ac <3 ) {
  57.         show_help_msg();
  58.         exit(0);
  59.         }
  60.   else if (ac==3)  {
  61.       if (!strcmp(av[2],"-l")) { show_help_msg() ; exit(0); }
  62.           hfile  = av[1];
  63.           vgname = av[2];
  64.          vgadd(hfile,vgname);
  65.         }
  66.  
  67.   else if (ac==4)  {
  68.       if (!strcmp(av[2],"-l")) { show_help_msg() ; exit(0); }
  69.           hfile    = av[1];
  70.           vsname   = av[2];
  71.           fmt      = av[3];
  72.          vsadd(hfile,vsname,fmt);
  73.         }
  74.  
  75.   else if (!strcmp(av[2],"-l")) {
  76.  
  77.          int i, n, vgref,ids[50];
  78.           hfile     = av[1];
  79.         sscanf(av[3],"%d",&vgref);
  80.         for(n=0,i=4;i<ac;i++,n++) { sscanf(av[i],"%d",&ids[n]); }
  81.         vsetlink(hfile,vgref,ids,n);
  82.         }
  83.  
  84.   else {
  85.         show_help_msg();
  86.         exit(0);
  87.         }
  88.  
  89.   } /* main */
  90.   
  91. int show_help_msg()  {
  92.  
  93.     printf("\nvmake: creates vsets.\n");
  94.     printf("\nUSAGE:\n");
  95.  
  96.   printf(" (1) vmake file vgname             (adds a new vgroup)\n");
  97.   printf(" (2) vmake file vsname format      (adds a new vdata)\n");
  98.   printf(" (3) vmake file -l vgref v1 .. vn  (links v1 v2 .. vn into vgref)\n");
  99.                                                                                                         
  100.     printf("\nwhere\n");
  101.      printf("  vgref is the ref of a vgroup\n");
  102.      printf("  v1,..,vn are refs of vgroups and vdatas\n");
  103.     printf("  format is <field=fmt,field=fmt,..>\n");
  104.     printf("    field is any text string\n");
  105.     printf("    fmt is one of [d,l,f,c] optionally preceded by a decimal.\n");
  106.     printf("\nTo create a vdata, vmake reads ascii data from stdin\n");
  107.  
  108.     printf("EXAMPLES:\n");
  109.      printf("\t cat dat.txt | vmake hh.hdf \"triangles\" \"PLIST3=3d\"\n");
  110.      printf("\t vmake abc.hdf \"xyvals\" \"X=d,Y=f\" < abc.dat\n");
  111.      printf("\n");
  112.  
  113.      return (1);
  114.  
  115.  } /* show_help_msg */
  116.  
  117. /* ------------------------------------------------------- */
  118.  
  119. int vsetlink  (hfile,vgid,ids,n)
  120. char * hfile;
  121. int vgid, n, ids[];
  122. {
  123.   DF * f;
  124.   VGROUP * vgmain, *vg;
  125.   VDATA * vs;
  126.   int err=0;
  127.   int i;
  128.  
  129.   if ( (f=DFopen(hfile,DFACC_ALL,0))==NULL) {
  130.       fprintf(stderr,"cannot open %s. DFerror=%d \n",hfile,DFerror); exit(0); 
  131.       }
  132.  
  133.   vgmain = (VGROUP*) Vattach(f,vgid,"w");
  134.   if(vgmain==NULL) { fprintf(stderr, "0\n"); DFclose(f); exit(-1);}
  135.  
  136.   for(i=0;i<n;i++) {
  137.       if     ( -1 != vexistvg(f,ids[i])) {
  138.             if ((vg=(VGROUP*) Vattach(f,ids[i],"r")) != NULL)  {
  139.              if (Vinsert(vgmain,(VDATA*) vg)  == -1)  { /*  is really VGROUP* */
  140.                     err = 1; 
  141.                    fprintf(stderr,"insert a vg (%d)fails!!\n",ids[i]);    
  142.                     }
  143.               Vdetach(vg);
  144.               }
  145.          }
  146.      else if ( -1 != vexistvs(f,ids[i])) {
  147.            if ((vs= (VDATA*) VSattach(f,ids[i],"r")) != NULL) { 
  148.              if (Vinsert(vgmain,(VDATA*) vs) == -1) { 
  149.                     err = 1;
  150.                    fprintf(stderr,"insert a vs (%d)fails!!\n",ids[i]);    
  151.                     }
  152.               VSdetach(vs);
  153.               }
  154.          }
  155.      else { fprintf(stderr,"no such vgroup or vdata [%d]\n",ids[i]);  err= 1; }
  156.       }
  157.  
  158.   Vdetach(vgmain);
  159.   DFclose(f);
  160.  
  161.   if (err) exit(-1); 
  162.   else fprintf(stderr,"1\n"); /* success */
  163.   return (1);
  164. } /* vsetlink */
  165.  
  166. /* ------------------------------------------------------- */
  167. /* 
  168. add a (new) vgroup to the file 
  169. */
  170.  
  171. int vgadd (hfile,vgname) 
  172. char * hfile;
  173. char * vgname;
  174. {
  175.   DF * f;
  176.   int ref; 
  177.   VGROUP * vg;
  178.  
  179.   if ( (f=DFopen(hfile,DFACC_ALL,0))==NULL) {
  180.       fprintf(stderr,"cannot open %s. DFerror=%d \n",hfile,DFerror); exit(0); 
  181.       }
  182.  
  183.   vg = (VGROUP*) Vattach(f,-1,"w");
  184.   if (vg==NULL) { fprintf(stderr,"cannot attach vg\n"); exit(0); }
  185.   ref = vg->oref;
  186.   Vsetname(vg,vgname);
  187.   Vdetach(vg);
  188.  
  189.   DFclose(f);
  190.   fprintf(stderr,"%d\n",ref);
  191.   return(1);
  192.  
  193. } /* vgadd */
  194.  
  195. /* ------------------------------------------------------- */
  196. /*
  197.  add a (new) vdata to the file.
  198.  Data will be ascii and will come in from stdin
  199.  according to the format (c-style).
  200. */
  201. vsadd (hfile,vsname,format)
  202. char * hfile;
  203. char * vsname;
  204. char * format;
  205.  
  206. {
  207.   int stat, i,n, nwritten;
  208.   unsigned char *buf;
  209.   char **fields;
  210.   int *type, *order, nfld;
  211.   char allfields[100];
  212.   DF * f;
  213.   VDATA *vs;
  214.   int ref,ftype;
  215.  
  216.   nfld = scanit(format,&fields,&type,&order);
  217.   if (nfld < 1) { fprintf(stderr,"bad fields\n"); exit(-1); }
  218.  
  219.   if ( (f=DFopen(hfile,DFACC_ALL,0))==NULL) {
  220.       fprintf(stderr,"cannot open %s. DFerror=%d \n",hfile,DFerror); exit(-1); 
  221.       }
  222.  
  223.   vs = (VDATA*) VSattach(f,-1,"w");
  224.   ref = vs->oref;
  225.  
  226. printf("vsadd: ref is %d\n",ref);
  227.  
  228.   allfields[0] = '\0';
  229.   for (i=0;i<nfld;i++) {
  230.       switch (type[i]) {
  231.           case 'd': ftype = LOCAL_INTTYPE;   break;
  232.           case 'f': ftype = LOCAL_FLOATTYPE; break;
  233.           case 'c': ftype = LOCAL_CHARTYPE;  break;
  234.           case 'l': ftype = LOCAL_LONGTYPE;  break;
  235.           default:  fprintf(stderr,"bad type [%c]\n",type[i]); exit(-1); break;
  236.           }
  237.       stat = VSfdefine(vs,fields[i],ftype,order[i]);
  238.       strcat(allfields,fields[i]);
  239.       strcat(allfields,",");
  240.      }
  241.  
  242.   i=strlen(allfields); allfields[i-1]='\0'; /* remove last comma */
  243.  
  244.   VSsetname(vs,vsname);
  245.   stat = VSsetfields(vs,allfields);
  246.  
  247.   nwritten = 0;
  248.   while( (n = getdata(&buf)) > 0) {
  249.      /*  printf("getdata rets n=%d .. ",n); */
  250.     stat = VSwrite(vs,buf,n,FULL_INTERLACE);
  251.      printf("+%d",stat); 
  252.      nwritten +=n;
  253.      if (stat < 1) fprintf(stderr,"Vswrite stat=%d\n",stat);
  254.      }
  255.   VSdetach(vs);
  256.  
  257.   DFclose(f);
  258.   fprintf(stderr,"%d\n",ref,nwritten);
  259.   return (1);
  260.  
  261. } /* vsadd */
  262.  
  263. /* ------------------------------------------------------------------ */
  264. /* This part of the code deals with formatting stdin input data.      */
  265. /* ------------------------------------------------------------------ */
  266.  
  267. #include <stdio.h>
  268.  
  269.  
  270. #define MAXVAR 32
  271. static char *fldptr[MAXVAR];
  272. static char flds[MAXVAR][100];
  273. static char fmts[MAXVAR]; 
  274. static int  fords[MAXVAR]; 
  275. static int  ftyp[MAXVAR]; 
  276. static int  ntotal = 0;
  277.  
  278.  
  279. /* scanf functions */
  280. static int getint  (x) int  *x; { return(scanf ("%d ",x)); }
  281. static int getfloat(x) float*x; { return(scanf ("%f ",x)); }
  282. static int getbyte (x) char *x; { return(scanf ("%c ",x)); }
  283. static int getlong (x) long *x; { return(scanf ("%ld ",x)); }
  284.  
  285. /* printf functions */
  286. static int putbyte (x) char *x; { putchar(*x);    return (1);   }
  287. static int putint  (x) int  *x; { printf("%d ",*x); return (1);}
  288. static int putfloat(x) float*x; { printf("%f ",*x); return (1);}
  289. static int putlong (x) long *x; { printf("%ld ",*x); return (1);}
  290.  
  291.  
  292. #define BUFSIZE 40000
  293.  
  294. int getdata (bp) unsigned char**bp; { 
  295.   int totalsize, nread, t,i,j,k;
  296.   unsigned char *b;
  297.   int maxrec;
  298.   int (*getfn[MAXVAR])();
  299.   int (*putfn[MAXVAR])();
  300.   int getsiz[MAXVAR];
  301.   unsigned char getbuffer[BUFSIZE];
  302.  
  303.      for(i=0;i<ntotal;i++) {
  304.         switch(fmts[i]) {
  305.            case 'd':
  306.              putfn[i] = putint; getfn[i]  = getint; getsiz[i] = sizeof(int);
  307.               break;
  308.            case 'f':
  309.               putfn[i] = putfloat; getfn[i]  = getfloat; getsiz[i] = sizeof(float);
  310.               break;
  311.            case 'c':
  312.               putfn[i]  = putbyte; getfn[i]  = getbyte; getsiz[i] = sizeof(char);
  313.               break;
  314.            case 'l':
  315.               putfn[i]  = putlong; getfn[i]  = getlong; getsiz[i] = sizeof(long);
  316.               break;
  317.            } 
  318.         }
  319.      for (totalsize=0, i=0;i<ntotal;i++) totalsize  += (fords[i]*getsiz[i]); 
  320.       maxrec = BUFSIZE/totalsize - 1; 
  321.  
  322.   /* begin reading in the ascii data from stdin */
  323.  
  324.     *bp = b = getbuffer;
  325.    for (nread=0, j=0;j<maxrec;j++,nread++) {
  326.       for(i=0;i<ntotal;i++) {
  327.            for(k=0;k<fords[i];k++) {
  328.               t = (getfn[i])(b);  
  329.             if (t==EOF)  return(nread); 
  330.               b+=getsiz[i];
  331.               }
  332.            }
  333.       }
  334.  
  335.    return (nread); /* no of recs read */
  336.  
  337. } /* getdata */
  338.  
  339.  int scanit (string,fields,type,order)
  340.  char *   string;
  341.  char *** fields;
  342.  int  **  type;
  343.  int  **  order;
  344.  {
  345.   int ns,i;
  346.   int p1,p2;
  347.   char ss[300];
  348.   int c;
  349.  
  350.  
  351.  compact(string,ss);
  352.  ns = strlen(ss); ss[ns++] = ','; 
  353.  
  354.  p1 = p2 = 0;
  355.  for(i=0;i<ns;i++) {
  356.      c = ss[i];
  357.      if(c== '=') {
  358.          p2 = i;
  359.          savfld(ss,p1,p2-1);
  360.          p1 = p2+1;
  361.          }
  362.      else if(c== ',') {
  363.          p2 = i;
  364.          savtype(ss,p1,p2-1);
  365.          p1 = p2+1;
  366.          }
  367.      }
  368.    for(i=0;i<ntotal;i++) {
  369.       fldptr[i] = flds[i];
  370.       switch(fmts[i]) {
  371.           case 'd':
  372.                 ftyp[i]   = 'd';
  373.                 break;
  374.           case 'f':
  375.                 ftyp[i]   = 'f';
  376.                 break;
  377.           case 'c':
  378.                 ftyp[i]   = 'c';
  379.                 break;
  380.           case 'l':
  381.                 ftyp[i]   = 'l';
  382.                 break;
  383.           } 
  384.       }
  385.  
  386.    *type   =  ftyp;
  387.    *order  =  fords;
  388.     *fields =  fldptr;
  389.    return (ntotal);
  390.  
  391.  } /* scanit */
  392.  
  393.  int compact (ss,dd) char *ss, *dd; {
  394.     int i,t,n = strlen(ss);
  395.     for(t=0,i=0;i<n;i++) if(ss[i]!=' ') { dd[t++] = ss[i]; }
  396.     dd[t] = '\0';
  397.     return (1);
  398.     }
  399.  
  400. /* ------------------------------------------------------------------ */
  401.  
  402. int savfld (ss,p1,p2) char *ss; int p1,p2; {
  403.   int t=p2-p1+1;
  404.   strncpy(flds[ntotal],&ss[p1],t);
  405.   flds[ntotal][t] = '\0';
  406.   return (1);
  407.  
  408.  } /* savfld */
  409.  
  410. int savtype (ss,p1,p2) char *ss; int p1,p2; {
  411.   char temp[20];
  412.   int t=p2-p1+1;
  413.   strncpy(temp,&ss[p1],p2-p1+1); temp[t] = '\0';
  414.   separate(temp,&fmts[ntotal],&fords[ntotal]);
  415.   ntotal++;
  416.   return (1);
  417.  
  418.   }
  419.  
  420. int separate(ss,fmt,num) char *ss; char *fmt; int*num; {
  421.     int i,n;
  422.     i=0;
  423.     n=strlen(ss);
  424.     while(i<n) {
  425.       if(ss[i]<'0' ||  ss[i] >'9') break; 
  426.       i++;
  427.       }
  428.     if(i>0) sscanf(ss,"%d",num); else *num= 1;
  429.     *fmt  = ss[i];
  430.     return (1);
  431.   }
  432.  
  433. /* ------------------------------------------------------------------ */
  434.