2011-04-14 20:37:47 +02:00
|
|
|
/*
|
2012-10-01 10:43:02 +02:00
|
|
|
* $Id$
|
2011-04-14 20:37:47 +02:00
|
|
|
*
|
2014-04-03 17:30:57 +02:00
|
|
|
* Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
|
|
|
|
* Copyright (c) 2002-2014, Professor Benoit Macq
|
2011-04-14 20:37:47 +02:00
|
|
|
* Copyright (c) 2010-2011, Kaori Hagihara
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "cache_manager.h"
|
|
|
|
|
2012-03-19 12:18:24 +01:00
|
|
|
cachelist_param_t * gene_cachelist(void)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
cachelist_param_t *cachelist;
|
|
|
|
|
|
|
|
cachelist = (cachelist_param_t *)malloc(sizeof(cachelist_param_t));
|
|
|
|
|
|
|
|
cachelist->first = NULL;
|
|
|
|
cachelist->last = NULL;
|
|
|
|
|
|
|
|
return cachelist;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void delete_cachelist(cachelist_param_t **cachelist)
|
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t *cachePtr, *cacheNext;
|
|
|
|
|
|
|
|
cachePtr = (*cachelist)->first;
|
|
|
|
while (cachePtr != NULL) {
|
|
|
|
cacheNext = cachePtr->next;
|
|
|
|
delete_cache(&cachePtr);
|
|
|
|
cachePtr = cacheNext;
|
|
|
|
}
|
|
|
|
free(*cachelist);
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t * gene_cache(const char *targetname, int csn, char *tid,
|
|
|
|
char *cid)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t *cache;
|
|
|
|
|
|
|
|
cache = (cache_param_t *)malloc(sizeof(cache_param_t));
|
|
|
|
cache->filename = strdup(targetname);
|
|
|
|
cache->tid = strdup(tid);
|
|
|
|
cache->csn = csn;
|
|
|
|
cache->cid = (char **)malloc(sizeof(char *));
|
|
|
|
*cache->cid = strdup(cid);
|
|
|
|
cache->numOfcid = 1;
|
2011-04-14 20:37:47 +02:00
|
|
|
#if 1
|
2017-05-09 15:44:46 +02:00
|
|
|
cache->metadatalist = NULL;
|
2011-04-14 20:37:47 +02:00
|
|
|
#else
|
2017-05-09 15:44:46 +02:00
|
|
|
cache->metadatalist = gene_metadatalist();
|
2011-04-14 20:37:47 +02:00
|
|
|
#endif
|
2017-05-09 15:44:46 +02:00
|
|
|
cache->ihdrbox = NULL;
|
|
|
|
cache->next = NULL;
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
return cache;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
void delete_cache(cache_param_t **cache)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
free((*cache)->filename);
|
|
|
|
free((*cache)->tid);
|
|
|
|
|
|
|
|
delete_metadatalist(&(*cache)->metadatalist);
|
|
|
|
|
|
|
|
if ((*cache)->ihdrbox) {
|
|
|
|
free((*cache)->ihdrbox);
|
|
|
|
}
|
|
|
|
for (i = 0; i < (*cache)->numOfcid; i++) {
|
|
|
|
free((*cache)->cid[i]);
|
|
|
|
}
|
|
|
|
free((*cache)->cid);
|
|
|
|
free(*cache);
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
void insert_cache_into_list(cache_param_t *cache, cachelist_param_t *cachelist)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
if (cachelist->first) {
|
|
|
|
cachelist->last->next = cache;
|
|
|
|
} else {
|
|
|
|
cachelist->first = cache;
|
|
|
|
}
|
|
|
|
cachelist->last = cache;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t * search_cache(const char targetname[],
|
|
|
|
cachelist_param_t *cachelist)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t *foundcache;
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
if (!targetname) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-03 18:20:00 +01:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
foundcache = cachelist->first;
|
|
|
|
|
|
|
|
while (foundcache != NULL) {
|
|
|
|
|
|
|
|
if (strcmp(targetname, foundcache->filename) == 0) {
|
|
|
|
return foundcache;
|
|
|
|
}
|
|
|
|
|
|
|
|
foundcache = foundcache->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t * search_cacheBycsn(int csn, cachelist_param_t *cachelist)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t *foundcache;
|
|
|
|
|
|
|
|
foundcache = cachelist->first;
|
|
|
|
|
|
|
|
while (foundcache != NULL) {
|
|
|
|
|
|
|
|
if (csn == foundcache->csn) {
|
|
|
|
return foundcache;
|
|
|
|
}
|
|
|
|
foundcache = foundcache->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t * search_cacheBycid(const char cid[],
|
|
|
|
cachelist_param_t *cachelist)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t *foundcache;
|
|
|
|
int i;
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
if (!cid) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-03 18:20:00 +01:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
foundcache = cachelist->first;
|
|
|
|
|
|
|
|
while (foundcache != NULL) {
|
|
|
|
for (i = 0; i < foundcache->numOfcid; i++)
|
|
|
|
if (strcmp(cid, foundcache->cid[i]) == 0) {
|
|
|
|
return foundcache;
|
|
|
|
}
|
|
|
|
foundcache = foundcache->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t * search_cacheBytid(const char tid[],
|
|
|
|
cachelist_param_t *cachelist)
|
2011-09-16 16:20:00 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t *foundcache;
|
2011-09-16 16:20:00 +02:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
if (!tid) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
foundcache = cachelist->first;
|
2011-11-03 18:20:00 +01:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
while (foundcache != NULL) {
|
|
|
|
if (strcmp(tid, foundcache->tid) == 0) {
|
|
|
|
return foundcache;
|
|
|
|
}
|
|
|
|
foundcache = foundcache->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
2011-09-16 16:20:00 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
void add_cachecid(const char *cid, cache_param_t *cache)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
if (!cid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cache->cid = realloc(cache->cid,
|
|
|
|
(OPJ_SIZE_T)(cache->numOfcid + 1) * sizeof(char *))) == NULL) {
|
|
|
|
fprintf(stderr, "failed to add new cid to cache table in add_cachecid()\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->cid[ cache->numOfcid] = strdup(cid);
|
|
|
|
|
|
|
|
cache->numOfcid ++;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
void update_cachetid(const char *tid, cache_param_t *cache)
|
2011-08-25 19:13:04 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
if (!tid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tid[0] != '0' && strcmp(tid, cache->tid) != 0) {
|
|
|
|
fprintf(stderr, "tid is updated to %s for %s\n", tid, cache->filename);
|
|
|
|
free(cache->tid);
|
|
|
|
cache->tid = strdup(tid);
|
|
|
|
}
|
2011-08-25 19:13:04 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
void remove_cidInCache(const char *cid, cache_param_t *cache);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
void remove_cachecid(const char *cid, cachelist_param_t *cachelist)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t *cache;
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
cache = search_cacheBycid(cid, cachelist);
|
|
|
|
remove_cidInCache(cid, cache);
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
void remove_cidInCache(const char *cid, cache_param_t *cache)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
int idx = -1;
|
|
|
|
char **tmp;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < cache->numOfcid; i++)
|
|
|
|
if (strcmp(cid, cache->cid[i]) == 0) {
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx == -1) {
|
|
|
|
fprintf(stderr, "cid: %s not found\n", cid);
|
|
|
|
return;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
tmp = cache->cid;
|
|
|
|
|
|
|
|
cache->cid = (char **)malloc((OPJ_SIZE_T)(cache->numOfcid - 1) * sizeof(
|
|
|
|
char *));
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < cache->numOfcid; i++) {
|
|
|
|
if (i != idx) {
|
|
|
|
cache->cid[j] = strdup(tmp[i]);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
free(tmp[i]);
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
2017-05-09 15:44:46 +02:00
|
|
|
free(tmp);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
cache->numOfcid --;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
void print_cache(cache_param_t *cache)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(stdout, "cache\n");
|
|
|
|
fprintf(stdout, "\t filename: %s\n", cache->filename);
|
|
|
|
fprintf(stdout, "\t tid: %s\n", cache->tid);
|
|
|
|
fprintf(stdout, "\t csn: %d\n", cache->csn);
|
|
|
|
fprintf(stdout, "\t cid:");
|
|
|
|
|
|
|
|
for (i = 0; i < cache->numOfcid; i++) {
|
|
|
|
fprintf(stdout, " %s", cache->cid[i]);
|
|
|
|
}
|
|
|
|
fprintf(stdout, "\n");
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:44:46 +02:00
|
|
|
void print_allcache(cachelist_param_t *cachelist)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2017-05-09 15:44:46 +02:00
|
|
|
cache_param_t *ptr;
|
|
|
|
|
|
|
|
fprintf(stdout, "cache list\n");
|
|
|
|
|
|
|
|
ptr = cachelist->first;
|
|
|
|
while (ptr != NULL) {
|
|
|
|
print_cache(ptr);
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|