Blame src/libotutil/ot-tool-util.c

rpm-build 0fba15
/*
rpm-build 0fba15
 * Copyright (C) 2015 Colin Walters <walters@verbum.org>
rpm-build 0fba15
 *
rpm-build 0fba15
 * SPDX-License-Identifier: LGPL-2.0+
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is free software; you can redistribute it and/or
rpm-build 0fba15
 * modify it under the terms of the GNU Lesser General Public
rpm-build 0fba15
 * License as published by the Free Software Foundation; either
rpm-build 0fba15
 * version 2 of the License, or (at your option) any later version.
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is distributed in the hope that it will be useful,
rpm-build 0fba15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0fba15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0fba15
 * Lesser General Public License for more details.
rpm-build 0fba15
 *
rpm-build 0fba15
 * You should have received a copy of the GNU Lesser General Public
rpm-build 0fba15
 * License along with this library; if not, write to the
rpm-build 0fba15
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build 0fba15
 * Boston, MA 02111-1307, USA.
rpm-build 0fba15
 */
rpm-build 0fba15
rpm-build 0fba15
#include "config.h"
rpm-build 0fba15
rpm-build 0fba15
#include "otutil.h"
rpm-build 0fba15
#include "ot-tool-util.h"
rpm-build 0fba15
rpm-build 0fba15
gboolean
rpm-build 0fba15
ot_parse_boolean (const char  *value,
rpm-build 0fba15
                  gboolean    *out_parsed,
rpm-build 0fba15
                  GError     **error)
rpm-build 0fba15
{
rpm-build 0fba15
#define ARG_EQ(x, y) (g_ascii_strcasecmp(x, y) == 0)
rpm-build 0fba15
  if (ARG_EQ(value, "1")
rpm-build 0fba15
      || ARG_EQ(value, "true")
rpm-build 0fba15
      || ARG_EQ(value, "yes"))
rpm-build 0fba15
    *out_parsed = TRUE;
rpm-build 0fba15
  else if (ARG_EQ(value, "0")
rpm-build 0fba15
           || ARG_EQ(value, "false")
rpm-build 0fba15
           || ARG_EQ(value, "no")
rpm-build 0fba15
           || ARG_EQ(value, "none"))
rpm-build 0fba15
    *out_parsed = FALSE;
rpm-build 0fba15
  else
rpm-build 0fba15
    {
rpm-build 0fba15
      return glnx_throw (error, "Invalid boolean argument '%s'", value);
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  return TRUE;
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
gboolean
rpm-build 0fba15
ot_parse_keyvalue (const char  *keyvalue,
rpm-build 0fba15
                   char       **out_key,
rpm-build 0fba15
                   char       **out_value,
rpm-build 0fba15
                   GError     **error)
rpm-build 0fba15
{
rpm-build 0fba15
  const char *eq = strchr (keyvalue, '=');
rpm-build 0fba15
  if (!eq)
rpm-build 0fba15
    {
rpm-build 0fba15
      return glnx_throw (error, "Missing '=' in KEY=VALUE for --set");
rpm-build 0fba15
    }
rpm-build 0fba15
  *out_key = g_strndup (keyvalue, eq - keyvalue);
rpm-build 0fba15
  *out_value = g_strdup (eq + 1);
rpm-build 0fba15
  return TRUE;
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
/**
rpm-build 0fba15
 * Note: temporarily copied from GLib: https://github.com/GNOME/glib/blob/a419146578a42c760cff684292465b38df855f75/glib/garray.c#L1664
rpm-build 0fba15
 * See documentation at: https://developer.gnome.org/glib/stable/glib-Pointer-Arrays.html#g-ptr-array-find-with-equal-func
rpm-build 0fba15
 *
rpm-build 0fba15
 * ot_ptr_array_find_with_equal_func: (skip)
rpm-build 0fba15
 * @haystack: pointer array to be searched
rpm-build 0fba15
 * @needle: pointer to look for
rpm-build 0fba15
 * @equal_func: (nullable): the function to call for each element, which should
rpm-build 0fba15
 *    return %TRUE when the desired element is found; or %NULL to use pointer
rpm-build 0fba15
 *    equality
rpm-build 0fba15
 * @index_: (optional) (out caller-allocates): return location for the index of
rpm-build 0fba15
 *    the element, if found
rpm-build 0fba15
 *
rpm-build 0fba15
 * Checks whether @needle exists in @haystack, using the given @equal_func.
rpm-build 0fba15
 * If the element is found, %TRUE is returned and the element’s index is
rpm-build 0fba15
 * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_
rpm-build 0fba15
 * is undefined. If @needle exists multiple times in @haystack, the index of
rpm-build 0fba15
 * the first instance is returned.
rpm-build 0fba15
 *
rpm-build 0fba15
 * @equal_func is called with the element from the array as its first parameter,
rpm-build 0fba15
 * and @needle as its second parameter. If @equal_func is %NULL, pointer
rpm-build 0fba15
 * equality is used.
rpm-build 0fba15
 *
rpm-build 0fba15
 * Returns: %TRUE if @needle is one of the elements of @haystack
rpm-build 0fba15
 * Since: 2.54
rpm-build 0fba15
 */
rpm-build 0fba15
gboolean
rpm-build 0fba15
ot_ptr_array_find_with_equal_func (GPtrArray     *haystack,
rpm-build 0fba15
                                   gconstpointer  needle,
rpm-build 0fba15
                                   GEqualFunc     equal_func,
rpm-build 0fba15
                                   guint         *index_)
rpm-build 0fba15
{
rpm-build 0fba15
  guint i;
rpm-build 0fba15
rpm-build 0fba15
  g_return_val_if_fail (haystack != NULL, FALSE);
rpm-build 0fba15
rpm-build 0fba15
  if (equal_func == NULL)
rpm-build 0fba15
    equal_func = g_direct_equal;
rpm-build 0fba15
rpm-build 0fba15
  for (i = 0; i < haystack->len; i++)
rpm-build 0fba15
    {
rpm-build 0fba15
      if (equal_func (g_ptr_array_index (haystack, i), needle))
rpm-build 0fba15
        {
rpm-build 0fba15
          if (index_ != NULL)
rpm-build 0fba15
            *index_ = i;
rpm-build 0fba15
          return TRUE;
rpm-build 0fba15
        }
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  return FALSE;
rpm-build 0fba15
}