[libbluray-devel] Find user data and cache dirs in linux(xdg), windows and darwin

hpi1 git at videolan.org
Wed Mar 13 10:09:56 CET 2013


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Wed Mar 13 09:13:28 2013 +0200| [ac7685af58a927b1305cbd1b4a7744d3c99bc500] | committer: hpi1

Find user data and cache dirs in linux(xdg), windows and darwin

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=ac7685af58a927b1305cbd1b4a7744d3c99bc500
---

 configure.ac           |    6 ++++
 src/Makefile.am        |   15 +++++++++
 src/file/dirs_darwin.c |   75 +++++++++++++++++++++++++++++++++++++++++
 src/file/dirs_win32.c  |   59 ++++++++++++++++++++++++++++++++
 src/file/dirs_xdg.c    |   88 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/file/file.h        |    7 ++++
 6 files changed, 250 insertions(+)

diff --git a/configure.ac b/configure.ac
index 0bdd9cb..33dbfc6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -39,11 +39,17 @@ case "${host_os}" in
         ;;
     esac
     ;;
+  *darwin*)
+    SYS=darwin
+    ;;
   *)
     SYS="${host_os}"
     ;;
 esac
 
+AM_CONDITIONAL(HAVE_WIN32,   test "${SYS}" = "mingw32")
+AM_CONDITIONAL(HAVE_DARWIN,  test "${SYS}" = "darwin")
+
 dnl messages
 library_not_found="Could not find required library!"
 function_not_found="Could not find required function!"
diff --git a/src/Makefile.am b/src/Makefile.am
index acb61ad..884e8bb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -73,6 +73,21 @@ libbluray_la_SOURCES = \
 	util/bits.h \
 	util/bits.c \
 	util/logging.h
+
+if HAVE_DARWIN
+libbluray_la_SOURCES+= \
+	file/dirs_darwin.c
+else
+if HAVE_WIN32
+libbluray_la_SOURCES+= \
+	file/dirs_win32.c
+else
+libbluray_la_SOURCES+= \
+	file/dirs_xdg.c
+endif
+endif
+
+
 libbluray_la_LDFLAGS= -version-info $(LT_VERSION_INFO)
 libbluray_la_LIBADD= $(LIBXML2_LIBS) $(FT2_LIBS)
 
diff --git a/src/file/dirs_darwin.c b/src/file/dirs_darwin.c
new file mode 100644
index 0000000..fe4dc6b
--- /dev/null
+++ b/src/file/dirs_darwin.c
@@ -0,0 +1,75 @@
+/*
+ * This file is part of libbluray
+ * Copyright (C) 2012   Konstantin Pavlov
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "file.h"
+
+#include <CoreFoundation/CoreFoundation.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util/strutl.h"
+#include "util/logging.h"
+
+#define USER_CACHE_DIR "Library/Caches"
+#define USER_DATA_DIR  "Library"
+
+
+const char *file_get_data_home(void)
+{
+    static char *dir       = NULL;
+    static int   init_done = 0;
+
+    if (!init_done) {
+        init_done = 1;
+
+        const char *user_home = getenv("HOME");
+        if (user_home && *user_home) {
+            return dir = str_printf("%s/%s", user_home, USER_DATA_DIR);
+        }
+
+        BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+    }
+
+    return dir;
+}
+
+const char *file_get_cache_home(void)
+{
+    static char *dir       = NULL;
+    static int   init_done = 0;
+
+    if (!init_done) {
+        init_done = 1;
+
+        const char *user_home = getenv("HOME");
+        if (user_home && *user_home) {
+            return dir = str_printf("%s/%s", user_home, USER_CACHE_DIR);
+        }
+
+        BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+    }
+
+    return dir;
+}
diff --git a/src/file/dirs_win32.c b/src/file/dirs_win32.c
new file mode 100644
index 0000000..b0e9142
--- /dev/null
+++ b/src/file/dirs_win32.c
@@ -0,0 +1,59 @@
+/*
+ * This file is part of libbluray
+ * Copyright (C) 2011  VideoLAN
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "file.h"
+
+#include "util/logging.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#include <shlobj.h>
+#include <w32api.h>
+#include <limits.h>
+#include <direct.h>
+
+
+const char *file_get_data_home(void)
+{
+    static char appdir[PATH_MAX] = "";
+    wchar_t wdir[MAX_PATH];
+
+    if (*appdir)
+        return appdir;
+
+    /* Get the "Application Data" folder for the user */
+    if (S_OK == SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
+                NULL, SHGFP_TYPE_CURRENT, wdir)) {
+        WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, PATH_MAX, NULL, NULL);
+        return appdir;
+    }
+
+    BD_DEBUG(DBG_FILE, "Can't find user configuration directory !\n");
+    return NULL;
+}
+
+const char *file_get_cache_home(void)
+{
+    return file_get_data_home();
+}
diff --git a/src/file/dirs_xdg.c b/src/file/dirs_xdg.c
new file mode 100644
index 0000000..7a41ece
--- /dev/null
+++ b/src/file/dirs_xdg.c
@@ -0,0 +1,88 @@
+/*
+ * This file is part of libbluray
+ * Copyright (C) 2013  Petri Hintukainen <phintuka at users.sourceforge.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "file.h"
+
+#include "util/strutl.h"
+#include "util/logging.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Based on XDG Base Directory Specification
+ * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
+ */
+
+#define USER_CACHE_DIR ".cache"
+#define USER_DATA_DIR  ".local/share"
+
+
+const char *file_get_data_home(void)
+{
+    static char *dir       = NULL;
+    static int   init_done = 0;
+
+    if (!init_done) {
+        init_done = 1;
+
+        const char *xdg_home = getenv("XDG_DATA_HOME");
+        if (xdg_home && *xdg_home) {
+            return dir = str_printf("%s", xdg_home);
+        }
+
+        const char *user_home = getenv("HOME");
+        if (user_home && *user_home) {
+            return dir = str_printf("%s/%s", user_home, USER_DATA_DIR);
+        }
+
+        BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+    }
+
+    return dir;
+}
+
+const char *file_get_cache_home(void)
+{
+    static char *dir       = NULL;
+    static int   init_done = 0;
+
+    if (!init_done) {
+        init_done = 1;
+
+        const char *xdg_cache = getenv("XDG_CACHE_HOME");
+        if (xdg_cache && *xdg_cache) {
+            return dir = str_printf("%s", xdg_cache);
+        }
+
+        const char *user_home = getenv("HOME");
+        if (user_home && *user_home) {
+            return dir = str_printf("%s/%s", user_home, USER_CACHE_DIR);
+        }
+
+        BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+    }
+
+    return dir;
+}
diff --git a/src/file/file.h b/src/file/file.h
index 3714d76..cad5d54 100644
--- a/src/file/file.h
+++ b/src/file/file.h
@@ -55,4 +55,11 @@ BD_PRIVATE BD_FILE_OPEN file_open_default(void);
 
 BD_PRIVATE extern BD_DIR_H* (*dir_open)(const char* dirname);
 
+/*
+ * User cache and data dirs
+ */
+
+BD_PRIVATE const char *file_get_data_home(void);
+BD_PRIVATE const char *file_get_cache_home(void);
+
 #endif /* FILE_H_ */



More information about the libbluray-devel mailing list