Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2015-05-05 23:05:31 +0300
committerCampbell Barton <ideasman42@gmail.com>2015-05-05 23:21:16 +0300
commite00142bfa73ace6ba2e66923e3c36b08b72c57ed (patch)
treea09c936cd138b6cc0b1064963ae0bcc219bb5cab /source/blender/blenlib
parentc641a5563fa034b8faa4d1aaeba1258f9df2e19b (diff)
BLI_path: add PATH search utility functions
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r--source/blender/blenlib/BLI_path_util.h5
-rw-r--r--source/blender/blenlib/intern/path_util.c103
2 files changed, 108 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h
index c35b01e7f55..0513625772d 100644
--- a/source/blender/blenlib/BLI_path_util.h
+++ b/source/blender/blenlib/BLI_path_util.h
@@ -78,6 +78,11 @@ void BLI_del_slash(char *string) ATTR_NONNULL();
const char *BLI_first_slash(const char *string) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
void BLI_path_native_slash(char *path) ATTR_NONNULL();
+#ifdef _WIN32
+bool BLI_path_binary_extensions_add_win32(char *name, const size_t maxlen);
+#endif
+bool BLI_path_binary_search(char *fullname, const size_t maxlen, const char *name);
+
void BLI_getlastdir(const char *dir, char *last, const size_t maxlen);
bool BLI_testextensie(const char *str, const char *ext) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
bool BLI_testextensie_n(const char *str, ...) ATTR_NONNULL(1) ATTR_SENTINEL(0);
diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c
index 58b01f7e140..67b28e82796 100644
--- a/source/blender/blenlib/intern/path_util.c
+++ b/source/blender/blenlib/intern/path_util.c
@@ -1020,6 +1020,109 @@ bool BLI_path_cwd(char *path)
return wasrelative;
}
+#ifdef _WIN32
+/**
+ * Tries appending each of the semicolon-separated extensions in the PATHEXT
+ * environment variable (Windows-only) onto *name in turn until such a file is found.
+ * Returns success/failure.
+ */
+bool BLI_path_binary_extensions_add_win32(char *name, const size_t maxlen)
+{
+ bool retval = false;
+ int type;
+
+ type = BLI_exists(name);
+ if ((type == 0) || S_ISDIR(type)) {
+ char filename[FILE_MAX];
+ char ext[FILE_MAX];
+ const char *extensions = getenv("PATHEXT");
+ if (extensions) {
+ const char *temp;
+ do {
+ strcpy(filename, name);
+ temp = strchr(extensions, ';');
+ if (temp) {
+ strncpy(ext, extensions, temp - extensions);
+ ext[temp - extensions] = 0;
+ extensions = temp + 1;
+ strcat(filename, ext);
+ }
+ else {
+ strcat(filename, extensions);
+ }
+
+ type = BLI_exists(filename);
+ if (type && (!S_ISDIR(type))) {
+ retval = true;
+ BLI_strncpy(name, filename, maxlen);
+ break;
+ }
+ } while (temp);
+ }
+ }
+ else {
+ retval = true;
+ }
+
+ return retval;
+}
+#endif /* WIN32 */
+
+/**
+ * Search for a binary (executable)
+ */
+bool BLI_path_binary_search(
+ char *fullname, const size_t maxlen,
+ const char *name)
+{
+ const char *path;
+ bool retval = false;
+
+#ifdef _WIN32
+ const char separator = ';';
+#else
+ const char separator = ':';
+#endif
+
+ path = getenv("PATH");
+ if (path) {
+ char filename[FILE_MAX];
+ const char *temp;
+
+ do {
+ temp = strchr(path, separator);
+ if (temp) {
+ strncpy(filename, path, temp - path);
+ filename[temp - path] = 0;
+ path = temp + 1;
+ }
+ else {
+ strncpy(filename, path, sizeof(filename));
+ }
+
+ BLI_path_append(filename, maxlen, name);
+ if (
+#ifdef _WIN32
+ BLI_path_binary_extensions_add_win32(filename, maxlen)
+#else
+ BLI_exists(filename)
+#endif
+ )
+ {
+ BLI_strncpy(fullname, filename, maxlen);
+ retval = true;
+ break;
+ }
+ } while (temp);
+ }
+
+ if (retval == false) {
+ *fullname = '\0';
+ }
+
+ return retval;
+}
+
/**
* Copies into *last the part of *dir following the second-last slash.
*/