~ubuntu-branches/ubuntu/wily/proj/wily

« back to all changes in this revision

Viewing changes to src/pj_initcache.c

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2009-10-05 16:28:55 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20091005162855-gnsd4jzn7wtw8uwg
Tags: 4.7.0-1
* New upstream release
* Datum grids files updated to version 1.5.
  (closes: #548297)
* Removed manpages.dpatch, applied upstream.
* Generalized libproj0.install file.
* Policy changed to 3.8.3, without futher changes.
* Minor change to nad2bin.1 man page.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id: pj_transform.c 1504 2009-01-06 02:11:57Z warmerdam $
 
3
 *
 
4
 * Project:  PROJ.4
 
5
 * Purpose:  init file definition cache.
 
6
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 
7
 *
 
8
 ******************************************************************************
 
9
 * Copyright (c) 2009, Frank Warmerdam
 
10
 *
 
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:
 
17
 *
 
18
 * The above copyright notice and this permission notice shall be included
 
19
 * in all copies or substantial portions of the Software.
 
20
 *
 
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
 *****************************************************************************/
 
29
 
 
30
#include <projects.h>
 
31
#include <string.h>
 
32
 
 
33
PJ_CVSID("$Id: pj_transform.c 1504 2009-01-06 02:11:57Z warmerdam $");
 
34
 
 
35
static int cache_count = 0;
 
36
static int cache_alloc = 0;
 
37
static char **cache_key = NULL;
 
38
static paralist **cache_paralist = NULL;
 
39
 
 
40
/************************************************************************/
 
41
/*                            pj_clone_paralist()                       */
 
42
/*                                                                      */
 
43
/*     Allocate a copy of a parameter list.                             */
 
44
/************************************************************************/
 
45
 
 
46
paralist *pj_clone_paralist( const paralist *list)
 
47
{
 
48
  paralist *list_copy = NULL, *next_copy = NULL;
 
49
 
 
50
  for( ; list != NULL; list = list->next )
 
51
    {
 
52
      paralist *newitem = (paralist *)
 
53
        pj_malloc(sizeof(paralist) + strlen(list->param));
 
54
 
 
55
      newitem->used = 0;
 
56
      newitem->next = 0;
 
57
      strcpy( newitem->param, list->param );
 
58
      
 
59
      if( list_copy == NULL )
 
60
        list_copy = newitem;
 
61
      else
 
62
        next_copy->next = newitem;
 
63
 
 
64
      next_copy = newitem;
 
65
    }
 
66
 
 
67
  return list_copy;
 
68
}
 
69
 
 
70
/************************************************************************/
 
71
/*                            pj_clear_initcache()                      */
 
72
/*                                                                      */
 
73
/*      Clear out all memory held in the init file cache.               */
 
74
/************************************************************************/
 
75
 
 
76
void pj_clear_initcache()
 
77
{
 
78
  if( cache_alloc > 0 )
 
79
  {
 
80
    int i;
 
81
 
 
82
    pj_acquire_lock();
 
83
 
 
84
    for( i = 0; i < cache_count; i++ )
 
85
      {
 
86
        paralist *n, *t = cache_paralist[i];
 
87
                
 
88
        pj_dalloc( cache_key[i] );
 
89
 
 
90
        /* free parameter list elements */
 
91
        for (; t != NULL; t = n) {
 
92
          n = t->next;
 
93
          pj_dalloc(t);
 
94
        }
 
95
      }
 
96
 
 
97
    pj_dalloc( cache_key );
 
98
    pj_dalloc( cache_paralist );
 
99
    cache_count = 0;
 
100
    cache_alloc= 0;
 
101
    cache_key = NULL;
 
102
    cache_paralist = NULL;
 
103
 
 
104
    pj_release_lock();
 
105
  }
 
106
}
 
107
 
 
108
/************************************************************************/
 
109
/*                            pj_search_initcache()                     */
 
110
/*                                                                      */
 
111
/*      Search for a matching definition in the init cache.             */
 
112
/************************************************************************/
 
113
 
 
114
paralist *pj_search_initcache( const char *filekey )
 
115
 
 
116
{
 
117
  int i;
 
118
  paralist *result = NULL;
 
119
 
 
120
  pj_acquire_lock();
 
121
 
 
122
  for( i = 0; result == NULL && i < cache_count; i++)
 
123
    {
 
124
      if( strcmp(filekey,cache_key[i]) == 0 )
 
125
        {
 
126
          result = pj_clone_paralist( cache_paralist[i] );
 
127
        }
 
128
    }
 
129
 
 
130
  pj_release_lock();
 
131
 
 
132
  return result;
 
133
}
 
134
 
 
135
/************************************************************************/
 
136
/*                            pj_insert_initcache()                     */
 
137
/*                                                                      */
 
138
/*      Insert a paralist definition in the init file cache.            */
 
139
/************************************************************************/
 
140
 
 
141
void pj_insert_initcache( const char *filekey, const paralist *list )
 
142
 
 
143
{
 
144
  pj_acquire_lock();
 
145
 
 
146
  /* 
 
147
  ** Grow list if required.
 
148
  */
 
149
  if( cache_count == cache_alloc )
 
150
    {
 
151
      char **cache_key_new;
 
152
      paralist **cache_paralist_new;
 
153
 
 
154
      cache_alloc = cache_alloc * 2 + 15;
 
155
 
 
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;
 
160
 
 
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;
 
167
    }
 
168
 
 
169
  /*
 
170
  ** Duplicate the filekey and paralist, and insert in cache.
 
171
  */
 
172
  cache_key[cache_count] = (char *) pj_malloc(strlen(filekey)+1);
 
173
  strcpy( cache_key[cache_count], filekey );
 
174
 
 
175
  cache_paralist[cache_count] = pj_clone_paralist( list );
 
176
 
 
177
  cache_count++;
 
178
 
 
179
  pj_release_lock();
 
180
}
 
181