Files
MaxScale/maxscale-system-test/maxtest/src/envv.cpp
Esa Korhonen 96ba2da40c MXS-2900 Move testcore library files to a dedicated directory
The library is now named "maxtest". The related include-files are, for
now, usable without designating the full include path. This may be changed
later, but would require modifying every test.
2020-03-02 11:17:30 +02:00

62 lines
1.2 KiB
C++

#include <string.h>
#include <string>
#include "envv.h"
char * readenv(const char * name, const char *format, ...)
{
char * env = getenv(name);
if (!env)
{
va_list valist;
va_start(valist, format);
int message_len = vsnprintf(NULL, 0, format, valist);
va_end(valist);
if (message_len < 0)
{
return NULL;
}
env = (char*)malloc(message_len + 1);
va_start(valist, format);
vsnprintf(env, message_len + 1, format, valist);
va_end(valist);
setenv(name, env, 1);
}
return env;
}
int readenv_int(const char * name, int def)
{
int x;
char * env = getenv(name);
if (env)
{
sscanf(env, "%d", &x);
}
else
{
x = def;
setenv(name, (std::to_string(x).c_str()), 1);
}
return x;
}
bool readenv_bool(const char * name, bool def)
{
char * env = getenv(name);
if (env)
{
return ((strcasecmp(env, "yes") == 0) ||
(strcasecmp(env, "y") == 0) ||
(strcasecmp(env, "true") == 0));
}
else
{
setenv(name, def ? "true" : "false", 1);
return def;
}
}