[trunk] updated copyright and added copyright notice required by ISO, in each file...
[openjpeg.git] / src / lib / openjpip / cache_manager.c
1 /*
2  * $Id$
3  *
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
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "cache_manager.h"
35
36 cachelist_param_t * gene_cachelist(void)
37 {
38   cachelist_param_t *cachelist;
39   
40   cachelist = (cachelist_param_t *)malloc( sizeof(cachelist_param_t));
41   
42   cachelist->first = NULL;
43   cachelist->last  = NULL;
44
45   return cachelist;
46 }
47
48 void delete_cachelist(cachelist_param_t **cachelist)
49 {
50   cache_param_t *cachePtr, *cacheNext;
51   
52   cachePtr = (*cachelist)->first;
53   while( cachePtr != NULL){
54     cacheNext=cachePtr->next;
55     delete_cache( &cachePtr);
56     cachePtr=cacheNext;
57   }
58   free( *cachelist);
59 }
60
61 cache_param_t * gene_cache( const char *targetname, int csn, char *tid, char *cid)
62 {
63   cache_param_t *cache;
64   
65   cache = (cache_param_t *)malloc( sizeof(cache_param_t));
66   cache->filename = strdup( targetname);
67   cache->tid = strdup( tid);
68   cache->csn = csn;
69   cache->cid = (char **)malloc( sizeof(char *));
70   *cache->cid = strdup( cid);
71   cache->numOfcid = 1;
72 #if 1
73   cache->metadatalist = NULL;
74 #else
75   cache->metadatalist = gene_metadatalist();
76 #endif
77   cache->ihdrbox = NULL;
78   cache->next = NULL;
79
80   return cache;
81 }
82
83 void delete_cache( cache_param_t **cache)
84 {
85   int i;
86   
87   free( (*cache)->filename);
88   free( (*cache)->tid);
89
90   delete_metadatalist( &(*cache)->metadatalist);
91
92   if((*cache)->ihdrbox)
93     free((*cache)->ihdrbox);
94   for( i=0; i<(*cache)->numOfcid; i++)
95     free( (*cache)->cid[i]);
96   free( (*cache)->cid);
97   free( *cache);
98 }
99
100 void insert_cache_into_list( cache_param_t *cache, cachelist_param_t *cachelist)
101 {
102   if( cachelist->first)
103     cachelist->last->next = cache;
104   else
105     cachelist->first = cache;
106   cachelist->last = cache;
107 }
108
109 cache_param_t * search_cache( const char targetname[], cachelist_param_t *cachelist)
110 {
111   cache_param_t *foundcache;
112
113   if( !targetname)
114     return NULL;
115
116   foundcache = cachelist->first;
117   
118   while( foundcache != NULL){
119     
120     if( strcmp( targetname, foundcache->filename) == 0)
121       return foundcache;
122       
123     foundcache = foundcache->next;
124   }
125   return NULL;
126 }
127
128 cache_param_t * search_cacheBycsn( int csn, cachelist_param_t *cachelist)
129 {
130   cache_param_t *foundcache;
131
132   foundcache = cachelist->first;
133   
134   while( foundcache != NULL){
135     
136     if(  csn == foundcache->csn)
137       return foundcache;
138     foundcache = foundcache->next;
139   }
140   return NULL;
141 }
142
143 cache_param_t * search_cacheBycid( const char cid[], cachelist_param_t *cachelist)
144 {
145   cache_param_t *foundcache;
146   int i;
147
148   if( !cid)
149     return NULL;
150
151   foundcache = cachelist->first;
152   
153   while( foundcache != NULL){
154     for( i=0; i<foundcache->numOfcid; i++)
155       if( strcmp( cid, foundcache->cid[i]) == 0)
156         return foundcache;
157     foundcache = foundcache->next;
158   }
159   return NULL;
160 }
161
162 cache_param_t * search_cacheBytid( const char tid[], cachelist_param_t *cachelist)
163 {
164   cache_param_t *foundcache;
165
166   if( !tid)
167     return NULL;
168
169   foundcache = cachelist->first;
170   
171   while( foundcache != NULL){
172     if( strcmp( tid, foundcache->tid) == 0)
173       return foundcache;
174     foundcache = foundcache->next;
175   }
176   return NULL;
177 }
178
179 void add_cachecid( const char *cid, cache_param_t *cache)
180 {
181   if( !cid)
182     return;
183   
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");
186     return;
187   }
188   
189   cache->cid[ cache->numOfcid] = strdup( cid);
190
191   cache->numOfcid ++;
192 }
193
194 void update_cachetid( const char *tid, cache_param_t *cache)
195 {
196   if( !tid)
197     return;
198
199   if( tid[0] != '0' && strcmp( tid, cache->tid) !=0){
200     fprintf( stderr, "tid is updated to %s for %s\n", tid, cache->filename);
201     free( cache->tid);
202     cache->tid = strdup( tid);
203   }
204 }
205
206 void remove_cidInCache( const char *cid, cache_param_t *cache);
207
208 void remove_cachecid( const char *cid, cachelist_param_t *cachelist)
209 {
210   cache_param_t *cache;
211
212   cache = search_cacheBycid( cid, cachelist);
213   remove_cidInCache( cid, cache);
214 }
215
216 void remove_cidInCache( const char *cid, cache_param_t *cache)
217 {
218   int idx = -1;
219   char **tmp;
220   int i, j;
221
222   for( i=0; i<cache->numOfcid; i++)
223     if( strcmp( cid, cache->cid[i]) == 0){
224       idx = i;
225       break;
226     }
227
228   if( idx == -1){
229     fprintf( stderr, "cid: %s not found\n", cid);
230     return;   
231   }
232   
233   tmp = cache->cid;
234
235   cache->cid = (char **)malloc( (OPJ_SIZE_T)(cache->numOfcid-1)*sizeof(char *));
236   
237   for( i=0, j=0; i<cache->numOfcid; i++){
238     if( i != idx){
239       cache->cid[j] = strdup( tmp[i]);
240       j++;
241     }
242     free( tmp[i]);
243   }
244   free( tmp);
245
246   cache->numOfcid --;
247 }
248
249 void print_cache( cache_param_t *cache)
250 {
251   int i;
252   
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:");
258
259   for( i=0; i<cache->numOfcid; i++)
260     fprintf( stdout," %s", cache->cid[i]);
261   fprintf( stdout,"\n");
262 }
263
264 void print_allcache( cachelist_param_t *cachelist)
265 {
266   cache_param_t *ptr;
267
268   fprintf( stdout,"cache list\n");
269   
270   ptr = cachelist->first;
271   while( ptr != NULL){
272     print_cache( ptr);
273     ptr=ptr->next;
274   }
275 }