From e4edf31c86f3ea891fe493450c6eb134542095e5 Mon Sep 17 00:00:00 2001
From: Matthias Vogelgesang <matthias.vogelgesang@gmail.com>
Date: Thu, 18 Apr 2013 09:04:30 +0200
Subject: Build list of cameras only in one place

---
 bin/tools/CMakeLists.txt |  6 +++--
 bin/tools/benchmark.c    | 51 +++++++++++-----------------------------
 bin/tools/common.c       | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
 bin/tools/common.h       | 28 ++++++++++++++++++++++
 bin/tools/grab.c         | 50 ++++++++++------------------------------
 5 files changed, 117 insertions(+), 78 deletions(-)
 create mode 100644 bin/tools/common.c
 create mode 100644 bin/tools/common.h

(limited to 'bin/tools')

diff --git a/bin/tools/CMakeLists.txt b/bin/tools/CMakeLists.txt
index cea2912..2bec9c9 100644
--- a/bin/tools/CMakeLists.txt
+++ b/bin/tools/CMakeLists.txt
@@ -27,11 +27,13 @@ add_executable(uca-gen-doc gen-doc.c)
 target_link_libraries(uca-gen-doc ${libs})
 
 # --- uca-grab
-add_executable(uca-grab grab.c)
+add_executable(uca-grab
+               grab.c common.c)
 target_link_libraries(uca-grab ringbuffer ${libs})
 
 # --- uca-benchmark
-add_executable(uca-benchmark benchmark.c)
+add_executable(uca-benchmark
+               benchmark.c common.c)
 target_link_libraries(uca-benchmark ${libs})
 
 install(TARGETS uca-benchmark uca-grab uca-gen-doc
diff --git a/bin/tools/benchmark.c b/bin/tools/benchmark.c
index 6d2ed9a..fee40f2 100644
--- a/bin/tools/benchmark.c
+++ b/bin/tools/benchmark.c
@@ -21,6 +21,8 @@
 #include <stdlib.h>
 #include "uca-camera.h"
 #include "uca-plugin-manager.h"
+#include "common.h"
+
 
 typedef void (*GrabFrameFunc) (UcaCamera *camera, gpointer buffer, guint n_frames);
 
@@ -36,35 +38,6 @@ sigint_handler(int signal)
     exit (signal);
 }
 
-static gchar *
-get_camera_list (void)
-{
-    GList *types;
-    GString *str;
-    UcaPluginManager *manager;
-
-    manager = uca_plugin_manager_new ();
-    types = uca_plugin_manager_get_available_cameras (manager);
-    str = g_string_new ("[ ");
-
-    if (types != NULL) {
-        for (GList *it = g_list_first (types); it != NULL; it = g_list_next (it)) {
-            gchar *name = (gchar *) it->data;
-
-            if (g_list_next (it) == NULL)
-                g_string_append_printf (str, "%s ]", name);
-            else
-                g_string_append_printf (str, "%s, ", name);
-        }
-    }
-    else {
-        g_string_append (str, "]");
-    }
-
-    g_object_unref (manager);
-    return g_string_free (str, FALSE);
-}
-
 static void
 log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user)
 {
@@ -244,7 +217,6 @@ main (int argc, char *argv[])
     GOptionContext *context;
     UcaPluginManager *manager;
     GIOChannel *log_channel;
-    gchar *cam_list;
     GError *error = NULL;
     static gint n_frames = 100;
     static gint n_runs = 3;
@@ -258,38 +230,41 @@ main (int argc, char *argv[])
     (void) signal (SIGINT, sigint_handler);
     g_type_init ();
 
-    cam_list = get_camera_list ();
-    context = g_option_context_new (cam_list);
+    manager = uca_plugin_manager_new ();
+    context = uca_option_context_new (manager);
     g_option_context_add_main_entries (context, entries, NULL);
-    g_free (cam_list);
 
     if (!g_option_context_parse (context, &argc, &argv, &error)) {
         g_print ("Failed parsing arguments: %s\n", error->message);
-        exit (1);
+        goto cleanup_manager;
     }
 
     if (argc < 2) {
         g_print ("%s\n", g_option_context_get_help (context, TRUE, NULL));
-        exit (0);
+        goto cleanup_manager;
     }
 
     log_channel = g_io_channel_new_file ("error.log", "a+", &error);
     g_assert_no_error (error);
     g_log_set_handler (NULL, G_LOG_LEVEL_MASK, log_handler, log_channel);
 
-    manager = uca_plugin_manager_new ();
     camera = uca_plugin_manager_get_camera (manager, argv[1], &error, NULL);
 
     if (camera == NULL) {
         g_error ("Initialization: %s", error->message);
-        return 1;
+        goto cleanup_camera;
     }
 
     benchmark (camera, n_runs, n_frames);
 
-    g_object_unref (camera);
     g_io_channel_shutdown (log_channel, TRUE, &error);
     g_assert_no_error (error);
 
+cleanup_camera:
+    g_object_unref (camera);
+
+cleanup_manager:
+    g_object_unref (manager);
+
     return 0;
 }
diff --git a/bin/tools/common.c b/bin/tools/common.c
new file mode 100644
index 0000000..6ada379
--- /dev/null
+++ b/bin/tools/common.c
@@ -0,0 +1,60 @@
+/* Copyright (C) 2011-2013 Matthias Vogelgesang <matthias.vogelgesang@kit.edu>
+   (Karlsruhe Institute of Technology)
+
+   This library is free software; you can redistribute it and/or modify it
+   under the terms of the GNU Lesser General Public License as published by the
+   Free Software Foundation; either version 2.1 of the License, or (at your
+   option) any later version.
+
+   This library is distributed in the hope that it will be useful, but WITHOUT
+   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+   details.
+
+   You should have received a copy of the GNU Lesser General Public License along
+   with this library; if not, write to the Free Software Foundation, Inc., 51
+   Franklin St, Fifth Floor, Boston, MA 02110, USA */
+
+#include "common.h"
+
+
+static gchar *
+get_camera_list (UcaPluginManager *manager)
+{
+    GList *types;
+    GString *str;
+
+    manager = uca_plugin_manager_new ();
+    types = uca_plugin_manager_get_available_cameras (manager);
+    str = g_string_new ("[ ");
+
+    if (types != NULL) {
+        for (GList *it = g_list_first (types); it != NULL; it = g_list_next (it)) {
+            gchar *name = (gchar *) it->data;
+
+            if (g_list_next (it) == NULL)
+                g_string_append_printf (str, "%s ]", name);
+            else
+                g_string_append_printf (str, "%s, ", name);
+        }
+    }
+    else {
+        g_string_append (str, "]");
+    }
+
+    g_object_unref (manager);
+    return g_string_free (str, FALSE);
+}
+
+GOptionContext *
+uca_option_context_new (UcaPluginManager *manager)
+{
+    GOptionContext *context;
+    gchar *camera_list;
+
+    camera_list = get_camera_list (manager);
+    context = g_option_context_new (camera_list);
+    g_free (camera_list);
+
+    return context;
+}
diff --git a/bin/tools/common.h b/bin/tools/common.h
new file mode 100644
index 0000000..5609e22
--- /dev/null
+++ b/bin/tools/common.h
@@ -0,0 +1,28 @@
+/* Copyright (C) 2011-2013 Matthias Vogelgesang <matthias.vogelgesang@kit.edu>
+   (Karlsruhe Institute of Technology)
+
+   This library is free software; you can redistribute it and/or modify it
+   under the terms of the GNU Lesser General Public License as published by the
+   Free Software Foundation; either version 2.1 of the License, or (at your
+   option) any later version.
+
+   This library is distributed in the hope that it will be useful, but WITHOUT
+   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+   details.
+
+   You should have received a copy of the GNU Lesser General Public License along
+   with this library; if not, write to the Free Software Foundation, Inc., 51
+   Franklin St, Fifth Floor, Boston, MA 02110, USA */
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <glib.h>
+#include "uca-plugin-manager.h"
+
+
+GOptionContext  *uca_option_context_new (UcaPluginManager *manager);
+
+
+#endif
diff --git a/bin/tools/grab.c b/bin/tools/grab.c
index 972f000..72ae3b2 100644
--- a/bin/tools/grab.c
+++ b/bin/tools/grab.c
@@ -23,6 +23,7 @@
 #include "uca-plugin-manager.h"
 #include "uca-camera.h"
 #include "ring-buffer.h"
+#include "common.h"
 
 #ifdef HAVE_LIBTIFF
 #include <tiffio.h>
@@ -36,35 +37,6 @@ typedef struct {
 } Options;
 
 
-static gchar *
-get_camera_list (void)
-{
-    GList *types;
-    GString *str;
-    UcaPluginManager *manager;
-
-    manager = uca_plugin_manager_new ();
-    types = uca_plugin_manager_get_available_cameras (manager);
-    str = g_string_new ("[ ");
-
-    if (types != NULL) {
-        for (GList *it = g_list_first (types); it != NULL; it = g_list_next (it)) {
-            gchar *name = (gchar *) it->data;
-
-            if (g_list_next (it) == NULL)
-                g_string_append_printf (str, "%s ]", name);
-            else
-                g_string_append_printf (str, "%s, ", name);
-        }
-    }
-    else {
-        g_string_append (str, "]");
-    }
-
-    g_object_unref (manager);
-    return g_string_free (str, FALSE);
-}
-
 static guint
 get_bytes_per_pixel (guint bits_per_pixel)
 {
@@ -236,7 +208,6 @@ main (int argc, char *argv[])
     GOptionContext *context;
     UcaPluginManager *manager;
     UcaCamera *camera;
-    gchar *cam_list;
     GError *error = NULL;
 
     static Options opts = {
@@ -254,32 +225,30 @@ main (int argc, char *argv[])
 
     g_type_init();
 
-    cam_list = get_camera_list ();
-    context = g_option_context_new (cam_list);
+    manager = uca_plugin_manager_new ();
+    context = uca_option_context_new (manager);
     g_option_context_add_main_entries (context, entries, NULL);
-    g_free (cam_list);
 
     if (!g_option_context_parse (context, &argc, &argv, &error)) {
         g_print ("Failed parsing arguments: %s\n", error->message);
-        exit (1);
+        goto cleanup_manager;
     }
 
     if (argc < 2) {
         g_print ("%s\n", g_option_context_get_help (context, TRUE, NULL));
-        exit (0);
+        goto cleanup_manager;
     }
 
     if (opts.n_frames < 0 && opts.duration < 0.0) {
         g_print ("You must specify at least one of --num-frames and --output.\n");
-        exit (1);
+        goto cleanup_manager;
     }
 
-    manager = uca_plugin_manager_new ();
     camera = uca_plugin_manager_get_camera (manager, argv[1], &error, NULL);
 
     if (camera == NULL) {
         g_print ("Error during initialization: %s\n", error->message);
-        exit (1);
+        goto cleanup_camera;
     }
 
     error = record_frames (camera, &opts);
@@ -287,6 +256,11 @@ main (int argc, char *argv[])
     if (error != NULL)
         g_print ("Error: %s\n", error->message);
 
+cleanup_camera:
     g_object_unref (camera);
+
+cleanup_manager:
+    g_object_unref (manager);
+
     return error != NULL ? 1 : 0;
 }
-- 
cgit v1.2.3