summaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2012-01-04 19:21:12 +0000
committerDavid Robillard <d@drobilla.net>2012-01-04 19:21:12 +0000
commit7823f743ee0c748ff4ced56838cd737516d857ab (patch)
tree083bcfd6edb27c53aa217da644f650e86f270070 /src/util.c
parent2f71e8570bcd37ad681bde7d51c433701a0028ab (diff)
downloadlilv-7823f743ee0c748ff4ced56838cd737516d857ab.tar.gz
lilv-7823f743ee0c748ff4ced56838cd737516d857ab.tar.bz2
lilv-7823f743ee0c748ff4ced56838cd737516d857ab.zip
Implement proper support for LV2_STATE_BUNDLE.
Test saving state to default bundle path. Fix memory leaks. git-svn-id: http://svn.drobilla.net/lad/trunk/lilv@3915 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 0af0e1a..1c9a46e 100644
--- a/src/util.c
+++ b/src/util.c
@@ -14,6 +14,8 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#define _POSIX_SOURCE 1 /* for wordexp */
+
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
@@ -21,6 +23,10 @@
#include "lilv_internal.h"
+#ifdef HAVE_WORDEXP
+# include <wordexp.h>
+#endif
+
char*
lilv_strjoin(const char* first, ...)
{
@@ -99,3 +105,36 @@ lilv_get_lang(void)
return lang;
}
+
+/** Expand variables (e.g. POSIX ~ or $FOO, Windows %FOO%) in @a path. */
+char*
+lilv_expand(const char* path)
+{
+#ifdef HAVE_WORDEXP
+ char* ret = NULL;
+ wordexp_t p;
+ if (wordexp(path, &p, 0)) {
+ LILV_ERRORF("Error expanding path `%s'\n", path);
+ return lilv_strdup(path);
+ }
+ if (p.we_wordc == 0) {
+ /* Literal directory path (e.g. no variables or ~) */
+ ret = lilv_strdup(path);
+ } else if (p.we_wordc == 1) {
+ /* Directory path expands (e.g. contains ~ or $FOO) */
+ ret = lilv_strdup(p.we_wordv[0]);
+ } else {
+ /* Multiple expansions in a single directory path? */
+ LILV_ERRORF("Malformed path `%s'\n", path);
+ ret = lilv_strdup(path);
+ }
+ wordfree(&p);
+#elif defined(__WIN32__)
+ static const size_t len = 32767;
+ char* ret = malloc(len);
+ ExpandEnvironmentStrings(path, ret, len);
+#else
+ char* ret = lilv_strdup(path);
+#endif
+ return ret;
+}