From b8343851afbe33b7ceb50893f9d7221cffce4dd1 Mon Sep 17 00:00:00 2001 From: Kjell Ahlstedt Date: Sun, 23 May 2021 14:59:54 +0200 Subject: Add Glib::Environ and tests/glibmm_environ Fixes #89 --- glib/glibmm/environ.cc | 58 ++++++++++++++++++++++++++++++ glib/glibmm/environ.h | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ glib/glibmm/filelist.am | 2 ++ glib/glibmm/meson.build | 1 + 4 files changed, 154 insertions(+) create mode 100755 glib/glibmm/environ.cc create mode 100755 glib/glibmm/environ.h (limited to 'glib/glibmm') diff --git a/glib/glibmm/environ.cc b/glib/glibmm/environ.cc new file mode 100755 index 00000000..4586ef58 --- /dev/null +++ b/glib/glibmm/environ.cc @@ -0,0 +1,58 @@ +/* Copyright (C) 2021 The glibmm Development Team + * + * 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, see . + */ + +#include +#include + +namespace Glib +{ + +Environ::Environ() +: envp(g_get_environ(), &g_strfreev) +{} + +Environ::Environ(const std::vector& env_vec) +: envp(g_new(char*, env_vec.size() + 1), &g_strfreev) +{ + for (unsigned int i = 0; i < env_vec.size(); ++i) + envp.get()[i] = g_strdup(env_vec[i].c_str()); + envp.get()[env_vec.size()] = nullptr; +} + +std::optional Environ::get(StdStringView variable) const +{ + const char* value = g_environ_getenv(envp.get(), variable.c_str()); + if (value) + return std::optional(std::in_place, value); + return std::optional(); +} + +void Environ::set(StdStringView variable, StdStringView value, bool overwrite) +{ + envp.reset(g_environ_setenv(envp.release(), variable.c_str(), value.c_str(), overwrite)); +} + +void Environ::unset(StdStringView variable) +{ + envp.reset(g_environ_unsetenv(envp.release(), variable.c_str())); +} + +std::vector Environ::to_vector() const +{ + return Glib::ArrayHandler::array_to_vector(envp.get(), Glib::OWNERSHIP_NONE); +} + +} // namespace Glib diff --git a/glib/glibmm/environ.h b/glib/glibmm/environ.h new file mode 100755 index 00000000..238ef532 --- /dev/null +++ b/glib/glibmm/environ.h @@ -0,0 +1,93 @@ +#ifndef _GLIBMM_ENVIRON_H +#define _GLIBMM_ENVIRON_H +/* Copyright (C) 2021 The glibmm Development Team + * + * 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, see . + */ + +#include +#include +#include +#include +#include + +namespace Glib +{ + +/** A convenience class for manipulating a copy of the environment variables. + * + * Useful for generating the @a envp parameter in calls to + * Glib::spawn_async_with_pipes(), Glib::spawn_async() and Glib::spawn_sync(). + * + * If you want to change the environment itself (i.e. not a copy of it), + * see Glib::getenv(), Glib::setenv() and Glib::unsetenv(). + * + * @newin{2,70} + */ +class Environ +{ +public: + /** Constructs a list of environment variables for the current process. + * + * Each item in the list is of the form 'NAME=VALUE'. + */ + GLIBMM_API Environ(); + + /** Constructs a %Glib::Environ instance from a vector. + * + * @param env_vec A vector with the environment variables. Each element in + * the vector must be of the form 'NAME=VALUE'. + */ + GLIBMM_API explicit Environ(const std::vector& env_vec); + + /** Gets the value of the environment variable @a variable. + * + * @param variable The environment variable to get, must not contain '='. + * @return The value of the environment variable, or an empty std::optional + * if the environment variable is not set in this %Environ. + */ + GLIBMM_API std::optional get(StdStringView variable) const; + + /// Same as get(). + GLIBMM_API std::optional operator[](StdStringView variable) const + { return get(variable); } + + /** Sets the environment variable @a variable in the provided list to @a value. + * + * @param variable The environment variable to set, must not contain '='. + * @param value The value to set the variable to. + * @param overwrite Whether to change the variable if it already exists. + */ + GLIBMM_API void set(StdStringView variable, StdStringView value, bool overwrite = true); + + /** Removes the environment variable @a variable from the provided list. + * + * @param variable The environment variable to remove, must not contain '='. + */ + GLIBMM_API void unset(StdStringView variable); + + /** Get a vector with the environment variables. + * + * @return A vector with the environment variables. Each element in the vector + * is of the form 'NAME=VALUE'. + */ + GLIBMM_API std::vector to_vector() const; + +private: + std::unique_ptr envp; +}; + +} // namespace Glib + +#endif /* _GLIBMM_ENVIRON_H */ diff --git a/glib/glibmm/filelist.am b/glib/glibmm/filelist.am index e606d5fd..6bd16f1c 100644 --- a/glib/glibmm/filelist.am +++ b/glib/glibmm/filelist.am @@ -8,6 +8,7 @@ glibmm_files_extra_cc = \ class.cc \ debug.cc \ dispatcher.cc \ + environ.cc \ error.cc \ exceptionhandler.cc \ extraclassinit.cc \ @@ -40,6 +41,7 @@ glibmm_files_extra_h = \ containerhandle_shared.h \ debug.h \ dispatcher.h \ + environ.h \ error.h \ exceptionhandler.h \ extraclassinit.h \ diff --git a/glib/glibmm/meson.build b/glib/glibmm/meson.build index b736a3cf..29f55bdd 100644 --- a/glib/glibmm/meson.build +++ b/glib/glibmm/meson.build @@ -76,6 +76,7 @@ glibmm_extra_h_cc_basenames = [ 'class', 'debug', 'dispatcher', + 'environ', 'error', 'exceptionhandler', 'extraclassinit', -- cgit v1.2.1