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

  1. /*
  2.  * qdb.c
  3.  *
  4.  * Functions for managing quote 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 "quotes.h"
  23.  
  24.  
  25. const char szQuoteExt[] = "*.qdb";
  26.  
  27. void QuoteNullifyDB(QUOTEDB *pqdb)
  28. /*
  29.  * Set an quote database structure to be empty.
  30.  *
  31.  * QUOTEDB *qdb    - the database structure to be nullified
  32.  */
  33. {
  34.     pqdb->dbfInfo = NULL;
  35.     pqdb->dbfText = NULL;
  36. }
  37.  
  38.  
  39. int QuoteOpenDB(char *pszFileName, QUOTEDB *pqdb, int iMode)
  40. /*
  41.  * Open an quote database for use.
  42.  *
  43.  * char *pszFileName    - the file name of the database to open (no extension)
  44.  * QUOTEDB *pqdb    - the database structure to point to the file
  45.  * int iMode        - the mode to open it in (S_IREAD, S_IWRITE, etc.)
  46.  *
  47.  * Returns:        - 1 on success
  48.  *              0 on failure
  49.  */
  50. {
  51.     char szInfoFile[QUOTE_MAX_DBNAME + 5], szTextFile[QUOTE_MAX_DBNAME + 5];
  52.     int iGDBM;
  53.  
  54.     if (strlen(pszFileName) > QUOTE_MAX_DBNAME)
  55.         return (0);
  56.  
  57.     sprintf(szInfoFile, "%s.qdb", pszFileName);
  58.     sprintf(szTextFile, "%s.tdb", pszFileName);
  59.  
  60.     if ((iMode & S_IWRITE) == S_IWRITE) {
  61.         iGDBM = GDBM_WRCREAT;
  62.     } else if ((iMode & S_IREAD) == S_IREAD) {
  63.         iGDBM = GDBM_READER;
  64.     } else {
  65.         /* invalid value for iMode, so stop */
  66.         return (0);
  67.     }
  68.  
  69.     if ((pqdb->dbfInfo = gdbm_open(szInfoFile, 512, iGDBM, iMode, NULL)) == NULL) {
  70.         QuoteNullifyDB(pqdb);
  71.         return (0);
  72.     }
  73.     if ((pqdb->dbfText = gdbm_open(szTextFile, 512, iGDBM, iMode, NULL)) == NULL) {
  74.         gdbm_close(pqdb->dbfInfo);
  75.         QuoteNullifyDB(pqdb);
  76.         return (0);
  77.     }
  78.  
  79.     return (1);
  80. }
  81.  
  82.  
  83. int QuoteDBStat(char *pszFileName, QDBINFO *pqdbinfo)
  84. /*
  85.  * Retrieve information about a quote database.
  86.  *
  87.  * char *pszFileName    - the file name of the database to check (no extension)
  88.  * QDBINFO *pqdbinfo    - the structure to hold database information (output)
  89.  *
  90.  * Returns:        - 1 if the database exists
  91.  *              0 if it does not
  92.  */
  93. {
  94.     char szInfoFile[QUOTE_MAX_DBNAME + 5], szTextFile[QUOTE_MAX_DBNAME + 5];
  95.     struct stat qstat, tstat;
  96.  
  97.     if (strlen(pszFileName) > QUOTE_MAX_DBNAME)
  98.         return (0);
  99.  
  100.     sprintf(szInfoFile, "%s.qdb", pszFileName);
  101.     sprintf(szTextFile, "%s.tdb", pszFileName);
  102.  
  103.     if ((stat(szInfoFile, &qstat) == -1) || (stat(szTextFile, &tstat) == -1))
  104.         return (0);
  105.  
  106.     /* permissions are those that both files have only */
  107.     pqdbinfo->flMode = (int)(qstat.st_mode & tstat.st_mode);
  108.  
  109.     /* the database size is the sum of the two files' sizes */
  110.     pqdbinfo->lSize = (long)(qstat.st_size + tstat.st_size);
  111.  
  112.     /* the creation time is the earliest out of the two files */
  113.     pqdbinfo->tCreation = (qstat.st_ctime < tstat.st_ctime)? qstat.st_ctime : tstat.st_ctime;
  114.  
  115.     /* the modification time is latest out of the two files */
  116.     pqdbinfo->tModification = (qstat.st_mtime > tstat.st_mtime)? qstat.st_mtime : tstat.st_mtime;
  117.  
  118.     return (1);
  119. }
  120.  
  121.  
  122. void QuoteReorganiseDB(QUOTEDB *pqdb)
  123. /*
  124.  * Re-organise an quote database.
  125.  *
  126.  * QUOTEDB *pqdb    - the database structure to re-organise
  127.  */
  128. {
  129.     gdbm_reorganize(pqdb->dbfInfo);
  130.     gdbm_reorganize(pqdb->dbfText);
  131. }
  132.  
  133.  
  134. void QuoteCloseDB(QUOTEDB *pqdb)
  135. /*
  136.  * Close an quote database.
  137.  *
  138.  * QUOTEDB *pqdb    - the database structure to close
  139.  */
  140. {
  141.     gdbm_close(pqdb->dbfInfo);
  142.     gdbm_close(pqdb->dbfText);
  143.     QuoteNullifyDB(pqdb);
  144. }
  145.  
  146.  
  147. void QuoteEmptyDB(QUOTEDB *pqdb)
  148. /*
  149.  * Remove all quotes from a database.
  150.  *
  151.  * QUOTEDB *padb    - the database to be emptied
  152.  */
  153. {
  154.     QUOTESEARCH    qs;
  155.     char        szDeleteCode[QUOTE_MAX_CODE + 1];
  156.     int        bDone;
  157.  
  158.     bDone = !QuoteFullSearchInit(pqdb, &qs);
  159.     while (!bDone) {
  160.         strcpy(szDeleteCode, qs.szLastCode);
  161.         bDone = !QuoteFullSearchNext(&qs);
  162.         QuoteDeleteQuote(pqdb, szDeleteCode);
  163.     }
  164.     QuoteFullSearchEnd(&qs);
  165. }
  166.