Mercurial > octave
changeset 32188:85255746fc21
Add isenv function (patch #10354).
* libinterp/corefcn/sysdep.cc (Fisenv): Add new function.
* liboctave/system/lo-sysdep.cc, liboctave/system/lo-sysdep.h (isenv_wrapper):
Add new function.
* liboctave/system/oct-env.cc, liboctave/system/oct-env.h (env::isenv):
Add new function.
* doc/interpreter/system.txi: Add reference to isenv.
* etc/NEWS.9.md: Add note about new function.
author | John Donoghue <john.donoghue@ieee.org> |
---|---|
date | Wed, 31 May 2023 16:29:09 -0400 |
parents | 829a32a4171c |
children | 7f068048e388 |
files | doc/interpreter/system.txi etc/NEWS.9.md libinterp/corefcn/sysdep.cc liboctave/system/lo-sysdep.cc liboctave/system/lo-sysdep.h liboctave/system/oct-env.cc liboctave/system/oct-env.h |
diffstat | 7 files changed, 65 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/interpreter/system.txi Tue Jul 11 19:43:03 2023 +0200 +++ b/doc/interpreter/system.txi Wed May 31 16:29:09 2023 -0400 @@ -425,6 +425,8 @@ @DOCSTRING(getenv) +@DOCSTRING(isenv) + @DOCSTRING(setenv) @DOCSTRING(unsetenv)
--- a/etc/NEWS.9.md Tue Jul 11 19:43:03 2023 +0200 +++ b/etc/NEWS.9.md Wed May 31 16:29:09 2023 -0400 @@ -122,6 +122,7 @@ ### Alphabetical list of new functions added in Octave 9 +* `isenv` * `isuniform` * `tensorprod`
--- a/libinterp/corefcn/sysdep.cc Tue Jul 11 19:43:03 2023 +0200 +++ b/libinterp/corefcn/sysdep.cc Wed May 31 16:29:09 2023 -0400 @@ -755,7 +755,7 @@ @noindent returns a string containing the value of your path. -@seealso{setenv, unsetenv} +@seealso{setenv, unsetenv, isenv} @end deftypefn */) { if (args.length () != 1) @@ -770,6 +770,43 @@ %!assert (ischar (getenv ("OCTAVE_HOME"))) */ +DEFUN (isenv, args, , + doc: /* -*- texinfo -*- +@deftypefn {} {@var{val} =} isenv (@var{var}) +Check if the environment variable @var{var} exists. + +This function returns true if an environment variable with the name @var{var} +exists. Otherwise, it returns false. + +For example, + +@example +tf = isenv ("PATH") +@end example + +@noindent +returns true if an environment variable with the name @qcode{"PATH"} exists. +@seealso{getenv, setenv, unsetenv} +@end deftypefn */) +{ + if (args.length () != 1) + print_usage (); + + std::string name = args(0).xstring_value ("isenv: VAR must be a string"); + + return ovl (sys::env::isenv (name)); +} + +/* +%!test +%! setenv ("dummy_variable_that_cannot_matter", "foobar"); +%! assert (isenv ("dummy_variable_that_cannot_matter"), true); +%! unsetenv ("dummy_variable_that_cannot_matter"); +%! assert (isenv ("dummy_variable_that_cannot_matter"), false); + +%!error <VAR must be a string> isenv (struct ()) +*/ + DEFUN (setenv, args, , doc: /* -*- texinfo -*- @deftypefn {} {} setenv ("@var{var}", @var{value}) @@ -782,7 +819,7 @@ Programming Note: @code{putenv} is an alias for @code{setenv} and can be used interchangeably. -@seealso{unsetenv, getenv} +@seealso{unsetenv, getenv, isenv} @end deftypefn */) { int nargin = args.length (); @@ -818,7 +855,7 @@ Return 0 if the variable was deleted, or did not exist, and -1 if an error occurred. -@seealso{setenv, getenv} +@seealso{setenv, getenv, isenv} @end deftypefn */) { if (args.length () != 1)
--- a/liboctave/system/lo-sysdep.cc Tue Jul 11 19:43:03 2023 +0200 +++ b/liboctave/system/lo-sysdep.cc Wed May 31 16:29:09 2023 -0400 @@ -705,6 +705,18 @@ #endif } +bool +isenv_wrapper (const std::string& name) +{ +#if defined (OCTAVE_USE_WINDOWS_API) + std::wstring wname = u8_to_wstring (name); + wchar_t *env = _wgetenv (wname.c_str ()); +#else + char *env = ::getenv (name.c_str ()); +#endif + return env != 0; +} + std::wstring u8_to_wstring (const std::string& utf8_string) {
--- a/liboctave/system/lo-sysdep.h Tue Jul 11 19:43:03 2023 +0200 +++ b/liboctave/system/lo-sysdep.h Wed May 31 16:29:09 2023 -0400 @@ -88,6 +88,8 @@ extern OCTAVE_API std::string getenv_wrapper (const std::string&); +extern OCTAVE_API bool isenv_wrapper (const std::string&); + extern OCTAVE_API int unsetenv_wrapper (const std::string&); extern OCTAVE_API std::wstring u8_to_wstring (const std::string&);
--- a/liboctave/system/oct-env.cc Tue Jul 11 19:43:03 2023 +0200 +++ b/liboctave/system/oct-env.cc Wed May 31 16:29:09 2023 -0400 @@ -299,6 +299,12 @@ ? s_instance->do_getenv (name) : ""; } +bool +env::isenv (const std::string& name) +{ + return isenv_wrapper (name); +} + void env::putenv (const std::string& name, const std::string& value) {
--- a/liboctave/system/oct-env.h Tue Jul 11 19:43:03 2023 +0200 +++ b/liboctave/system/oct-env.h Wed May 31 16:29:09 2023 -0400 @@ -80,6 +80,8 @@ static std::string getenv (const std::string& name); + static bool isenv (const std::string& name); + static void putenv (const std::string& name, const std::string& value); static bool have_x11_display ();