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

  1. #include <stdlib.h>
  2. #include <mem.h>
  3. #include <ctype.h>
  4. #include "dbapi.h"
  5.  
  6. /*man******************************************************************
  7. NAME
  8.     dbaddfld
  9.  
  10. SYNOPSIS
  11.     #include "dbapi.h"
  12.  
  13.     DBFFIELD *dbaddfld(DBFFIELD *base,char *name,
  14.             int type,word len,int dec)
  15.  
  16. DESCRIPTION
  17.     dbaddfld adds an entry into field list which can be used
  18.     to create dbase file. base is a pointer to first entry in
  19.     list, name is field name (max 10 chars) type is field type
  20.     identifier ( 'C','N','L','D' or 'M') len is a field length
  21.     and dec is number of decimal places for 'N' type.
  22.     the field definitions will be used for newly created
  23.     dbase file and will be automatically disposed when the
  24.     file is closed.
  25.  
  26. DIAGNOSTICS
  27.     returns pointer to newly created field definition if successful
  28.     or NULL if runs out of memory (already existing entries, if any,
  29.     are _not_ disposed)
  30.  
  31. EXAMPLE
  32.     #include "dbapi.h"
  33.  
  34.     DBFFILE *createxxx(void)
  35.     {
  36.     DBFFILE *dbf=NULL;
  37.     DBFFIELD *nfd;
  38.         nfd=dbaddfld(NULL,"ACCOUNT",'C',6,0);
  39.         if (nfd) {
  40.             dbaddfld(nfd,"CLIENT",'C',52,0);
  41.             dbaddfld(nfd,"PHONE",'C',20,0);
  42.             dbaddfld(nfd,"REGNUM",'C',11,0);
  43.             unlink("CLIENTS.DBF");
  44.             dbf=dbfcreate("CLIENTS.DBF",3,nfd);
  45.         }
  46.         return dbf;
  47.     }
  48.     
  49. SEE ALSO
  50.     dbfcreate
  51. **********************************************************************/
  52. DBFFIELD *dbaddfld(DBFFIELD *base,char *name,int type,word len,int dec)
  53. {
  54. int i;
  55. DBFFIELD *fld;
  56.     fld=malloc(sizeof(DBFFIELD));
  57.     if (fld) {
  58.         setmem(fld,sizeof(DBFFIELD),0);
  59.         fld->f.type=(byte)type;
  60.         fld->f.deccount=(byte)dec;
  61.         fld->f.len=len;
  62.         for (i=0;i<10;i++) {
  63.             if (name[i]=='\0')
  64.                 break;
  65.             fld->f.name[i]=toupper(name[i]);
  66.         }
  67.         if (base) {
  68.             while (base->next)
  69.                 base=base->next;
  70.             base->next=fld;
  71.         }
  72.         return fld;
  73.     }
  74.     return NULL;
  75. }
  76.  
  77. /*man*************************************************************
  78. NAME
  79.     dbkillflds
  80.  
  81. SYNOPSIS
  82.     #include "dbapi.h"
  83.  
  84.     void dbkillflds(DBFFIELD *f)
  85.  
  86. DESCRIPTION
  87.     disposes the whole field list built with dbaddfld. normally
  88.     not needed as dbfclose disposes the list automatically but
  89.     could be used if dbaddfld fails.
  90.  
  91. SEE ALSO
  92.     dbaddfld
  93.  
  94. *****************************************************************/
  95. void dbkillflds(DBFFIELD *f)
  96. {
  97.     if (f) {
  98.         if (f->next)
  99.             dbkillflds(f->next);
  100.         free(f);
  101.     }
  102. }
  103.