diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2017-03-16 12:40:30 +0100 |
|---|---|---|
| committer | Even Rouault <even.rouault@spatialys.com> | 2017-03-16 14:32:26 +0100 |
| commit | d849419552c36d2f6f74973d075783666e179734 (patch) | |
| tree | 3c5e40242fedf551f21dd1b883cf8943d3646e02 /src | |
| parent | 25066cfb3b90dc447b26cf4f796e7ed8f1377585 (diff) | |
| download | PROJ-d849419552c36d2f6f74973d075783666e179734.tar.gz PROJ-d849419552c36d2f6f74973d075783666e179734.zip | |
Add pj_find_file() function to retrieve the full filename of a proj resource file.
Will help GDAL finding where +nadgrids=... or +geoidgrids=... resouces are located
to be able to directly open them.
Diffstat (limited to 'src')
| -rw-r--r-- | src/pj_open_lib.c | 66 | ||||
| -rw-r--r-- | src/proj.def | 1 | ||||
| -rw-r--r-- | src/proj_api.h | 3 |
3 files changed, 61 insertions, 9 deletions
diff --git a/src/pj_open_lib.c b/src/pj_open_lib.c index 12aae017..99f3f69b 100644 --- a/src/pj_open_lib.c +++ b/src/pj_open_lib.c @@ -91,11 +91,12 @@ void pj_set_searchpath ( int count, const char **path ) } /************************************************************************/ -/* pj_open_lib() */ +/* pj_open_lib_ex() */ /************************************************************************/ -PAFile -pj_open_lib(projCtx ctx, const char *name, const char *mode) { +static PAFile +pj_open_lib_ex(projCtx ctx, const char *name, const char *mode, + char* out_full_filename, size_t out_full_filename_size) { char fname[MAX_PATH_FILENAME+1]; const char *sysname; PAFile fid; @@ -109,6 +110,9 @@ pj_open_lib(projCtx ctx, const char *name, const char *mode) { #ifndef _WIN32_WCE + if( out_full_filename != NULL && out_full_filename_size > 0 ) + out_full_filename[0] = '\0'; + /* check if ~/name */ if (*name == '~' && strchr(dir_chars,name[1]) ) if ((sysname = getenv("HOME")) != NULL) { @@ -142,7 +146,14 @@ pj_open_lib(projCtx ctx, const char *name, const char *mode) { sysname = name; if ((fid = pj_ctx_fopen(ctx, sysname, mode)) != NULL) + { + if( out_full_filename != NULL && out_full_filename_size > 0 ) + { + strncpy(out_full_filename, sysname, out_full_filename_size); + out_full_filename[out_full_filename_size-1] = '\0'; + } errno = 0; + } /* If none of those work and we have a search path, try it */ if (!fid && path_count > 0) @@ -154,17 +165,19 @@ pj_open_lib(projCtx ctx, const char *name, const char *mode) { fid = pj_ctx_fopen(ctx, sysname, mode); } if (fid) + { + if( out_full_filename != NULL && out_full_filename_size > 0 ) + { + strncpy(out_full_filename, sysname, out_full_filename_size); + out_full_filename[out_full_filename_size-1] = '\0'; + } errno = 0; + } } if( ctx->last_errno == 0 && errno != 0 ) pj_ctx_set_errno( ctx, errno ); - /* WARNING: GDAL in alg/gdalapplyverticalshiftgrid.cpp relies on the */ - /* fact that we emit a log message with this debug level and with */ - /* a string that has "fopen(full_path)" in it ! */ - /* This is messy I know, and we should likely have a proper API to do */ - /* that */ pj_log( ctx, PJ_LOG_DEBUG_MAJOR, "pj_open_lib(%s): call fopen(%s) - %s\n", name, sysname, @@ -175,3 +188,40 @@ pj_open_lib(projCtx ctx, const char *name, const char *mode) { return NULL; #endif /* _WIN32_WCE */ } + +/************************************************************************/ +/* pj_open_lib() */ +/************************************************************************/ + +PAFile +pj_open_lib(projCtx ctx, const char *name, const char *mode) { + return pj_open_lib_ex(ctx, name, mode, NULL, 0); +} + +/************************************************************************/ +/* pj_find_file() */ +/************************************************************************/ + +/** Returns the full filename corresponding to a proj resource file specified + * as a short filename. + * + * @param ctx context. + * @param short_filename short filename (e.g. egm96_15.gtx) + * @param out_full_filename output buffer, of size out_full_filename_size, that + * will receive the full filename on success. + * Will be zero-terminated. + * @param out_full_filename_size size of out_full_filename. + * @return 1 if the file was found, 0 otherwise. + */ +int pj_find_file(projCtx ctx, const char *short_filename, + char* out_full_filename, size_t out_full_filename_size) +{ + PAFile f = pj_open_lib_ex(ctx, short_filename, "rb", out_full_filename, + out_full_filename_size); + if( f != NULL ) + { + pj_ctx_fclose(ctx, f); + return 1; + } + return 0; +} diff --git a/src/proj.def b/src/proj.def index 0a688276..3792d3c0 100644 --- a/src/proj.def +++ b/src/proj.def @@ -106,3 +106,4 @@ EXPORTS pj_lp_dist @106 pj_xy_dist @107 pj_xyz_dist @108 + pj_find_file @109 diff --git a/src/proj_api.h b/src/proj_api.h index 033c1282..2add2946 100644 --- a/src/proj_api.h +++ b/src/proj_api.h @@ -202,7 +202,8 @@ void pj_ctx_fclose(projCtx ctx, PAFile file); char *pj_ctx_fgets(projCtx ctx, char *line, int size, PAFile file); PAFile pj_open_lib(projCtx, const char *, const char *); - +int pj_find_file(projCtx ctx, const char *short_filename, + char* out_full_filename, size_t out_full_filename_size); #ifdef __cplusplus } |
