home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / cpm68k / sdb.lbr / IEX.CQ / IEX.C
Text File  |  1986-10-31  |  7KB  |  281 lines

  1. /* SDB - import/export command routines */
  2.  
  3. #include <stdio.h>
  4. #include "sdbio.h"
  5.  
  6. extern int dbv_token;
  7. extern char dbv_tstring[];
  8. extern int dbv_tvalue;
  9.  
  10. #ifdef CPM68K
  11. struct scan *db_ropen();
  12. #endif
  13.  
  14. /* db_import - import tuples from a file */
  15. int db_import(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  16.   char *fmt;
  17. {
  18.     struct scan *sptr;
  19.     struct attribute *aptr;
  20.     char fname[STRINGMAX+1],avalue[STRINGMAX+1];
  21.     int tcnt,astart,i,eofile;
  22.     FILE *fp;
  23.  
  24.     /* check for a command line */
  25.     if (fmt != NULL)
  26.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  27.  
  28.     /* checks for "<filename> into <relation-name>" */
  29.     if (db_ntoken() == ID)
  30.         strcat(dbv_tstring,".dat");
  31.     else if (dbv_token != STRING)
  32.         return (db_ferror(SYNTAX));
  33.     strcpy(fname,dbv_tstring);
  34.     if (db_ntoken() != INTO)
  35.         return (db_ferror(SYNTAX));
  36.     if (db_ntoken() != ID)
  37.         return (db_ferror(SYNTAX));
  38.  
  39.     /* open the relation */
  40.     if ((sptr = db_ropen(dbv_tstring)) == NULL)
  41.         return (FALSE);
  42.  
  43.     /* open the input file */
  44.     if ((fp = fopen(fname,"r")) == NULL)
  45.         return (db_ferror(INPFNF));
  46.  
  47.     /* import tuples */
  48.     eofile = FALSE;
  49.     for (tcnt = 0; ; tcnt++) {
  50.  
  51.         /* get attribute values */
  52.         astart = 1;
  53.         for (i = 0; i < NATTRS; i++) {
  54.  
  55.             /* get a pointer to the current attribute */
  56.             aptr = &sptr->sc_relation->rl_header.hd_attrs[i];
  57.  
  58.             /* check for the last attribute */
  59.             if (aptr->at_name[0] == 0)
  60.                 break;
  61.  
  62.             /* input the tuple */
  63.             if (fgets(avalue,STRINGMAX,fp) == 0) {
  64.                 eofile = TRUE;
  65.                 break;
  66.             }
  67.             avalue[strlen(avalue)-1] = EOS;
  68.  
  69.             /* store the attribute value */
  70.             db_aput(aptr,&sptr->sc_tuple[astart],avalue);
  71.  
  72.             /* update the attribute start */
  73.             astart += aptr->at_size;
  74.         }
  75.  
  76.         /* store the new tuple */
  77.         if (!eofile) {
  78.             if (!db_rstore(sptr)) {
  79.                 db_rclose(sptr);
  80.                 return (FALSE);
  81.             }
  82.         }
  83.         else
  84.             break;
  85.     }
  86.  
  87.     /* close the relation */
  88.     db_rclose(sptr);
  89.  
  90.     /* close the input file */
  91.     fclose(fp);
  92.  
  93.     /* check number of tuples imported */
  94.     if (tcnt != 0) {
  95.  
  96.         /* print tuple count */
  97.         printf("[ %d imported ]\n",tcnt);
  98.     }
  99.     else
  100.         printf("[ none imported ]\n");
  101.  
  102.     /* return successfully */
  103.     return (TRUE);
  104. }
  105.  
  106. /* db_export - export tuples to a file */
  107. int db_export(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  108.   char *fmt;
  109. {
  110.     struct scan *sptr;
  111.     struct attribute *aptr;
  112.     char rname[STRINGMAX+1],avalue[STRINGMAX+1];
  113.     int tcnt,astart,i;
  114.     FILE *fp;
  115.  
  116.     /* check for a command line */
  117.     if (fmt != NULL)
  118.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  119.  
  120.     /* checks for "<relation-name> [ into <filename> ]" */
  121.     if (db_ntoken() != ID)
  122.         return (db_ferror(SYNTAX));
  123.     strcpy(rname,dbv_tstring);
  124.     if (!db_to(&fp,".dat"))
  125.         return (FALSE);
  126.  
  127.     /* open the relation */
  128.     if ((sptr = db_ropen(rname)) == NULL)
  129.         return (FALSE);
  130.  
  131.     /* export tuples */
  132.     for (tcnt = 0; db_rfetch(sptr); tcnt++) {
  133.  
  134.         /* get attribute values */
  135.         astart = 1;
  136.         for (i = 0; i < NATTRS; i++) {
  137.  
  138.             /* get a pointer to the current attribute */
  139.             aptr = &sptr->sc_relation->rl_header.hd_attrs[i];
  140.  
  141.             /* check for the last attribute */
  142.             if (aptr->at_name[0] == 0)
  143.                 break;
  144.  
  145.             /* get the attribute value */
  146.             db_aget(aptr,&sptr->sc_tuple[astart],avalue);
  147.  
  148.             /* output the tuple */
  149.             fprintf(fp,"%s\n",avalue);
  150.  
  151.             /* update the attribute start */
  152.             astart += aptr->at_size;
  153.         }
  154.     }
  155.  
  156.     /* close the relation */
  157.     db_rclose(sptr);
  158.  
  159.     /* close the output file */
  160.     if (fp != stdout)
  161.         fclose(fp);
  162.  
  163.     /* check number of tuples exported */
  164.     if (tcnt != 0) {
  165.  
  166.         /* print tuple count */
  167.         printf("[ %d exported ]\n",tcnt);
  168.     }
  169.     else
  170.         printf("[ none exported ]\n");
  171.  
  172.     /* return successfully */
  173.     return (TRUE);
  174. }
  175.  
  176. /* db_squeeze - squeeze deleted tuples from a relation file */
  177. int db_squeeze(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  178.   char *fmt;
  179. {
  180.     struct scan *sptr;
  181.  
  182.     /* check for a command line */
  183.     if (fmt != NULL)
  184.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  185.  
  186.     /* checks for "<relation-name>" */
  187.     if (db_ntoken() != ID)
  188.         return (db_ferror(SYNTAX));
  189.  
  190.     /* open the relation */
  191.     if ((sptr = db_ropen(dbv_tstring)) == NULL)
  192.         return (FALSE);
  193.  
  194.     /* compress the relation file */
  195.     if (!db_rcompress(sptr)) {
  196.         db_rclose(sptr);
  197.         return (FALSE);
  198.     }
  199.  
  200.     /* close the relation */
  201.     db_rclose(sptr);
  202.  
  203.     /* return successfully */
  204.     return (TRUE);
  205. }
  206.  
  207. /* db_extract - extract a relation definition */
  208. int db_extract(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  209.   char *fmt;
  210. {
  211.     struct scan *sptr;
  212.     struct attribute *aptr;
  213.     char rname[STRINGMAX+1],aname[ANSIZE+1],*atype;
  214.     int i;
  215.     FILE *fp;
  216.  
  217.     /* check for a command line */
  218.     if (fmt != NULL)
  219.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  220.  
  221.     /* checks for "<relation-name> [ into <filename> ]" */
  222.     if (db_ntoken() != ID)
  223.         return (db_ferror(SYNTAX));
  224.     strcpy(rname,dbv_tstring);
  225.     if (!db_to(&fp,".def"))
  226.         return (FALSE);
  227.  
  228.     /* open the relation */
  229.     if ((sptr = db_ropen(rname)) == NULL)
  230.         return (FALSE);
  231.  
  232.     /* output the relation definition */
  233.     fprintf(fp,"create %s (\n",rname);
  234.  
  235.     /* get attribute values */
  236.     for (i = 0; i < NATTRS; i++) {
  237.  
  238.         /* get a pointer to the current attribute */
  239.         aptr = &sptr->sc_relation->rl_header.hd_attrs[i];
  240.  
  241.         /* check for the last attribute */
  242.         if (aptr->at_name[0] == 0)
  243.             break;
  244.  
  245.         /* get the attribute name */
  246.         strncpy(aname,aptr->at_name,ANSIZE); aname[ANSIZE] = 0;
  247.  
  248.         /* determine the attribute type */
  249.         switch (aptr->at_type) {
  250.         case TCHAR:
  251.                 atype = "char";
  252.                 break;
  253.         case TNUM:
  254.                 atype = "num";
  255.                 break;
  256.         default:
  257.                 atype = "<error>";
  258.                 break;
  259.         }
  260.  
  261.         /* output the attribute definition */
  262.         if (strlen(aname) < 8)
  263.             fprintf(fp,"\t%s\t\t%s\t%d\n",aname,atype,aptr->at_size);
  264.         else
  265.             fprintf(fp,"\t%s\t%s\t%d\n",aname,atype,aptr->at_size);
  266.     }
  267.  
  268.     /* output the relation size */
  269.     fprintf(fp,") %d\n",sptr->sc_relation->rl_tmax);
  270.  
  271.     /* close the relation */
  272.     db_rclose(sptr);
  273.  
  274.     /* close the output file */
  275.     if (fp != stdout)
  276.         fclose(fp);
  277.  
  278.     /* return successfully */
  279.     return (TRUE);
  280. }
  281.