2 * $Id: cache_manager.c 53 2011-05-09 16:55:39Z kaori $
4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, 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 //! maximum length of channel identifier
37 #define MAX_LENOFCID 30
39 cachelist_param_t * gene_cachelist()
41 cachelist_param_t *cachelist;
43 cachelist = (cachelist_param_t *)malloc( sizeof(cachelist_param_t));
45 cachelist->first = NULL;
46 cachelist->last = NULL;
51 void delete_cachelist(cachelist_param_t **cachelist)
53 cache_param_t *cachePtr, *cacheNext;
55 cachePtr = (*cachelist)->first;
56 while( cachePtr != NULL){
57 cacheNext=cachePtr->next;
58 delete_cache( &cachePtr);
64 cache_param_t * gene_cache( char *targetname, int csn, char *tid, char *cid)
68 cache = (cache_param_t *)malloc( sizeof(cache_param_t));
69 strcpy( cache->filename, targetname);
70 strcpy( cache->tid, tid);
72 cache->cid = (char **)malloc( sizeof(char *));
73 *cache->cid = (char *)malloc( MAX_LENOFCID);
74 strcpy( *cache->cid, cid);
77 cache->metadatalist = NULL;
79 cache->metadatalist = gene_metadatalist();
81 cache->ihdrbox = NULL;
87 void delete_cache( cache_param_t **cache)
91 delete_metadatalist( &(*cache)->metadatalist);
94 free((*cache)->ihdrbox);
95 for( i=0; i<(*cache)->numOfcid; i++)
96 free( (*cache)->cid[i]);
101 void insert_cache_into_list( cache_param_t *cache, cachelist_param_t *cachelist)
103 if( cachelist->first)
104 cachelist->last->next = cache;
106 cachelist->first = cache;
107 cachelist->last = cache;
110 cache_param_t * search_cache( char targetname[], cachelist_param_t *cachelist)
112 cache_param_t *foundcache;
114 foundcache = cachelist->first;
116 while( foundcache != NULL){
118 if( strcmp( targetname, foundcache->filename) == 0)
121 foundcache = foundcache->next;
126 cache_param_t * search_cacheBycsn( int csn, cachelist_param_t *cachelist)
128 cache_param_t *foundcache;
130 foundcache = cachelist->first;
132 while( foundcache != NULL){
134 if( csn == foundcache->csn)
136 foundcache = foundcache->next;
141 cache_param_t * search_cacheBycid( char cid[], cachelist_param_t *cachelist)
143 cache_param_t *foundcache;
146 foundcache = cachelist->first;
148 while( foundcache != NULL){
149 for( i=0; i<foundcache->numOfcid; i++)
150 if( strcmp( cid, foundcache->cid[i]) == 0)
152 foundcache = foundcache->next;
157 cache_param_t * search_cacheBytid( char tid[], cachelist_param_t *cachelist)
159 cache_param_t *foundcache;
161 foundcache = cachelist->first;
163 while( foundcache != NULL){
164 if( strcmp( tid, foundcache->tid) == 0)
166 foundcache = foundcache->next;
171 void add_cachecid( char *cid, cache_param_t *cache)
178 cache->cid = (char **)malloc( (cache->numOfcid+1)*sizeof(char *));
180 for( i=0; i<cache->numOfcid; i++){
181 cache->cid[i] = (char *)malloc( MAX_LENOFCID);
182 strcpy( cache->cid[i], tmp[i]);
187 cache->cid[ cache->numOfcid] = (char *)malloc( MAX_LENOFCID);
188 strcpy( cache->cid[ cache->numOfcid], cid);
193 void update_cachetid( char *tid, cache_param_t *cache)
195 if( tid[0] != '0' && strcmp( tid, cache->tid) !=0){
196 fprintf( stderr, "tid is updated to %s for %s\n", tid, cache->filename);
197 strcpy( cache->tid, tid);
201 void remove_cidInCache( char *cid, cache_param_t *cache);
203 void remove_cachecid( char *cid, cachelist_param_t *cachelist)
205 cache_param_t *cache;
207 cache = search_cacheBycid( cid, cachelist);
208 remove_cidInCache( cid, cache);
211 void remove_cidInCache( char *cid, cache_param_t *cache)
217 for( i=0; i<cache->numOfcid; i++)
218 if( strcmp( cid, cache->cid[i]) == 0){
224 fprintf( stderr, "cid: %s not found\n", cid);
230 cache->cid = (char **)malloc( (cache->numOfcid-1)*sizeof(char *));
232 for( i=0, j=0; i<cache->numOfcid; i++){
234 cache->cid[j] = (char *)malloc( MAX_LENOFCID);
235 strcpy( cache->cid[j], tmp[i]);
245 void print_cache( cache_param_t *cache)
249 fprintf( stdout,"cache\n");
250 fprintf( stdout,"\t filename: %s\n", cache->filename);
251 fprintf( stdout,"\t tid: %s\n", cache->tid);
252 fprintf( stdout,"\t csn: %d\n", cache->csn);
253 fprintf( stdout,"\t cid:");
255 for( i=0; i<cache->numOfcid; i++)
256 fprintf( stdout," %s", cache->cid[i]);
257 fprintf( stdout,"\n");
260 void print_allcache( cachelist_param_t *cachelist)
264 fprintf( stdout,"cache list\n");
266 ptr = cachelist->first;