1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
// (c) 2016 Henner Zeller <h.zeller@acm.org>
//
// This program 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 version 2.
//
// This program 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 this program. If not, see <http://gnu.org/licenses/gpl-2.0.txt>
#include "utils.h"
#include <strings.h>
#include <unistd.h>
namespace timg {
bool GetBoolenEnv(const char *env_name, bool default_value) {
const char *const value = getenv(env_name);
if (!value) return default_value;
return (atoi(value) > 0 || strcasecmp(value, "on") == 0 ||
strcasecmp(value, "yes") == 0);
}
float GetFloatEnv(const char *env_var, float default_value) {
const char *value = getenv(env_var);
if (!value) return default_value;
char *err = nullptr;
float result = strtof(value, &err);
return (*err == '\0' ? result : default_value);
}
std::string HumanReadableByteValue(int64_t byte_count) {
float print_bytes = byte_count;
const char *unit = "Bytes";
if (print_bytes > (10LL << 30)) {
print_bytes /= (1 << 30);
unit = "GiB";
}
else if (print_bytes > (10 << 20)) {
print_bytes /= (1 << 20);
unit = "MiB";
}
else if (print_bytes > (10 << 10)) {
print_bytes /= (1 << 10);
unit = "KiB";
}
char buf[32];
snprintf(buf, sizeof(buf), "%.1f %s", print_bytes, unit);
return buf;
}
} // namespace timg
|