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

  1. /*
  2.  * authors.c
  3.  *
  4.  * Functions for manipulating individual authors.
  5.  *
  6.  *      Created: 23rd 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 "authors.h"
  20.  
  21.  
  22. void AuthorAddAuthor(AUTHORDB *padb, char *pszCode, AUTHORINFO *pai, char *pszDesc)
  23. /*
  24.  * Add an author to a database. If there is already an author with this code,
  25.  * replace it.
  26.  *
  27.  * AUTHORDB *padb    - the database to add the author to
  28.  * char *pszCode    - the code associated with this author
  29.  * AUTHORINFO *pai    - the author information structure
  30.  * char *pszDesc    - the author description
  31.  */
  32. {
  33.     datum key, content;
  34.  
  35.     key.dptr = pszCode;
  36.     key.dsize = strlen(pszCode) + 1;
  37.     content.dptr = (char *)pai;
  38.     content.dsize = sizeof(AUTHORINFO);
  39.     gdbm_store(padb->dbfInfo, key, content, GDBM_REPLACE);
  40.  
  41.     if (pszDesc != NULL) {
  42.         content.dptr = pszDesc;
  43.         content.dsize = strlen(pszDesc) + 1;
  44.         gdbm_store(padb->dbfDesc, key, content, GDBM_REPLACE);
  45.     }
  46. }
  47.  
  48.  
  49. int AuthorGetAuthor(AUTHORDB *padb, char *pszCode, AUTHORINFO **ppai, char **ppszDesc)
  50. /*
  51.  * Get the author information and description from a database. Space for the
  52.  * AUTHORINFO structure and the description string will be created.
  53.  *
  54.  * AUTHORDB *padb    - the database to get the author from
  55.  * char *pszCode    - the code of the author to get
  56.  * AUTHORINFO **ppai    - the author information structure (output)
  57.  * char **ppszDesc    - the author description (output)
  58.  *
  59.  * Return: 0 - the author code is not in the database
  60.  *         1 - the author has been retrieved
  61.  */
  62. {
  63.     datum key, ret;
  64.  
  65.     key.dptr = pszCode;
  66.     key.dsize = strlen(pszCode) + 1;
  67.  
  68.     if (ppai != NULL) {
  69.         ret = gdbm_fetch(padb->dbfInfo, key);
  70.         (*ppai) = (AUTHORINFO *)ret.dptr;
  71.     }
  72.  
  73.     if (ppszDesc != NULL) {
  74.         ret = gdbm_fetch(padb->dbfDesc, key);
  75.         (*ppszDesc) = ret.dptr;
  76.     }
  77.  
  78.     if (ppai != NULL)
  79.         return ((*ppai) != NULL);
  80.     else if (ppszDesc != NULL)
  81.         return ((*ppszDesc) != NULL);
  82.     else
  83.         /* nothing to do! */
  84.         return (0);
  85. }
  86.  
  87.  
  88. void AuthorDeleteAuthor(AUTHORDB *padb, char *pszCode)
  89. /*
  90.  * Delete an author from a database.
  91.  *
  92.  * AUTHORDB *padb    - the database to delete the author from
  93.  * char *pszCode    - the code of the author to delete
  94.  */
  95. {
  96.     datum key;
  97.  
  98.     key.dptr = pszCode;
  99.     key.dsize = strlen(pszCode) + 1;
  100.  
  101.     gdbm_delete(padb->dbfInfo, key);
  102.     gdbm_delete(padb->dbfDesc, key);
  103. }
  104.  
  105.  
  106. void AuthorFreeAuthor(AUTHORINFO **ppai, char **ppszDesc)
  107. /*
  108.  * Reclaim the memory used by an author and make the pointers to the
  109.  * author NULL.
  110.  *
  111.  * AUTHORINFO **ppai    - the author information structure to by reclaimed.
  112.  * char **ppszDesc    - the description string to be reclaimed.
  113.  */
  114. {
  115.     if (ppai != NULL) {
  116.         free(*ppai);
  117.         (*ppai) = NULL;
  118.     }
  119.  
  120.     if (ppszDesc != NULL) {
  121.         free(*ppszDesc);
  122.         (*ppszDesc) = NULL;
  123.     }
  124. }
  125.  
  126. int AuthorExists(AUTHORDB *padb, char *pszCode)
  127. /*
  128.  * Test for the existence of an author code in a database. Note: this function
  129.  * will return 0 if the author exists in only one of the databases (i.e. they
  130.  * are corrupt).
  131.  *
  132.  * QUOTEDB *pqdb    - the database to check in
  133.  * char *pszCode    - the code of the author to check for
  134.  *
  135.  * Returns        - 1 if the author exists
  136.  *              0 otherwise
  137.  */
  138. {
  139.     int ret;
  140.     datum key;
  141.  
  142.     key.dptr = pszCode;
  143.     key.dsize = strlen(pszCode) + 1;
  144.  
  145.     ret = gdbm_exists(padb->dbfInfo, key);
  146.     ret = ret && gdbm_exists(padb->dbfDesc, key);
  147.  
  148.     return (ret);
  149. }
  150.