summaryrefslogtreecommitdiff
path: root/glib/glibmm
diff options
context:
space:
mode:
authorKjell Ahlstedt <kjellahlstedt@gmail.com>2021-05-23 14:59:54 +0200
committerKjell Ahlstedt <kjellahlstedt@gmail.com>2021-05-23 14:59:54 +0200
commitb8343851afbe33b7ceb50893f9d7221cffce4dd1 (patch)
treecd6b2698ccc8ab789cb58d75763aaf55d680b54b /glib/glibmm
parent85c707d3e77c993ec221f08adffdd0918b60fbc7 (diff)
downloadglibmm-b8343851afbe33b7ceb50893f9d7221cffce4dd1.tar.gz
Add Glib::Environ and tests/glibmm_environ
Fixes #89
Diffstat (limited to 'glib/glibmm')
-rwxr-xr-xglib/glibmm/environ.cc58
-rwxr-xr-xglib/glibmm/environ.h93
-rw-r--r--glib/glibmm/filelist.am2
-rw-r--r--glib/glibmm/meson.build1
4 files changed, 154 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <glibmm/environ.h>
+#include <glibmm/vectorutils.h>
+
+namespace Glib
+{
+
+Environ::Environ()
+: envp(g_get_environ(), &g_strfreev)
+{}
+
+Environ::Environ(const std::vector<std::string>& 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<std::string> Environ::get(StdStringView variable) const
+{
+ const char* value = g_environ_getenv(envp.get(), variable.c_str());
+ if (value)
+ return std::optional<std::string>(std::in_place, value);
+ return std::optional<std::string>();
+}
+
+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<std::string> Environ::to_vector() const
+{
+ return Glib::ArrayHandler<std::string>::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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <glibmm/ustring.h>
+#include <memory>
+#include <optional>
+#include <string>
+#include <vector>
+
+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<std::string>& 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<std::string> get(StdStringView variable) const;
+
+ /// Same as get().
+ GLIBMM_API std::optional<std::string> 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<std::string> to_vector() const;
+
+private:
+ std::unique_ptr<char*, decltype(&g_strfreev)> 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',