localPackages.biu: add env

This commit is contained in:
陈浩南 2024-06-09 18:12:23 +08:00
parent fb4a9644fc
commit 1949ea7d78
7 changed files with 35 additions and 6 deletions

View File

@ -1,3 +1,3 @@
CompileFlags:
Add: [ -Wall, -Wextra, -std=c++23 ]
Compiler: gcc
Compiler: g++

View File

@ -22,7 +22,7 @@ find_path(NAMEOF_INCLUDE_DIR nameof.hpp REQUIRED)
# find_library(BACKTRACE_LIB backtrace REQUIRED)
# add_library(biu SHARED src/common.cpp src/logger.cpp src/string.cpp)
add_library(biu SHARED src/common.cpp)
add_library(biu SHARED src/common.cpp src/env.cpp)
target_include_directories(biu PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>

View File

@ -40,3 +40,10 @@ std::regex operator""_re(const char* str, std::size_t len);
inline namespace stream_operators { using namespace magic_enum::iostream_operators; }
```
# `env`
```c++
bool is_interactive();
std::optional<std::string> env(std::string name);
std::map<std::string, std::string> env();
```

View File

@ -2,6 +2,7 @@
// # include <biu/atomic/atomic.tpp>
// # include <biu/called_by.hpp>
# include <biu/common.tpp>
# include <biu/env.hpp>
// # include <biu/concepts.tpp>
// # include <biu/format.tpp>
// # include <biu/logger.tpp>

View File

@ -0,0 +1,8 @@
# pragma once
# include <biu/common.hpp>
namespace biu::env
{
bool is_interactive();
std::optional<std::string> env(std::string name);
}

View File

@ -5,8 +5,7 @@
namespace biu
{
std::regex literals::operator""_re(const char* str, std::size_t len)
{ return std::regex{str, len}; }
std::regex literals::operator""_re(const char* str, std::size_t len) { return std::regex{str, len}; }
namespace common
{
void block_forever() { std::promise<void>().get_future().wait(); std::unreachable(); }
@ -25,12 +24,14 @@ namespace biu
if (stdin)
{
process = std::make_unique<bp::child>
(program.string(), bp::args(args), bp::std_out > stdout, bp::std_err > stderr, bp::std_in < input, env);
(program.string(), bp::args(args), bp::std_out > stdout, bp::std_err > stderr,
bp::std_in < input, env);
input << *stdin;
input.pipe().close();
}
else process = std::make_unique<bp::child>
(program.string(), bp::args(args), bp::std_out > stdout, bp::std_err > stderr, bp::std_in < bp::null, env);
(program.string(), bp::args(args), bp::std_out > stdout, bp::std_err > stderr,
bp::std_in < bp::null, env);
process->wait();
return
{

View File

@ -0,0 +1,12 @@
# include <biu.hpp>
# include <boost/process.hpp>
namespace biu::env
{
bool is_interactive() { return isatty(fileno(stdin)); }
std::optional<std::string> env(std::string name)
{
if (auto value = std::getenv(name.c_str()); !value) return std::nullopt;
else return value;
}
}