Skip to content

Commit

Permalink
Environment package functions for N8 standard library.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Nov 30, 2024
1 parent e384001 commit bc3fd8f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
63 changes: 63 additions & 0 deletions std/n8std/Env.cpp
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
}
40 changes: 40 additions & 0 deletions std/n8std/Env.hpp
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

0 comments on commit bc3fd8f

Please sign in to comment.