Bladeren bron

Changed API of ctkCmdLineModuleDefaultPathBuilder to be more generic

Matt Clarkson 12 jaren geleden
bovenliggende
commit
0e55196c19

+ 30 - 25
Libs/CommandLineModules/Core/Testing/Cpp/ctkCmdLineModuleDefaultPathBuilderTest.cpp

@@ -48,59 +48,64 @@ int ctkCmdLineModuleDefaultPathBuilderTest(int argc, char* argv[])
 
   ctkCmdLineModuleDefaultPathBuilder builder;
 
-  QStringList defaultList = builder.build();
+  QStringList defaultList = builder.getDirectoryList();
   if (defaultList.size() != 0)
   {
     qDebug() << "The default list should be empty";
     return EXIT_FAILURE;
   }
 
-  builder.setLoadFromCurrentDir(true);
+  builder.addCurrentDir();
+  QStringList result = builder.getDirectoryList();
 
-  QStringList result = builder.build();
   qDebug() << "1. Built:" << result;
 
-  if (result.size() != 2)
+  if (result.size() != 1)
   {
-    qDebug() << "The flag setLoadFromCurrentDir enables scanning of the current working directory plus the subfolder cli-modules";
+    qDebug() << "Calling addCurrentDir should add one directory to the list of directories.";
     return EXIT_FAILURE;
   }
 
-  builder.setLoadFromApplicationDir(true);
+  builder.addCurrentDir("cli-modules");
+  result = builder.getDirectoryList();
 
-  result = builder.build();
   qDebug() << "2. Built:" << result;
 
-  if (result.size() != 4)
+  if (result.size() != 2)
   {
-    qDebug() << "The flag setLoadFromApplicationDir enables scanning of the current installation directory plus the subfolder cli-modules";
+    qDebug() << "Calling addCurrentDir(cli-modules) should add one directory to the list of directories.";
     return EXIT_FAILURE;
   }
 
-  builder.setLoadFromCurrentDir(false);
+  builder.clear();
+
+  builder.addApplicationDir();
+  result = builder.getDirectoryList();
 
-  result = builder.build();
   qDebug() << "3. Built:" << result;
 
-  if (!result.contains(runtimeDirectory.absolutePath()))
+  if (result.size() != 1)
   {
-    qDebug() << "Loading from the application diretory (where THIS application is located), should produce the same path as passed in via the command line argument ${CTK_CMAKE_RUNTIME_OUTPUT_DIRECTORY}";
+    qDebug() << "Calling addApplicationDir should return the application installation dir.";
+    return EXIT_FAILURE;
   }
 
-  builder.setLoadFromHomeDir(true);
+  builder.addApplicationDir("cli-modules");
+  result = builder.getDirectoryList();
 
-  result = builder.build();
   qDebug() << "4. Built:" << result;
 
-  if (result.size() != 4)
+  if (result.size() != 2)
   {
-    qDebug() << "Should now be loading from applicationDir, applicationDir/cli-modules, homeDir, homeDir/cli-modules";
+    qDebug() << "Calling addApplicationDir(cli-modules) should return 2 directories, one the sub-directory of the other.";
     return EXIT_FAILURE;
   }
 
-  builder.setLoadFromCtkModuleLoadPath(true);
+  builder.clear();
+
+  builder.addCtkModuleLoadPathDir();
+  result = builder.getDirectoryList();
 
-  result = builder.build();
   qDebug() << "5. Built:" << result;
 
   // If the environment variable CTK_MODULE_LOAD_PATH exists, it should point to a valid directory.
@@ -114,20 +119,20 @@ int ctkCmdLineModuleDefaultPathBuilderTest(int argc, char* argv[])
 
     qDebug() << "CTK_MODULE_LOAD_PATH does exist, and is set to:" << env.value("CTK_MODULE_LOAD_PATH") << ", and isExists() returns " << loadDir.exists();
 
-    if (loadDir.exists() && result.size() != 5)
+    if (loadDir.exists() && result.size() != 1)
     {
-      qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist and is valid, so there should be 5 entries";
+      qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist and is valid, so there should be 1 entries";
       return EXIT_FAILURE;
     }
-    else if (!loadDir.exists() && result.size() != 4)
+    else if (!loadDir.exists() && result.size() != 0)
     {
-      qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist but is invalid, so there should be 4 entries";
+      qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist but is invalid, so there should be 0 entries";
       return EXIT_FAILURE;
     }
   }
-  else if (result.size() != 4)
+  else if (result.size() != 0)
   {
-    qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did not exist, so there should still be 4 entries as previous test";
+    qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did not exist, so there should still be 0 entries.";
     return EXIT_FAILURE;
   }
 

+ 125 - 63
Libs/CommandLineModules/Core/ctkCmdLineModuleDefaultPathBuilder.cpp

@@ -23,6 +23,7 @@
 #include <QDebug>
 #include <QCoreApplication>
 #include <cstdlib>
+#include <QStringList>
 
 //----------------------------------------------------------------------------
 struct ctkCmdLineModuleDefaultPathBuilderPrivate
@@ -30,18 +31,21 @@ struct ctkCmdLineModuleDefaultPathBuilderPrivate
 public:
   ctkCmdLineModuleDefaultPathBuilderPrivate();
   ~ctkCmdLineModuleDefaultPathBuilderPrivate();
-  QStringList build() const;
+  void clear();
+  void setStrictMode(const bool& strict);
+  bool strictMode() const;
+  void addHomeDir(const QString& subFolder = QString());
+  void addCurrentDir(const QString& subFolder = QString());
+  void addApplicationDir(const QString& subFolder = QString());
+  void addCtkModuleLoadPathDir(const QString& subFolder = QString());
+  QStringList getDirectoryList() const;
 
-  void setLoadFromHomeDir(bool doLoad);
-  void setLoadFromCurrentDir(bool doLoad);
-  void setLoadFromApplicationDir(bool doLoad);
-  void setLoadFromCtkModuleLoadPath(bool doLoad);
+  bool isStrictMode;
+  QStringList directoryList;
 
-  bool LoadFromHomeDir;
-  bool LoadFromCurrentDir;
-  bool LoadFromApplicationDir;
-  bool LoadFromCtkModuleLoadPath;
+private:
 
+  QString addSubFolder(const QString& folder, const QString& subFolder);
 };
 
 //-----------------------------------------------------------------------------
@@ -49,92 +53,123 @@ public:
 
 //-----------------------------------------------------------------------------
 ctkCmdLineModuleDefaultPathBuilderPrivate::ctkCmdLineModuleDefaultPathBuilderPrivate()
-: LoadFromHomeDir(false)
-, LoadFromCurrentDir(false)
-, LoadFromApplicationDir(false)
-, LoadFromCtkModuleLoadPath(false)
+: isStrictMode(false)
 {
-
 }
 
+
 //-----------------------------------------------------------------------------
 ctkCmdLineModuleDefaultPathBuilderPrivate::~ctkCmdLineModuleDefaultPathBuilderPrivate()
 {
-
 }
 
+
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromHomeDir(bool doLoad)
+void ctkCmdLineModuleDefaultPathBuilderPrivate::clear()
 {
-  LoadFromHomeDir = doLoad;
+  directoryList.clear();
 }
 
+
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromCurrentDir(bool doLoad)
+void ctkCmdLineModuleDefaultPathBuilderPrivate::setStrictMode(const bool& strict)
 {
-  LoadFromCurrentDir = doLoad;
+  isStrictMode = strict;
 }
 
+
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromApplicationDir(bool doLoad)
+bool ctkCmdLineModuleDefaultPathBuilderPrivate::strictMode() const
 {
-  LoadFromApplicationDir = doLoad;
+  return isStrictMode;
 }
 
+
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromCtkModuleLoadPath(bool doLoad)
+QString ctkCmdLineModuleDefaultPathBuilderPrivate::addSubFolder(
+    const QString& folder, const QString& subFolder)
 {
-  LoadFromCtkModuleLoadPath = doLoad;
+  if (subFolder.length() > 0)
+  {
+    return folder + QDir::separator() + subFolder;
+  }
+  else
+  {
+    return folder;
+  }
 }
 
 
 //-----------------------------------------------------------------------------
-QStringList ctkCmdLineModuleDefaultPathBuilderPrivate::build() const
+void ctkCmdLineModuleDefaultPathBuilderPrivate::addHomeDir(const QString& subFolder)
 {
-  QStringList result;
+  if (QDir::home().exists())
+  {
+    QString result = addSubFolder(QDir::homePath(), subFolder);
+    directoryList << result;
+  }
+}
 
-  QString suffix = "cli-modules";
 
-  if (LoadFromCtkModuleLoadPath)
+//-----------------------------------------------------------------------------
+void ctkCmdLineModuleDefaultPathBuilderPrivate::addCurrentDir(const QString& subFolder)
+{
+  if (QDir::current().exists())
   {
-    char *ctkModuleLoadPath = getenv("CTK_MODULE_LOAD_PATH");
-    if (ctkModuleLoadPath != NULL)
-    {
-      QDir dir = QDir(QString(ctkModuleLoadPath));
-      if (dir.exists())
-      {
-        result << dir.canonicalPath();
-      }
-    }
+    QString result = addSubFolder(QDir::currentPath(), subFolder);
+    directoryList << result;
   }
+}
+
+
+//-----------------------------------------------------------------------------
+void ctkCmdLineModuleDefaultPathBuilderPrivate::addApplicationDir(const QString& subFolder)
+{
+  QString result = addSubFolder(QCoreApplication::applicationDirPath(), subFolder);
+  directoryList << result;
+}
 
-  if (LoadFromHomeDir)
+
+//-----------------------------------------------------------------------------
+void ctkCmdLineModuleDefaultPathBuilderPrivate::addCtkModuleLoadPathDir(
+    const QString& subFolder)
+{
+  char *ctkModuleLoadPath = getenv("CTK_MODULE_LOAD_PATH");
+  if (ctkModuleLoadPath != NULL)
   {
-    if (QDir::home().exists())
-    {
-      result << QDir::homePath();
-      result << QDir::homePath() + QDir::separator() + suffix;
-    }
+    QDir dir = QDir(QString(ctkModuleLoadPath));
+    QString result = addSubFolder(dir.canonicalPath(), subFolder);
+    directoryList << result;
   }
+}
 
-  if (LoadFromCurrentDir)
+
+//-----------------------------------------------------------------------------
+QStringList ctkCmdLineModuleDefaultPathBuilderPrivate::getDirectoryList() const
+{
+  if (!isStrictMode)
+  {
+    return directoryList;
+  }
+  else
   {
-    if (QDir::current().exists())
+    QStringList filteredList;
+    foreach (QString directory, directoryList)
     {
-      result << QDir::currentPath();
-      result << QDir::currentPath() + QDir::separator() + suffix;
+      QDir dir(directory);
+      if (dir.exists())
+      {
+        filteredList << directory;
+      }
     }
-  }
 
-  if (LoadFromApplicationDir)
-  {
-    result << QCoreApplication::applicationDirPath();
-    result << QCoreApplication::applicationDirPath() + QDir::separator() + suffix;
+    qDebug() << "Filtered directory list " << directoryList << " to " << filteredList;
+    return filteredList;
   }
-
-  return result;
 }
 
+
+
 //-----------------------------------------------------------------------------
 // ctkCmdLineModuleDefaultPathBuilder methods
 
@@ -149,32 +184,59 @@ ctkCmdLineModuleDefaultPathBuilder::~ctkCmdLineModuleDefaultPathBuilder()
 {
 }
 
+
 //-----------------------------------------------------------------------------
-QStringList ctkCmdLineModuleDefaultPathBuilder::build() const
+void ctkCmdLineModuleDefaultPathBuilder::clear()
 {
-  return d->build();
+  d->clear();
 }
 
+
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilder::setLoadFromHomeDir(bool doLoad)
+void ctkCmdLineModuleDefaultPathBuilder::setStrictMode(const bool& strict)
 {
-  d->setLoadFromHomeDir(doLoad);
+  d->setStrictMode(strict);
 }
 
+
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilder::setLoadFromCurrentDir(bool doLoad)
+bool ctkCmdLineModuleDefaultPathBuilder::strictMode() const
 {
-  d->setLoadFromCurrentDir(doLoad);
+  return d->strictMode();
 }
 
+
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilder::setLoadFromApplicationDir(bool doLoad)
+void ctkCmdLineModuleDefaultPathBuilder::addHomeDir(const QString& subFolder)
 {
-  d->setLoadFromApplicationDir(doLoad);
+  d->addHomeDir(subFolder);
 }
 
+
+//-----------------------------------------------------------------------------
+void ctkCmdLineModuleDefaultPathBuilder::addCurrentDir(const QString& subFolder)
+{
+  d->addCurrentDir(subFolder);
+}
+
+
+//-----------------------------------------------------------------------------
+void ctkCmdLineModuleDefaultPathBuilder::addApplicationDir(const QString& subFolder)
+{
+  d->addApplicationDir(subFolder);
+}
+
+
+//-----------------------------------------------------------------------------
+void ctkCmdLineModuleDefaultPathBuilder::addCtkModuleLoadPathDir(
+    const QString& subFolder)
+{
+  d->addCtkModuleLoadPathDir(subFolder);
+}
+
+
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilder::setLoadFromCtkModuleLoadPath(bool doLoad)
+QStringList ctkCmdLineModuleDefaultPathBuilder::getDirectoryList() const
 {
-  d->setLoadFromCtkModuleLoadPath(doLoad);
+  return d->getDirectoryList();
 }

+ 46 - 28
Libs/CommandLineModules/Core/ctkCmdLineModuleDefaultPathBuilder.h

@@ -29,24 +29,23 @@ struct ctkCmdLineModuleDefaultPathBuilderPrivate;
 
 /**
  * \class ctkCmdLineModuleDefaultPathBuilder
- * \brief Builds up a list of directory paths to search for command
- * line modules.
+ * \brief Builds up a list of directory paths to search for command line modules.
  * \ingroup CommandLineModulesCore_API
  * \author m.clarkson@ucl.ac.uk
  *
- * Implements the following basic strategy, depending on which boolean
- * flags are on: By default they are all off, as directory scanning is
- * often time consuming.
+ * Simple class to enable the user to easily add various directories
+ * to a list of directories. You create this object, add a load of directories
+ * and then call getDirectoryList() to get a StringList of directory locations.
  *
+ * The choices are:
  * <pre>
- * 1. CTK_MODULE_LOAD_PATH environment variable
- * 2. Home directory
- * 3. Home directory / cli-modules
- * 4. Current working directory
- * 5. Current working directory / cli-modules
- * 6. Application directory
- * 7. Application directory / cli-modules
+ * 1. The directory defined by the CTK_MODULE_LOAD_PATH environment variable or any sub-directory under this.
+ * 2. The directory defined by the users HOME directory, or any sub-directory under this.
+ * 3. The directory defined by the current working directory, or any sub-directory under this.
+ * 4. The directory defined by the application installation directory or any sub-directory under this.
  * </pre>
+ *
+ * A strictMode flag exists to decide if this class only returns directories that already exist.
  */
 class CTK_CMDLINEMODULECORE_EXPORT ctkCmdLineModuleDefaultPathBuilder
 {
@@ -57,35 +56,54 @@ public:
   ~ctkCmdLineModuleDefaultPathBuilder();
 
   /**
-   * @brief Instruct the builder to include the users
-   * home directory and sub-folder cli-modules.
+   * @brief Clears the current list of directories.
+   */
+  virtual void clear();
+
+  /**
+   * @brief Sets strict mode which checks that all directories already exist.
+   * @param strict if true this object will only return existing directories,
+   * if false, the return StringList is un-checked.
+   */
+  virtual void setStrictMode(const bool& strict);
+
+  /**
+   * @brief Returns the strict mode flag.
+   */
+  virtual bool strictMode() const;
+
+  /**
+   * @brief Adds the users home directory, or if specified a sub-directory.
+   *
+   * This depends on QDir::home() existing. If this is not the case,
+   * then this method will do nothing and ignore the request.
    */
-  virtual void setLoadFromHomeDir(bool doLoad);
+  virtual void addHomeDir(const QString& subFolder = QString());
 
   /**
-   * @brief Instruct the builder to include the current
-   * running directory and sub-folder cli-modules.
+   * @brief Adds the current working directory, or if specified a sub-directory.
+   *
+   * This depends on QDir::current() existing. If this is not the case,
+   * then this method will do nothing and ignore the request.
    */
-  virtual void setLoadFromCurrentDir(bool doLoad);
+  virtual void addCurrentDir(const QString& subFolder = QString());
 
   /**
-   * @brief Instruct the builder to include the application
-   * installation directory and sub-folder cli-modules.
+   * @brief Adds the application installation directory, or if specified a sub-directory.
    */
-  virtual void setLoadFromApplicationDir(bool doLoad);
+  virtual void addApplicationDir(const QString& subFolder = QString());
 
   /**
-   * @brief Instruct the builder to include the path denoted
-   * by the environment variable CTK_MODULE_LOAD_PATH.
+   * @brief Adds the directory denoted by the environment variable CTK_MODULE_LOAD_PATH,
+   * or if specified a sub-directory.
    */
-  virtual void setLoadFromCtkModuleLoadPath(bool doLoad);
+  virtual void addCtkModuleLoadPathDir(const QString& subFolder = QString());
 
   /**
-   * @brief Builds the list of paths to search and returns them
-   * as QStringList
-   * @return a QStringList of directory path names
+   * @brief Returns the QStringList containing directories.
+   * @return QStringList of directories or, if in strict mode, directories that already exist.
    */
-  virtual QStringList build() const;
+  virtual QStringList getDirectoryList() const;
 
 private: