home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / dbm / src / nsres.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.0 KB  |  310 lines

  1. #include "watcomfx.h"
  2.  
  3. #include "nsres.h"
  4.  
  5. #ifdef BSDI
  6. #include <stdlib.h>
  7. #else
  8. #include <malloc.h>
  9. #endif
  10.  
  11. #include <string.h>
  12.  
  13. struct RESDATABASE
  14. {
  15.     DB *hdb;
  16.     NSRESTHREADINFO *threadinfo;
  17.     char * pbuf[MAXBUFNUM];
  18. } ;
  19. typedef struct RESDATABASE *  RESHANDLE;
  20.  
  21. typedef struct STRINGDATA
  22. {
  23.     char *str;
  24.     unsigned int charsetid;
  25. } STRINGDATA;
  26.  
  27.  
  28. typedef unsigned int CHARSETTYPE;
  29. #define RES_LOCK    if (hres->threadinfo)   hres->threadinfo->fn_lock(hres->threadinfo->lock);
  30. #define RES_UNLOCK  if (hres->threadinfo)   hres->threadinfo->fn_unlock(hres->threadinfo->lock);
  31.  
  32. int GenKeyData(const char *library, int32 id, DBT *key);
  33.  
  34. /* 
  35.   Right now, the page size used for resource is same as for Navigator cache
  36.   database
  37.  */
  38. HASHINFO res_hash_info = {
  39.         32*1024,
  40.         0,
  41.         0,
  42.         0,
  43.         0,   /* 64 * 1024U  */
  44.         0};
  45.  
  46. int GenKeyData(const char *library, int32 id, DBT *key)
  47. {
  48.     char idstr[10];
  49.     static char * strdata = NULL;
  50.     int len;
  51.  
  52.     if (strdata)
  53.         free (strdata);
  54.  
  55.     if (id == 0)
  56.         idstr[0] = '\0';
  57.     else
  58.     {
  59.         sprintf(idstr, "%d", id);
  60.         /*    itoa(id, idstr, 10);  */
  61.     }
  62.  
  63.     if (library == NULL)
  64.         len = strlen(idstr) + 1;
  65.     else
  66.         len = strlen(library) + strlen(idstr) + 1;
  67.     strdata = (char *) malloc (len);
  68.     strcpy(strdata, library);
  69.     strcat(strdata, idstr);
  70.  
  71.     key->size = len;
  72.     key->data = strdata;
  73.  
  74.     return 1;
  75. }
  76.  
  77. NSRESHANDLE NSResCreateTable(const char *filename, NSRESTHREADINFO *threadinfo)
  78. {
  79.     RESHANDLE hres;
  80.     int flag;
  81.  
  82.     flag = O_RDWR | O_CREAT;
  83.  
  84.     hres = (RESHANDLE) malloc ( sizeof(struct RESDATABASE) );
  85.     memset(hres, 0, sizeof(struct RESDATABASE));
  86.  
  87.     if (threadinfo && threadinfo->lock && threadinfo->fn_lock 
  88.       && threadinfo->fn_unlock)
  89.     {
  90.         hres->threadinfo = (NSRESTHREADINFO *) malloc( sizeof(NSRESTHREADINFO) );
  91.         hres->threadinfo->lock = threadinfo->lock;
  92.         hres->threadinfo->fn_lock = threadinfo->fn_lock;
  93.         hres->threadinfo->fn_unlock = threadinfo->fn_unlock;
  94.     }
  95.  
  96.  
  97.     RES_LOCK
  98.  
  99.     hres->hdb = dbopen(filename, flag, 0644, DB_HASH, &res_hash_info);
  100.  
  101.     RES_UNLOCK
  102.  
  103.     if(!hres->hdb)
  104.         return NULL;
  105.  
  106.     return (NSRESHANDLE) hres;
  107. }
  108.  
  109. NSRESHANDLE NSResOpenTable(const char *filename, NSRESTHREADINFO *threadinfo)
  110. {
  111.     RESHANDLE hres;
  112.     int flag;
  113.  
  114.     flag = O_RDONLY;  /* only open database for reading */
  115.  
  116.     hres = (RESHANDLE) malloc ( sizeof(struct RESDATABASE) );
  117.     memset(hres, 0, sizeof(struct RESDATABASE));
  118.  
  119.     if (threadinfo && threadinfo->lock && threadinfo->fn_lock 
  120.       && threadinfo->fn_unlock)
  121.     {
  122.         hres->threadinfo = (NSRESTHREADINFO *) malloc( sizeof(NSRESTHREADINFO) );
  123.         hres->threadinfo->lock = threadinfo->lock;
  124.         hres->threadinfo->fn_lock = threadinfo->fn_lock;
  125.         hres->threadinfo->fn_unlock = threadinfo->fn_unlock;
  126.     }
  127.  
  128.  
  129.     RES_LOCK
  130.  
  131.     hres->hdb = dbopen(filename, flag, 0644, DB_HASH, &res_hash_info);
  132.  
  133.     RES_UNLOCK
  134.  
  135.     if(!hres->hdb)
  136.         return NULL;
  137.  
  138.     return (NSRESHANDLE) hres;
  139. }
  140.  
  141.  
  142.  
  143. void NSResCloseTable(NSRESHANDLE handle)
  144. {
  145.     RESHANDLE hres;
  146.     int i;
  147.  
  148.     if (handle == NULL)
  149.         return;
  150.     hres = (RESHANDLE) handle;
  151.  
  152.     RES_LOCK
  153.  
  154.     (*hres->hdb->sync)(hres->hdb, 0);
  155.     (*hres->hdb->close)(hres->hdb);
  156.  
  157.     RES_UNLOCK
  158.  
  159.     for (i = 0; i < MAXBUFNUM; i++)
  160.     {
  161.         if (hres->pbuf[i])
  162.             free (hres->pbuf[i]);
  163.     }
  164.  
  165.     if (hres->threadinfo)
  166.         free (hres->threadinfo);
  167.     free (hres);
  168. }
  169.  
  170.  
  171. char *NSResLoadString(NSRESHANDLE handle, const char * library, int32 id, 
  172.     unsigned int charsetid, char *retbuf)
  173. {
  174.     int status;
  175.     RESHANDLE hres;
  176.     DBT key, data;
  177.     if (handle == NULL)
  178.         return NULL;
  179.  
  180.     hres = (RESHANDLE) handle;
  181.     GenKeyData(library, id, &key);
  182.  
  183.     RES_LOCK
  184.  
  185.     status = (*hres->hdb->get)(hres->hdb, &key, &data, 0);
  186.  
  187.     RES_UNLOCK
  188.  
  189.     if (retbuf)
  190.     {
  191.         memcpy(retbuf, (char *)data.data + sizeof(CHARSETTYPE), data.size - sizeof(CHARSETTYPE));
  192.         return retbuf;
  193.     }
  194.     else 
  195.     {
  196.         static int WhichString = 0;
  197.         static int bFirstTime = 1;
  198.         char *szLoadedString;
  199.         int i;
  200.  
  201.         RES_LOCK
  202.  
  203.         if (bFirstTime) {
  204.             for (i = 0; i < MAXBUFNUM; i++)
  205.                 hres->pbuf[i] = (char *) malloc(MAXSTRINGLEN * sizeof(char));
  206.             bFirstTime = 0; 
  207.         } 
  208.  
  209.         szLoadedString = hres->pbuf[WhichString];
  210.         WhichString++;
  211.  
  212.         /* reset to 0, if WhichString reaches to the end */
  213.         if (WhichString == MAXBUFNUM)  
  214.             WhichString = 0;
  215.  
  216.         if (status == 0)
  217.             memcpy(szLoadedString, (char *) data.data + sizeof(CHARSETTYPE), 
  218.               data.size - sizeof(CHARSETTYPE));
  219.         else
  220.             szLoadedString[0] = 0; 
  221.  
  222.         RES_UNLOCK
  223.  
  224.         return szLoadedString;
  225.     }
  226. }
  227.  
  228. int32 NSResGetSize(NSRESHANDLE handle, const char *library, int32 id)
  229. {
  230.     int status;
  231.     RESHANDLE hres;
  232.     DBT key, data;
  233.     if (handle == NULL)
  234.         return 0;
  235.     hres = (RESHANDLE) handle;
  236.     GenKeyData(library, id, &key);
  237.  
  238.     RES_LOCK
  239.  
  240.     status = (*hres->hdb->get)(hres->hdb, &key, &data, 0);
  241.  
  242.     RES_UNLOCK
  243.  
  244.     return data.size - sizeof(CHARSETTYPE);
  245. }
  246.  
  247. int32 NSResLoadResource(NSRESHANDLE handle, const char *library, int32 id, char *retbuf)
  248. {
  249.     int status;
  250.     RESHANDLE hres;
  251.     DBT key, data;
  252.     if (handle == NULL)
  253.         return 0;
  254.     hres = (RESHANDLE) handle;
  255.     GenKeyData(library, id, &key);
  256.  
  257.     RES_LOCK
  258.  
  259.     status = (*hres->hdb->get)(hres->hdb, &key, &data, 0);
  260.  
  261.     RES_UNLOCK
  262.  
  263.     if (retbuf)
  264.     {
  265.         memcpy(retbuf, (char *)data.data + sizeof(CHARSETTYPE), data.size - sizeof(CHARSETTYPE));
  266.         return data.size;
  267.     }
  268.     else
  269.         return 0;
  270. }
  271.  
  272. int NSResAddString(NSRESHANDLE handle, const char *library, int32 id, 
  273.   const char *string, unsigned int charset)
  274. {
  275.     int status;
  276.     RESHANDLE hres;
  277.     DBT key, data;
  278.     char * recdata;
  279.  
  280.     if (handle == NULL)
  281.         return 0;
  282.     hres = (RESHANDLE) handle;
  283.  
  284.     GenKeyData(library, id, &key);
  285.  
  286.     data.size = sizeof(CHARSETTYPE) + (strlen(string) + 1) ;
  287.  
  288.     recdata = (char *) malloc(data.size) ;
  289.  
  290.     /* set charset to the first field of record data */
  291.     *((CHARSETTYPE *)recdata) = (CHARSETTYPE)charset;
  292.  
  293.     /* set data field */
  294.     memcpy(recdata+sizeof(CHARSETTYPE), string, strlen(string) + 1);
  295.  
  296.     data.data = recdata;
  297.  
  298.     RES_LOCK
  299.  
  300.     status = (*hres->hdb->put)(hres->hdb, &key, &data, 0);
  301.  
  302.  
  303.     if (recdata)
  304.         free(recdata);
  305.  
  306.     RES_UNLOCK
  307.  
  308.     return status;
  309. }
  310.