home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / quot210s.zip / src / adb.c next >
C/C++ Source or Header  |  1998-12-03  |  4KB  |  167 lines

  1. /*
  2.  * adb.c
  3.  *
  4.  * Functions for managing author databases.
  5.  *
  6.  *      Created: 18th January, 1997
  7.  * Version 1.00: 18th January, 1997
  8.  * Version 2.00: 24th November, 1997
  9.  * Version 2.10: 1st December, 1998
  10.  *
  11.  * (C) 1997-1998 Nicholas Paul Sheppard
  12.  *
  13.  * This file is distributed under the GNU General Public License. See the
  14.  * file copying.txt for details.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <gnu/gdbm.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include "authors.h"
  23.  
  24.  
  25. const char szAuthorExt[] = "*.adb";
  26.  
  27.  
  28. void AuthorNullifyDB(AUTHORDB *padb)
  29. /*
  30.  * Set an author database structure to be empty.
  31.  *
  32.  * AUTHORDB *padb    - the database structure to be nullified
  33.  */
  34. {
  35.     padb->dbfInfo = NULL;
  36.     padb->dbfDesc = NULL;
  37. }
  38.  
  39.  
  40. int AuthorOpenDB(char *pszFileName, AUTHORDB *padb, int iMode)
  41. /*
  42.  * Open an author database for use.
  43.  *
  44.  * char *pszFileName    - the file name of the database to open (no extension)
  45.  * AUTHORDB *padb    - the database structure to point to the file
  46.  * int iMode        - the mode to open it in (S_IREAD, S_IWRITE, etc.)
  47.  *
  48.  * Returns:        - 1 on success
  49.  *              0 on failure
  50.  */
  51. {
  52.     char szInfoFile[AUTHOR_MAX_DBNAME + 5], szDescFile[AUTHOR_MAX_DBNAME + 5];
  53.     int iGDBM;
  54.  
  55.     if (strlen(pszFileName) > AUTHOR_MAX_DBNAME)
  56.         return (0);
  57.  
  58.     sprintf(szInfoFile, "%s.adb", pszFileName);
  59.     sprintf(szDescFile, "%s.ddb", pszFileName);
  60.  
  61.     if ((iMode & S_IWRITE) == S_IWRITE) {
  62.         iGDBM = GDBM_WRCREAT;
  63.     } else if ((iMode & S_IREAD) == S_IREAD) {
  64.         iGDBM = GDBM_READER;
  65.     } else {
  66.         /* invalid value for iMode, so stop */
  67.         return (0);
  68.     }
  69.  
  70.     if ((padb->dbfInfo = gdbm_open(szInfoFile, 512, iGDBM, iMode, NULL)) == NULL) {
  71.         AuthorNullifyDB(padb);
  72.         return (0);
  73.     }
  74.     if ((padb->dbfDesc = gdbm_open(szDescFile, 512, iGDBM, iMode, NULL)) == NULL) {
  75.         gdbm_close(padb->dbfInfo);
  76.             AuthorNullifyDB(padb);
  77.         return (0);
  78.     }
  79.  
  80.     return (1);
  81. }
  82.  
  83.  
  84. int AuthorDBStat(char *pszFileName, ADBINFO *padbinfo)
  85. /*
  86.  * Retrieve information about an author database.
  87.  *
  88.  * char *pszFileName    - the file name of the database to check (no extension)
  89.  * ADBINFO *padbinfo    - the structure to hold database information (output)
  90.  *
  91.  * Returns:        - 1 if the database exists
  92.  *              0 if it does not
  93.  */
  94. {
  95.     char szInfoFile[AUTHOR_MAX_DBNAME + 5], szDescFile[AUTHOR_MAX_DBNAME + 5];
  96.     struct stat astat, dstat;
  97.  
  98.     if (strlen(pszFileName) > AUTHOR_MAX_DBNAME)
  99.         return (0);
  100.  
  101.     sprintf(szInfoFile, "%s.adb", pszFileName);
  102.     sprintf(szDescFile, "%s.ddb", pszFileName);
  103.  
  104.     if ((stat(szInfoFile, &astat) == -1) || (stat(szDescFile, &dstat) == -1))
  105.         return (0);
  106.  
  107.     /* permissions are those that both files have only */
  108.     padbinfo->flMode = (int)(astat.st_mode & dstat.st_mode);
  109.  
  110.     /* the database size is the sum of the two files' sizes */
  111.     padbinfo->lSize = (long)(astat.st_size + dstat.st_size);
  112.  
  113.     /* the creation time is the earliest out of the two files */
  114.     padbinfo->tCreation = (astat.st_ctime < dstat.st_ctime)? astat.st_ctime : dstat.st_ctime;
  115.  
  116.     /* the modification time is latest out of the two files */
  117.     padbinfo->tModification = (astat.st_mtime > dstat.st_mtime)? astat.st_mtime : dstat.st_mtime;
  118.  
  119.     return (1);
  120. }
  121.  
  122.  
  123. void AuthorReorganiseDB(AUTHORDB *padb)
  124. /*
  125.  * Re-organise an author database.
  126.  *
  127.  * AUTHORDB *padb    - the database structure to re-organise
  128.  */
  129. {
  130.     gdbm_reorganize(padb->dbfInfo);
  131.     gdbm_reorganize(padb->dbfDesc);
  132. }
  133.  
  134.  
  135. void AuthorCloseDB(AUTHORDB *padb)
  136. /*
  137.  * Close an author database.
  138.  *
  139.  * AUTHORDB *padb    - the database structure to close
  140.  */
  141. {
  142.     gdbm_close(padb->dbfInfo);
  143.     gdbm_close(padb->dbfDesc);
  144.     AuthorNullifyDB(padb);
  145. }
  146.  
  147.  
  148. void AuthorEmptyDB(AUTHORDB *padb)
  149. /*
  150.  * Remove all authors from a database.
  151.  *
  152.  * AUTHORDB *padb    - the database to be emptied
  153.  */
  154. {
  155.     AUTHORSEARCH    as;
  156.     char        szDeleteCode[AUTHOR_MAX_CODE + 1];
  157.     int        bDone;
  158.  
  159.     bDone = !AuthorFullSearchInit(padb, &as);
  160.     while (!bDone) {
  161.         strcpy(szDeleteCode, as.szLastCode);
  162.         bDone = !AuthorFullSearchNext(&as);
  163.         AuthorDeleteAuthor(padb, szDeleteCode);
  164.     }
  165.     AuthorFullSearchEnd(&as);
  166. }
  167.