stc
Loading...
Searching...
No Matches
Namespaces | Classes | Enumerations | Functions
stc Namespace Reference

Namespaces

namespace  colour
 Module for ANSI colours.
 
namespace  FntParser
 
namespace  math
 
namespace  StdFix
 
namespace  string
 
namespace  testutil
 
namespace  Unix
 

Classes

class  FileLock
 
class  PasswordIO
 

Enumerations

enum class  StreamType { STDOUT , STDERR , OTHER }
 

Functions

void setEnv (const char *name, const char *value, bool replace=true)
 
std::string getEnv (const char *name, const std::string &fail="")
 
std::filesystem::path expandUserPath (const std::string &inputPath)
 
std::filesystem::path getHome ()
 
std::string syscommand (const std::string &command, int *codeOutput=nullptr)
 
std::string syscommand (std::vector< const char * > command, int *codeOutput=nullptr)
 
void syscommandNoCapture (std::vector< const char * > command, int *codeOutput=nullptr)
 
std::optional< std::string > getHostname ()
 
bool isStreamTTY (StreamType type=StreamType::STDOUT)
 
std::string executablePath ()
 
template<typename CharT >
static constexpr StreamType getOutputStreamType (std::basic_ostream< CharT > &ss)
 
template<typename CharT >
bool isCppStreamTTY (std::basic_ostream< CharT > &ss)
 

Enumeration Type Documentation

◆ StreamType

enum class stc::StreamType
strong
Enumerator
STDOUT 
STDERR 
OTHER 

Function Documentation

◆ executablePath()

std::string stc::executablePath ( )
inline

Wrapper around _NSGetExecutablePath (Crapplebook ShitOS), GetModuleFileNameA (Microsoft "Excessively verbose for the shits and giggles" Windows), and /proc/self/exe (everything else)

◆ expandUserPath()

std::filesystem::path stc::expandUserPath ( const std::string &  inputPath)
inline

Expands a user path (AKA a path starting with ~), independently of the OS. Returns the path unmodified if the path isn't a user path.

This requires several OS-specific calls (specifically between Windows and UNIX, from what I can tell. There's not much of a difference in this area between for an instance Linux and Mac. Special implementation requirements will be taken when we get to a point where it's needed)

◆ getEnv()

std::string stc::getEnv ( const char *  name,
const std::string &  fail = "" 
)
inline

Wrapper around various secure getenv methods. Falls back to std::getenv with a warning if none is available.

◆ getHome()

std::filesystem::path stc::getHome ( )
inline

Returns the user's home directory.

Note that the windows implementation is wank, and may not be as reliable as the UNIX implementation.

◆ getHostname()

std::optional< std::string > stc::getHostname ( )
inline

Returns the hostname of the machine.

◆ getOutputStreamType()

template<typename CharT >
static constexpr StreamType stc::getOutputStreamType ( std::basic_ostream< CharT > &  ss)
staticconstexpr

Determines the StreamType for a given input stream. This is primarily intended as a utility function for isCppStreamTTY, but can be used standalone

For now, it does not handle application-level redirects of std::cout, only OS-level redirects. This means that if you redirect the internal buffer, the function still detects it as stdout. This may or may not be a feature depending on your point of view, so for the time being, it's up to you to determine how to handle such redirects.

Returns
StreamType::STDOUT if ss is std::cout or std::wcout, StreamType::STDERR if ss is std::cerr or std::wcerr, and StreamType::OTHER for anything else.

◆ isCppStreamTTY()

template<typename CharT >
bool stc::isCppStreamTTY ( std::basic_ostream< CharT > &  ss)
inline
Returns
Whether or not a given basic_ostream is a TTY. Shorthand for isStreamTTY(getOutputStreamType(stream))

◆ isStreamTTY()

bool stc::isStreamTTY ( StreamType  type = StreamType::STDOUT)
inline

Utility wrapper around isatty (UNIX)/_isatty (Windows). Can be used to determine if a stream type is a TTY or redirected to a file or similar.

You probably don't want to use this function directly; use isCppStreamTTY instead.

Parameters
typeA StreamType. Because of how low-level this operates, this function only tells you what the OS redirects stdout to. Useful if you have control of the streams.

◆ setEnv()

void stc::setEnv ( const char *  name,
const char *  value,
bool  replace = true 
)
inline

Wrapper around setenv/unsetenv (UNIX) and _putenv_s (Windows)

Parameters
nameThe name of the environment variable
valueThe value of the environment variable. If null, the environment variable is deleted.
replaceWhether or not to replace the environment variable if it exists. Only respected on UNIX. true is the only cross-platform-compatible value.

◆ syscommand() [1/2]

std::string stc::syscommand ( const std::string &  command,
int *  codeOutput = nullptr 
)
inline

std::system alternative that returns the output from the subprocess.

WARNING: This function spawns a shell under the hood, as it uses popen. DO NOT pass user input to this command; it is a security vulnerability, and distraction & rm -rf / as user input will ruin your day at best. This command, like std::system, requires significant input cleaning before use. If you're dealing with user input, use syscommand(std::vector<const char*>, int*) instead.

See also
https://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html

◆ syscommand() [2/2]

std::string stc::syscommand ( std::vector< const char * >  command,
int *  codeOutput = nullptr 
)
inline

Lower-level command that does the same thing syscommand(std::string, int*) does, but using a vector for arguments instead of a single string.

This is in theory slightly safer than using a string directly, provided the command you invoke is safe. Unless you do something dumb, all arguments are passed directly to the command (provided as the first argument of the vector), so unless the command you're invoking can do something unsafe, it's safe from && as input.

There's still lots of ways user input can and will fuck you over, so do be careful.

Not currently supported on Windows due to CreatePipe being a pain to work with.

◆ syscommandNoCapture()

void stc::syscommandNoCapture ( std::vector< const char * >  command,
int *  codeOutput = nullptr 
)
inline

Same as syscommand(std::vector<const char*>, int*), but doesn't return output. This is intended as a hardened std::system.