read schema files for website
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
#include "simulator.h"
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *TAG = "storage";
|
||||
|
||||
@@ -49,59 +51,82 @@ void initialize_storage()
|
||||
void load_file(const char *filename)
|
||||
{
|
||||
ESP_LOGI(TAG, "Loading file: %s", filename);
|
||||
FILE *f = fopen(filename, "r");
|
||||
if (f == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to open file for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
char line[128];
|
||||
int line_count = 0;
|
||||
char **lines = read_lines_filtered(filename, &line_count);
|
||||
uint8_t line_number = 0;
|
||||
while (fgets(line, sizeof(line), f))
|
||||
for (int i = 0; i < line_count; ++i)
|
||||
{
|
||||
char *pos = strchr(line, '\n');
|
||||
if (pos)
|
||||
{
|
||||
*pos = '\0';
|
||||
}
|
||||
|
||||
if (strlen(line) == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
char *trimmed = line;
|
||||
while (*trimmed == ' ' || *trimmed == '\t')
|
||||
{
|
||||
trimmed++;
|
||||
}
|
||||
if (*trimmed == '#' || *trimmed == '\0')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
char time[10] = {0};
|
||||
int red, green, blue, white, brightness, saturation;
|
||||
|
||||
int items_scanned = sscanf(line, "%d,%d,%d,%d,%d,%d", &red, &green, &blue, &white, &brightness, &saturation);
|
||||
int items_scanned =
|
||||
sscanf(lines[i], "%d,%d,%d,%d,%d,%d", &red, &green, &blue, &white, &brightness, &saturation);
|
||||
if (items_scanned == 6)
|
||||
{
|
||||
int total_minutes = line_number * 30;
|
||||
int hours = total_minutes / 60;
|
||||
int minutes = total_minutes % 60;
|
||||
|
||||
snprintf(time, sizeof(time), "%02d%02d", hours, minutes);
|
||||
|
||||
add_light_item(time, red, green, blue, white, brightness, saturation);
|
||||
line_number++;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "Could not parse line: %s", line);
|
||||
ESP_LOGW(TAG, "Could not parse line: %s", lines[i]);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
free_lines(lines, line_count);
|
||||
ESP_LOGI(TAG, "Finished loading file. Loaded %d entries.", line_number);
|
||||
}
|
||||
|
||||
char **read_lines_filtered(const char *filename, int *out_count)
|
||||
{
|
||||
char fullpath[128];
|
||||
snprintf(fullpath, sizeof(fullpath), "/spiffs/%s", filename[0] == '/' ? filename + 1 : filename);
|
||||
FILE *f = fopen(fullpath, "r");
|
||||
if (!f)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to open file: %s", fullpath);
|
||||
*out_count = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t capacity = 16;
|
||||
size_t count = 0;
|
||||
char **lines = (char **)malloc(capacity * sizeof(char *));
|
||||
char line[256];
|
||||
while (fgets(line, sizeof(line), f))
|
||||
{
|
||||
// Zeilenumbruch entfernen
|
||||
char *pos = strchr(line, '\n');
|
||||
if (pos)
|
||||
*pos = '\0';
|
||||
// Trim vorne
|
||||
char *trimmed = line;
|
||||
while (*trimmed == ' ' || *trimmed == '\t')
|
||||
trimmed++;
|
||||
// Leere oder Kommentarzeile überspringen
|
||||
if (*trimmed == '\0' || *trimmed == '#')
|
||||
continue;
|
||||
// Trim hinten
|
||||
size_t len = strlen(trimmed);
|
||||
while (len > 0 && (trimmed[len - 1] == ' ' || trimmed[len - 1] == '\t'))
|
||||
trimmed[--len] = '\0';
|
||||
// Kopieren
|
||||
if (count >= capacity)
|
||||
{
|
||||
capacity *= 2;
|
||||
lines = (char **)realloc(lines, capacity * sizeof(char *));
|
||||
}
|
||||
lines[count++] = strdup(trimmed);
|
||||
}
|
||||
fclose(f);
|
||||
*out_count = (int)count;
|
||||
return lines;
|
||||
}
|
||||
|
||||
void free_lines(char **lines, int count)
|
||||
{
|
||||
for (int i = 0; i < count; ++i)
|
||||
free(lines[i]);
|
||||
free(lines);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user