Fledge
An open source edge computing platform for industrial users
/home/ubuntu/fledge/C/common/include/utils.h

Converts a string representation of a boolean value to a boolean type.This function takes a string input and checks if it represents a boolean value. It recognizes "true", "1", and their case-insensitive variants as true. Any other string will be interpreted as false.

Parameters
strThe string to convert to a boolean. Can be "true", "false", "1", "0", etc.
Returns
true if the input string represents a true value; false otherwise.
Note
This function is case-insensitive and will convert the input string to lowercase before comparison.

bool result1 = stringToBool("True"); // result1 is true bool result2 = stringToBool("false"); // result2 is false bool result3 = stringToBool("1"); // result3 is true bool result4 = stringToBool("0"); // result4 is false

#ifndef _FLEDGE_UTILS_H
#define _FLEDGE_UTILS_H
/*
* Fledge general utilities
*
* Copyright (c) 2018 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Massimiliano Pinto
*/
#include <string>
#include <algorithm>
#define _FLEDGE_ROOT_PATH "/usr/local/fledge"
using namespace std;
static const string getRootDir()
{
const char* rootDir = getenv("FLEDGE_ROOT");
return (rootDir ? string(rootDir) : string(_FLEDGE_ROOT_PATH));
}
static const string getDataDir()
{
const char* dataDir = getenv("FLEDGE_DATA");
return (dataDir ? string(dataDir) : string(getRootDir() + "/data"));
}
static std::string getDebugTracePath()
{
return getDataDir() + "/logs/debug-trace";
}
static bool stringToBool(const std::string& str)
{
std::string lowerStr = str;
std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
return (lowerStr == "true" || lowerStr == "1");
}
#endif