[Spice-devel] [PATCH spice-xpi] xpi: add a few g_environ functions from GLib >= 2.28

Christophe Fergeau cfergeau at redhat.com
Tue Jan 29 05:14:32 PST 2013


Looks good, ACK,

Christophe

On Tue, Jan 29, 2013 at 01:22:03PM +0100, Marc-André Lureau wrote:
> ---
>  SpiceXPI/src/plugin/Makefile.am   |   2 +
>  SpiceXPI/src/plugin/glib-compat.c | 188 ++++++++++++++++++++++++++++++++++++++
>  SpiceXPI/src/plugin/glib-compat.h |  49 ++++++++++
>  SpiceXPI/src/plugin/plugin.h      |   1 +
>  4 files changed, 240 insertions(+)
>  create mode 100644 SpiceXPI/src/plugin/glib-compat.c
>  create mode 100644 SpiceXPI/src/plugin/glib-compat.h
> 
> diff --git a/SpiceXPI/src/plugin/Makefile.am b/SpiceXPI/src/plugin/Makefile.am
> index 08d89d7..7bc87df 100644
> --- a/SpiceXPI/src/plugin/Makefile.am
> +++ b/SpiceXPI/src/plugin/Makefile.am
> @@ -35,6 +35,8 @@ libnsISpicec_la_LIBADD =			\
>  libnsISpicec_la_SOURCES =			\
>  	$(top_srcdir)/common/common.h		\
>  	$(top_srcdir)/common/rederrorcodes.h	\
> +	glib-compat.c				\
> +	glib-compat.h				\
>  	controller.cpp				\
>  	controller.h				\
>  	np_entry.cpp				\
> diff --git a/SpiceXPI/src/plugin/glib-compat.c b/SpiceXPI/src/plugin/glib-compat.c
> new file mode 100644
> index 0000000..f360677
> --- /dev/null
> +++ b/SpiceXPI/src/plugin/glib-compat.c
> @@ -0,0 +1,188 @@
> +/* GLIB - Library of useful routines for C programming
> + * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
> + *
> + * 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 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., 59 Temple Place - Suite 330,
> + * Boston, MA 02111-1307, USA.
> + */
> +
> +/*
> + * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
> + * file for a list of people on the GLib Team.  See the ChangeLog
> + * files for a list of changes.  These files are distributed with
> + * GLib at ftp://ftp.gtk.org/pub/gtk/.
> + */
> +
> +#include <unistd.h>
> +#include <string.h>
> +
> +#include "glib-compat.h"
> +
> +#if !GLIB_CHECK_VERSION (2,32,0)
> +static gint
> +g_environ_find (gchar       **envp,
> +                const gchar  *variable)
> +{
> +  gint len, i;
> +
> +  if (envp == NULL)
> +    return -1;
> +
> +  len = strlen (variable);
> +
> +  for (i = 0; envp[i]; i++)
> +    {
> +      if (strncmp (envp[i], variable, len) == 0 &&
> +          envp[i][len] == '=')
> +        return i;
> +    }
> +
> +  return -1;
> +}
> +
> +/**
> + * g_environ_getenv:
> + * @envp: (allow-none) (array zero-terminated=1) (transfer none): an environment
> + *     list (eg, as returned from g_get_environ()), or %NULL
> + *     for an empty environment list
> + * @variable: the environment variable to get, in the GLib file name
> + *     encoding
> + *
> + * Returns the value of the environment variable @variable in the
> + * provided list @envp.
> + *
> + * The name and value are in the GLib file name encoding.
> + * On UNIX, this means the actual bytes which might or might not
> + * be in some consistent character set and encoding. On Windows,
> + * it is in UTF-8. On Windows, in case the environment variable's
> + * value contains references to other environment variables, they
> + * are expanded.
> + *
> + * Return value: the value of the environment variable, or %NULL if
> + *     the environment variable is not set in @envp. The returned
> + *     string is owned by @envp, and will be freed if @variable is
> + *     set or unset again.
> + *
> + * Since: 2.32
> + */
> +const gchar *
> +g_environ_getenv (gchar       **envp,
> +                  const gchar  *variable)
> +{
> +  gint index;
> +
> +  g_return_val_if_fail (variable != NULL, NULL);
> +
> +  index = g_environ_find (envp, variable);
> +  if (index != -1)
> +    return envp[index] + strlen (variable) + 1;
> +  else
> +    return NULL;
> +}
> +
> +/**
> + * g_environ_setenv:
> + * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
> + *     list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()), or %NULL
> + *     for an empty environment list
> + * @variable: the environment variable to set, must not contain '='
> + * @value: the value for to set the variable to
> + * @overwrite: whether to change the variable if it already exists
> + *
> + * Sets the environment variable @variable in the provided list
> + * @envp to @value.
> + *
> + * Both the variable's name and value should be in the GLib
> + * file name encoding. On UNIX, this means that they can be
> + * arbitrary byte strings. On Windows, they should be in UTF-8.
> + *
> + * Return value: (array zero-terminated=1) (transfer full): the
> + *     updated environment list. Free it using g_strfreev().
> + *
> + * Since: 2.32
> + */
> +gchar **
> +g_environ_setenv (gchar       **envp,
> +                  const gchar  *variable,
> +                  const gchar  *value,
> +                  gboolean      overwrite)
> +{
> +  gint index;
> +
> +  g_return_val_if_fail (variable != NULL, NULL);
> +  g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
> +
> +  index = g_environ_find (envp, variable);
> +  if (index != -1)
> +    {
> +      if (overwrite)
> +        {
> +          g_free (envp[index]);
> +          envp[index] = g_strdup_printf ("%s=%s", variable, value);
> +        }
> +    }
> +  else
> +    {
> +      gint length;
> +
> +      length = envp ? g_strv_length (envp) : 0;
> +      envp = g_renew (gchar *, envp, length + 2);
> +      envp[length] = g_strdup_printf ("%s=%s", variable, value);
> +      envp[length + 1] = NULL;
> +    }
> +
> +  return envp;
> +}
> +#endif /* !GLIB_CHECK_VERSION (2,32,0) */
> +
> +
> +#if !GLIB_CHECK_VERSION (2,28,0)
> +#ifndef G_OS_WIN32
> +/* According to the Single Unix Specification, environ is not
> + * in any system header, although unistd.h often declares it.
> + */
> +extern char **environ;
> +
> +gchar **
> +g_get_environ (void)
> +{
> +  return g_strdupv (environ);
> +}
> +
> +#else /* G_OS_WIN32 */
> +
> +gchar **
> +g_get_environ (void)
> +{
> +  gunichar2 *strings;
> +  gchar **result;
> +  gint i, n;
> +
> +  strings = GetEnvironmentStringsW ();
> +  for (n = 0, i = 0; strings[n]; i++)
> +    n += wcslen (strings + n) + 1;
> +
> +  result = g_new (char *, i + 1);
> +  for (n = 0, i = 0; strings[n]; i++)
> +    {
> +      result[i] = g_utf16_to_utf8 (strings + n, -1, NULL, NULL, NULL);
> +      n += wcslen (strings + n) + 1;
> +    }
> +  FreeEnvironmentStringsW (strings);
> +  result[i] = NULL;
> +
> +  return result;
> +}
> +#endif /* G_OS_WIN32 */
> +#endif /* !GLIB_CHECK_VERSION (2,28,0) */
> diff --git a/SpiceXPI/src/plugin/glib-compat.h b/SpiceXPI/src/plugin/glib-compat.h
> new file mode 100644
> index 0000000..0724417
> --- /dev/null
> +++ b/SpiceXPI/src/plugin/glib-compat.h
> @@ -0,0 +1,49 @@
> +/* GLIB - Library of useful routines for C programming
> + * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
> + *
> + * 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 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., 59 Temple Place - Suite 330,
> + * Boston, MA 02111-1307, USA.
> + */
> +
> +/*
> + * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
> + * file for a list of people on the GLib Team.  See the ChangeLog
> + * files for a list of changes.  These files are distributed with
> + * GLib at ftp://ftp.gtk.org/pub/gtk/.
> + */
> +
> +#ifndef GLIB_COMPAT_H_
> +# define GLIB_COMPAT_H_
> +
> +#include <glib.h>
> +
> +G_BEGIN_DECLS
> +
> +#if !GLIB_CHECK_VERSION (2,32,0)
> +const gchar *      g_environ_getenv          (gchar       **envp,
> +                                              const gchar  *variable);
> +gchar **           g_environ_setenv          (gchar       **envp,
> +                                              const gchar  *variable,
> +                                              const gchar  *value,
> +                                              gboolean      overwrite);
> +#endif
> +
> +#if !GLIB_CHECK_VERSION (2,28,0)
> +gchar **           g_get_environ             (void);
> +#endif
> +
> +G_END_DECLS
> +
> +#endif /* GLIB_COMPAT_H_ */
> diff --git a/SpiceXPI/src/plugin/plugin.h b/SpiceXPI/src/plugin/plugin.h
> index 38a8c82..6305dcc 100644
> --- a/SpiceXPI/src/plugin/plugin.h
> +++ b/SpiceXPI/src/plugin/plugin.h
> @@ -58,6 +58,7 @@ extern "C" {
>  #include "pluginbase.h"
>  #include "controller.h"
>  #include "common.h"
> +#include "glib-compat.h"
>  
>  class nsPluginInstance: public nsPluginInstanceBase
>  {
> -- 
> 1.8.1.1.439.g50a6b54
> 
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/spice-devel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/spice-devel/attachments/20130129/9d6bcc8d/attachment.pgp>


More information about the Spice-devel mailing list