summaryrefslogtreecommitdiff
path: root/src/lib/eina
diff options
context:
space:
mode:
authorMarcel Hollerbach <mail@marcel-hollerbach.de>2018-08-20 12:56:51 -0400
committerMike Blumenkrantz <zmike@samsung.com>2018-08-20 12:56:51 -0400
commite31ce7b28711c43d37fb5d8469a236e6a17460ab (patch)
tree250554d94a78a8ede3f974dfe365c9f60742b603 /src/lib/eina
parentaa416afffbbf064010ccf46b7c67e56227b3cd43 (diff)
eina_vpath: introduce eina_vpath in style of snprintf
Summary: this can be usefull for later regactor usages. Depends on D6742 Reviewers: zmike, stefan_schmidt, #committers Reviewed By: zmike, #committers Subscribers: #reviewers, segfaultxavi, cedric, #committers, zmike Tags: PHID-PROJ-55rnlag4d454jfmlmuhu Differential Revision: https://phab.enlightenment.org/D6743
Diffstat (limited to 'src/lib/eina')
-rw-r--r--src/lib/eina/eina_vpath.c42
-rw-r--r--src/lib/eina/eina_vpath.h14
2 files changed, 42 insertions, 14 deletions
diff --git a/src/lib/eina/eina_vpath.c b/src/lib/eina/eina_vpath.c
index c958bbe4f9..bc7c4d0045 100644
--- a/src/lib/eina/eina_vpath.c
+++ b/src/lib/eina/eina_vpath.c
@@ -214,6 +214,19 @@ _fetch_user_homedir(char **str, const char *name, const char *error)
214EAPI char * 214EAPI char *
215eina_vpath_resolve(const char* path) 215eina_vpath_resolve(const char* path)
216{ 216{
217 char buf[PATH_MAX];
218
219 if (eina_vpath_resolve_snprintf(buf, sizeof(buf), path) > 0)
220 return strdup(buf);
221 return NULL;
222}
223
224EAPI int
225eina_vpath_resolve_snprintf(char *str, size_t size, const char *format, ...)
226{
227 va_list args;
228 char *path;
229 int len;
217 // XXX: implement parse of path then look up in hash if not just create 230 // XXX: implement parse of path then look up in hash if not just create
218 // object where path and result are the same and return that with 231 // object where path and result are the same and return that with
219 // path set and result set to resolved path - return obj handler calls 232 // path set and result set to resolved path - return obj handler calls
@@ -223,7 +236,11 @@ eina_vpath_resolve(const char* path)
223 /* FIXME: not working for WIndows */ 236 /* FIXME: not working for WIndows */
224 // /* <- full path 237 // /* <- full path
225 238
226 if (!path) return NULL; 239 path = alloca(size + 1);
240
241 va_start(args, format);
242 len = vsnprintf(path, size, format, args);
243 va_end(args);
227 244
228 if (path[0] == '~') 245 if (path[0] == '~')
229 { 246 {
@@ -237,7 +254,7 @@ eina_vpath_resolve(const char* path)
237 // ~username/ <- homedir of user "username" 254 // ~username/ <- homedir of user "username"
238 else 255 else
239 { 256 {
240 char *p, *name, buf[PATH_MAX]; 257 char *p, *name;
241 258
242 for (p = path + 1; *p; p++) 259 for (p = path + 1; *p; p++)
243 { 260 {
@@ -248,21 +265,19 @@ eina_vpath_resolve(const char* path)
248 name[p - path - 1] = 0; 265 name[p - path - 1] = 0;
249 266
250 if (!_fetch_user_homedir(&home, name, path)) 267 if (!_fetch_user_homedir(&home, name, path))
251 return NULL; 268 return 0;
252 path = p; 269 path = p;
253 } 270 }
254 if (home) 271 if (home)
255 { 272 {
256 char buf[PATH_MAX]; 273 return snprintf(str, size, "%s%s", home, path);
257 snprintf(buf, sizeof(buf), "%s%s", home, path);
258 return strdup(buf);
259 } 274 }
260 } 275 }
261 // (:xxx:)/* ... <- meta hash table 276 // (:xxx:)/* ... <- meta hash table
262 else if ((path[0] == '(') && (path[1] == ':')) 277 else if ((path[0] == '(') && (path[1] == ':'))
263 { 278 {
264 const char *p, *end, *meta; 279 const char *p, *end, *meta;
265 char *name, buf[PATH_MAX]; 280 char *name;
266 int max_len = strlen(path); 281 int max_len = strlen(path);
267 Eina_Bool found = EINA_FALSE; 282 Eina_Bool found = EINA_FALSE;
268 283
@@ -280,13 +295,13 @@ eina_vpath_resolve(const char* path)
280 if (!found) 295 if (!found)
281 { 296 {
282 ERR("(: Needs to have a matching ':)'\nThe string was: %s", path); 297 ERR("(: Needs to have a matching ':)'\nThe string was: %s", path);
283 return NULL; 298 return 0;
284 } 299 }
285 300
286 if (*p != '/') 301 if (*p != '/')
287 { 302 {
288 ERR("A / is expected after :)\nThe string was: %s", path); 303 ERR("A / is expected after :)\nThe string was: %s", path);
289 return NULL; 304 return 0;
290 } 305 }
291 306
292 if (found) 307 if (found)
@@ -297,25 +312,24 @@ eina_vpath_resolve(const char* path)
297 meta = _eina_vpath_data_get(name); 312 meta = _eina_vpath_data_get(name);
298 if (meta) 313 if (meta)
299 { 314 {
300 snprintf(buf, sizeof(buf), "%s%s", meta, end + 2); 315 return snprintf(str, size, "%s%s", meta, end + 2);
301 return strdup(buf);
302 } 316 }
303 else 317 else
304 { 318 {
305 ERR("Meta key '%s' was not registered!\nThe string was: %s", name, path); 319 ERR("Meta key '%s' was not registered!\nThe string was: %s", name, path);
306 return NULL; 320 return 0;
307 } 321 }
308 } 322 }
309 } 323 }
310 //just return the path, since we assume that this is a normal path 324 //just return the path, since we assume that this is a normal path
311 else 325 else
312 { 326 {
313 return strdup(path); 327 return snprintf(str, size, "%s", path);
314 } 328 }
315 329
316 ERR("The path has to start with either '~/' or '(:NAME:)/' or be a normal path \nThe string was: %s", path); 330 ERR("The path has to start with either '~/' or '(:NAME:)/' or be a normal path \nThe string was: %s", path);
317 331
318 return NULL; 332 return 0;
319} 333}
320 334
321EAPI void 335EAPI void
diff --git a/src/lib/eina/eina_vpath.h b/src/lib/eina/eina_vpath.h
index 5c1e80e5bf..287623fcda 100644
--- a/src/lib/eina/eina_vpath.h
+++ b/src/lib/eina/eina_vpath.h
@@ -94,5 +94,19 @@ typedef const char * Eina_Vpath;
94 */ 94 */
95EAPI char *eina_vpath_resolve(Eina_Vpath path); 95EAPI char *eina_vpath_resolve(Eina_Vpath path);
96 96
97/*
98 * Translate a virtual path into a normal path, and print it into str.
99 *
100 * @param str the buffer to stuff the characters into
101 * @param size the size of the buffer
102 * @param format A snprintf style format string, which will get evalulated after the vpath strings are getting replaced
103 * @param ... The arguments for the format string
104 *
105 * @return the number of characters that are written into str, on a error a value < 0 is returned.
106 *
107 * @since 1.21
108 *
109 */
110EAPI int eina_vpath_resolve_snprintf(char *str, size_t size, const char *format, ...);
97#endif 111#endif
98#endif 112#endif