6
6
#include < liblocate/liblocate.h>
7
7
8
8
9
- namespace cpplocate
9
+ namespace
10
10
{
11
11
12
12
13
- std::string getExecutablePath ( )
13
+ std::string obtainStringFromLibLocate ( char * path, unsigned int length )
14
14
{
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 )
23
16
{
24
- result = std::string (path, length );
17
+ return std::string ();
25
18
}
26
19
20
+ auto result = std::string (path, length);
21
+
27
22
// liblocate transfer ownership of pointer behind path variable
28
23
free (path);
29
24
30
25
return result;
31
26
}
32
27
33
- std::string getBundlePath ()
28
+
29
+ } // namespace
30
+
31
+
32
+ namespace cpplocate
33
+ {
34
+
35
+
36
+ std::string getExecutablePath ()
34
37
{
35
38
char * path = nullptr ;
36
39
unsigned int length = 0 ;
37
40
38
- ::getBundlePath (&path, &length);
41
+ ::getExecutablePath (&path, &length);
39
42
40
- auto result = std::string ();
43
+ return obtainStringFromLibLocate (path, length);
44
+ }
41
45
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 ;
46
50
47
- // liblocate transfer ownership of pointer behind path variable
48
- free (path);
51
+ ::getBundlePath (&path, &length);
49
52
50
- return result ;
53
+ return obtainStringFromLibLocate (path, length) ;
51
54
}
52
55
53
56
std::string getModulePath ()
@@ -57,17 +60,7 @@ std::string getModulePath()
57
60
58
61
::getModulePath (&path, &length);
59
62
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);
71
64
}
72
65
73
66
std::string getLibraryPath (void * symbol)
@@ -77,17 +70,7 @@ std::string getLibraryPath(void * symbol)
77
70
78
71
::getLibraryPath (symbol, &path, &length);
79
72
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);
91
74
}
92
75
93
76
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
97
80
98
81
::locatePath (&path, &length, relPath.c_str(), (unsigned int )relPath.size(), systemDir.c_str(), (unsigned int )systemDir.size(), symbol);
99
82
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);
111
84
}
112
85
113
86
std::string pathSeparator ()
@@ -126,17 +99,7 @@ std::string libPrefix()
126
99
127
100
::libPrefix (&prefix, &length);
128
101
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);
140
103
}
141
104
142
105
std::string libExtension ()
@@ -146,15 +109,29 @@ std::string libExtension()
146
109
147
110
::libExtension (&extension, &length);
148
111
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 ;
150
120
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)
152
126
{
153
- result = std::string (extension, length );
127
+ result[i] = obtainStringFromLibLocate (extensions[i], lengths[i] );
154
128
}
155
129
156
- // liblocate transfer ownership of pointer behind path variable
157
- free (extension);
130
+ if (count > 0 )
131
+ {
132
+ free (extensions);
133
+ free (lengths);
134
+ }
158
135
159
136
return result;
160
137
}
@@ -166,17 +143,47 @@ std::string homeDir()
166
143
167
144
::homeDir (&dir, &length);
168
145
169
- auto result = std::string ();
146
+ return obtainStringFromLibLocate (dir, length);
147
+ }
170
148
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 ;
175
153
176
- // liblocate transfer ownership of pointer behind path variable
177
- free (dir);
154
+ ::profileDir (&dir, &length);
178
155
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);
180
187
}
181
188
182
189
std::string configDir (const std::string & application)
@@ -186,17 +193,17 @@ std::string configDir(const std::string & application)
186
193
187
194
::configDir (&dir, &length, application.c_str(), (unsigned int )application.size());
188
195
189
- auto result = std::string ();
196
+ return obtainStringFromLibLocate (dir, length);
197
+ }
190
198
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 ;
195
203
196
- // liblocate transfer ownership of pointer behind path variable
197
- free (dir);
204
+ ::tempDir (&dir, &length, application.c_str(), (unsigned int )application.size());
198
205
199
- return result ;
206
+ return obtainStringFromLibLocate (dir, length) ;
200
207
}
201
208
202
209
0 commit comments