[Bf-blender-cvs] [6308c16] master: Refactor: BLI_path_util (split out app directory access)

Campbell Barton noreply at git.blender.org
Sun Nov 23 18:56:41 CET 2014


Commit: 6308c16675fa7d5d6a3b91eb8591402c65b767d6
Author: Campbell Barton
Date:   Sun Nov 23 14:37:13 2014 +0100
Branches: master
https://developer.blender.org/rB6308c16675fa7d5d6a3b91eb8591402c65b767d6

Refactor: BLI_path_util (split out app directory access)

This module is intended for path manipulation functions
but had utility functions added to access various directories.

===================================================================

M	source/blender/blenfont/intern/blf_lang.c
M	source/blender/blenfont/intern/blf_translation.c
A	source/blender/blenkernel/BKE_appdir.h
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/appdir.c
M	source/blender/blenkernel/intern/blender.c
M	source/blender/blenkernel/intern/modifier.c
M	source/blender/blenkernel/intern/pointcache.c
M	source/blender/blenkernel/intern/smoke.c
M	source/blender/blenlib/BLI_path_util.h
M	source/blender/blenlib/CMakeLists.txt
M	source/blender/blenlib/intern/path_util.c
M	source/blender/editors/interface/interface_icons.c
M	source/blender/editors/interface/resources.c
M	source/blender/editors/render/render_preview.c
M	source/blender/editors/space_file/file_ops.c
M	source/blender/editors/space_file/filesel.c
M	source/blender/editors/space_file/space_file.c
M	source/blender/editors/space_view3d/view3d_ops.c
M	source/blender/freestyle/intern/application/AppConfig.cpp
M	source/blender/freestyle/intern/application/Controller.cpp
M	source/blender/freestyle/intern/python/BPy_Freestyle.cpp
M	source/blender/freestyle/intern/system/StringUtils.h
M	source/blender/imbuf/intern/colormanagement.c
M	source/blender/makesrna/intern/rna_userdef.c
M	source/blender/python/intern/bpy.c
M	source/blender/python/intern/bpy_app.c
M	source/blender/python/intern/bpy_interface.c
M	source/blender/render/intern/source/render_result.c
M	source/blender/windowmanager/intern/wm_files.c
M	source/blender/windowmanager/intern/wm_init_exit.c
M	source/blender/windowmanager/intern/wm_operators.c
M	source/creator/creator.c
M	source/gameengine/GamePlayer/ghost/GPG_ghost.cpp
M	source/gameengine/Ketsji/KX_PythonInit.cpp

===================================================================

diff --git a/source/blender/blenfont/intern/blf_lang.c b/source/blender/blenfont/intern/blf_lang.c
index 12d7182..308e081 100644
--- a/source/blender/blenfont/intern/blf_lang.c
+++ b/source/blender/blenfont/intern/blf_lang.c
@@ -42,6 +42,7 @@
 #include "BLI_path_util.h"
 #include "BLI_string.h"
 
+#include "BKE_appdir.h"
 
 #include "DNA_userdef_types.h"
 
diff --git a/source/blender/blenfont/intern/blf_translation.c b/source/blender/blenfont/intern/blf_translation.c
index 150ff1b..276c681 100644
--- a/source/blender/blenfont/intern/blf_translation.c
+++ b/source/blender/blenfont/intern/blf_translation.c
@@ -42,6 +42,8 @@
 #include "BLI_path_util.h"
 #include "BLI_string.h"
 
+#include "BKE_appdir.h"
+
 #include "DNA_userdef_types.h" /* For user settings. */
 
 #include "BPY_extern.h"
diff --git a/source/blender/blenkernel/BKE_appdir.h b/source/blender/blenkernel/BKE_appdir.h
new file mode 100644
index 0000000..3b2e70b
--- /dev/null
+++ b/source/blender/blenkernel/BKE_appdir.h
@@ -0,0 +1,85 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#ifndef __BKE_APPDIR_H__
+#define __BKE_APPDIR_H__
+
+/** \file BKE_appdir.h
+ *  \ingroup bli
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+const char *BLI_getDefaultDocumentFolder(void);
+
+const char *BLI_get_folder(int folder_id, const char *subfolder);
+const char *BLI_get_folder_create(int folder_id, const char *subfolder);
+const char *BLI_get_user_folder_notest(int folder_id, const char *subfolder);
+const char *BLI_get_folder_version(const int id, const int ver, const bool do_check);
+
+
+/* Initialize path to program executable */
+void BLI_init_program_path(const char *argv0);
+/* Initialize path to temporary directory.
+ * NOTE: On Window userdir will be set to the temporary directory! */
+void BLI_temp_dir_init(char *userdir);
+
+const char *BLI_program_path(void);
+const char *BLI_program_dir(void);
+const char *BLI_temp_dir_session(void);
+const char *BLI_temp_dir_base(void);
+void BLI_system_temporary_dir(char *dir);
+void BLI_temp_dir_session_purge(void);
+
+
+/* folder_id */
+
+/* general, will find based on user/local/system priority */
+#define BLENDER_DATAFILES           2
+
+/* user-specific */
+#define BLENDER_USER_CONFIG         31
+#define BLENDER_USER_DATAFILES      32
+#define BLENDER_USER_SCRIPTS        33
+#define BLENDER_USER_AUTOSAVE       34
+
+/* system */
+#define BLENDER_SYSTEM_DATAFILES    52
+#define BLENDER_SYSTEM_SCRIPTS      53
+#define BLENDER_SYSTEM_PYTHON       54
+
+/* for BLI_get_folder_version only */
+#define BLENDER_RESOURCE_PATH_USER      0
+#define BLENDER_RESOURCE_PATH_LOCAL     1
+#define BLENDER_RESOURCE_PATH_SYSTEM    2
+
+#define BLENDER_STARTUP_FILE    "startup.blend"
+#define BLENDER_USERPREF_FILE   "userpref.blend"
+#define BLENDER_QUIT_FILE       "quit.blend"
+#define BLENDER_BOOKMARK_FILE   "bookmarks.txt"
+#define BLENDER_HISTORY_FILE    "recent-files.txt"
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  /* __BKE_APPDIR_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 6550882..fb717b1 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -37,6 +37,7 @@ set(INC
 	../modifiers
 	../nodes
 	../render/extern/include
+	../../../intern/ghost
 	../../../intern/guardedalloc
 	../../../intern/glew-mx
 	../../../intern/iksolver/extern
@@ -63,6 +64,7 @@ set(SRC
 	intern/addon.c
 	intern/anim.c
 	intern/anim_sys.c
+	intern/appdir.c
 	intern/armature.c
 	intern/autoexec.c
 	intern/blender.c
@@ -176,6 +178,7 @@ set(SRC
 	BKE_addon.h
 	BKE_anim.h
 	BKE_animsys.h
+	BKE_appdir.h
 	BKE_armature.h
 	BKE_autoexec.h
 	BKE_blender.h
@@ -276,6 +279,12 @@ set(SRC
 
 add_definitions(${GL_DEFINITIONS})
 
+if(WIN32)
+	list(APPEND INC
+		../../../intern/utfconv
+	)
+endif()
+
 if(WITH_AUDASPACE)
 	list(APPEND INC
 		../../../intern/audaspace/intern
diff --git a/source/blender/blenkernel/intern/appdir.c b/source/blender/blenkernel/intern/appdir.c
new file mode 100644
index 0000000..66dc4c1
--- /dev/null
+++ b/source/blender/blenkernel/intern/appdir.c
@@ -0,0 +1,806 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+/** \file blender/blenlib/intern/appdir.c
+ *  \ingroup bke
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "BLI_utildefines.h"
+#include "BLI_string.h"
+#include "BLI_fileops.h"
+#include "BLI_path_util.h"
+
+#include "BKE_appdir.h"  /* own include */
+
+#include "GHOST_Path-api.h"
+
+#include "../blenkernel/BKE_blender.h"  /* BLENDER_VERSION, bad level include (no function call) */
+
+#include "MEM_guardedalloc.h"
+
+#ifdef WIN32
+#  include "utf_winfunc.h"
+#  include "utfconv.h"
+#  include <io.h>
+#  ifdef _WIN32_IE
+#    undef _WIN32_IE
+#  endif
+#  define _WIN32_IE 0x0501
+#  include <windows.h>
+#  include <shlobj.h>
+#  include "BLI_winstuff.h"
+#else /* non windows */
+#  ifdef WITH_BINRELOC
+#    include "binreloc.h"
+#  endif
+#  include <unistd.h>  /* mkdtemp on OSX (and probably all *BSD?), not worth making specific check for this OS. */
+#endif /* WIN32 */
+
+/* local */
+static char bprogname[FILE_MAX];    /* full path to program executable */
+static char bprogdir[FILE_MAX];     /* full path to directory in which executable is located */
+static char btempdir_base[FILE_MAX];          /* persistent temporary directory */
+static char btempdir_session[FILE_MAX] = "";  /* volatile temporary directory */
+
+/* This is now only used to really get the user's default document folder */
+/* On Windows I chose the 'Users/<MyUserName>/Documents' since it's used
+ * as default location to save documents */
+const char *BLI_getDefaultDocumentFolder(void)
+{
+#ifndef WIN32
+	const char * const xdg_documents_dir = getenv("XDG_DOCUMENTS_DIR");
+
+	if (xdg_documents_dir)
+		return xdg_documents_dir;
+
+	return getenv("HOME");
+#else /* Windows */
+	static char documentfolder[MAXPATHLEN];
+	HRESULT hResult;
+
+	/* Check for %HOME% env var */
+	if (uput_getenv("HOME", documentfolder, MAXPATHLEN)) {
+		if (BLI_is_dir(documentfolder)) return documentfolder;
+	}
+				
+	/* add user profile support for WIN 2K / NT.
+	 * This is %APPDATA%, which translates to either
+	 * %USERPROFILE%\Application Data or since Vista
+	 * to %USERPROFILE%\AppData\Roaming
+	 */
+	hResult = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, documentfolder);
+		
+	if (hResult == S_OK) {
+		if (BLI_is_dir(documentfolder)) return documentfolder;
+	}
+		
+	return NULL;
+#endif /* WIN32 */
+}
+
+
+// #define PATH_DEBUG
+
+/* returns a formatted representation of the specified version number. Non-reentrant! */
+static char *blender_version_decimal(const int ver)
+{
+	static char version_str[5];
+	sprintf(version_str, "%d.%02d", ver / 100, ver % 100);
+	return version_str;
+}
+
+/**
+ * Concatenates path_base, (optional) path_sep and (optional) folder_name into targetpath,
+ * returning true if result points to a directory.
+ */
+static bool test_path(char *targetpath, const char *path_base, const char *path_sep, const char *folder_name)
+{
+	char tmppath[FILE_MAX];
+	
+	if (path_sep) BLI_join_dirfile(tmppath, sizeof(tmppath), path_base, path_sep);
+	else BLI_strncpy(tmppath, path_base, sizeof(tmppath));
+
+	/* rare cases folder_name is omitted (when looking for ~/.blender/2.xx dir only) */
+	if (folder_name)
+		BLI_make_file_string("/", targetpath, tmppath, folder_name);
+	else
+		BLI_strncpy(targetpath, tmppath, sizeof(tmppath));
+	/* FIXME: why is "//" on front of tmppath expanded to "/" (by BLI_join_dirfile)
+	 * if folder_name is specified but not otherwise? */
+
+	if (BLI_is_dir(targetpath)) {
+#ifdef PATH_DEBUG
+		printf("\t%s found: %s\n", __func__, targetpath);
+#endif
+		return true;
+	}
+	else {
+#ifdef PATH_DEBUG
+		printf("\t%s missing: %s\n", __func__, targetpath);
+#endif
+		//targetpath[0] = '\0';
+		return false;
+	}
+}
+
+/**
+ * Puts the value of the specified environment variable into *path if it exists
+ * and points at a directory. Returns true if this was done.
+ */
+static bool test_env_path(char *path, const char *envvar)
+{
+	const char *env = envvar ? getenv(envvar) : NULL;
+	if (!env) return false;
+	
+	if (BLI_is_dir(env)) {
+		BLI_strncpy(path, env, FILE_MAX);
+#ifdef PATH_DEBUG
+		printf("\t%s env %s found: %s\n", __func__, envvar, env);
+#endif
+		return true;
+	}
+	else {
+		path[0] = '\0';
+#ifdef PATH_DEBUG
+		printf("\t%s env %s missing: %s\n", __func__, envvar, env);
+#endif
+		return false;
+	}
+}
+
+/**
+ * Constructs in \a targetpath the name of a directory relative to a version-specific
+ * subdirectory in the parent directory of the Blender executable.
+ *
+ * \param targetpath  String to return path
+ * \p

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list