2012-12-14 16:16:09 +00:00
|
|
|
#include "cache.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "../tools.h"
|
|
|
|
|
|
|
|
struct CacheFile
|
|
|
|
{
|
2013-06-16 17:06:10 +00:00
|
|
|
char* datapath;
|
2012-12-14 16:16:09 +00:00
|
|
|
char* filepath;
|
|
|
|
};
|
|
|
|
|
2013-03-09 09:54:31 +00:00
|
|
|
CacheFile* cacheFileCreateAccessor(const char* module, const char* ext, const char* tag1, int tag2, int tag3, int tag4, int tag5, int tag6)
|
2012-12-14 16:16:09 +00:00
|
|
|
{
|
|
|
|
CacheFile* result;
|
|
|
|
|
|
|
|
result = (CacheFile*)malloc(sizeof(CacheFile));
|
2013-06-16 17:06:10 +00:00
|
|
|
result->datapath = malloc(sizeof(char) * 501);
|
2012-12-14 16:16:09 +00:00
|
|
|
result->filepath = malloc(sizeof(char) * 501);
|
|
|
|
|
2013-06-16 17:06:10 +00:00
|
|
|
snprintf(result->datapath, 500, "/usr/share/paysages3d/%s-%s-%d-%d-%d-%d-%d.%s", module, tag1, tag2, tag3, tag4, tag5, tag6, ext);
|
2013-03-09 09:54:31 +00:00
|
|
|
snprintf(result->filepath, 500, "./cache/%s-%s-%d-%d-%d-%d-%d.%s", module, tag1, tag2, tag3, tag4, tag5, tag6, ext);
|
2012-12-14 16:16:09 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cacheFileDeleteAccessor(CacheFile* cache)
|
|
|
|
{
|
2013-06-16 17:06:10 +00:00
|
|
|
free(cache->datapath);
|
2012-12-14 16:16:09 +00:00
|
|
|
free(cache->filepath);
|
|
|
|
free(cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cacheFileIsReadable(CacheFile* cache)
|
|
|
|
{
|
|
|
|
FILE* f = fopen(cache->filepath, "rb");
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-16 17:06:10 +00:00
|
|
|
FILE* f = fopen(cache->datapath, "rb");
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-14 16:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int cacheFileIsWritable(CacheFile* cache)
|
|
|
|
{
|
|
|
|
UNUSED(cache);
|
|
|
|
|
|
|
|
FILE* f = fopen("./cache/.test", "wb");
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cacheFileGetPath(CacheFile* cache)
|
|
|
|
{
|
2013-06-20 10:32:50 +00:00
|
|
|
FILE* f = fopen(cache->datapath, "rb");
|
2013-06-16 17:06:10 +00:00
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
2013-06-20 10:32:50 +00:00
|
|
|
return cache->datapath;
|
2013-06-16 17:06:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 10:32:50 +00:00
|
|
|
return cache->filepath;
|
2013-06-16 17:06:10 +00:00
|
|
|
}
|
2012-12-14 16:16:09 +00:00
|
|
|
}
|