Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-proj4 / src / pj_open_lib.c @ 7098

History | View | Annotate | Download (5.61 KB)

1
/******************************************************************************
2
 * $Id: pj_open_lib.c,v 1.6 2004/09/16 15:14:01 fwarmerdam Exp $
3
 *
4
 * Project:  PROJ.4
5
 * Purpose:  Implementation of pj_open_lib(), and pj_set_finder().  These
6
 *           provide a standard interface for opening projections support
7
 *           data files.
8
 * Author:   Gerald Evenden, Frank Warmerdam <warmerdam@pobox.com>
9
 *
10
 ******************************************************************************
11
 * Copyright (c) 1995, Gerald Evenden
12
 * Copyright (c) 2002, Frank Warmerdam <warmerdam@pobox.com>
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a
15
 * copy of this software and associated documentation files (the "Software"),
16
 * to deal in the Software without restriction, including without limitation
17
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18
 * and/or sell copies of the Software, and to permit persons to whom the
19
 * Software is furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included
22
 * in all copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30
 * DEALINGS IN THE SOFTWARE.
31
 ******************************************************************************
32
 *
33
 * $Log: pj_open_lib.c,v $
34
 * Revision 1.6  2004/09/16 15:14:01  fwarmerdam
35
 * * src/pj_open_lib.c: added pj_set_searchpath() provided by Eric Miller.
36
 *
37
 * Revision 1.5  2002/12/14 20:15:30  warmerda
38
 * updated headers
39
 *
40
 */
41

    
42
#define PJ_LIB__
43
#include <projects.h>
44
#include <stdio.h>
45
#include <string.h>
46
#include <errno.h>
47

    
48
PJ_CVSID("$Id: pj_open_lib.c,v 1.6 2004/09/16 15:14:01 fwarmerdam Exp $");
49

    
50
static const char *(*pj_finder)(const char *) = NULL;
51
static int path_count = 0;
52
static char **search_path = NULL;
53
static char * proj_lib_name =
54
#ifdef PROJ_LIB
55
PROJ_LIB;
56
#else
57
0;
58
#endif
59

    
60
/************************************************************************/
61
/*                           pj_set_finder()                            */
62
/************************************************************************/
63

    
64
void pj_set_finder( const char *(*new_finder)(const char *) )
65

    
66
{
67
    pj_finder = new_finder;
68
}
69

    
70
/************************************************************************/
71
/*                         pj_set_searchpath()                          */
72
/*                                                                      */
73
/*      Path control for callers that can't practically provide         */
74
/*      pj_set_finder() style callbacks.                                */
75
/************************************************************************/
76

    
77
void pj_set_searchpath ( int count, const char **path )
78
{
79
    int i;
80

    
81
    if (path_count > 0 && search_path != NULL)
82
    {
83
        for (i = 0; i < path_count; i++)
84
        {
85
            pj_dalloc(search_path[i]);
86
        }
87
        pj_dalloc(search_path);
88
        path_count = 0;
89
        search_path = NULL;
90
    }
91

    
92
    search_path = pj_malloc(sizeof *search_path * count);
93
    for (i = 0; i < count; i++)
94
    {
95
        search_path[i] = pj_malloc(strlen(path[i]) + 1);
96
        strcpy(search_path[i], path[i]);
97
    }
98

    
99
    path_count = count;
100
}
101

    
102
/************************************************************************/
103
/*                            pj_open_lib()                             */
104
/************************************************************************/
105

    
106
FILE *
107
pj_open_lib(char *name, char *mode) {
108
    char fname[MAX_PATH_FILENAME+1];
109
    const char *sysname;
110
    FILE *fid;
111
    int n = 0;
112
    int i;
113

    
114
    /* check if ~/name */
115
    if (*name == '~' && name[1] == DIR_CHAR)
116
        if (sysname = getenv("HOME")) {
117
            (void)strcpy(fname, sysname);
118
            fname[n = strlen(fname)] = DIR_CHAR;
119
            fname[++n] = '\0';
120
            (void)strcpy(fname+n, name + 1);
121
            sysname = fname;
122
        } else
123
            return NULL;
124

    
125
    /* or fixed path: /name, ./name or ../name */
126
    else if (*name == DIR_CHAR || (*name == '.' && name[1] == DIR_CHAR) ||
127
             (!strncmp(name, "..", 2) && name[2] == DIR_CHAR) )
128
        sysname = name;
129

    
130
    /* or try to use application provided file finder */
131
    else if( pj_finder != NULL && pj_finder( name ) != NULL )
132
        sysname = pj_finder( name );
133

    
134
    /* or is environment PROJ_LIB defined */
135
    else if ((sysname = getenv("PROJ_LIB")) || (sysname = proj_lib_name)) {
136
        (void)strcpy(fname, sysname);
137
        fname[n = strlen(fname)] = DIR_CHAR;
138
        fname[++n] = '\0';
139
        (void)strcpy(fname+n, name);
140
        sysname = fname;
141
    } else /* just try it bare bones */
142
        sysname = name;
143

    
144
    if (fid = fopen(sysname, mode))
145
        errno = 0;
146

    
147
    /* If none of those work and we have a search path, try it */
148
    if (!fid && path_count > 0)
149
    {
150
        for (i = 0; fid == NULL && i < path_count; i++)
151
        {
152
            sprintf(fname, "%s%c%s", search_path[i], DIR_CHAR, name);
153
            sysname = fname;
154
            fid = fopen (sysname, mode);
155
        }
156
        if (fid)
157
            errno = 0;
158
    }
159

    
160
    if( getenv( "PROJ_DEBUG" ) != NULL )
161
        fprintf( stderr, "pj_open_lib(%s): call fopen(%s) - %s\n",
162
                 name, sysname,
163
                 fid == NULL ? "failed" : "succeeded" );
164

    
165
    return(fid);
166
}