4 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2014, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 #include "cache_manager.h"
36 cachelist_param_t * gene_cachelist(void)
38 cachelist_param_t *cachelist;
40 cachelist = (cachelist_param_t *)malloc( sizeof(cachelist_param_t));
42 cachelist->first = NULL;
43 cachelist->last = NULL;
48 void delete_cachelist(cachelist_param_t **cachelist)
50 cache_param_t *cachePtr, *cacheNext;
52 cachePtr = (*cachelist)->first;
53 while( cachePtr != NULL){
54 cacheNext=cachePtr->next;
55 delete_cache( &cachePtr);
61 cache_param_t * gene_cache( const char *targetname, int csn, char *tid, char *cid)
65 cache = (cache_param_t *)malloc( sizeof(cache_param_t));
66 cache->filename = strdup( targetname);
67 cache->tid = strdup( tid);
69 cache->cid = (char **)malloc( sizeof(char *));
70 *cache->cid = strdup( cid);
73 cache->metadatalist = NULL;
75 cache->metadatalist = gene_metadatalist();
77 cache->ihdrbox = NULL;
83 void delete_cache( cache_param_t **cache)
87 free( (*cache)->filename);
90 delete_metadatalist( &(*cache)->metadatalist);
93 free((*cache)->ihdrbox);
94 for( i=0; i<(*cache)->numOfcid; i++)
95 free( (*cache)->cid[i]);
100 void insert_cache_into_list( cache_param_t *cache, cachelist_param_t *cachelist)
102 if( cachelist->first)
103 cachelist->last->next = cache;
105 cachelist->first = cache;
106 cachelist->last = cache;
109 cache_param_t * search_cache( const char targetname[], cachelist_param_t *cachelist)
111 cache_param_t *foundcache;
116 foundcache = cachelist->first;
118 while( foundcache != NULL){
120 if( strcmp( targetname, foundcache->filename) == 0)
123 foundcache = foundcache->next;
128 cache_param_t * search_cacheBycsn( int csn, cachelist_param_t *cachelist)
130 cache_param_t *foundcache;
132 foundcache = cachelist->first;
134 while( foundcache != NULL){
136 if( csn == foundcache->csn)
138 foundcache = foundcache->next;
143 cache_param_t * search_cacheBycid( const char cid[], cachelist_param_t *cachelist)
145 cache_param_t *foundcache;
151 foundcache = cachelist->first;
153 while( foundcache != NULL){
154 for( i=0; i<foundcache->numOfcid; i++)
155 if( strcmp( cid, foundcache->cid[i]) == 0)
157 foundcache = foundcache->next;
162 cache_param_t * search_cacheBytid( const char tid[], cachelist_param_t *cachelist)
164 cache_param_t *foundcache;
169 foundcache = cachelist->first;
171 while( foundcache != NULL){
172 if( strcmp( tid, foundcache->tid) == 0)
174 foundcache = foundcache->next;
179 void add_cachecid( const char *cid, cache_param_t *cache)
184 if( (cache->cid = realloc( cache->cid, (OPJ_SIZE_T)(cache->numOfcid+1)*sizeof(char *))) == NULL){
185 fprintf( stderr, "failed to add new cid to cache table in add_cachecid()\n");
189 cache->cid[ cache->numOfcid] = strdup( cid);
194 void update_cachetid( const char *tid, cache_param_t *cache)
199 if( tid[0] != '0' && strcmp( tid, cache->tid) !=0){
200 fprintf( stderr, "tid is updated to %s for %s\n", tid, cache->filename);
202 cache->tid = strdup( tid);
206 void remove_cidInCache( const char *cid, cache_param_t *cache);
208 void remove_cachecid( const char *cid, cachelist_param_t *cachelist)
210 cache_param_t *cache;
212 cache = search_cacheBycid( cid, cachelist);
213 remove_cidInCache( cid, cache);
216 void remove_cidInCache( const char *cid, cache_param_t *cache)
222 for( i=0; i<cache->numOfcid; i++)
223 if( strcmp( cid, cache->cid[i]) == 0){
229 fprintf( stderr, "cid: %s not found\n", cid);
235 cache->cid = (char **)malloc( (OPJ_SIZE_T)(cache->numOfcid-1)*sizeof(char *));
237 for( i=0, j=0; i<cache->numOfcid; i++){
239 cache->cid[j] = strdup( tmp[i]);
249 void print_cache( cache_param_t *cache)
253 fprintf( stdout,"cache\n");
254 fprintf( stdout,"\t filename: %s\n", cache->filename);
255 fprintf( stdout,"\t tid: %s\n", cache->tid);
256 fprintf( stdout,"\t csn: %d\n", cache->csn);
257 fprintf( stdout,"\t cid:");
259 for( i=0; i<cache->numOfcid; i++)
260 fprintf( stdout," %s", cache->cid[i]);
261 fprintf( stdout,"\n");
264 void print_allcache( cachelist_param_t *cachelist)
268 fprintf( stdout,"cache list\n");
270 ptr = cachelist->first;