home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DB2TXT02.ZIP / DBLIST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-12-28  |  5.3 KB  |  222 lines

  1. /*     DBASE II FILE TRANSLATOR    VERSION 2.00-C
  2. **
  3. **    Date: 17 September 1984
  4. **    Author: James Bumgardner
  5. **
  6. **     USAGE:
  7. **        DBLIST datafile [>tofile] [$options]
  8. **    OPTIONS:
  9. **        S  (structure only, mutually exclusive of other options)
  10. **        P  (pack; skip deleted records, retain field size)
  11. **        L  (list only)
  12. **    This program, along with an associated .OBJ file, and DBF.DQC
  13. **      replaces all the material in DBAS2TXT.LBR.
  14. **
  15. **      REVISIONS:
  16. **    Source code now in C (compiled with smc-m80 compiler)
  17. **    Works with any DBF file from version 2
  18. **    Accepts redirection
  19. **    Lists structure ala DBASE
  20. **    Allows option of skipping deleted records (Packing)
  21. **    If deleted records are skipped, delete/eof byte is not printed in 
  22. **      order to retain field sizes
  23. **
  24. **    NOTE: The dBase II Program is copyrighted and all rights
  25. **    are reserved by Ashton-Tate.  All the routines in the DBLIST
  26. **    program were conceived independently, and any similarities
  27. **    between DBLIST, and routines in the dBase program are only
  28. **    a result of parallel purpose.
  29. **                James Bumgardner 9/17/84
  30. **                805-254-3481 Voice
  31. **                818-365-2996 RBBS
  32. */
  33.  
  34. #include "PREFIX.H"
  35.  
  36. #asm
  37. IF1
  38. .PRINTX /* DBLIST.C */
  39. ENDIF
  40. external    fprintf,strlen
  41. #endasm
  42.  
  43. int ofp;        /* file pointer */
  44. int vers,recl,rech,month,day,year,rell,relh,type,wid,dec;    
  45. char dbfile[13];
  46. char field[10];
  47. char invar[5];
  48.  
  49. main(argc,argv) 
  50.     int argc, *argv;
  51. {
  52.     int    ch,c,c1,c2,s,l,d,df;
  53.     char    *p;
  54.     if (argc == 1)
  55.         tutor();
  56.     strcopy(argv[1],dbfile);
  57. /* GET ARGUMENTS */
  58.     if (argc > 2)    {
  59.         strcopy(argv[2],invar);
  60.         if (invar[0] != '$')    {
  61.             if (argc == 4)
  62.                 strcopy(argv[3],invar);
  63.         }
  64.     }
  65.     d = FALSE;    /* PACK */
  66.     s = FALSE;    /* STRUCTURE ONLY */
  67.     l = FALSE;    /* LIST ONLY */
  68.     if (invar[0] == '$')    {
  69.         p = invar;
  70.         for (++p; *p; ++p)    {
  71.             if (*p == 'P')    d = TRUE;
  72.             else    {
  73.                 if (*p == 'S')    s = TRUE;
  74.                 else    {
  75.                     if (*p == 'L')    l = TRUE;
  76.                     else    tutor();
  77.                 }
  78.             }
  79.         }
  80.     }
  81. /* ASSUME .DBF EXTENSION */
  82.     if (instr(dbfile,".") == NULL)    {
  83.         p = dbfile;
  84.         while    (*p)
  85.             ++p;
  86.         *p++ = '.';    *p++ = 'D';
  87.         *p++ = 'B';    *p++ = 'F';    *p++ = '\0';
  88.     }
  89.     if ((ofp = fopen(dbfile,"r")) == NULL)
  90.         flerror();
  91. /* GET HEADER INFO */
  92.     fputs("\nDBASE TRANSLATOR\n",stderr);
  93.     if ((vers = cget(ofp)) == EOF)        erxit();
  94.     if (vers != 2)     badvers();
  95.     if ((recl = cget(ofp)) == EOF)        erxit();
  96.     if ((rech = cget(ofp)) == EOF)        erxit();
  97.     recl += rech * 256;
  98.     if ((month = cget(ofp)) == EOF)    erxit();
  99.     if ((day = cget(ofp)) == EOF)        erxit();
  100.     if ((year = cget(ofp)) == EOF)        erxit();
  101.     if ((rell = cget(ofp)) == EOF)        erxit;
  102.     if ((relh = cget(ofp)) == EOF)        erxit;
  103.     rell += relh * 256;
  104. /* LIST STRUCTURE */
  105.     c1 = 1;
  106.     if ((ch = cget(ofp)) == EOF)    erxit();
  107.     C = 8;
  108.     if (l == FALSE)    {
  109.         fputs("\nSTRUCTURE FOR FILE:  ",stdout);
  110.         prfname(dbfile);
  111.         fprintf(stdout,"\nNUMBER OF RECORDS:   %5d",recl); 
  112.         fprintf(stdout,"\nDATE OF LAST UPDATE: %d/%d/%d\n",month,day,year);
  113.         fputs("\nFLD       NAME      TYPE WIDTH   DEC",stdout);
  114.         while (ch != CR)    {
  115.             p = field;
  116.             *p++ = ch;
  117.             for (c2 = 10; c2; --c2)
  118.                 if ((*p++ = cget(ofp)) == EOF)    erxit();
  119.             if ((type = cget(ofp)) == EOF)    erxit();
  120.             if ((wid = cget(ofp)) == EOF)    erxit();
  121.             if ((ch = cget(ofp)) == EOF)    erxit();
  122.             if ((ch = cget(ofp)) == EOF)     erxit();
  123.             if ((dec = cget(ofp)) == EOF)    erxit();
  124.             fprintf(stdout,"\n%3d     %10s   %c    %3d    %3d",c1,field,type,wid,dec);
  125.             ++c1;
  126.             c += 16;
  127.             if ((ch = cget(ofp)) == EOF)    erxit();
  128.         }
  129.         fprintf(stdout,"\n** TOTAL **             %5d\n",rell);
  130.         if (s)     exit();
  131.     }
  132. /* SKIP TO DATA */
  133.     while (++c != 521)    {
  134.         if ((ch = cget(ofp)) == EOF)
  135.             erxit();
  136.     }
  137. /* LIST DATA */
  138.     fputs("\n\n",stderr);
  139.     while ((ch = cget(ofp)) != EOF && ch != CPMEOF)    {
  140.         c = rell;    /* RECLEN COUNT */
  141.         df = TRUE;        /* DO-NOT FLAG */
  142.         if (ch != '*' || d == FALSE)    {
  143.             fputs("\n",stdout);
  144.             if (d == FALSE)
  145.                 fputc(ch,stdout);
  146.         }
  147.         else
  148.             df = FALSE;
  149.         while (--c)    {    /* print rec */
  150.             if ((ch = cget(ofp)) == EOF)
  151.                 erxit;
  152.             if (df)    
  153.                 fputc(ch,stdout);
  154.         }
  155.     }
  156.     fputs("\n\nEnd of DBASE file.",stderr);
  157.     fclose(ofp);
  158. }    /* end of main */
  159.  
  160. /*
  161. ** copy strings (would be a macro, but this is Small-C)
  162. */
  163. strcopy(str1,str2) char *str1, *str2;    /* copy str1 into str2 */
  164. {
  165.     while ((*str2++ = *str1++) != '\0');
  166. }
  167.  
  168. prfname(str1) char *str1;
  169. {
  170.     int     i;
  171.     while ((i = *str1++) != '\0')
  172.         fputc(i,stdout);
  173. }
  174.  
  175. /*
  176. ** find any occurence of str2 in str1, and return position
  177. */
  178. instr(str1,str2) char *str1, *str2;
  179. {
  180.     int i, j, k, m;
  181.     m = strlen(str2);
  182.     if ((k = (strlen(str1) - m)) < 0)
  183.         return (NULL);
  184.     for (i = 0; i <= k; ++i) {
  185.         for (j = 0; j < m; ++j) {
  186.             if (str1[i+j] != str2[j])
  187.                 break;
  188.         }
  189.         if (j == m)
  190.             return(i);
  191.     }
  192.     return (NULL);
  193. }
  194.  
  195. tutor()
  196. {
  197.     fputs("DBASE TRANSLATOR   Version 2.00\n",stderr);
  198.     fputs("\nUSAGE:\n\tdblist datafile [>tofile] [$options]\n",stderr);
  199.     fputs("\nOPTIONS:\n\tP  (pack)\n\tS  (structure only)",stderr);
  200.     fputs("\n\tL  (list only)\n",stderr);
  201.     exit();
  202. }
  203.  
  204. erxit()
  205. {
  206.     fputs("\nEND OF FILE ENCOUNTERED\n",stderr);
  207.     exit();
  208. }
  209.  
  210. flerror()
  211. {
  212.     fputs("\nCAN'T OPEN FILE\n",stderr);
  213.     exit();
  214. }
  215.  
  216. badvers()
  217. {
  218.     fputs("\nDBASE FILE IS NOT VERSION 2\n",stderr);
  219.     exit();
  220. }
  221. /* End of DBLIST.C */
  222.