diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2020-01-27 18:14:56 +0100 |
|---|---|---|
| committer | Even Rouault <even.rouault@spatialys.com> | 2020-01-27 18:45:07 +0100 |
| commit | ea6245ea32f0b0ee414c22951b1f5dc4048782de (patch) | |
| tree | 65b0e20d6a42840384b9aab01f0667f79fbfa582 /src | |
| parent | a4c5cc1e42559f1d92c6b7655680d11f1eead703 (diff) | |
| download | PROJ-ea6245ea32f0b0ee414c22951b1f5dc4048782de.tar.gz PROJ-ea6245ea32f0b0ee414c22951b1f5dc4048782de.zip | |
projinfo: add --searchpaths switch
Diffstat (limited to 'src')
| -rw-r--r-- | src/4D_api.cpp | 19 | ||||
| -rw-r--r-- | src/apps/projinfo.cpp | 14 | ||||
| -rw-r--r-- | src/filemanager.cpp | 63 | ||||
| -rw-r--r-- | src/filemanager.hpp | 5 |
4 files changed, 76 insertions, 25 deletions
diff --git a/src/4D_api.cpp b/src/4D_api.cpp index dabd44a0..91510c35 100644 --- a/src/4D_api.cpp +++ b/src/4D_api.cpp @@ -1484,23 +1484,10 @@ PJ_INFO proj_info (void) { /* build search path string */ auto ctx = pj_get_default_ctx(); if (!ctx || ctx->search_paths.empty()) { - // Env var mostly for testing purposes and being independent from - // an existing installation - const char* ignoreUserWritableDirectory = - getenv("PROJ_SKIP_READ_USER_WRITABLE_DIRECTORY"); - if( ignoreUserWritableDirectory == nullptr || - ignoreUserWritableDirectory[0] == '\0' ) { - buf = path_append(buf, - pj_context_get_user_writable_directory(ctx, false).c_str(), - &buf_size); - } - const std::string envPROJ_LIB = NS_PROJ::FileManager::getProjLibEnvVar(ctx); - buf = path_append(buf, envPROJ_LIB.empty() ? nullptr : envPROJ_LIB.c_str(), &buf_size); -#ifdef PROJ_LIB - if (envPROJ_LIB.empty()) { - buf = path_append(buf, PROJ_LIB, &buf_size); + const auto searchpaths = pj_get_default_searchpaths(ctx); + for( const auto& path: searchpaths ) { + buf = path_append(buf, path.c_str(), &buf_size); } -#endif } else { for (const auto &path : ctx->search_paths) { buf = path_append(buf, path.c_str(), &buf_size); diff --git a/src/apps/projinfo.cpp b/src/apps/projinfo.cpp index 27ea278a..360aafc4 100644 --- a/src/apps/projinfo.cpp +++ b/src/apps/projinfo.cpp @@ -98,7 +98,8 @@ static void usage() { << std::endl << " [--identify] [--3d]" << std::endl << " [--c-ify] [--single-line]" << std::endl - << " {object_definition} | (-s {srs_def} -t {srs_def})" + << " --searchpaths | {object_definition} | (-s " + "{srs_def} -t {srs_def})" << std::endl; std::cerr << std::endl; std::cerr << "-o: formats is a comma separated combination of: " @@ -1057,6 +1058,17 @@ int main(int argc, char **argv) { outputOpt.strict = false; } else if (ci_equal(arg, "--3d")) { promoteTo3D = true; + } else if (ci_equal(arg, "--searchpaths")) { +#ifdef _WIN32 + constexpr char delim = ';'; +#else + constexpr char delim = ':'; +#endif + const auto paths = split(proj_info().searchpath, delim); + for (const auto &path : paths) { + std::cout << path << std::endl; + } + std::exit(0); } else if (arg == "-?" || arg == "--help") { usage(); } else if (arg[0] == '-') { diff --git a/src/filemanager.cpp b/src/filemanager.cpp index 005e734b..b38aeb5a 100644 --- a/src/filemanager.cpp +++ b/src/filemanager.cpp @@ -1196,9 +1196,8 @@ static bool is_rel_or_absolute_filename(const char *name) { // --------------------------------------------------------------------------- #ifdef _WIN32 -static const char *get_path_from_win32_projlib(PJ_CONTEXT *ctx, - const char *name, - std::string &out) { + +static std::string pj_get_win32_projlib() { /* Check if proj.db lieves in a share/proj dir parallel to bin/proj.dll */ /* Based in * https://stackoverflow.com/questions/9112893/how-to-get-path-to-executable-in-c-running-on-windows @@ -1214,10 +1213,10 @@ static const char *get_path_from_win32_projlib(PJ_CONTEXT *ctx, DWORD last_error = GetLastError(); if (result == 0) { - return nullptr; + return std::string(); } else if (result == path_size - 1) { if (ERROR_INSUFFICIENT_BUFFER != last_error) { - return nullptr; + return std::string(); } path_size = path_size * 2; } else { @@ -1227,18 +1226,33 @@ static const char *get_path_from_win32_projlib(PJ_CONTEXT *ctx, // Now remove the program's name. It was (example) // "C:\programs\gmt6\bin\gdal_translate.exe" wout.resize(wcslen(wout.c_str())); - out = NS_PROJ::WStringToUTF8(wout); + std::string out = NS_PROJ::WStringToUTF8(wout); size_t k = out.size(); while (k > 0 && out[--k] != '\\') { } out.resize(k); - out += "/../share/proj/"; + out += "/../share/proj"; + return out; +} + +// --------------------------------------------------------------------------- + +static const char *get_path_from_win32_projlib(PJ_CONTEXT *ctx, + const char *name, + std::string &out) { + + out = pj_get_win32_projlib(); + if (out.empty()) { + return nullptr; + } + out += '/'; out += name; return NS_PROJ::FileManager::exists(ctx, out.c_str()) ? out.c_str() : nullptr; } + #endif /************************************************************************/ @@ -1402,6 +1416,41 @@ pj_open_lib_internal(projCtx ctx, const char *name, const char *mode, } /************************************************************************/ +/* pj_get_default_searchpaths() */ +/************************************************************************/ + +std::vector<std::string> pj_get_default_searchpaths(PJ_CONTEXT *ctx) { + std::vector<std::string> ret; + + // Env var mostly for testing purposes and being independent from + // an existing installation + const char *ignoreUserWritableDirectory = + getenv("PROJ_SKIP_READ_USER_WRITABLE_DIRECTORY"); + if (ignoreUserWritableDirectory == nullptr || + ignoreUserWritableDirectory[0] == '\0') { + ret.push_back(pj_context_get_user_writable_directory(ctx, false)); + } + const std::string envPROJ_LIB = NS_PROJ::FileManager::getProjLibEnvVar(ctx); + if (!envPROJ_LIB.empty()) { + ret.push_back(envPROJ_LIB); + } +#ifdef _WIN32 + if (envPROJ_LIB.empty()) { + const std::string win32Dir = pj_get_win32_projlib(); + if (!win32Dir.empty()) { + ret.push_back(win32Dir); + } + } +#endif +#ifdef PROJ_LIB + if (envPROJ_LIB.empty()) { + ret.push_back(PROJ_LIB); + } +#endif + return ret; +} + +/************************************************************************/ /* pj_open_file_with_manager() */ /************************************************************************/ diff --git a/src/filemanager.hpp b/src/filemanager.hpp index 554bd325..787af342 100644 --- a/src/filemanager.hpp +++ b/src/filemanager.hpp @@ -29,6 +29,8 @@ #define FILEMANAGER_HPP_INCLUDED #include <memory> +#include <string> +#include <vector> #include "proj.h" #include "proj/util.hpp" @@ -92,9 +94,10 @@ class File { std::unique_ptr<File> pj_network_file_open(PJ_CONTEXT *ctx, const char *filename); - NS_PROJ_END +std::vector<std::string> pj_get_default_searchpaths(PJ_CONTEXT *ctx); + //! @endcond Doxygen_Suppress #endif // FILEMANAGER_HPP_INCLUDED |
