diff options
| author | Matthias Vogelgesang <matthias.vogelgesang@kit.edu> | 2016-04-01 16:23:27 +0200 | 
|---|---|---|
| committer | Matthias Vogelgesang <matthias.vogelgesang@kit.edu> | 2016-04-01 16:23:27 +0200 | 
| commit | b83e7a311580e9c0ed7bd58637b01c10d4b4f8a3 (patch) | |
| tree | 03305140e9fb17885b83c25bfd91081119ece146 | |
| parent | fa3f523af9ac95945af59bdaa729d71d79327a76 (diff) | |
cli: pass -p/--property assignment to constructor
| -rw-r--r-- | bin/tools/benchmark.c | 4 | ||||
| -rw-r--r-- | bin/tools/common.c | 54 | ||||
| -rw-r--r-- | bin/tools/common.h | 4 | ||||
| -rw-r--r-- | bin/tools/grab.c | 4 | 
4 files changed, 59 insertions, 7 deletions
diff --git a/bin/tools/benchmark.c b/bin/tools/benchmark.c index 01702fe..b3a873f 100644 --- a/bin/tools/benchmark.c +++ b/bin/tools/benchmark.c @@ -335,7 +335,7 @@ main (int argc, char *argv[])  #endif      manager = uca_plugin_manager_new (); -    context = uca_option_context_new (manager); +    context = uca_common_context_new (manager);      g_option_context_add_main_entries (context, entries, NULL);      if (!g_option_context_parse (context, &argc, &argv, &error)) { @@ -352,7 +352,7 @@ main (int argc, char *argv[])      g_assert_no_error (error);      g_log_set_handler (NULL, G_LOG_LEVEL_MASK, log_handler, log_channel); -    camera = uca_plugin_manager_get_camera (manager, argv[argc - 1], &error, NULL); +    camera = uca_common_get_camera (manager, argv[argc - 1], &error);      if (camera == NULL) {          g_print ("Initialization: %s\n", error->message); diff --git a/bin/tools/common.c b/bin/tools/common.c index 3721ff0..8de5e87 100644 --- a/bin/tools/common.c +++ b/bin/tools/common.c @@ -17,6 +17,7 @@  #include "common.h" +static gchar **uca_prop_assignment_array = NULL;  static gchar *  get_camera_list (UcaPluginManager *manager) @@ -48,14 +49,65 @@ get_camera_list (UcaPluginManager *manager)  }  GOptionContext * -uca_option_context_new (UcaPluginManager *manager) +uca_common_context_new (UcaPluginManager *manager)  {      GOptionContext *context; +    GOptionGroup *common;      gchar *camera_list; +    static GOptionEntry entries[] = { +        { "prop", 'p', 0, G_OPTION_ARG_STRING_ARRAY, &uca_prop_assignment_array, "Property assignment via `name=value'", NULL }, +        { NULL } +    }; +      camera_list = get_camera_list (manager);      context = g_option_context_new (camera_list);      g_free (camera_list); +    common = g_option_group_new ("properties", "Property options", "Show help for property assignment", NULL, NULL); +    g_option_group_add_entries (common, entries); +    g_option_context_add_group (context, common); +      return context;  } + +UcaCamera * +uca_common_get_camera (UcaPluginManager *manager, const gchar *name, GError **error) +{ +    UcaCamera *camera; +    GParameter *params; +    guint n_props; + +    n_props = g_strv_length (uca_prop_assignment_array); +    params = g_new0 (GParameter, n_props); + +    for (guint i = 0; i < n_props; i++) { +        gchar **split; + +        split = g_strsplit (uca_prop_assignment_array[i], "=", 2); + +        if (g_strv_length (split) < 2) +            goto cleanup; + +        params[i].name = g_strdup (split[0]); + +        /* We cannot check the type before instantiation, classic chicken-egg +         * situation ... so, let's try string. */ +        g_value_init (¶ms[i].value, G_TYPE_STRING); +        g_value_set_string (¶ms[i].value, split[1]); + +cleanup: +        g_strfreev (split); +    } + +    camera = uca_plugin_manager_get_camerav (manager, name, n_props, params, error); + +    for (guint i = 0; i < n_props; i++) { +        /* cast is legit, because we created the string */ +        g_free ((gchar *) params[i].name); +    } + +    g_free (params); + +    return camera; +} diff --git a/bin/tools/common.h b/bin/tools/common.h index 5609e22..51e2c41 100644 --- a/bin/tools/common.h +++ b/bin/tools/common.h @@ -22,7 +22,7 @@  #include "uca-plugin-manager.h" -GOptionContext  *uca_option_context_new (UcaPluginManager *manager); - +GOptionContext  *uca_common_context_new (UcaPluginManager *manager); +UcaCamera       *uca_common_get_camera (UcaPluginManager *manager, const gchar *name, GError **error);  #endif diff --git a/bin/tools/grab.c b/bin/tools/grab.c index 9eaa48b..e7c6eff 100644 --- a/bin/tools/grab.c +++ b/bin/tools/grab.c @@ -240,7 +240,7 @@ main (int argc, char *argv[])  #endif      manager = uca_plugin_manager_new (); -    context = uca_option_context_new (manager); +    context = uca_common_context_new (manager);      g_option_context_add_main_entries (context, entries, NULL);      if (!g_option_context_parse (context, &argc, &argv, &error)) { @@ -258,7 +258,7 @@ main (int argc, char *argv[])          goto cleanup_manager;      } -    camera = uca_plugin_manager_get_camera (manager, argv[argc - 1], &error, NULL); +    camera = uca_common_get_camera (manager, argv[argc - 1], &error);      if (camera == NULL) {          g_print ("Error during initialization: %s\n", error->message);  | 
