aboutsummaryrefslogtreecommitdiff
path: root/src/pj_open_lib.c
blob: 180c5497351c14488cce6d82ebc7769697bdfe23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/******************************************************************************
 * $Id$
 *
 * Project:  PROJ.4
 * Purpose:  Implementation of pj_open_lib(), and pj_set_finder().  These
 *           provide a standard interface for opening projections support
 *           data files. 
 * Author:   Gerald Evenden, Frank Warmerdam <warmerdam@pobox.com>
 *
 ******************************************************************************
 * Copyright (c) 1995, Gerald Evenden
 * Copyright (c) 2002, Frank Warmerdam <warmerdam@pobox.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************
 *
 * $Log$
 * Revision 1.5  2002/12/14 20:15:30  warmerda
 * updated headers
 *
 */

#define PJ_LIB__
#include <projects.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

PJ_CVSID("$Id$");

static const char *(*pj_finder)(const char *) = NULL;
static char * proj_lib_name =
#ifdef PROJ_LIB
PROJ_LIB;
#else
0;
#endif

/************************************************************************/
/*                           pj_set_finder()                            */
/************************************************************************/

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

{
    pj_finder = new_finder;
}

/************************************************************************/
/*                            pj_open_lib()                             */
/************************************************************************/

FILE *
pj_open_lib(char *name, char *mode) {
	char fname[MAX_PATH_FILENAME+1];
        const char *sysname;
	FILE *fid;
	int n = 0;

	/* check if ~/name */
	if (*name == '~' && name[1] == DIR_CHAR)
		if (sysname = getenv("HOME")) {
			(void)strcpy(fname, sysname);
			fname[n = strlen(fname)] = DIR_CHAR;
			fname[++n] = '\0';
			(void)strcpy(fname+n, name + 1);
			sysname = fname;
		} else
			return NULL;

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

        /* or try to use application provided file finder */
        else if( pj_finder != NULL && pj_finder( name ) != NULL )
            sysname = pj_finder( name );
        
	/* or is environment PROJ_LIB defined */
	else if ((sysname = getenv("PROJ_LIB")) || (sysname = proj_lib_name)) {
		(void)strcpy(fname, sysname);
		fname[n = strlen(fname)] = DIR_CHAR;
		fname[++n] = '\0';
		(void)strcpy(fname+n, name);
		sysname = fname;
	} else /* just try it bare bones */
		sysname = name;

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

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