Safe Communication Framework 0.1.0 build feea4f9
ERTMS/ETCS safe communication (Subset-037 EuroRadio, Subset-098 RBC/RBC), CENELEC EN 50128 SIL 4 oriented
Loading...
Searching...
No Matches
config.c File Reference

Minimal key = value configuration-file loader. More...

#include "safecomm/config/config.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
Include dependency graph for config.c:

Go to the source code of this file.

Functions

static char * trim (char *s)
static void strip_inline_comment (char *s)
static bool store_entry (safecomm_config_t *cfg, const char *key, const char *value)
safecomm_status_t safecomm_config_load (const char *path, safecomm_config_t *out)
 Load path into out (fully overwrites it).
const char * safecomm_config_get_str (const safecomm_config_t *cfg, const char *key)
 Raw string value for key.
safecomm_status_t safecomm_config_get_u32 (const safecomm_config_t *cfg, const char *key, uint32_t *out)
 Unsigned-integer value for key.
safecomm_status_t safecomm_config_get_bool (const safecomm_config_t *cfg, const char *key, bool *out)
 Boolean value for key ("true"/"false"/"1"/"0", case-insensitive).

Detailed Description

Minimal key = value configuration-file loader.

Non-vital: performs syntax parsing only. Every consumer range-checks the values it reads (see docs/architecture/ADR-002).

Definition in file config.c.

Function Documentation

◆ trim()

char * trim ( char * s)
static

Trim ASCII whitespace from both ends of s in place; returns s.

Definition at line 17 of file config.c.

◆ strip_inline_comment()

void strip_inline_comment ( char * s)
static

Cut s at the first '#' that is preceded by whitespace or start.

Definition at line 44 of file config.c.

◆ store_entry()

bool store_entry ( safecomm_config_t * cfg,
const char * key,
const char * value )
static

Definition at line 58 of file config.c.

◆ safecomm_config_load()

safecomm_status_t safecomm_config_load ( const char * path,
safecomm_config_t * out )

Load path into out (fully overwrites it).

Reads at most SAFECOMM_CONFIG_MAX_ENTRIES key = value lines. Blank lines and lines whose first non-space character is '#' are skipped. Leading/trailing whitespace is trimmed from both key and value; an inline # ... comment after the value is stripped. No semantic checks.

Parameters
pathfile path (NUL-terminated). Non-NULL.
outreceives the parsed entries. Non-NULL.
Returns
SAFECOMM_STATUS_OK, SAFECOMM_STATUS_INVALID_PARAM (NULL args), SAFECOMM_STATUS_TRANSPORT (file cannot be opened / read), SAFECOMM_STATUS_BUFFER_TOO_SMALL (a line or the entry table overflows).
Note
REQ-SC-CFG-002.

Definition at line 74 of file config.c.

◆ safecomm_config_get_str()

const char * safecomm_config_get_str ( const safecomm_config_t * cfg,
const char * key )

Raw string value for key.

Returns
pointer into cfg (valid for its lifetime), or NULL if absent.
Note
REQ-SC-CFG-005.

Definition at line 141 of file config.c.

◆ safecomm_config_get_u32()

safecomm_status_t safecomm_config_get_u32 ( const safecomm_config_t * cfg,
const char * key,
uint32_t * out )

Unsigned-integer value for key.

Parameters
cfgloaded config. Non-NULL.
keyflat "group.name". Non-NULL.
outreceives the value on success. Non-NULL.
Returns
SAFECOMM_STATUS_OK, SAFECOMM_STATUS_INVALID_PARAM (absent key or not a base-10 uint32).
Note
REQ-SC-CFG-006. Callers still range-check the result.

Definition at line 159 of file config.c.

◆ safecomm_config_get_bool()

safecomm_status_t safecomm_config_get_bool ( const safecomm_config_t * cfg,
const char * key,
bool * out )

Boolean value for key ("true"/"false"/"1"/"0", case-insensitive).

Returns
SAFECOMM_STATUS_OK or SAFECOMM_STATUS_INVALID_PARAM.
Note
REQ-SC-CFG-007.

Definition at line 178 of file config.c.