nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
branding.hpp
1// fs/include/nk/fs/branding.hpp
2//
3// Lives in fs/ (L0) because that is the lowest layer reachable by every
4// consumer under the layering guard (value:{value}, fs:{fs}): FsContext (fs/)
5// needs envGet for the NK_FS / NK_CWD path-resolution fallbacks, and
6// core/ + toolboxes may include fs/. Header-only (inline/constexpr), no deps
7// beyond <cstdlib>/<string>. The NK_FS/NK_CWD vars it serves are
8// themselves filesystem config, so fs/ is a natural home.
9#pragma once
10
11#include <cstdlib>
12#include <string>
13
14#ifdef _WIN32
15# include <stdlib.h>
16#endif
17
18namespace nk {
19
20// Project-name prefix for user-facing identifiers that must change if
21// the project is ever renamed. Intentionally narrow — covers only the
22// environment-variable namespace (NK_FS, NK_CWD, …) that
23// end-user scripts `setenv`/`getenv` touch and would break on rename.
24//
25// NOT covered here (rename these manually alongside the prefix update):
26// • C++ namespace `nk`, target `nk`
27// • Error-identifier strings such as "nk:assert" (defined across
28// toolboxes/builtin/src and toolboxes/signal/src)
29// • Documentation, README, CMake project name
30inline constexpr const char *kEnvPrefix = "NK";
31
32inline std::string envVarName(const char *suffix)
33{
34 return std::string(kEnvPrefix) + "_" + suffix;
35}
36
37// Cross-platform environment-variable read: returns "" when unset.
38// On MSVC std::getenv is deprecated; use _dupenv_s for the same behaviour
39// without the C4996 warning.
40inline std::string envGet(const char *name)
41{
42#ifdef _WIN32
43 char *buf = nullptr;
44 size_t len = 0;
45 _dupenv_s(&buf, &len, name);
46 std::string s = buf ? buf : "";
47 std::free(buf);
48 return s;
49#else
50 const char *v = std::getenv(name);
51 return v ? v : "";
52#endif
53}
54
55inline void envSet(const char *name, const std::string &value)
56{
57#ifdef _WIN32
58 _putenv_s(name, value.c_str());
59#else
60 setenv(name, value.c_str(), 1);
61#endif
62}
63
64} // namespace nk
65
void setenv(const std::string &name, const std::string &value="")
Sets or clears an operating system environment variable (setenv(name, value)).