getIntEnv

C++ Function Reference

1 Signature

static int getIntEnv(const char *name, int def)

2 Description

Retrieves integer value from environment variable.

3 Parameters

  • name (const char *): Environment variable name
  • def (int): Default value if not found or invalid

4 Returns

int Parsed value or default

5 Details

nameEnvironment variable name defDefault value if not found or invalid int Parsed value or defaultHandles empty or missing environment variablesValidates numeric formatEnsures positive integer valuesIssues warning for invalid values

6 Call Graph

Function dependencies

7 Source Code

File: inst/include/Utilities/openme-utils.hppLines 66-82

static int getIntEnv(const char *name, int def)
{
    const char *val = getenv(name);
    if (val==NULL) return def;
    size_t nchar = strlen(val);
    if (nchar==0) return def;
    char *end;
    errno = 0;
    long int ans = strtol(val, &end, 10);  // ignores leading whitespace. If it fully consumed the string, *end=='\0' and isspace('\0')==false
    while (isspace(*end)) end++;  // ignore trailing whitespace
    if (errno || (size_t)(end-val)!=nchar || ans<1 || ans>INT_MAX) {
        Rcpp::warning(("Ignoring invalid %s==\"%s\". Not an integer >= 1. Please remove any characters that are not a digit [0-9]."), name, val);
        return def;
        
    }
    return (int)ans;
}

8 Usage Example

#include "BigDataStatMeth.hpp"

// Example usage
auto result = getIntEnv(...);