LCOV - code coverage report
Current view: top level - glib/gio/tests - gapplication-example-cmdline3.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 0 43 0.0 %
Date: 2024-04-23 05:16:05 Functions: 0 3 0.0 %
Branches: 0 8 0.0 %

           Branch data     Line data    Source code
       1                 :            : #include <gio/gio.h>
       2                 :            : #include <stdlib.h>
       3                 :            : #include <string.h>
       4                 :            : 
       5                 :            : static gboolean
       6                 :          0 : my_cmdline_handler (gpointer data)
       7                 :            : {
       8                 :          0 :   GApplicationCommandLine *cmdline = data;
       9                 :            :   gchar **args;
      10                 :            :   gchar **argv;
      11                 :            :   gint argc;
      12                 :            :   gint arg1;
      13                 :            :   gboolean arg2;
      14                 :            :   gboolean help;
      15                 :            :   GOptionContext *context;
      16                 :          0 :   GOptionEntry entries[] = {
      17                 :            :     { "arg1", 0, 0, G_OPTION_ARG_INT, &arg1, NULL, NULL },
      18                 :            :     { "arg2", 0, 0, G_OPTION_ARG_NONE, &arg2, NULL, NULL },
      19                 :            :     { "help", '?', 0, G_OPTION_ARG_NONE, &help, NULL, NULL },
      20                 :            :     G_OPTION_ENTRY_NULL
      21                 :            :   };
      22                 :            :   GError *error;
      23                 :            :   gint i;
      24                 :            : 
      25                 :          0 :   args = g_application_command_line_get_arguments (cmdline, &argc);
      26                 :            : 
      27                 :            :   /* We have to make an extra copy of the array, since g_option_context_parse()
      28                 :            :    * assumes that it can remove strings from the array without freeing them.
      29                 :            :    */
      30                 :          0 :   argv = g_new (gchar*, argc + 1);
      31         [ #  # ]:          0 :   for (i = 0; i <= argc; i++)
      32                 :          0 :     argv[i] = args[i];
      33                 :            : 
      34                 :          0 :   context = g_option_context_new (NULL);
      35                 :          0 :   g_option_context_set_help_enabled (context, FALSE);
      36                 :          0 :   g_option_context_add_main_entries (context, entries, NULL);
      37                 :            : 
      38                 :          0 :   arg1 = 0;
      39                 :          0 :   arg2 = FALSE;
      40                 :          0 :   help = FALSE;
      41                 :          0 :   error = NULL;
      42         [ #  # ]:          0 :   if (!g_option_context_parse (context, &argc, &argv, &error))
      43                 :            :     {
      44                 :          0 :       g_application_command_line_printerr (cmdline, "%s\n", error->message);
      45                 :          0 :       g_error_free (error);
      46                 :          0 :       g_application_command_line_set_exit_status (cmdline, 1);
      47                 :            :     }
      48         [ #  # ]:          0 :   else if (help)
      49                 :            :     {
      50                 :            :       gchar *text;
      51                 :          0 :       text = g_option_context_get_help (context, FALSE, NULL);
      52                 :          0 :       g_application_command_line_print (cmdline, "%s",  text);
      53                 :          0 :       g_free (text);
      54                 :            :     }
      55                 :            :   else
      56                 :            :     {
      57                 :          0 :       g_application_command_line_print (cmdline, "arg1 is %d and arg2 is %s\n",
      58         [ #  # ]:          0 :                                         arg1, arg2 ? "TRUE" : "FALSE");
      59                 :          0 :       g_application_command_line_set_exit_status (cmdline, 0);
      60                 :            :     }
      61                 :            : 
      62                 :          0 :   g_free (argv);
      63                 :          0 :   g_strfreev (args);
      64                 :            : 
      65                 :          0 :   g_option_context_free (context);
      66                 :            : 
      67                 :            :   /* we are done handling this commandline */
      68                 :          0 :   g_object_unref (cmdline);
      69                 :            : 
      70                 :          0 :   return G_SOURCE_REMOVE;
      71                 :            : }
      72                 :            : 
      73                 :            : static int
      74                 :          0 : command_line (GApplication            *application,
      75                 :            :               GApplicationCommandLine *cmdline)
      76                 :            : {
      77                 :            :   /* keep the application running until we are done with this commandline */
      78                 :          0 :   g_application_hold (application);
      79                 :            : 
      80                 :          0 :   g_object_set_data_full (G_OBJECT (cmdline),
      81                 :            :                           "application", application,
      82                 :            :                           (GDestroyNotify)g_application_release);
      83                 :            : 
      84                 :          0 :   g_object_ref (cmdline);
      85                 :          0 :   g_idle_add (my_cmdline_handler, cmdline);
      86                 :            : 
      87                 :          0 :   return 0;
      88                 :            : }
      89                 :            : 
      90                 :            : int
      91                 :          0 : main (int argc, char **argv)
      92                 :            : {
      93                 :            :   GApplication *app;
      94                 :            :   int status;
      95                 :            : 
      96                 :          0 :   app = g_application_new ("org.gtk.TestApplication",
      97                 :            :                            G_APPLICATION_HANDLES_COMMAND_LINE);
      98                 :          0 :   g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
      99                 :          0 :   g_application_set_inactivity_timeout (app, 10000);
     100                 :            : 
     101                 :          0 :   status = g_application_run (app, argc, argv);
     102                 :            : 
     103                 :          0 :   g_object_unref (app);
     104                 :            : 
     105                 :          0 :   return status;
     106                 :            : }

Generated by: LCOV version 1.14