Skip to content

Commit fb59300

Browse files
committed
Add library prefixes interface (closes #39) and start on additional directory queries (refs #21)
1 parent efb84dc commit fb59300

File tree

9 files changed

+392
-90
lines changed

9 files changed

+392
-90
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ set(META_AUTHOR_ORGANIZATION "CG Internals GmbH")
4949
set(META_AUTHOR_DOMAIN "https://github.com/cginternals/cpplocate/")
5050
set(META_AUTHOR_MAINTAINER "[email protected]")
5151
set(META_VERSION_MAJOR "2")
52-
set(META_VERSION_MINOR "1")
52+
set(META_VERSION_MINOR "2")
5353
set(META_VERSION_PATCH "0")
5454
set(META_VERSION_REVISION "${GIT_REV}")
5555
set(META_VERSION "${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH}")

source/cpplocate/include/cpplocate/cpplocate.h

+69-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
#include <string>
6+
#include <vector>
67

78
#include <cpplocate/cpplocate_api.h>
89

@@ -90,6 +91,7 @@ CPPLOCATE_API std::string getLibraryPath(void * symbol);
9091
*/
9192
CPPLOCATE_API std::string locatePath(const std::string & relPath, const std::string & systemDir, void * symbol);
9293

94+
9395
/**
9496
* @brief
9597
* Get platform specific path separator
@@ -110,13 +112,23 @@ CPPLOCATE_API std::string libPrefix();
110112

111113
/**
112114
* @brief
113-
* Get platform specific shared library extension
115+
* Get main platform specific shared library extension
114116
*
115117
* @return
116-
* Library prefix (e.g., 'dll', or 'so')
118+
* Library prefix (e.g., 'dll', 'dylib', or 'so')
117119
*/
118120
CPPLOCATE_API std::string libExtension();
119121

122+
/**
123+
* @brief
124+
* Get the list platform specific shared library extensions
125+
*
126+
* @return
127+
* List of library prefixes (e.g., ['dll'], ['so'], or ['dylib', 'so'])
128+
*/
129+
CPPLOCATE_API std::vector<std::string> libExtensions();
130+
131+
120132
/**
121133
* @brief
122134
* Get home directory of the current user
@@ -126,6 +138,25 @@ CPPLOCATE_API std::string libExtension();
126138
*/
127139
CPPLOCATE_API std::string homeDir();
128140

141+
/**
142+
* @brief
143+
* Get profile directory of the current user
144+
*
145+
* @return
146+
* Profile directory
147+
*/
148+
CPPLOCATE_API std::string profileDir();
149+
150+
/**
151+
* @brief
152+
* Get home directory of the current user
153+
*
154+
* @return
155+
* Home directory
156+
*/
157+
CPPLOCATE_API std::string documentDir();
158+
159+
129160
/**
130161
* @brief
131162
* Get config directory for the named application
@@ -138,5 +169,41 @@ CPPLOCATE_API std::string homeDir();
138169
*/
139170
CPPLOCATE_API std::string configDir(const std::string & application);
140171

172+
/**
173+
* @brief
174+
* Get config directory for the named application
175+
*
176+
* @param[in] application
177+
* Application name
178+
*
179+
* @return
180+
* Config directory
181+
*/
182+
CPPLOCATE_API std::string roamingDir(const std::string & application);
183+
184+
/**
185+
* @brief
186+
* Get config directory for the named application
187+
*
188+
* @param[in] application
189+
* Application name
190+
*
191+
* @return
192+
* Config directory
193+
*/
194+
CPPLOCATE_API std::string localDir(const std::string & application);
195+
196+
/**
197+
* @brief
198+
* Get config directory for the named application
199+
*
200+
* @param[in] application
201+
* Application name
202+
*
203+
* @return
204+
* Config directory
205+
*/
206+
CPPLOCATE_API std::string tempDir(const std::string & application);
207+
141208

142209
} // namespace cpplocate

source/cpplocate/source/cpplocate.cpp

+93-86
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,51 @@
66
#include <liblocate/liblocate.h>
77

88

9-
namespace cpplocate
9+
namespace
1010
{
1111

1212

13-
std::string getExecutablePath()
13+
std::string obtainStringFromLibLocate(char * path, unsigned int length)
1414
{
15-
char * path = nullptr;
16-
unsigned int length = 0;
17-
18-
::getExecutablePath(&path, &length);
19-
20-
auto result = std::string();
21-
22-
if (length > 0)
15+
if (path == nullptr || length == 0)
2316
{
24-
result = std::string(path, length);
17+
return std::string();
2518
}
2619

20+
auto result = std::string(path, length);
21+
2722
// liblocate transfer ownership of pointer behind path variable
2823
free(path);
2924

3025
return result;
3126
}
3227

33-
std::string getBundlePath()
28+
29+
} // namespace
30+
31+
32+
namespace cpplocate
33+
{
34+
35+
36+
std::string getExecutablePath()
3437
{
3538
char * path = nullptr;
3639
unsigned int length = 0;
3740

38-
::getBundlePath(&path, &length);
41+
::getExecutablePath(&path, &length);
3942

40-
auto result = std::string();
43+
return obtainStringFromLibLocate(path, length);
44+
}
4145

42-
if (length > 0)
43-
{
44-
result = std::string(path, length);
45-
}
46+
std::string getBundlePath()
47+
{
48+
char * path = nullptr;
49+
unsigned int length = 0;
4650

47-
// liblocate transfer ownership of pointer behind path variable
48-
free(path);
51+
::getBundlePath(&path, &length);
4952

50-
return result;
53+
return obtainStringFromLibLocate(path, length);
5154
}
5255

5356
std::string getModulePath()
@@ -57,17 +60,7 @@ std::string getModulePath()
5760

5861
::getModulePath(&path, &length);
5962

60-
auto result = std::string();
61-
62-
if (length > 0)
63-
{
64-
result = std::string(path, length);
65-
}
66-
67-
// liblocate transfer ownership of pointer behind path variable
68-
free(path);
69-
70-
return result;
63+
return obtainStringFromLibLocate(path, length);
7164
}
7265

7366
std::string getLibraryPath(void * symbol)
@@ -77,17 +70,7 @@ std::string getLibraryPath(void * symbol)
7770

7871
::getLibraryPath(symbol, &path, &length);
7972

80-
auto result = std::string();
81-
82-
if (length > 0)
83-
{
84-
result = std::string(path, length);
85-
}
86-
87-
// liblocate transfer ownership of pointer behind path variable
88-
free(path);
89-
90-
return result;
73+
return obtainStringFromLibLocate(path, length);
9174
}
9275

9376
std::string locatePath(const std::string & relPath, const std::string & systemDir, void * symbol)
@@ -97,17 +80,7 @@ std::string locatePath(const std::string & relPath, const std::string & systemDi
9780

9881
::locatePath(&path, &length, relPath.c_str(), (unsigned int)relPath.size(), systemDir.c_str(), (unsigned int)systemDir.size(), symbol);
9982

100-
auto result = std::string();
101-
102-
if (length > 0)
103-
{
104-
result = std::string(path, length);
105-
}
106-
107-
// liblocate transfer ownership of pointer behind path variable
108-
free(path);
109-
110-
return result;
83+
return obtainStringFromLibLocate(path, length);
11184
}
11285

11386
std::string pathSeparator()
@@ -126,17 +99,7 @@ std::string libPrefix()
12699

127100
::libPrefix(&prefix, &length);
128101

129-
auto result = std::string();
130-
131-
if (length > 0)
132-
{
133-
result = std::string(prefix, length);
134-
}
135-
136-
// liblocate transfer ownership of pointer behind path variable
137-
free(prefix);
138-
139-
return result;
102+
return obtainStringFromLibLocate(prefix, length);
140103
}
141104

142105
std::string libExtension()
@@ -146,15 +109,29 @@ std::string libExtension()
146109

147110
::libExtension(&extension, &length);
148111

149-
auto result = std::string();
112+
return obtainStringFromLibLocate(extension, length);
113+
}
114+
115+
std::vector<std::string> libExtensions()
116+
{
117+
char ** extensions = nullptr;
118+
unsigned int * lengths = nullptr;
119+
unsigned int count = 0;
150120

151-
if (length > 0)
121+
::libExtensions(&extensions, &lengths, &count);
122+
123+
auto result = std::vector<std::string>(count);
124+
125+
for (auto i = 0u; i < count; ++i)
152126
{
153-
result = std::string(extension, length);
127+
result[i] = obtainStringFromLibLocate(extensions[i], lengths[i]);
154128
}
155129

156-
// liblocate transfer ownership of pointer behind path variable
157-
free(extension);
130+
if (count > 0)
131+
{
132+
free(extensions);
133+
free(lengths);
134+
}
158135

159136
return result;
160137
}
@@ -166,17 +143,47 @@ std::string homeDir()
166143

167144
::homeDir(&dir, &length);
168145

169-
auto result = std::string();
146+
return obtainStringFromLibLocate(dir, length);
147+
}
170148

171-
if (length > 0)
172-
{
173-
result = std::string(dir, length);
174-
}
149+
std::string profileDir()
150+
{
151+
char * dir = nullptr;
152+
unsigned int length = 0;
175153

176-
// liblocate transfer ownership of pointer behind path variable
177-
free(dir);
154+
::profileDir(&dir, &length);
178155

179-
return result;
156+
return obtainStringFromLibLocate(dir, length);
157+
}
158+
159+
std::string documentDir()
160+
{
161+
char * dir = nullptr;
162+
unsigned int length = 0;
163+
164+
::documentDir(&dir, &length);
165+
166+
return obtainStringFromLibLocate(dir, length);
167+
}
168+
169+
std::string roamingDir(const std::string & application)
170+
{
171+
char * dir = nullptr;
172+
unsigned int length = 0;
173+
174+
::roamingDir(&dir, &length, application.c_str(), (unsigned int)application.size());
175+
176+
return obtainStringFromLibLocate(dir, length);
177+
}
178+
179+
std::string localDir(const std::string & application)
180+
{
181+
char * dir = nullptr;
182+
unsigned int length = 0;
183+
184+
::localDir(&dir, &length, application.c_str(), (unsigned int)application.size());
185+
186+
return obtainStringFromLibLocate(dir, length);
180187
}
181188

182189
std::string configDir(const std::string & application)
@@ -186,17 +193,17 @@ std::string configDir(const std::string & application)
186193

187194
::configDir(&dir, &length, application.c_str(), (unsigned int)application.size());
188195

189-
auto result = std::string();
196+
return obtainStringFromLibLocate(dir, length);
197+
}
190198

191-
if (length > 0)
192-
{
193-
result = std::string(dir, length);
194-
}
199+
std::string tempDir(const std::string & application)
200+
{
201+
char * dir = nullptr;
202+
unsigned int length = 0;
195203

196-
// liblocate transfer ownership of pointer behind path variable
197-
free(dir);
204+
::tempDir(&dir, &length, application.c_str(), (unsigned int)application.size());
198205

199-
return result;
206+
return obtainStringFromLibLocate(dir, length);
200207
}
201208

202209

0 commit comments

Comments
 (0)