Settings-Datei lesen fertig

This commit is contained in:
Bernhard
2026-01-20 20:11:36 +01:00
parent 874ebf51ba
commit 281e70d91e
6 changed files with 90 additions and 19 deletions

86
settings/settings.c Normal file → Executable file
View File

@@ -1,7 +1,7 @@
#include <main.h>
#include <settings/settings.h>
#include <ctype.h>
struct APP_SETTINGS settings;
@@ -16,7 +16,7 @@ void Settings_InitDefaultValues()
// #define LOG_INFO 6 /* informational */
// #define LOG_DEBUG 7 /* debug-level messages */
settings.iDebugLevel = LOG_INFO;
settings.iCanSimu = 0;
settings.iCanSimu = 1;
settings.iMotorCount = 1;
settings.iMotorPwrMinRaw = 38;
@@ -38,17 +38,58 @@ void Settings_InitDefaultValues()
}
char *trim_str(const char *s)
{
// Führende Leerzeichen finden
while (isspace((unsigned char)*s))
s++;
// Falls der String leer ist
if (*s == 0)
return strdup("");
// Letztes Zeichen finden
const char *end = s + strlen(s) - 1;
while (end > s && isspace((unsigned char)*end))
end--;
// Länge des neuen Strings berechnen
size_t len = (end - s) + 1;
// Speicher reservieren (+1 für das Null-Byte)
char *new_str = malloc(len + 1);
if (new_str)
{
memcpy(new_str, s, len);
new_str[len] = '\0';
}
return new_str;
}
void Settings_ReadConfFile()
{
const char *filename = "/etc/CanRtDriver.conf";
//const char *filename = "/etc/CanRtDriver.conf";
char filename[MAX_PATH + 50];
sprintf(filename, "%s.conf", settings.sExePath);
FILE *file = fopen(filename, "r");
if (file == NULL)
{
mylog(LOG_ERR, "Failed to open settings file %s", filename);
return;
mylog(LOG_INFO, "SETTINGS: File %s noch found", filename);
sprintf(filename, "/etc/CanRtDriver.conf");
file = fopen(filename, "r");
if (file == NULL)
{
mylog(LOG_ERR, "SETTINGS: No conf file found!");
return;
}
}
mylog(LOG_INFO, "SETTINGS: Reading %s", filename);
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), file))
{
@@ -62,13 +103,42 @@ void Settings_ReadConfFile()
line[strcspn(line, "\r\n")] = 0;
// 3. Split line to key and value
char *key = strtok(line, "=");
char *value = strtok(NULL, "=");
char *key = trim_str(strtok(line, "="));
char *value = trim_str(strtok(NULL, "="));
if ((key != NULL) && (value != NULL))
{
mylog(LOG_DEBUG, "SETTINGS: Found key: '%s' | value: '%s'\n", key, value);
//mylog(LOG_INFO, "SETTINGS: Found key: '%s' | value: '%s'", key, value);
if (strcmp(key, "DebugLevel") == 0)
{
settings.iDebugLevel = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iDebugLevel);
}
else if (strcmp(key, "CanSimu") == 0)
{
settings.iCanSimu = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iCanSimu);
}
else if (strcmp(key, "MotorPowerMinRaw") == 0)
{
settings.iMotorPwrMinRaw = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMinRaw);
}
else if (strcmp(key, "MotorPowerMaxRaw") == 0)
{
settings.iMotorPwrMaxRaw = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMaxRaw);
}
else if (strcmp(key, "MotorPowerStepCount") == 0)
{
settings.iMotorPwrStepCount = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrStepCount);
}
else
{
mylog(LOG_WARNING, "SETTING: Unknown key: %s", key);
}
// // Beispiel: Wert verarbeiten
// if (strcmp(key, "port") == 0)
// {