changeset 1243:c7a710f806a1 piper

Add omitted files, again!
author Chris Cannam
date Tue, 01 Nov 2016 14:43:51 +0000
parents c401e738793f
children 604b0b2a58e1
files base/HelperExecPath.cpp base/HelperExecPath.h
diffstat 2 files changed, 166 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/base/HelperExecPath.cpp	Tue Nov 01 14:43:51 2016 +0000
@@ -0,0 +1,99 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Sonic Visualiser
+    An audio file viewer and annotation editor.
+    Centre for Digital Music, Queen Mary, University of London.
+    This file copyright 2006-2016 Chris Cannam and QMUL.
+    
+    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.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#include "HelperExecPath.h"
+
+#include <QCoreApplication>
+#include <QFile>
+#include <QDir>
+#include <QFileInfo>
+
+static QStringList
+getSuffixes()
+{
+    if (sizeof(void *) == 8) {
+        return { "-64", "", "-32" };
+    } else {
+        return { "", "-32" };
+    }
+}
+
+static bool
+isGood(QString path)
+{
+    return QFile(path).exists() && QFileInfo(path).isExecutable();
+}
+
+QStringList
+HelperExecPath::getHelperExecutables(QString basename)
+{
+    QStringList dummy;
+    return search(basename, dummy);
+}
+
+QString
+HelperExecPath::getHelperExecutable(QString basename)
+{
+    QStringList execs = getHelperExecutables(basename);
+    if (execs.empty()) return "";
+    else return execs[0];
+}
+
+QStringList
+HelperExecPath::getHelperDirPaths()
+{
+    QStringList dirs;
+    QString myDir = QCoreApplication::applicationDirPath();
+    dirs.push_back(myDir + "/helpers");
+    dirs.push_back(myDir);
+    return dirs;
+}
+
+QStringList
+HelperExecPath::getHelperCandidatePaths(QString basename)
+{
+    QStringList candidates;
+    (void)search(basename, candidates);
+    return candidates;
+}
+
+QStringList
+HelperExecPath::search(QString basename, QStringList &candidates)
+{
+    // Helpers are expected to exist either in the same directory as
+    // this executable was found, or in a subdirectory called helpers.
+
+    QString extension = "";
+#ifdef _WIN32
+    extension = ".exe";
+#endif
+
+    QStringList executables;
+    QStringList dirs = getHelperDirPaths();
+    
+    for (QString s: getSuffixes()) {
+        for (QString d: dirs) {
+            QString path = d + QDir::separator() + basename + s + extension;
+            candidates.push_back(path);
+            if (isGood(path)) {
+                executables.push_back(path);
+                break;
+            }
+        }
+    }
+
+    return executables;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/base/HelperExecPath.h	Tue Nov 01 14:43:51 2016 +0000
@@ -0,0 +1,67 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Sonic Visualiser
+    An audio file viewer and annotation editor.
+    Centre for Digital Music, Queen Mary, University of London.
+    This file copyright 2006-2016 Chris Cannam and QMUL.
+    
+    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.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#ifndef SV_HELPER_EXEC_PATH_H
+#define SV_HELPER_EXEC_PATH_H
+
+#include <QStringList>
+
+/**
+ * Class to find helper executables that have been installed alongside
+ * the application. There may be more than one executable available
+ * with a given base name, because it's possible to have more than one
+ * implementation of a given service. For example, a plugin helper or
+ * scanner may exist in both 32-bit and 64-bit variants.
+ *
+ * This class encodes both the expected locations of helper
+ * executables, and the expected priority between different
+ * implementations (e.g. preferring the architecture that matches that
+ * of the host).
+ */
+class HelperExecPath
+{
+public:
+    /**
+     * Find a helper executable with the given base name in the bundle
+     * directory or installation location, if one exists, and return
+     * its full path. Equivalent to calling getHelperExecutables() and
+     * taking the first result from the returned list (or "" if empty).
+     */
+    static QString getHelperExecutable(QString basename);
+    
+    /**
+     * Find all helper executables with the given base name in the
+     * bundle directory or installation location, and return their
+     * full paths in order of priority.
+     */
+    static QStringList getHelperExecutables(QString basename);
+
+    /**
+     * Return the list of directories searched for helper
+     * executables.
+     */
+    static QStringList getHelperDirPaths();
+    
+    /**
+     * Return the list of executable paths examined in the search for
+     * the helper executable with the given basename.
+     */
+    static QStringList getHelperCandidatePaths(QString basename);
+
+private:
+    static QStringList search(QString, QStringList &);
+};
+
+#endif