Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2006 / libraries / libjni-proj4 / src / pj_gridlist.c @ 42148

History | View | Annotate | Download (8.52 KB)

1
/******************************************************************************
2
 * $Id: pj_gridlist.c,v 1.3 2003/03/18 16:26:58 warmerda Exp $
3
 *
4
 * Project:  PROJ.4
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
8
 *
9
 ******************************************************************************
10
 * Copyright (c) 2000, Frank Warmerdam <warmerdam@pobox.com>
11
 *
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:
18
 *
19
 * The above copyright notice and this permission notice shall be included
20
 * in all copies or substantial portions of the Software.
21
 *
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
 ******************************************************************************
30
 *
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
34
 *
35
 * Revision 1.2  2003/03/17 19:45:47  warmerda
36
 * support '@' marker for optional grids
37
 *
38
 * Revision 1.1  2003/03/15 06:01:18  warmerda
39
 * New
40
 *
41
 */
42

    
43
#define PJ_LIB__
44

    
45
#include <projects.h>
46
#include <string.h>
47
#include <math.h>
48
#include <assert.h>
49

    
50
static PJ_GRIDINFO *grid_list = NULL;
51

    
52
/* used only by pj_load_nadgrids() and pj_deallocate_grids() */
53

    
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;
58

    
59
/************************************************************************/
60
/*                        pj_deallocate_grids()                         */
61
/*                                                                      */
62
/*      Deallocate all loaded grids.                                    */
63
/************************************************************************/
64

    
65
void pj_deallocate_grids()
66

    
67
{
68
    while( grid_list != NULL )
69
    {
70
        PJ_GRIDINFO *item = grid_list;
71
        grid_list = grid_list->next;
72
        item->next = NULL;
73

    
74
        pj_gridinfo_free( item );
75
    }
76

    
77
    if( last_nadgrids != NULL )
78
    {
79
        pj_dalloc( last_nadgrids );
80
        last_nadgrids = NULL;
81

    
82
        pj_dalloc( last_nadgrids_list );
83
        last_nadgrids_list = NULL;
84

    
85
        last_nadgrids_count = 0;
86
        last_nadgrids_max = 0;
87
    }
88
}
89

    
90
/************************************************************************/
91
/*                       pj_gridlist_merge_grid()                       */
92
/*                                                                      */
93
/*      Find/load the named gridfile and merge it into the              */
94
/*      last_nadgrids_list.                                             */
95
/************************************************************************/
96

    
97
static int pj_gridlist_merge_gridfile( const char *gridname )
98

    
99
{
100
    int i, got_match=0;
101
    PJ_GRIDINFO *this_grid, *tail = NULL;
102

    
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)
109
    {
110
        if( strcmp(this_grid->gridname,gridname) == 0 )
111
        {
112
            got_match = 1;
113

    
114
            /* dont add to the list if it is invalid. */
115
            if( this_grid->ct == NULL )
116
                return 0;
117

    
118
            /* do we need to grow the list? */
119
            if( last_nadgrids_count >= last_nadgrids_max - 2 )
120
            {
121
                PJ_GRIDINFO **new_list;
122
                int new_max = last_nadgrids_max + 20;
123

    
124
                new_list = (PJ_GRIDINFO **) pj_malloc(sizeof(void*) * new_max);
125
                if( last_nadgrids_list != NULL )
126
                {
127
                    memcpy( new_list, last_nadgrids_list, 
128
                            sizeof(void*) * last_nadgrids_max );
129
                    pj_dalloc( last_nadgrids_list );
130
                }
131

    
132
                last_nadgrids_list = new_list;
133
                last_nadgrids_max = new_max;
134
            }
135

    
136
            /* add to the list */
137
            last_nadgrids_list[last_nadgrids_count++] = this_grid;
138
            last_nadgrids_list[last_nadgrids_count] = NULL;
139
        }
140

    
141
        tail = this_grid;
142
    }
143

    
144
    if( got_match )
145
        return 1;
146

    
147
/* -------------------------------------------------------------------- */
148
/*      Try to load the named grid.                                     */
149
/* -------------------------------------------------------------------- */
150
    this_grid = pj_gridinfo_init( gridname );
151

    
152
    if( this_grid == NULL )
153
    {
154
        /* we should get at least a stub grid with a missing "ct" member */
155
        assert( FALSE );
156
        return 0;
157
    }
158
    
159
    if( tail != NULL )
160
        tail->next = this_grid;
161
    else
162
        grid_list = this_grid;
163

    
164
/* -------------------------------------------------------------------- */
165
/*      Recurse to add the grid now that it is loaded.                  */
166
/* -------------------------------------------------------------------- */
167
    return pj_gridlist_merge_gridfile( gridname );
168
}
169

    
170
/************************************************************************/
171
/*                     pj_gridlist_from_nadgrids()                      */
172
/*                                                                      */
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
/************************************************************************/
179

    
180
PJ_GRIDINFO **pj_gridlist_from_nadgrids( const char *nadgrids, int *grid_count)
181

    
182
{
183
    const char *s;
184

    
185
    pj_errno = 0;
186
    *grid_count = 0;
187

    
188
    if( last_nadgrids != NULL 
189
        && strcmp(nadgrids,last_nadgrids) == 0 )
190
    {
191
        *grid_count = last_nadgrids_count;
192
        return last_nadgrids_list;
193
    }
194

    
195
/* -------------------------------------------------------------------- */
196
/*      Free old one, if any, and make space for new list.              */
197
/* -------------------------------------------------------------------- */
198
    if( last_nadgrids != NULL )
199
    {
200
        pj_dalloc(last_nadgrids);
201
    }
202
    
203
    last_nadgrids = (char *) pj_malloc(strlen(nadgrids)+1);
204
    strcpy( last_nadgrids, nadgrids );
205

    
206
    last_nadgrids_count = 0;
207

    
208
/* -------------------------------------------------------------------- */
209
/*      Loop processing names out of nadgrids one at a time.            */
210
/* -------------------------------------------------------------------- */
211
    for( s = nadgrids; *s != '\0'; )
212
    {
213
        int   end_char;
214
        int   required = 1;
215
        char  name[128];
216

    
217
        if( *s == '@' )
218
        {
219
            required = 0;
220
            s++;
221
        }
222

    
223
        for( end_char = 0; 
224
             s[end_char] != '\0' && s[end_char] != ','; 
225
             end_char++ ) {}
226

    
227
        if( end_char > sizeof(name) )
228
        {
229
            pj_errno = -38;
230
            return NULL;
231
        }
232
        
233
        strncpy( name, s, end_char );
234
        name[end_char] = '\0';
235

    
236
        s += end_char;
237
        if( *s == ',' )
238
            s++;
239

    
240
        if( !pj_gridlist_merge_gridfile( name ) && required )
241
        {
242
            pj_errno = -38;
243
            return NULL;
244
        }
245
        else
246
            pj_errno = 0;
247
    }
248

    
249
    if( last_nadgrids_count > 0 )
250
    {
251
        *grid_count = last_nadgrids_count;
252
        return last_nadgrids_list;
253
    }
254
    else
255
        return NULL;
256
}