diff options
| author | Kristian Evers <kristianevers@gmail.com> | 2019-01-09 23:55:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-09 23:55:39 +0100 |
| commit | 763ff547aea5e1013b49254b62fc06060eccfaaf (patch) | |
| tree | daf7a4e66059e2021a398bc80e93a120a1147bad /src/open_lib.cpp | |
| parent | 6d5924842035ae6b5af8a6685f5c531e667c7f18 (diff) | |
| parent | 03b8e99d388013558b1a2ec02a82af4763b26b34 (diff) | |
| download | PROJ-763ff547aea5e1013b49254b62fc06060eccfaaf.tar.gz PROJ-763ff547aea5e1013b49254b62fc06060eccfaaf.zip | |
Merge pull request #1218 from rouault/search_path
Add API in proj.h to set a file finder callback and search paths; support multiple directories in PROJ_LIB
Diffstat (limited to 'src/open_lib.cpp')
| -rw-r--r-- | src/open_lib.cpp | 297 |
1 files changed, 184 insertions, 113 deletions
diff --git a/src/open_lib.cpp b/src/open_lib.cpp index f4fa313f..510704e9 100644 --- a/src/open_lib.cpp +++ b/src/open_lib.cpp @@ -30,18 +30,20 @@ #define PJ_LIB__ +#ifndef FROM_PROJ_CPP +#define FROM_PROJ_CPP +#endif + #include <errno.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "proj_internal.h" +#include "proj/internal/internal.hpp" + #include "proj_internal.h" -static const char *(*pj_finder)(const char *) = nullptr; -static int path_count = 0; -static char **search_path = nullptr; static const char * proj_lib_name = #ifdef PROJ_LIB PROJ_LIB; @@ -56,54 +58,98 @@ nullptr; void pj_set_finder( const char *(*new_finder)(const char *) ) { - pj_finder = new_finder; + auto ctx = pj_get_default_ctx(); + if( ctx ) { + ctx->file_finder_legacy = new_finder; + } } /************************************************************************/ -/* pj_set_searchpath() */ -/* */ -/* Path control for callers that can't practically provide */ -/* pj_set_finder() style callbacks. Call with (0,NULL) as args */ -/* to clear the searchpath set. */ +/* proj_context_set_file_finder() */ /************************************************************************/ -void pj_set_searchpath ( int count, const char **path ) +/** \brief Assign a file finder callback to a context. + * + * This callback will be used whenever PROJ must open one of its resource files + * (proj.db database, grids, etc...) + * + * The callback will be called with the context currently in use at the moment + * where it is used (not necessarily the one provided during this call), and + * with the provided user_data (which may be NULL). + * The user_data must remain valid during the whole lifetime of the context. + * + * A finder set on the default context will be inherited by contexts created + * later. + * + * @param ctx PROJ context, or NULL for the default context. + * @param finder Finder callback. May be NULL + * @param user_data User data provided to the finder callback. May be NULL. + * + * @since PROJ 6.0 + */ +void proj_context_set_file_finder(PJ_CONTEXT *ctx, proj_file_finder finder, + void* user_data) { - int i; + if( !ctx ) + ctx = pj_get_default_ctx(); + if( !ctx ) + return; + ctx->file_finder = finder; + ctx->file_finder_user_data = user_data; +} - if (path_count > 0 && search_path != nullptr) - { - for (i = 0; i < path_count; i++) - { - pj_dalloc(search_path[i]); - } - pj_dalloc(search_path); - path_count = 0; - search_path = nullptr; - } +/************************************************************************/ +/* proj_context_set_search_paths() */ +/************************************************************************/ - if( count > 0 ) - { - search_path = static_cast<char**>(pj_malloc(sizeof *search_path * count)); - for (i = 0; i < count; i++) + +/** \brief Sets search paths. + * + * Those search paths will be used whenever PROJ must open one of its resource files + * (proj.db database, grids, etc...) + * + * If set on the default context, they will be inherited by contexts created + * later. + * + * @param ctx PROJ context, or NULL for the default context. + * @param count_paths Number of paths. 0 if paths == NULL. + * @param paths Paths. May be NULL. + * + * @since PROJ 6.0 + */ +void proj_context_set_search_paths(PJ_CONTEXT *ctx, + int count_paths, + const char* const* paths) +{ + if( !ctx ) + ctx = pj_get_default_ctx(); + if( !ctx ) + return; + try { + std::vector<std::string> vector_of_paths; + for (int i = 0; i < count_paths; i++) { - search_path[i] = static_cast<char*>(pj_malloc(strlen(path[i]) + 1)); - strcpy(search_path[i], path[i]); + vector_of_paths.emplace_back(paths[i]); } + ctx->set_search_paths(vector_of_paths); + } catch( const std::exception& ) + { } - - path_count = count; } -/* just a couple of helper functions that lets other functions - access the otherwise private search path */ -const char * const *proj_get_searchpath(void) { - return (const char * const *)search_path; -} +/************************************************************************/ +/* pj_set_searchpath() */ +/* */ +/* Path control for callers that can't practically provide */ +/* pj_set_finder() style callbacks. Call with (0,NULL) as args */ +/* to clear the searchpath set. */ +/************************************************************************/ -int proj_get_path_count(void) { - return path_count; +void pj_set_searchpath ( int count, const char **path ) +{ + proj_context_set_search_paths( nullptr, count, const_cast<const char* const*>(path) ); } + /************************************************************************/ /* pj_open_lib_ex() */ /************************************************************************/ @@ -111,83 +157,73 @@ int proj_get_path_count(void) { 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; - int n = 0; - int i; + try { + std::string fname; + const char *sysname = nullptr; + PAFile fid = nullptr; #ifdef WIN32 - static const char dir_chars[] = "/\\"; + static const char dir_chars[] = "/\\"; + const char dirSeparator = ';'; #else - static const char dir_chars[] = "/"; + static const char dir_chars[] = "/"; + const char dirSeparator = ':'; #endif - if( out_full_filename != nullptr && out_full_filename_size > 0 ) - out_full_filename[0] = '\0'; - - /* check if ~/name */ - if (*name == '~' && strchr(dir_chars,name[1]) ) - if ((sysname = getenv("HOME")) != nullptr) { - if( strlen(sysname) + 1 + strlen(name) + 1 > sizeof(fname) ) - { - return nullptr; - } - (void)strcpy(fname, sysname); - fname[n = (int)strlen(fname)] = DIR_CHAR; - fname[++n] = '\0'; - (void)strcpy(fname+n, name + 1); - sysname = fname; - } else - return nullptr; - - /* or fixed path: /name, ./name or ../name */ - else if (strchr(dir_chars,*name) - || (*name == '.' && strchr(dir_chars,name[1])) - || (!strncmp(name, "..", 2) && strchr(dir_chars,name[2])) - || (name[1] == ':' && strchr(dir_chars,name[2])) ) - sysname = name; - - /* or try to use application provided file finder */ - else if( pj_finder != nullptr && pj_finder( name ) != nullptr ) - sysname = pj_finder( name ); - - /* or is environment PROJ_LIB defined */ - else if ((sysname = getenv("PROJ_LIB")) || (sysname = proj_lib_name)) { - if( strlen(sysname) + 1 + strlen(name) + 1 > sizeof(fname) ) - { - return nullptr; + if( ctx == nullptr ) { + ctx = pj_get_default_ctx(); } - (void)strcpy(fname, sysname); - fname[n = (int)strlen(fname)] = DIR_CHAR; - fname[++n] = '\0'; - (void)strcpy(fname+n, name); - sysname = fname; - } else /* just try it bare bones */ - sysname = name; - - if ((fid = pj_ctx_fopen(ctx, sysname, mode)) != nullptr) - { + if( out_full_filename != nullptr && 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; - } + out_full_filename[0] = '\0'; - /* If none of those work and we have a search path, try it */ - if (!fid && path_count > 0) - { - for (i = 0; fid == nullptr && i < path_count; i++) - { - if( strlen(search_path[i]) + 1 + strlen(name) + 1 <= sizeof(fname) ) - { - sprintf(fname, "%s%c%s", search_path[i], DIR_CHAR, name); - sysname = fname; + /* check if ~/name */ + if (*name == '~' && strchr(dir_chars,name[1]) ) + if ((sysname = getenv("HOME")) != nullptr) { + fname = sysname; + fname += DIR_CHAR; + fname += name; + sysname = fname.c_str(); + } else + return nullptr; + + /* or fixed path: /name, ./name or ../name */ + else if (strchr(dir_chars,*name) + || (*name == '.' && strchr(dir_chars,name[1])) + || (!strncmp(name, "..", 2) && strchr(dir_chars,name[2])) + || (name[1] == ':' && strchr(dir_chars,name[2])) ) + sysname = name; + + /* or try to use application provided file finder */ + else if( ctx && ctx->file_finder != nullptr && (sysname = ctx->file_finder( ctx, name, ctx->file_finder_user_data )) != nullptr ) + ; + + else if( ctx && ctx->file_finder_legacy != nullptr && (sysname = ctx->file_finder_legacy( name )) != nullptr ) + ; + + /* or is environment PROJ_LIB defined */ + else if ((sysname = getenv("PROJ_LIB")) != nullptr) { + + auto paths = NS_PROJ::internal::split(std::string(sysname), dirSeparator); + for( const auto& path: paths ) { + fname = path; + fname += DIR_CHAR; + fname += name; + sysname = fname.c_str(); fid = pj_ctx_fopen(ctx, sysname, mode); + if( fid ) + break; } - } - if (fid) + + /* or hardcoded path */ + } else if ((sysname = proj_lib_name) != nullptr) { + fname = sysname; + fname += DIR_CHAR; + fname += name; + sysname = fname.c_str(); + } else /* just try it bare bones */ + sysname = name; + + if ( fid != nullptr || (fid = pj_ctx_fopen(ctx, sysname, mode)) != nullptr) { if( out_full_filename != nullptr && out_full_filename_size > 0 ) { @@ -196,17 +232,52 @@ pj_open_lib_ex(projCtx ctx, const char *name, const char *mode, } errno = 0; } - } - if( ctx->last_errno == 0 && errno != 0 ) - pj_ctx_set_errno( ctx, errno ); + /* If none of those work and we have a search path, try it */ + if (!fid && ctx && !ctx->search_paths.empty() ) + { + for( const auto& path: ctx->search_paths ) { + try { + fname = path; + fname += DIR_CHAR; + fname += name; + sysname = fname.c_str(); + fid = pj_ctx_fopen(ctx, sysname, mode); + } catch( const std::exception& ) + { + } + if( fid ) + break; + } + if (fid) + { + if( out_full_filename != nullptr && 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 ); - pj_log( ctx, PJ_LOG_DEBUG_MAJOR, - "pj_open_lib(%s): call fopen(%s) - %s", - name, sysname, - fid == nullptr ? "failed" : "succeeded" ); + pj_log( ctx, PJ_LOG_DEBUG_MAJOR, + "pj_open_lib(%s): call fopen(%s) - %s", + name, sysname, + fid == nullptr ? "failed" : "succeeded" ); - return(fid); + return(fid); + } + catch( const std::exception& ) { + + pj_log( ctx, PJ_LOG_DEBUG_MAJOR, + "pj_open_lib(%s): out of memory", + name ); + + return nullptr; + } } /************************************************************************/ |
