summaryrefslogtreecommitdiff
path: root/node-startup-controller/glib-extensions.c
blob: 113e006bb732b363c7593d67cc8e11b4f37a5ca7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* vi:set et ai sw=2 sts=2 ts=2: */
/* SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2012, GENIVI
 *
 * This file is part of node-startup-controller.
 *
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License (MPL), v. 2.0.
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * For further information see http://www.genivi.org/.
 *
 * List of changes:
 * 2015-04-30, Jonathan Maw, List of changes started
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>

#include <node-startup-controller/glib-extensions.h>



/**
 * SECTION: glib-extensions
 * @title: GLib extensions
 * @short_description: Auxiliary functions which are not provided by GLib.
 * @stability: Internal
 *
 * Auxiliary functions related to the GLib library but not provided by GLib.
 */



/**
 * g_variant_lookup_value_with_int_key:
 * @dictionary: A dictionary #GVariant.
 * @key: The key to lookup in the @dictionary.
 * @expected_type: A #GVariantType to check that the value corresponding to the @key has
 * the correct type. 
 *
 * Looks up a value in a dictionary #GVariant using an integer @key.
 * This function only works with dictionaries of the type a{ias}.
 * In the event that dictionary has the type a{ias}, the @key is found and the value
 * belonging to this @key has the correct #GVariantType, then the value is returned.
 * Otherwise the returned value is %NULL.
 *
 * Returns: The value associated with the the dictionary key, or %NULL.
 */
GVariant *
g_variant_lookup_value_with_int_key (GVariant           *dictionary,
                                     const gint          key,
                                     const GVariantType *expected_type)
{
  GVariantIter iter;
  GVariant    *value = NULL;
  gint32       current_key;


  g_return_val_if_fail (dictionary != NULL, NULL);
  g_return_val_if_fail (expected_type != NULL, NULL);
  
  if (!g_variant_is_of_type (dictionary, G_VARIANT_TYPE ("a{ias}")))
    return NULL;

  g_variant_iter_init (&iter, dictionary);
  while (g_variant_iter_loop (&iter, "{i@as}", &current_key, &value))
    {
      if (current_key == key)
        {
          if (value != NULL && g_variant_is_of_type (value, expected_type))
            return value;
          else
            return NULL;
        }
    }

  return NULL;
}



/**
 * g_variant_string_array_has_string:
 * @array: A #GVariant holding an array of strings.
 * @str: A string to check for in @array.
 *
 * Checks if @array includes the string @str.
 *
 * Returns: TRUE if @array includes the string @str, otherwise FALSE.
 */
gboolean
g_variant_string_array_has_string (GVariant    *array,
                                   const gchar *str)
{
  gboolean found = FALSE;
  gchar   *current_str;
  guint    n;

  for (n = 0; array != NULL && !found && n < g_variant_n_children (array); n++)
    {
      g_variant_get_child (array, n, "&s", &current_str);
      if (g_strcmp0 (str, current_str) == 0)
        found = TRUE;
    }

  return found;
}



/**
 * g_int_pointer_compare:
 * @a: A #gconstpointer.
 * @b: Another #gconstpointer.
 *
 * Compares @a and @b, assuming that they are integers represented as pointers. Returns
 * a negative value if @a is less than @b, zero if they are equal and a positive value if
 * @b is greater than @a.
 *
 * Returns: A negative value if @a is lesser, a positive value if @a is greater, and zero
 * if @a is equal to @b.
 */
gint
g_int_pointer_compare (gconstpointer a, gconstpointer b)
{
  return GPOINTER_TO_INT (a) - GPOINTER_TO_INT (b);
}