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,
66 cache = (cache_param_t *)malloc(sizeof(cache_param_t));
67 cache->filename = strdup(targetname);
68 cache->tid = strdup(tid);
70 cache->cid = (char **)malloc(sizeof(char *));
71 *cache->cid = strdup(cid);
74 cache->metadatalist = NULL;
76 cache->metadatalist = gene_metadatalist();
78 cache->ihdrbox = NULL;
84 void delete_cache(cache_param_t **cache)
88 free((*cache)->filename);
91 delete_metadatalist(&(*cache)->metadatalist);
93 if ((*cache)->ihdrbox) {
94 free((*cache)->ihdrbox);
96 for (i = 0; i < (*cache)->numOfcid; i++) {
97 free((*cache)->cid[i]);
103 void insert_cache_into_list(cache_param_t *cache, cachelist_param_t *cachelist)
105 if (cachelist->first) {
106 cachelist->last->next = cache;
108 cachelist->first = cache;
110 cachelist->last = cache;
113 cache_param_t * search_cache(const char targetname[],
114 cachelist_param_t *cachelist)
116 cache_param_t *foundcache;
122 foundcache = cachelist->first;
124 while (foundcache != NULL) {
126 if (strcmp(targetname, foundcache->filename) == 0) {
130 foundcache = foundcache->next;
135 cache_param_t * search_cacheBycsn(int csn, cachelist_param_t *cachelist)
137 cache_param_t *foundcache;
139 foundcache = cachelist->first;
141 while (foundcache != NULL) {
143 if (csn == foundcache->csn) {
146 foundcache = foundcache->next;
151 cache_param_t * search_cacheBycid(const char cid[],
152 cachelist_param_t *cachelist)
154 cache_param_t *foundcache;
161 foundcache = cachelist->first;
163 while (foundcache != NULL) {
164 for (i = 0; i < foundcache->numOfcid; i++)
165 if (strcmp(cid, foundcache->cid[i]) == 0) {
168 foundcache = foundcache->next;
173 cache_param_t * search_cacheBytid(const char tid[],
174 cachelist_param_t *cachelist)
176 cache_param_t *foundcache;
182 foundcache = cachelist->first;
184 while (foundcache != NULL) {
185 if (strcmp(tid, foundcache->tid) == 0) {
188 foundcache = foundcache->next;
193 void add_cachecid(const char *cid, cache_param_t *cache)
199 if ((cache->cid = realloc(cache->cid,
200 (OPJ_SIZE_T)(cache->numOfcid + 1) * sizeof(char *))) == NULL) {
201 fprintf(stderr, "failed to add new cid to cache table in add_cachecid()\n");
205 cache->cid[ cache->numOfcid] = strdup(cid);
210 void update_cachetid(const char *tid, cache_param_t *cache)
216 if (tid[0] != '0' && strcmp(tid, cache->tid) != 0) {
217 fprintf(stderr, "tid is updated to %s for %s\n", tid, cache->filename);
219 cache->tid = strdup(tid);
223 void remove_cidInCache(const char *cid, cache_param_t *cache);
225 void remove_cachecid(const char *cid, cachelist_param_t *cachelist)
227 cache_param_t *cache;
229 cache = search_cacheBycid(cid, cachelist);
230 remove_cidInCache(cid, cache);
233 void remove_cidInCache(const char *cid, cache_param_t *cache)
239 for (i = 0; i < cache->numOfcid; i++)
240 if (strcmp(cid, cache->cid[i]) == 0) {
246 fprintf(stderr, "cid: %s not found\n", cid);
252 cache->cid = (char **)malloc((OPJ_SIZE_T)(cache->numOfcid - 1) * sizeof(
255 for (i = 0, j = 0; i < cache->numOfcid; i++) {
257 cache->cid[j] = strdup(tmp[i]);
267 void print_cache(cache_param_t *cache)
271 fprintf(stdout, "cache\n");
272 fprintf(stdout, "\t filename: %s\n", cache->filename);
273 fprintf(stdout, "\t tid: %s\n", cache->tid);
274 fprintf(stdout, "\t csn: %d\n", cache->csn);
275 fprintf(stdout, "\t cid:");
277 for (i = 0; i < cache->numOfcid; i++) {
278 fprintf(stdout, " %s", cache->cid[i]);
280 fprintf(stdout, "\n");
283 void print_allcache(cachelist_param_t *cachelist)
287 fprintf(stdout, "cache list\n");
289 ptr = cachelist->first;
290 while (ptr != NULL) {