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

  1. /*
  2.  * quotes.c
  3.  *
  4.  * Functions for manipulating individual quotes.
  5.  *
  6.  *      Created: 22nd January, 1997
  7.  * Version 1.00: 19th March, 1997
  8.  * Version 2.00: 21st November, 1997
  9.  *
  10.  * (C) 1997 Nicholas Paul Sheppard
  11.  *
  12.  * This file is distributed under the GNU General Public License. See the
  13.  * file copying.txt for details.
  14.  */
  15.  
  16. #include <gnu/gdbm.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include "quotes.h"
  20.  
  21.  
  22. void QuoteAddQuote(QUOTEDB *pqdb, char *pszCode, QUOTEINFO *pqi, char *pszText)
  23. /*
  24.  * Add a quote to a database. If there is already a quote with this code,
  25.  * replace it.
  26.  *
  27.  * QUOTEDB *pqdb    - the database to add the quote to
  28.  * char *pszCode    - the code associated with this quote
  29.  * QUOTEINFO *pqi    - the quote information structure
  30.  * char *pszText    - the quote text
  31.  */
  32. {
  33.     datum key, content;
  34.  
  35.     key.dptr = pszCode;
  36.     key.dsize = strlen(pszCode) + 1;
  37.     content.dptr = (char *)pqi;
  38.     content.dsize = sizeof(QUOTEINFO);
  39.     gdbm_store(pqdb->dbfInfo, key, content, GDBM_REPLACE);
  40.  
  41.     if (pszText != NULL) {
  42.         content.dptr = pszText;
  43.         content.dsize = strlen(pszText) + 1;
  44.         gdbm_store(pqdb->dbfText, key, content, GDBM_REPLACE);
  45.     }
  46. }
  47.  
  48.  
  49. int QuoteGetQuote(QUOTEDB *pqdb, char *pszCode, QUOTEINFO **ppqi, char **ppszText)
  50. /*
  51.  * Get the quote information and text from a database. Space for the the
  52.  * QUOTEINFO structure and the text string will be created.
  53.  *
  54.  * QUOTEDB *pqdb    - the database to get the quote from
  55.  * char *pszCode    - the code of the quote to get
  56.  * QUOTEINFO **ppqi    - the quote information structure (output)
  57.  * char **ppszText    - the quote text (output)
  58.  *
  59.  * Return: 0 - the quote code is not in the database
  60.  *         1 - the quote has been retrieved
  61.  */
  62. {
  63.     datum key, ret;
  64.  
  65.     key.dptr = pszCode;
  66.     key.dsize = strlen(pszCode) + 1;
  67.  
  68.     if (ppqi != NULL) {
  69.         ret = gdbm_fetch(pqdb->dbfInfo, key);
  70.         (*ppqi) = (QUOTEINFO *)ret.dptr;
  71.     }
  72.  
  73.     if (ppszText != NULL) {
  74.         ret = gdbm_fetch(pqdb->dbfText, key);
  75.         (*ppszText) = ret.dptr;
  76.     }
  77.  
  78.     if (ppqi != NULL)
  79.         return ((*ppqi) != NULL);
  80.     else if (ppszText != NULL)
  81.         return ((*ppszText) != NULL);
  82.     else
  83.         /* nothing to do! */
  84.         return (0);
  85. }
  86.  
  87.  
  88. void QuoteDeleteQuote(QUOTEDB *pqdb, char *pszCode)
  89. /*
  90.  * Delete a quote from a database.
  91.  *
  92.  * QUOTEDB *pqdb    - the database to delete the quote from
  93.  * char *pszCode    - the code of the quote to delete
  94.  */
  95. {
  96.     datum key;
  97.  
  98.     key.dptr = pszCode;
  99.     key.dsize = strlen(pszCode) + 1;
  100.  
  101.     gdbm_delete(pqdb->dbfInfo, key);
  102.     gdbm_delete(pqdb->dbfText, key);
  103. }
  104.  
  105.  
  106. void QuoteFreeQuote(QUOTEINFO **ppqi, char **ppszText)
  107. /*
  108.  * Reclaim the memory used by a quote and make the pointers to the
  109.  * quote NULL.
  110.  *
  111.  * QUOTEINFO **ppqi    - the quote information structure to by reclaimed.
  112.  * char **ppszText    - the text string to be reclaimed.
  113.  */
  114. {
  115.     if (ppqi != NULL) {
  116.         free(*ppqi);
  117.         (*ppqi) = NULL;
  118.     }
  119.  
  120.     if (ppszText != NULL) {
  121.         free(*ppszText);
  122.         (*ppszText) = NULL;
  123.     }
  124. }
  125.  
  126.  
  127. int QuoteExists(QUOTEDB *pqdb, char *pszCode)
  128. /*
  129.  * Test for the existence of a quote code in a database. Note: this function
  130.  * will return 0 if the quote exists in only one of the databases (i.e. they
  131.  * are corrupt).
  132.  *
  133.  * QUOTEDB *pqdb    - the database to check in
  134.  * char *pszCode    - the code of the quote to check for
  135.  *
  136.  * Returns        - 1 if the quote exists
  137.  *              0 otherwise
  138.  */
  139. {
  140.     int ret;
  141.     datum key;
  142.  
  143.     key.dptr = pszCode;
  144.     key.dsize = strlen(pszCode) + 1;
  145.  
  146.     ret = gdbm_exists(pqdb->dbfInfo, key);
  147.     ret = ret && gdbm_exists(pqdb->dbfText, key);
  148.  
  149.     return (ret);
  150. }
  151.