-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Environment package functions for N8 standard library.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,63 @@ | ||
/* | ||
* Copyright (c) 2024 - Nathanne Isip | ||
* This file is part of N8. | ||
* | ||
* N8 is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, | ||
* or (at your option) any later version. | ||
* | ||
* N8 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with N8. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "n8std/IO.hpp" | ||
|
||
#include <n8/ast/TerminativeSignal.hpp> | ||
|
||
#include <cstdlib> | ||
|
||
N8_FUNC(env_get) { | ||
if(args.size() != 1) | ||
throw TerminativeThrowSignal( | ||
std::move(address), | ||
"Expecting 1 argument, got " + | ||
std::to_string(args.size()) | ||
); | ||
|
||
DynamicObject value = args.at(0); | ||
return DynamicObject( | ||
std::string(std::getenv( | ||
value.toString().c_str() | ||
)) | ||
); | ||
} | ||
|
||
N8_FUNC(env_set) { | ||
if(args.size() != 2) | ||
throw TerminativeThrowSignal( | ||
std::move(address), | ||
"Expecting 2 argument, got " + | ||
std::to_string(args.size()) | ||
); | ||
|
||
DynamicObject key = args.at(0), | ||
value = args.at(1); | ||
|
||
#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64) | ||
return DynamicObject(_putenv(std::string( | ||
key.toString() + "=" + value.toString() | ||
).c_str()) != 0); | ||
#else | ||
return DynamicObject(setenv( | ||
key.toString().c_str(), | ||
value.toString().c_str(), | ||
1 | ||
) != 0); | ||
#endif | ||
} |
This file contains 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,40 @@ | ||
/* | ||
* Copyright (c) 2024 - Nathanne Isip | ||
* This file is part of N8. | ||
* | ||
* N8 is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, | ||
* or (at your option) any later version. | ||
* | ||
* N8 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with N8. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef N8_STDLIB_ENV_CC | ||
#define N8_STDLIB_ENV_CC | ||
|
||
#ifdef __clang__ | ||
# pragma clang diagnostic push | ||
# pragma clang diagnostic ignored "-Wreturn-type-c-linkage" | ||
#endif | ||
|
||
#include "N8Library.hpp" | ||
|
||
N8_LIB_START | ||
|
||
N8_FUNC(env_set); | ||
N8_FUNC(env_get); | ||
|
||
N8_LIB_END | ||
|
||
#ifdef __clang__ | ||
# pragma clang diagnostic pop | ||
#endif | ||
|
||
#endif |