home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / DBAPI.ZIP / DBFOPEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-25  |  2.5 KB  |  117 lines

  1. #include <fcntl.h>
  2. #include <io.h>
  3. #include <stdlib.h>
  4. #include <mem.h>
  5. #include <errno.h>
  6. #include <dos.h>
  7. #include "dbapi.h"
  8.  
  9. /*man******************************************************************
  10. NAME
  11.     dbfopen
  12.  
  13. SYNOPSIS
  14.     #include "dbapi.h"
  15.  
  16.     DBFFILE *dbfopen(char *fn)
  17.  
  18. DESCRIPTION
  19.     dbfopen opens an existing dbase file, automatically handling
  20.     both version2 and version3 files.
  21.  
  22. DIAGNOSTICS
  23.     pointer to opened file is returned if everything's ok, NULL
  24.     if something goes wrong.
  25. **********************************************************************/
  26. DBFFILE *dbfopen(char *fn)
  27. {
  28. int fh;
  29. FILE *fp;
  30. DBFFILE *dbf;
  31. DBFFIELD *fld,*ofld=NULL;
  32. char c;
  33.     fh=_open(fn,O_BINARY|O_RDWR|O_DENYNONE);
  34.     if (fh) {
  35.         fp=fdopen(fh,"r+b");
  36.         if (fp) {
  37.             dbf=malloc(sizeof(DBFFILE));
  38.             if (dbf) {
  39.                 setmem(dbf,sizeof(DBFFILE),0);
  40.                 dbf->fp=fp;
  41.                 dbf->ver=getc(fp);
  42.                 switch (dbf->ver) {
  43.                     default:
  44.                         fread(&dbf->update,3,1,fp);
  45.                         fread(&dbf->nrecords,4,1,fp);
  46.                         fseek(fp,2,SEEK_CUR);
  47.                         fh=fread(&dbf->reclen,2,1,fp);
  48.                         if (fh)
  49.                             fseek(fp,20,SEEK_CUR);
  50.                         break;
  51.                     case 2:
  52.                         fread(&dbf->nrecords,2,1,fp);
  53.                         fread(&dbf->update,3,1,fp);
  54.                         fh=fread(&dbf->reclen,2,1,fp);
  55.                         break;
  56.                 }
  57.                 if (fh) {
  58.                 /* read descriptors now... */
  59.                     while (1) {
  60.                         if (fread(&c,1,1,fp)) {
  61.                             if (c!=0x0d) {
  62.                                 fld=malloc(sizeof(DBFFIELD));
  63.                                 if (fld) {
  64.                                     dbf->nfields++;
  65.                                     setmem(fld,sizeof(DBFFIELD),0);
  66.                                     *fld->f.name=c;
  67.                                     fread(&fld->f.name[1],10,1,fp);
  68.                                     fread(&fld->f.type,1,1,fp);
  69.                                     if (dbf->ver!=2)
  70.                                         fseek(fp,4,SEEK_CUR);
  71.                                     fread(&fld->f.len,1,1,fp);
  72.                                     if (dbf->ver==2)
  73.                                         fseek(fp,2,SEEK_CUR);
  74.                                     fread(&fld->f.deccount,1,1,fp);
  75.                                     if (dbf->ver!=2)
  76.                                         fseek(fp,14,SEEK_CUR);
  77.                                     if (ofld) {
  78.                                         ofld->next=fld;
  79.                                         ofld=fld;
  80.                                     }
  81.                                     else {
  82.                                         ofld=dbf->fields=fld;
  83.                                     }
  84.                                 }
  85.                                 else {
  86.                                     dbfclose(dbf);
  87.                                     errno=ENOMEM;
  88.                                     return NULL;
  89.                                 }
  90.                             }
  91.                             else {
  92.                                 dbf->hdrlen=(word)ftell(fp);
  93.                                 return dbf;
  94.                             }
  95.                         }
  96.                         else {
  97.                             dbfclose(dbf);
  98.                             errno=EBADF;
  99.                             return NULL;
  100.                         }
  101.                     }
  102.                 }
  103.                 else {
  104.                     dbfclose(dbf);
  105.                     errno=EBADF;
  106.                     return NULL;
  107.                 }
  108.             }
  109.             else
  110.                 fclose(fp);
  111.         }
  112.         else
  113.             _close(fh);
  114.     }
  115.     return NULL;
  116. }
  117.