Back to index

lightning-sunbird  0.9+nobinonly
nsres.c
Go to the documentation of this file.
00001 #include "watcomfx.h"
00002 
00003 #include "nsres.h"
00004 
00005 #include <stdio.h>
00006 
00007 #include <stdlib.h>
00008 
00009 #include <string.h>
00010 
00011 struct RESDATABASE
00012 {
00013        DB *hdb;
00014        NSRESTHREADINFO *threadinfo;
00015        char * pbuf[MAXBUFNUM];
00016 } ;
00017 typedef struct RESDATABASE *  RESHANDLE;
00018 
00019 typedef struct STRINGDATA
00020 {
00021        char *str;
00022        unsigned int charsetid;
00023 } STRINGDATA;
00024 
00025 
00026 typedef unsigned int CHARSETTYPE;
00027 #define RES_LOCK     if (hres->threadinfo)   hres->threadinfo->fn_lock(hres->threadinfo->lock);
00028 #define RES_UNLOCK  if (hres->threadinfo)   hres->threadinfo->fn_unlock(hres->threadinfo->lock);
00029 
00030 int GenKeyData(const char *library, int32 id, DBT *key);
00031 
00032 /* 
00033   Right now, the page size used for resource is same as for Navigator cache
00034   database
00035  */
00036 HASHINFO res_hash_info = {
00037         32*1024,
00038         0,
00039         0,
00040         0,
00041         0,   /* 64 * 1024U  */
00042         0};
00043 
00044 int GenKeyData(const char *library, int32 id, DBT *key)
00045 {
00046        char idstr[10];
00047        static char * strdata = NULL;
00048        size_t len;
00049 
00050        if (strdata)
00051               free (strdata);
00052 
00053        if (id == 0)
00054               idstr[0] = '\0';
00055        else
00056        {
00057               sprintf(idstr, "%d", id);
00058               /*     itoa(id, idstr, 10);  */
00059        }
00060 
00061        if (library == NULL)
00062               len = strlen(idstr) + 1;
00063        else
00064               len = strlen(library) + strlen(idstr) + 1;
00065        strdata = (char *) malloc (len);
00066        strcpy(strdata, library);
00067        strcat(strdata, idstr);
00068 
00069        key->size = len;
00070        key->data = strdata;
00071 
00072        return 1;
00073 }
00074 
00075 NSRESHANDLE NSResCreateTable(const char *filename, NSRESTHREADINFO *threadinfo)
00076 {
00077        RESHANDLE hres;
00078        int flag;
00079 
00080        flag = O_RDWR | O_CREAT;
00081 
00082        hres = (RESHANDLE) calloc ( 1, sizeof(struct RESDATABASE) );
00083 
00084        if (threadinfo && threadinfo->lock && threadinfo->fn_lock 
00085          && threadinfo->fn_unlock)
00086        {
00087               hres->threadinfo = (NSRESTHREADINFO *) malloc( sizeof(NSRESTHREADINFO) );
00088               hres->threadinfo->lock = threadinfo->lock;
00089               hres->threadinfo->fn_lock = threadinfo->fn_lock;
00090               hres->threadinfo->fn_unlock = threadinfo->fn_unlock;
00091        }
00092 
00093 
00094        RES_LOCK
00095 
00096        hres->hdb = dbopen(filename, flag, 0644, DB_HASH, &res_hash_info);
00097 
00098        RES_UNLOCK
00099 
00100        if(!hres->hdb)
00101               return NULL;
00102 
00103        return (NSRESHANDLE) hres;
00104 }
00105 
00106 NSRESHANDLE NSResOpenTable(const char *filename, NSRESTHREADINFO *threadinfo)
00107 {
00108        RESHANDLE hres;
00109        int flag;
00110 
00111        flag = O_RDONLY;  /* only open database for reading */
00112 
00113        hres = (RESHANDLE) calloc ( 1, sizeof(struct RESDATABASE) );
00114 
00115        if (threadinfo && threadinfo->lock && threadinfo->fn_lock 
00116          && threadinfo->fn_unlock)
00117        {
00118               hres->threadinfo = (NSRESTHREADINFO *) malloc( sizeof(NSRESTHREADINFO) );
00119               hres->threadinfo->lock = threadinfo->lock;
00120               hres->threadinfo->fn_lock = threadinfo->fn_lock;
00121               hres->threadinfo->fn_unlock = threadinfo->fn_unlock;
00122        }
00123 
00124 
00125        RES_LOCK
00126 
00127        hres->hdb = dbopen(filename, flag, 0644, DB_HASH, &res_hash_info);
00128 
00129        RES_UNLOCK
00130 
00131        if(!hres->hdb)
00132               return NULL;
00133 
00134        return (NSRESHANDLE) hres;
00135 }
00136 
00137 
00138 
00139 void NSResCloseTable(NSRESHANDLE handle)
00140 {
00141        RESHANDLE hres;
00142        int i;
00143 
00144        if (handle == NULL)
00145               return;
00146        hres = (RESHANDLE) handle;
00147 
00148        RES_LOCK
00149 
00150        (*hres->hdb->sync)(hres->hdb, 0);
00151        (*hres->hdb->close)(hres->hdb);
00152 
00153        RES_UNLOCK
00154 
00155        for (i = 0; i < MAXBUFNUM; i++)
00156        {
00157               if (hres->pbuf[i])
00158                      free (hres->pbuf[i]);
00159        }
00160 
00161        if (hres->threadinfo)
00162               free (hres->threadinfo);
00163        free (hres);
00164 }
00165 
00166 
00167 char *NSResLoadString(NSRESHANDLE handle, const char * library, int32 id, 
00168        unsigned int charsetid, char *retbuf)
00169 {
00170        int status;
00171        RESHANDLE hres;
00172        DBT key, data;
00173        if (handle == NULL)
00174               return NULL;
00175 
00176        hres = (RESHANDLE) handle;
00177        GenKeyData(library, id, &key);
00178 
00179        RES_LOCK
00180 
00181        status = (*hres->hdb->get)(hres->hdb, &key, &data, 0);
00182 
00183        RES_UNLOCK
00184 
00185        if (retbuf)
00186        {
00187               memcpy(retbuf, (char *)data.data + sizeof(CHARSETTYPE), data.size - sizeof(CHARSETTYPE));
00188               return retbuf;
00189        }
00190        else 
00191        {
00192               static int WhichString = 0;
00193               static int bFirstTime = 1;
00194               char *szLoadedString;
00195               int i;
00196 
00197               RES_LOCK
00198 
00199               if (bFirstTime) {
00200                      for (i = 0; i < MAXBUFNUM; i++)
00201                             hres->pbuf[i] = (char *) malloc(MAXSTRINGLEN * sizeof(char));
00202                      bFirstTime = 0; 
00203               } 
00204 
00205               szLoadedString = hres->pbuf[WhichString];
00206               WhichString++;
00207 
00208               /* reset to 0, if WhichString reaches to the end */
00209               if (WhichString == MAXBUFNUM)  
00210                      WhichString = 0;
00211 
00212               if (status == 0)
00213                      memcpy(szLoadedString, (char *) data.data + sizeof(CHARSETTYPE), 
00214                        data.size - sizeof(CHARSETTYPE));
00215               else
00216                      szLoadedString[0] = 0; 
00217 
00218               RES_UNLOCK
00219 
00220               return szLoadedString;
00221        }
00222 }
00223 
00224 int32 NSResGetSize(NSRESHANDLE handle, const char *library, int32 id)
00225 {
00226        int status;
00227        RESHANDLE hres;
00228        DBT key, data;
00229        if (handle == NULL)
00230               return 0;
00231        hres = (RESHANDLE) handle;
00232        GenKeyData(library, id, &key);
00233 
00234        RES_LOCK
00235 
00236        status = (*hres->hdb->get)(hres->hdb, &key, &data, 0);
00237 
00238        RES_UNLOCK
00239 
00240        return data.size - sizeof(CHARSETTYPE);
00241 }
00242 
00243 int32 NSResLoadResource(NSRESHANDLE handle, const char *library, int32 id, char *retbuf)
00244 {
00245        int status;
00246        RESHANDLE hres;
00247        DBT key, data;
00248        if (handle == NULL)
00249               return 0;
00250        hres = (RESHANDLE) handle;
00251        GenKeyData(library, id, &key);
00252 
00253        RES_LOCK
00254 
00255        status = (*hres->hdb->get)(hres->hdb, &key, &data, 0);
00256 
00257        RES_UNLOCK
00258 
00259        if (retbuf)
00260        {
00261               memcpy(retbuf, (char *)data.data + sizeof(CHARSETTYPE), data.size - sizeof(CHARSETTYPE));
00262               return data.size;
00263        }
00264        else
00265               return 0;
00266 }
00267 
00268 int NSResAddString(NSRESHANDLE handle, const char *library, int32 id, 
00269   const char *string, unsigned int charset)
00270 {
00271        int status;
00272        RESHANDLE hres;
00273        DBT key, data;
00274        char * recdata;
00275 
00276        if (handle == NULL)
00277               return 0;
00278        hres = (RESHANDLE) handle;
00279 
00280        GenKeyData(library, id, &key);
00281 
00282        data.size = sizeof(CHARSETTYPE) + (strlen(string) + 1) ;
00283 
00284        recdata = (char *) malloc(data.size) ;
00285 
00286        /* set charset to the first field of record data */
00287        *((CHARSETTYPE *)recdata) = (CHARSETTYPE)charset;
00288 
00289        /* set data field */
00290        memcpy(recdata+sizeof(CHARSETTYPE), string, strlen(string) + 1);
00291 
00292        data.data = recdata;
00293 
00294        RES_LOCK
00295 
00296        status = (*hres->hdb->put)(hres->hdb, &key, &data, 0);
00297 
00298 
00299        if (recdata)
00300               free(recdata);
00301 
00302        RES_UNLOCK
00303 
00304        return status;
00305 }