Repository navigation
feat(os_functions): loadenv builtin #1477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||||
| //============================================================================= | ||||||||
| // Copyright (c) 2016-present Allan CORNET (Nelson) | ||||||||
| //============================================================================= | ||||||||
| // This file is part of Nelson. | ||||||||
| //============================================================================= | ||||||||
| // LICENCE_BLOCK_BEGIN | ||||||||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||||||||
| // LICENCE_BLOCK_END | ||||||||
| //============================================================================= | ||||||||
| #include "loadenvBuiltin.hpp" | ||||||||
| #include "InputOutputArgumentsCheckers.hpp" | ||||||||
| #include "LoadEnvironment.hpp" | ||||||||
| #include "Error.hpp" | ||||||||
| #include "i18n.hpp" | ||||||||
| #include "characters_encoding.hpp" | ||||||||
| #include <algorithm> | ||||||||
| #include <vector> | ||||||||
| //============================================================================= | ||||||||
| using namespace Nelson; | ||||||||
| //============================================================================= | ||||||||
| ArrayOfVector | ||||||||
| Nelson::OsFunctionsGateway::loadenvBuiltin(Evaluator* eval, int nLhs, const ArrayOfVector& argIn) | ||||||||
| { | ||||||||
| ArrayOfVector retval; | ||||||||
| nargincheck(argIn, 1, 1); | ||||||||
| nargoutcheck(nLhs, 0, 1); | ||||||||
|
|
||||||||
| std::wstring filename = argIn[0].getContentAsWideString(); | ||||||||
| bool applyToEnv = (nLhs == 0); | ||||||||
|
|
||||||||
| std::wstring errorMessage; | ||||||||
| std::vector<std::pair<std::wstring, std::wstring>> result; | ||||||||
| try { | ||||||||
| result = LoadEnvironment(filename, applyToEnv, errorMessage); | ||||||||
| } catch (const std::exception& ex) { | ||||||||
| errorMessage = utf8_to_wstring(std::string("Exception: ")) + utf8_to_wstring(ex.what()); | ||||||||
| } catch (...) { | ||||||||
| if (errorMessage.empty()) | ||||||||
| errorMessage = _W("Unknown error while loading environment"); | ||||||||
| } | ||||||||
|
|
||||||||
| if (!errorMessage.empty()) { | ||||||||
| Error(errorMessage); | ||||||||
| return retval; | ||||||||
| } | ||||||||
|
|
||||||||
| if (nLhs == 1) { | ||||||||
| wstringVector keys; | ||||||||
| wstringVector values; | ||||||||
| keys.reserve(result.size()); | ||||||||
| values.reserve(result.size()); | ||||||||
| for (const auto& kv : result) { | ||||||||
| keys.push_back(kv.first); | ||||||||
| values.push_back(kv.second); | ||||||||
| } | ||||||||
|
|
||||||||
| FunctionDef* funcDef = nullptr; | ||||||||
| if (!eval) { | ||||||||
| Error(_W("Evaluator is not initialized.")); | ||||||||
| } | ||||||||
| Context* context = eval->getContext(); | ||||||||
| if (!context) { | ||||||||
| Error(_W("Context is not initialized.")); | ||||||||
|
||||||||
| Error(_W("Context is not initialized.")); | |
| Error(_W("Context is not initialized.")); | |
| return retval; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| //============================================================================= | ||
| // Copyright (c) 2016-present Allan CORNET (Nelson) | ||
| //============================================================================= | ||
| // This file is part of Nelson. | ||
| //============================================================================= | ||
| // LICENCE_BLOCK_BEGIN | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| // LICENCE_BLOCK_END | ||
| //============================================================================= | ||
| #pragma once | ||
| //============================================================================= | ||
| #include "ArrayOf.hpp" | ||
| #include "Evaluator.hpp" | ||
| //============================================================================= | ||
| namespace Nelson::OsFunctionsGateway { | ||
| //============================================================================= | ||
| ArrayOfVector | ||
| loadenvBuiltin(Evaluator* eval, int nLhs, const ArrayOfVector& argIn); | ||
| //============================================================================= | ||
| } // namespace Nelson | ||
| //============================================================================= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <xmldoc> | ||
| <copyright>SAME AS NELSON SOFTWARE</copyright> | ||
| <language>en_US</language> | ||
| <keyword>loadenv</keyword> | ||
| <short_description | ||
| >Load environment variables defined in .env or regular text files.</short_description> | ||
| <syntax> | ||
| <syntax_item>loadenv(filename)</syntax_item> | ||
| <syntax_item>D = loadenv(filename)</syntax_item> | ||
| </syntax> | ||
| <param_input> | ||
| <param_input_item> | ||
| <param_name>filename</param_name> | ||
| <param_description | ||
| >string scalar, character vector: environment filename.</param_description> | ||
| </param_input_item> | ||
| </param_input> | ||
| <param_output> | ||
| <param_output_item> | ||
| <param_name>s</param_name> | ||
| <param_description | ||
| >dictionary: the environment variables and their values.</param_description> | ||
| </param_output_item> | ||
| </param_output> | ||
| <description> | ||
| <p><b | ||
| >loadenv(filename)</b> loads environment variables from a .env or plain text file by parsing one key-value pair per line and sets them as environment variables in the Nelson environment.</p> | ||
| <p><b | ||
| >D = loadenv(filename)</b> returns a dictionary containing the parsed key-value pairs. When an output argument is specified, loadenv does not modify the Nelson environment.</p> | ||
| </description> | ||
| <used_function /> | ||
| <bibliography /> | ||
| <examples> | ||
| <example_item> | ||
| <example_item_type>nelson</example_item_type> | ||
| <example_item_description /> | ||
| <example_item_data | ||
| runnable="cli" | ||
| ><![CDATA[env_file = [modulepath('os_functions', 'tests'), '/sample.env']; | ||
| D = loadenv(env_file) | ||
| getenv('Key1') | ||
| loadenv(env_file) | ||
| getenv('Key1')]]></example_item_data> | ||
| </example_item> | ||
| </examples> | ||
| <see_also> | ||
| <see_also_item> | ||
| <link linkend="${os_functions}setenv">setenv</link> | ||
| </see_also_item> | ||
| <see_also_item> | ||
| <link linkend="${os_functions}getenv">getenv</link> | ||
| </see_also_item> | ||
| </see_also> | ||
| <history> | ||
| <history_item> | ||
| <history_version>1.15.0</history_version> | ||
| <history_description>initial version</history_description> | ||
| </history_item> | ||
| </history> | ||
| <authors> | ||
| <author_item>Allan CORNET</author_item> | ||
| </authors> | ||
| </xmldoc> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <xmldoc> | ||
| <copyright>SAME AS NELSON SOFTWARE</copyright> | ||
| <language>fr_FR</language> | ||
| <keyword>loadenv</keyword> | ||
| <short_description | ||
| >Charger les variables d'environnement définies dans des fichiers .env ou des fichiers texte ordinaires.</short_description> | ||
| <syntax> | ||
| <syntax_item>loadenv(filename)</syntax_item> | ||
| <syntax_item>D = loadenv(filename)</syntax_item> | ||
| </syntax> | ||
| <param_input> | ||
| <param_input_item> | ||
| <param_name>filename</param_name> | ||
| <param_description | ||
| >chaine de caractères: nom du fichier d'environnement.</param_description> | ||
| </param_input_item> | ||
| </param_input> | ||
| <param_output> | ||
| <param_output_item> | ||
| <param_name>s</param_name> | ||
| <param_description | ||
| >dictionnaire: les variables d'environnement et leurs valeurs.</param_description> | ||
| </param_output_item> | ||
| </param_output> | ||
| <description> | ||
| <p><b | ||
| >loadenv(filename)</b> charge les variables d'environnement à partir d'un fichier .env ou texte brut en analysant une paire clé-valeur par ligne et les définit comme variables d'environnement dans l'environnement Nelson.</p> | ||
| <p><b | ||
| >D = loadenv(filename)</b> renvoie un dictionnaire contenant les paires clé-valeur analysées. Lorsqu'un argument de sortie est spécifié, loadenv ne modifie pas l'environnement Nelson.</p> | ||
| </description> | ||
| <used_function /> | ||
| <bibliography /> | ||
| <examples> | ||
| <example_item> | ||
| <example_item_type>nelson</example_item_type> | ||
| <example_item_description /> | ||
| <example_item_data | ||
| runnable="cli" | ||
| ><![CDATA[env_file = [modulepath('os_functions', 'tests'), '/sample.env']; | ||
| D = loadenv(env_file) | ||
| getenv('Key1') | ||
| loadenv(env_file) | ||
| getenv('Key1')]]></example_item_data> | ||
| </example_item> | ||
| </examples> | ||
| <see_also> | ||
| <see_also_item> | ||
| <link linkend="${os_functions}setenv">setenv</link> | ||
| </see_also_item> | ||
| <see_also_item> | ||
| <link linkend="${os_functions}getenv">getenv</link> | ||
| </see_also_item> | ||
| </see_also> | ||
| <history> | ||
| <history_item> | ||
| <history_version>1.15.0</history_version> | ||
| <history_description>initial version</history_description> | ||
| </history_item> | ||
| </history> | ||
| <authors> | ||
| <author_item>Allan CORNET</author_item> | ||
| </authors> | ||
| </xmldoc> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After checking if
evalis null and callingError(), execution continues to line 61 whereeval->getContext()is called. This can cause a null pointer dereference ifevalis null. Add an early return after the Error() call on line 59.