Skip to content

Commit ef62770

Browse files
committed
Add functions to get and set GDAL and proj4 data dir paths
1 parent 22c2296 commit ef62770

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

source/pdal/pdalc_config.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,67 @@ namespace pdal
1313
{
1414
namespace capi
1515
{
16+
std::ofstream pdalcConfigLog("pdalc_config.log");
17+
18+
void setenv(const char *name, const char *value)
19+
{
20+
#if defined(_MSC_VER)
21+
_putenv_s(name, value);
22+
#else
23+
std::setenv(std::string(name + "=" + value).c_str());
24+
#endif
25+
}
26+
27+
size_t PDALGetGdalDataPath(char *path, size_t size)
28+
{
29+
size_t length = 0;
30+
31+
if (path && size > 0)
32+
{
33+
path[0] = '\0';
34+
path[size-1] = '\0';
35+
36+
std::string s = std::getenv("GDAL_DATA");
37+
std::strncpy(path, s.c_str(), size - 1);
38+
length = std::min(s.length(), size - 1);
39+
}
40+
41+
return length;
42+
}
43+
44+
size_t PDALGetProj4DataPath(char *path, size_t size)
45+
{
46+
size_t length = 0;
47+
48+
if (path && size > 0)
49+
{
50+
path[0] = '\0';
51+
path[size-1] = '\0';
52+
53+
std::string s = std::getenv("PROJ_LIB");
54+
std::strncpy(path, s.c_str(), size - 1);
55+
length = std::min(s.length(), size - 1);
56+
}
57+
58+
return length;
59+
}
60+
61+
void PDALSetGdalDataPath(const char *path)
62+
{
63+
if (path)
64+
{
65+
setenv("GDAL_DATA", path);
66+
}
67+
}
68+
69+
void PDALSetProj4DataPath(const char *path)
70+
{
71+
if (path)
72+
{
73+
setenv("PROJ_LIB", path);
74+
}
75+
}
76+
1677
size_t PDALFullVersionString(char *version, size_t size)
1778
{
1879
size_t length = 0;

source/pdal/pdalc_config.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,38 @@ namespace pdal
2323
#else
2424
#include <stdbool.h>
2525
#endif
26+
/**
27+
* Retrieves the path to the GDAL data directory.
28+
*
29+
* @param[out] path The buffer used to retrieve the path
30+
* @param size The size of the provided buffer
31+
* @return The size of the retrieved path
32+
*/
33+
PDALC_API size_t PDALGetGdalDataPath(char *path, size_t size);
34+
35+
/**
36+
* Retrieves the path to the proj4 data directory.
37+
*
38+
* @param[out] path The buffer used to retrieve the path
39+
* @param size The size of the provided buffer
40+
* @return The size of the retrieved path
41+
*/
42+
PDALC_API size_t PDALGetProj4DataPath(char *path, size_t size);
43+
44+
/**
45+
* Sets the path to the GDAL data directory.
46+
*
47+
* @oaram path The path to set
48+
*/
49+
PDALC_API void PDALSetGdalDataPath(const char *path);
50+
51+
/**
52+
* Sets the path to the proj4 data directory.
53+
*
54+
* @oaram path The path to set
55+
*/
56+
PDALC_API void PDALSetProj4DataPath(const char *path);
57+
2658
/**
2759
* Retrieves the full PDAL version string.
2860
* The full version string includes the major version number, the minor version

tests/pdal/test_pdalc_config.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,44 @@
1414

1515
SUITE(test_pdalc_config);
1616

17+
TEST testPDALGetSetGdalDataPath(void)
18+
{
19+
char *original = getenv("GDAL_DATA");
20+
char path[1024];
21+
size_t size = PDALGetGdalDataPath(path, 1024);
22+
ASSERT_STR_EQ(original, path);
23+
ASSERT_EQ(strlen(path), size);
24+
25+
char *expected = "An arbitrary string set as the GDAL data path";
26+
PDALSetGdalDataPath(expected);
27+
size = PDALGetGdalDataPath(path, 1024);
28+
ASSERT_STR_EQ(expected, path);
29+
ASSERT_EQ(size, strlen(path));
30+
31+
PDALSetGdalDataPath(original);
32+
33+
PASS();
34+
}
35+
36+
TEST testPDALGetSetProj4DataPath(void)
37+
{
38+
char *original = getenv("PROJ_LIB");
39+
char path[1024];
40+
size_t size = PDALGetProj4DataPath(path, 1024);
41+
ASSERT_STR_EQ(original, path);
42+
ASSERT_EQ(size, strlen(path));
43+
44+
char *expected = "An arbitrary string set as the proj4 data path";
45+
PDALSetProj4DataPath(expected);
46+
size = PDALGetProj4DataPath(path, 1024);
47+
ASSERT_STR_EQ(expected, path);
48+
ASSERT_EQ(size, strlen(path));
49+
50+
PDALSetProj4DataPath(original);
51+
52+
PASS();
53+
}
54+
1755
TEST testPDALVersionInfo(void)
1856
{
1957
int versionInteger = PDALVersionInteger();
@@ -86,6 +124,8 @@ TEST testPDALPluginInstallPath(void)
86124

87125
GREATEST_SUITE(test_pdalc_config)
88126
{
127+
RUN_TEST(testPDALGetSetGdalDataPath);
128+
RUN_TEST(testPDALGetSetProj4DataPath);
89129
RUN_TEST(testPDALVersionInfo);
90130
RUN_TEST(testPDALDebugInformation);
91131
RUN_TEST(testPDALPluginInstallPath);

0 commit comments

Comments
 (0)