1
/******************************************************************************
2
* $Id: pj_transform.c 1504 2009-01-06 02:11:57Z warmerdam $
5
* Purpose: init file definition cache.
6
* Author: Frank Warmerdam, warmerdam@pobox.com
8
******************************************************************************
9
* Copyright (c) 2009, Frank Warmerdam
11
* Permission is hereby granted, free of charge, to any person obtaining a
12
* copy of this software and associated documentation files (the "Software"),
13
* to deal in the Software without restriction, including without limitation
14
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
* and/or sell copies of the Software, and to permit persons to whom the
16
* Software is furnished to do so, subject to the following conditions:
18
* The above copyright notice and this permission notice shall be included
19
* in all copies or substantial portions of the Software.
21
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
* DEALINGS IN THE SOFTWARE.
28
*****************************************************************************/
33
PJ_CVSID("$Id: pj_transform.c 1504 2009-01-06 02:11:57Z warmerdam $");
35
static int cache_count = 0;
36
static int cache_alloc = 0;
37
static char **cache_key = NULL;
38
static paralist **cache_paralist = NULL;
40
/************************************************************************/
41
/* pj_clone_paralist() */
43
/* Allocate a copy of a parameter list. */
44
/************************************************************************/
46
paralist *pj_clone_paralist( const paralist *list)
48
paralist *list_copy = NULL, *next_copy = NULL;
50
for( ; list != NULL; list = list->next )
52
paralist *newitem = (paralist *)
53
pj_malloc(sizeof(paralist) + strlen(list->param));
57
strcpy( newitem->param, list->param );
59
if( list_copy == NULL )
62
next_copy->next = newitem;
70
/************************************************************************/
71
/* pj_clear_initcache() */
73
/* Clear out all memory held in the init file cache. */
74
/************************************************************************/
76
void pj_clear_initcache()
84
for( i = 0; i < cache_count; i++ )
86
paralist *n, *t = cache_paralist[i];
88
pj_dalloc( cache_key[i] );
90
/* free parameter list elements */
91
for (; t != NULL; t = n) {
97
pj_dalloc( cache_key );
98
pj_dalloc( cache_paralist );
102
cache_paralist = NULL;
108
/************************************************************************/
109
/* pj_search_initcache() */
111
/* Search for a matching definition in the init cache. */
112
/************************************************************************/
114
paralist *pj_search_initcache( const char *filekey )
118
paralist *result = NULL;
122
for( i = 0; result == NULL && i < cache_count; i++)
124
if( strcmp(filekey,cache_key[i]) == 0 )
126
result = pj_clone_paralist( cache_paralist[i] );
135
/************************************************************************/
136
/* pj_insert_initcache() */
138
/* Insert a paralist definition in the init file cache. */
139
/************************************************************************/
141
void pj_insert_initcache( const char *filekey, const paralist *list )
147
** Grow list if required.
149
if( cache_count == cache_alloc )
151
char **cache_key_new;
152
paralist **cache_paralist_new;
154
cache_alloc = cache_alloc * 2 + 15;
156
cache_key_new = (char **) pj_malloc(sizeof(char*) * cache_alloc);
157
memcpy( cache_key, cache_key_new, sizeof(char*) * cache_count);
158
pj_dalloc( cache_key );
159
cache_key = cache_key_new;
161
cache_paralist_new = (paralist **)
162
pj_malloc(sizeof(paralist*) * cache_alloc);
163
memcpy( cache_paralist_new, cache_paralist,
164
sizeof(paralist*) * cache_count );
165
pj_dalloc( cache_paralist );
166
cache_paralist = cache_paralist_new;
170
** Duplicate the filekey and paralist, and insert in cache.
172
cache_key[cache_count] = (char *) pj_malloc(strlen(filekey)+1);
173
strcpy( cache_key[cache_count], filekey );
175
cache_paralist[cache_count] = pj_clone_paralist( list );