changeset 1280:d6a65dc6d0ac

Plugins can access the command-line arguments used to launch Orthanc
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 03 Feb 2015 10:25:56 +0100
parents 7f3a65e84d4b
children 8dac11c78d71
files NEWS OrthancServer/main.cpp Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/OrthancPlugins.h Plugins/OrthancCPlugin/OrthancCPlugin.h Plugins/Samples/Basic/Plugin.c
diffstat 6 files changed, 134 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Mon Feb 02 16:14:29 2015 +0100
+++ b/NEWS	Tue Feb 03 10:25:56 2015 +0100
@@ -15,6 +15,7 @@
 -------
 
 * Introspection of plugins
+* Plugins can access the command-line arguments used to launch Orthanc
 * Plugins can extend Orthanc Explorer with custom JavaScript
 * Plugins can get/set global properties to save their configuration
 * Scan of folders for plugins
--- a/OrthancServer/main.cpp	Mon Feb 02 16:14:29 2015 +0100
+++ b/OrthancServer/main.cpp	Tue Feb 03 10:25:56 2015 +0100
@@ -381,7 +381,7 @@
 
 
 
-static bool StartOrthanc()
+static bool StartOrthanc(int argc, char *argv[])
 {
   std::auto_ptr<IDatabaseWrapper> database;
   database.reset(Configuration::CreateDatabaseWrapper());
@@ -465,6 +465,7 @@
 
 #if ENABLE_PLUGINS == 1
     OrthancPlugins orthancPlugins(context);
+    orthancPlugins.SetCommandLineArguments(argc, argv);
     orthancPlugins.SetOrthancRestApi(restApi);
 
     PluginsManager pluginsManager;
@@ -626,7 +627,7 @@
     {
       OrthancInitialize(configurationFile);
 
-      bool reset = StartOrthanc();
+      bool reset = StartOrthanc(argc, argv);
       if (reset)
       {
         OrthancFinalize();
--- a/Plugins/Engine/OrthancPlugins.cpp	Mon Feb 02 16:14:29 2015 +0100
+++ b/Plugins/Engine/OrthancPlugins.cpp	Tue Feb 03 10:25:56 2015 +0100
@@ -192,12 +192,16 @@
     boost::thread  changeThread_;
     bool done_;
     Properties properties_;
+    int argc_;
+    char** argv_;
 
     PImpl(ServerContext& context) : 
       context_(context), 
       restApi_(NULL),
       hasStorageArea_(false),
-      done_(false)
+      done_(false),
+      argc_(1),
+      argv_(NULL)
     {
       memset(&storageArea_, 0, sizeof(storageArea_));
     }
@@ -1058,6 +1062,31 @@
         return true;
       }
 
+      case _OrthancPluginService_GetCommandLineArgumentsCount:
+      {
+        const _OrthancPluginReturnSingleValue& p =
+          *reinterpret_cast<const _OrthancPluginReturnSingleValue*>(parameters);
+        *(p.resultUint32) = pimpl_->argc_ - 1;
+        return true;
+      }
+
+      case _OrthancPluginService_GetCommandLineArgument:
+      {
+        const _OrthancPluginGlobalProperty& p =
+          *reinterpret_cast<const _OrthancPluginGlobalProperty*>(parameters);
+        
+        if (p.property + 1 > pimpl_->argc_)
+        {
+          return false;
+        }
+        else
+        {
+          std::string arg = std::string(pimpl_->argv_[p.property + 1]);
+          *(p.result) = CopyString(arg);
+          return true;
+        }
+      }
+
       default:
         return false;
     }
@@ -1191,4 +1220,16 @@
       return it->second.c_str();
     }
   }
+
+
+  void OrthancPlugins::SetCommandLineArguments(int argc, char* argv[])
+  {
+    if (argc < 1 || argv == NULL)
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+
+    pimpl_->argc_ = argc;
+    pimpl_->argv_ = argv;
+  }
 }
--- a/Plugins/Engine/OrthancPlugins.h	Mon Feb 02 16:14:29 2015 +0100
+++ b/Plugins/Engine/OrthancPlugins.h	Tue Feb 03 10:25:56 2015 +0100
@@ -112,5 +112,7 @@
 
     const char* GetProperty(const char* plugin,
                             _OrthancPluginProperty property) const;
+
+    void SetCommandLineArguments(int argc, char* argv[]);
   };
 }
--- a/Plugins/OrthancCPlugin/OrthancCPlugin.h	Mon Feb 02 16:14:29 2015 +0100
+++ b/Plugins/OrthancCPlugin/OrthancCPlugin.h	Tue Feb 03 10:25:56 2015 +0100
@@ -251,6 +251,8 @@
     _OrthancPluginService_SetPluginProperty = 7,
     _OrthancPluginService_GetGlobalProperty = 8,
     _OrthancPluginService_SetGlobalProperty = 9,
+    _OrthancPluginService_GetCommandLineArgumentsCount = 10,
+    _OrthancPluginService_GetCommandLineArgument = 11,
 
     /* Registration of callbacks */
     _OrthancPluginService_RegisterRestCallback = 1000,
@@ -1887,6 +1889,79 @@
 
 
 
+  typedef struct
+  {
+    int32_t   *resultInt32;
+    uint32_t  *resultUint32;
+    int64_t   *resultInt64;
+    uint64_t  *resultUint64;
+  } _OrthancPluginReturnSingleValue;
+
+  /**
+   * @brief Get the number of command-line arguments.
+   *
+   * Retrieve the number of command-line arguments that were used to launch Orthanc.
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @return The number of arguments.
+   **/
+  ORTHANC_PLUGIN_INLINE uint32_t OrthancPluginGetCommandLineArgumentsCount(
+    OrthancPluginContext*  context)
+  {
+    uint32_t count = 0;
+
+    _OrthancPluginReturnSingleValue params;
+    memset(&params, 0, sizeof(params));
+    params.resultUint32 = &count;
+
+    if (context->InvokeService(context, _OrthancPluginService_GetCommandLineArgumentsCount, &params))
+    {
+      /* Error */
+      return 0;
+    }
+    else
+    {
+      return count;
+    }
+  }
+
+
+
+  /**
+   * @brief Get the value of a command-line argument.
+   *
+   * Get the value of one of the command-line arguments that were used
+   * to launch Orthanc. The number of available arguments can be
+   * retrieved by OrthancPluginGetCommandLineArgumentsCount().
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param argument The index of the argument.
+   * @return The value of the argument, or NULL in the case of an error. This
+   * string must be freed by OrthancPluginFreeString().
+   **/
+  ORTHANC_PLUGIN_INLINE char* OrthancPluginGetCommandLineArgument(
+    OrthancPluginContext*  context,
+    uint32_t               argument)
+  {
+    char* result;
+
+    _OrthancPluginGlobalProperty params;
+    params.result = &result;
+    params.property = (int32_t) argument;
+    params.value = NULL;
+
+    if (context->InvokeService(context, _OrthancPluginService_GetCommandLineArgument, &params))
+    {
+      /* Error */
+      return NULL;
+    }
+    else
+    {
+      return result;
+    }
+  }
+
+
 
 
 #ifdef  __cplusplus
--- a/Plugins/Samples/Basic/Plugin.c	Mon Feb 02 16:14:29 2015 +0100
+++ b/Plugins/Samples/Basic/Plugin.c	Tue Feb 03 10:25:56 2015 +0100
@@ -280,7 +280,7 @@
 {
   OrthancPluginMemoryBuffer tmp;
   char info[1024], *s;
-  int counter;
+  int counter, i;
 
   context = c;
   OrthancPluginLogWarning(context, "Sample plugin is initializing");
@@ -316,6 +316,16 @@
   OrthancPluginLogWarning(context, info);
   OrthancPluginFreeString(context, s);
 
+  /* Print the command-line arguments of Orthanc */
+  counter = OrthancPluginGetCommandLineArgumentsCount(context);
+  for (i = 0; i < counter; i++)
+  {
+    s = OrthancPluginGetCommandLineArgument(context, i);
+    sprintf(info, "  Command-line argument %d: \"%s\"", i, s);
+    OrthancPluginLogWarning(context, info);
+    OrthancPluginFreeString(context, s);    
+  }
+
   /* Register the callbacks */
   OrthancPluginRegisterRestCallback(context, "/(plu.*)/hello", Callback1);
   OrthancPluginRegisterRestCallback(context, "/plu.*/image", Callback2);