1
/******************************************************************************
2
* $Id: pj_gridlist.c,v 1.3 2003/03/18 16:26:58 warmerda Exp $
5
* Purpose: Code to manage the list of currently loaded (cached) PJ_GRIDINFOs
6
* See pj_gridinfo.c for details of loading individual grids.
7
* Author: Frank Warmerdam, warmerdam@pobox.com
9
******************************************************************************
10
* Copyright (c) 2000, Frank Warmerdam <warmerdam@pobox.com>
12
* Permission is hereby granted, free of charge, to any person obtaining a
13
* copy of this software and associated documentation files (the "Software"),
14
* to deal in the Software without restriction, including without limitation
15
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
16
* and/or sell copies of the Software, and to permit persons to whom the
17
* Software is furnished to do so, subject to the following conditions:
19
* The above copyright notice and this permission notice shall be included
20
* in all copies or substantial portions of the Software.
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28
* DEALINGS IN THE SOFTWARE.
29
******************************************************************************
31
* $Log: pj_gridlist.c,v $
32
* Revision 1.3 2003/03/18 16:26:58 warmerda
33
* clear error if missing file is not required
35
* Revision 1.2 2003/03/17 19:45:47 warmerda
36
* support '@' marker for optional grids
38
* Revision 1.1 2003/03/15 06:01:18 warmerda
50
static PJ_GRIDINFO *grid_list = NULL;
52
/* used only by pj_load_nadgrids() and pj_deallocate_grids() */
54
static int last_nadgrids_max = 0;
55
static int last_nadgrids_count = 0;
56
static PJ_GRIDINFO **last_nadgrids_list = NULL;
57
static char *last_nadgrids = NULL;
59
/************************************************************************/
60
/* pj_deallocate_grids() */
62
/* Deallocate all loaded grids. */
63
/************************************************************************/
65
void pj_deallocate_grids()
68
while( grid_list != NULL )
70
PJ_GRIDINFO *item = grid_list;
71
grid_list = grid_list->next;
74
pj_gridinfo_free( item );
77
if( last_nadgrids != NULL )
79
pj_dalloc( last_nadgrids );
82
pj_dalloc( last_nadgrids_list );
83
last_nadgrids_list = NULL;
85
last_nadgrids_count = 0;
86
last_nadgrids_max = 0;
90
/************************************************************************/
91
/* pj_gridlist_merge_grid() */
93
/* Find/load the named gridfile and merge it into the */
94
/* last_nadgrids_list. */
95
/************************************************************************/
97
static int pj_gridlist_merge_gridfile( const char *gridname )
101
PJ_GRIDINFO *this_grid, *tail = NULL;
103
/* -------------------------------------------------------------------- */
104
/* Try to find in the existing list of loaded grids. Add all */
105
/* matching grids as with NTv2 we can get many grids from one */
106
/* file (one shared gridname). */
107
/* -------------------------------------------------------------------- */
108
for( this_grid = grid_list; this_grid != NULL; this_grid = this_grid->next)
110
if( strcmp(this_grid->gridname,gridname) == 0 )
114
/* dont add to the list if it is invalid. */
115
if( this_grid->ct == NULL )
118
/* do we need to grow the list? */
119
if( last_nadgrids_count >= last_nadgrids_max - 2 )
121
PJ_GRIDINFO **new_list;
122
int new_max = last_nadgrids_max + 20;
124
new_list = (PJ_GRIDINFO **) pj_malloc(sizeof(void*) * new_max);
125
if( last_nadgrids_list != NULL )
127
memcpy( new_list, last_nadgrids_list,
128
sizeof(void*) * last_nadgrids_max );
129
pj_dalloc( last_nadgrids_list );
132
last_nadgrids_list = new_list;
133
last_nadgrids_max = new_max;
136
/* add to the list */
137
last_nadgrids_list[last_nadgrids_count++] = this_grid;
138
last_nadgrids_list[last_nadgrids_count] = NULL;
147
/* -------------------------------------------------------------------- */
148
/* Try to load the named grid. */
149
/* -------------------------------------------------------------------- */
150
this_grid = pj_gridinfo_init( gridname );
152
if( this_grid == NULL )
154
/* we should get at least a stub grid with a missing "ct" member */
160
tail->next = this_grid;
162
grid_list = this_grid;
164
/* -------------------------------------------------------------------- */
165
/* Recurse to add the grid now that it is loaded. */
166
/* -------------------------------------------------------------------- */
167
return pj_gridlist_merge_gridfile( gridname );
170
/************************************************************************/
171
/* pj_gridlist_from_nadgrids() */
173
/* This functions loads the list of grids corresponding to a */
174
/* particular nadgrids string into a list, and returns it. The */
175
/* list is kept around till a request is made with a different */
176
/* string in order to cut down on the string parsing cost, and */
177
/* the cost of building the list of tables each time. */
178
/************************************************************************/
180
PJ_GRIDINFO **pj_gridlist_from_nadgrids( const char *nadgrids, int *grid_count)
188
if( last_nadgrids != NULL
189
&& strcmp(nadgrids,last_nadgrids) == 0 )
191
*grid_count = last_nadgrids_count;
192
return last_nadgrids_list;
195
/* -------------------------------------------------------------------- */
196
/* Free old one, if any, and make space for new list. */
197
/* -------------------------------------------------------------------- */
198
if( last_nadgrids != NULL )
200
pj_dalloc(last_nadgrids);
203
last_nadgrids = (char *) pj_malloc(strlen(nadgrids)+1);
204
strcpy( last_nadgrids, nadgrids );
206
last_nadgrids_count = 0;
208
/* -------------------------------------------------------------------- */
209
/* Loop processing names out of nadgrids one at a time. */
210
/* -------------------------------------------------------------------- */
211
for( s = nadgrids; *s != '\0'; )
224
s[end_char] != '\0' && s[end_char] != ',';
227
if( end_char > sizeof(name) )
233
strncpy( name, s, end_char );
234
name[end_char] = '\0';
240
if( !pj_gridlist_merge_gridfile( name ) && required )
249
if( last_nadgrids_count > 0 )
251
*grid_count = last_nadgrids_count;
252
return last_nadgrids_list;