IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 5, 2007, 3:53:56 PM (19 years ago)
Author:
jhoblitt
Message:

fix pxdata.c.template

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/pxdata.c.template

    r12095 r14028  
    3232#include "pxdata.h"
    3333
    34 int test_f(const char *path);
     34static psHash *cachedData = NULL;
     35
     36static int test_f(const char *path);
    3537
    3638psString pxDataGet(const char *filename)
     
    5052    if (test_f(filepath)) {
    5153        // if we do, slurp the file
    52         psString text = psSlurpFilename(filepath);
    53         if (!text) {
     54        psString data = psSlurpFilename(filepath);
     55        if (!data) {
    5456            psError(PS_ERR_IO, false, "failed to slurp %s", filepath);
    5557            psFree(filepath);
    5658            return NULL;
    5759        }
     60        psTrace("pxdata", PS_LOG_INFO, "slupred %s", filepath);
    5861        psFree(filepath);
    5962
    60         return text;
     63        return data;
    6164    }
    6265    psFree(filepath);
    6366
    6467    // look under our share path
    65     psStringAppend(&filepath, "%s/%s", "PKGDATADIR", filename);
     68    psStringAppend(&filepath, "%s/%s", "/home/moanui/jhoblitt/jhroot/i686-pc-linux-gnu//share/ipptools", filename);
    6669
    6770    // see if we have a valid filename
    6871    if (test_f(filepath)) {
    6972        // if we do, slurp the file
    70         psString text = psSlurpFilename(filepath);
    71         if (!text) {
     73        psString data = psSlurpFilename(filepath);
     74        if (!data) {
    7275            psError(PS_ERR_IO, false, "failed to slurp %s", filepath);
    7376            psFree(filepath);
    7477            return NULL;
    7578        }
     79        psTrace("pxdata", PS_LOG_INFO, "slupred %s", filepath);
    7680        psFree(filepath);
    7781
    78         return text;
     82        return data;
    7983    }
    8084    psFree(filepath);
     
    8690}
    8791
    88 int test_f(const char *path)
     92static int test_f(const char *path)
    8993{
    9094        // copied from coreutils 6.4 test.c licenced under GPL v2
     
    9397                   && S_ISREG (stat_buf.st_mode));
    9498}
     99
     100psString pxDataGetCached(const char *filename)
     101{
     102    bool persistence = p_psMemAllocatePersistent(true);
     103
     104    // make sure the cache is initalized
     105    if (!cachedData) {
     106        cachedData = psHashAlloc(10);
     107    }
     108
     109    // look to see if we've cached this file
     110    {
     111        psString data = psHashLookup(cachedData, filename);
     112        if (data) {
     113            p_psMemAllocatePersistent(persistence);
     114            return psStringCopy(data);
     115        }
     116    }
     117
     118    // we didn't have a cached copy so we need to try to read the file
     119    psString data = pxDataGet(filename);
     120    if (!data) {
     121        p_psMemAllocatePersistent(persistence);
     122        psError(PS_ERR_IO, false, "pxDataGet(%s) failed", filename);
     123        return NULL;
     124    }
     125
     126    // store a copy of the file
     127    if (!psHashAdd(cachedData, filename, data)) {
     128        psFree(data);
     129        p_psMemAllocatePersistent(persistence);
     130        psError(PS_ERR_UNKNOWN, false, "psHashAdd() failed");
     131        return NULL;
     132    }
     133
     134    p_psMemAllocatePersistent(persistence);
     135
     136    return psStringCopy(data);
     137}
Note: See TracChangeset for help on using the changeset viewer.