#include "config_parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Config *config_new() {
Config *config = (Config *)malloc(sizeof(Config));
if (!config) return NULL;
config->sections = NULL;
return config;
}
void free_lines(ConfigLine *head) {
while (head) {
ConfigLine *tmp = head;
head = head->next;
free(tmp);
}
}
void config_free(Config *config) {
ConfigSection *sec = config->sections;
while (sec) {
ConfigSection *tmp_sec = sec;
sec = sec->next;
free_lines(tmp_sec->lines);
free(tmp_sec);
}
free(config);
}
void add_line(ConfigLine **head, const char *line) {
ConfigLine *new_line = (ConfigLine *)malloc(sizeof(ConfigLine));
if (!new_line) {
fprintf(stderr, "Memory allocation failed in add_line\n");
exit(EXIT_FAILURE); // 或者返回错误码,根据你的需求
}
strncpy(new_line->line, line, sizeof(new_line->line) - 1);
new_line->line[sizeof(new_line->line) - 1] = '\0';
new_line->next = *head;
*head = new_line;
}
ConfigSection *find_section(Config *config, const char *section_type, const char *key) {
if (!config || !section_type) return NULL;
ConfigSection *sec = config->sections;
while (sec) {
if (sec->type && strcmp(sec->type, section_type) == 0) {
if (key == NULL && sec->name == NULL) {
return sec;
} else if (key && sec->name && strcmp(sec->name, key) == 0) {
return sec;
}
}
sec = sec->next;
}
return NULL;
}
ConfigSection *create_section(const char *section, const char *key) {
ConfigSection *new_sec = (ConfigSection *)malloc(sizeof(ConfigSection));
if (!new_sec) {
fprintf(stderr, "Memory allocation failed in create_section\n");
return NULL;
}
strncpy(new_sec->name, section, sizeof(new_sec->name) - 1);
new_sec->name[sizeof(new_sec->name) - 1] = '\0';
if (key) {
strncpy(new_sec->key, key, sizeof(new_sec->key) - 1);
} else {
new_sec->key[0] = '\0';
}
new_sec->key[sizeof(new_sec->key) - 1] = '\0';
new_sec->lines = NULL;
new_sec->next = NULL;
return new_sec;
}
int config_add_section(Config *config, const char *section, const char *key) {
if (find_section(config, section, key)) {
return -1; // 已存在
}
ConfigSection *new_sec = create_section(section, key);
new_sec->next = config->sections;
config->sections = new_sec;
return 0;
}
int config_read(Config *config, const char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) return -1;
ConfigSection *current_sec = NULL;
char line[512];
while (fgets(line, sizeof(line), fp)) {
char key[64] = {0};
char val[128] = {0};
if (sscanf(line, "%63[^=]=%127[^\n]", key, val) == 2 && strcmp(key, "bss") == 0) {
ConfigSection *new_sec = create_section("BSS", val);
if (!new_sec) {
fclose(fp);
return -1;
}
new_sec->next = config->sections;
config->sections = new_sec;
current_sec = new_sec;
} else {
if (!current_sec) {
if (!config->sections) {
config->sections = create_section("General", NULL);
if (!config->sections) {
fclose(fp);
return -1;
}
}
current_sec = config->sections;
}
add_line(¤t_sec->lines, line);
}
}
fclose(fp);
return 0;
}
int config_write(Config *config, const char *filename) {
FILE *fp = fopen(filename, "w");
if (!fp) return -1;
for (ConfigSection *sec = config->sections; sec; sec = sec->next) {
for (ConfigLine *line = sec->lines; line; line = line->next) {
fputs(line->line, fp);
}
}
fclose(fp);
return 0;
}
int config_get_value(Config *config, const char *section, const char *key, const char *name, char *value, size_t size) {
ConfigSection *sec = find_section(config, section, key);
if (!sec) return -1;
for (ConfigLine *line = sec->lines; line; line = line->next) {
char k[128], v[128];
if (sscanf(line->line, "%127[^=]=%127[^\n]", k, v) == 2) {
if (strcmp(k, name) == 0) {
strncpy(value, v, size - 1);
value[size - 1] = '\0';
return 0;
}
}
}
return -1;
}
int config_set_value(Config *config, const char *section, const char *key, const char *name, const char *value) {
ConfigSection *sec = find_section(config, section, key);
if (!sec) return -1;
for (ConfigLine *line = sec->lines; line; line = line->next) {
char k[128], v[128];
if (sscanf(line->line, "%127[^=]=%127[^\n]", k, v) == 2) {
if (strcmp(k, name) == 0) {
snprintf(line->line, sizeof(line->line), "%s=%s\n", name, value);
return 0;
}
}
}
char new_line[512];
snprintf(new_line, sizeof(new_line), "%s=%s\n", name, value);
add_line(&sec->lines, new_line);
return 0;
}
int config_remove_section(Config *config, const char *section, const char *key) {
ConfigSection **prev = &config->sections;
ConfigSection *curr = config->sections;
while (curr) {
if (strcmp(curr->name, section) == 0 && strcmp(curr->key, key) == 0) {
*prev = curr->next;
free_lines(curr->lines);
free(curr);
return 0;
}
prev = &curr->next;
curr = curr->next;
}
return -1;
}
int config_remove_key(Config *config, const char *section, const char *key, const char *name) {
ConfigSection *sec = find_section(config, section, key);
if (!sec) return -1;
ConfigLine **prev = &sec->lines;
ConfigLine *curr = sec->lines;
while (curr) {
char k[128], v[128];
if (sscanf(curr->line, "%127[^=]=%127[^\n]", k, v) == 2 && strcmp(k, name) == 0) {
*prev = curr->next;
free(curr);
return 0;
}
prev = &curr->next;
curr = curr->next;
}
return -1;
}
int config_read_wpa(Config *config, const char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) return -1;
ConfigSection *current_sec = NULL;
char line[512];
while (fgets(line, sizeof(line), fp)) {
char sec_name[64], key[128];
if (sscanf(line, "%63[^=]={%127[^}]", sec_name, key) == 2) {
ConfigSection *new_sec = create_section(sec_name, key);
new_sec->next = config->sections;
config->sections = new_sec;
current_sec = new_sec;
} else if (strncmp(line, "}", 1) == 0) {
continue;
} else if (current_sec) {
add_line(¤t_sec->lines, line);
}
}
fclose(fp);
return 0;
}
int config_write_wpa(Config *config, const char *filename) {
FILE *fp = fopen(filename, "w");
if (!fp) return -1;
for (ConfigSection *sec = config->sections; sec; sec = sec->next) {
if (strlen(sec->key) > 0) {
fprintf(fp, "%s={%s\n", sec->name, sec->key);
}
for (ConfigLine *line = sec->lines; line; line = line->next) {
fputs(line->line, fp);
}
if (strlen(sec->key) > 0) {
fputs("}\n", fp);
}
}
fclose(fp);
return 0;
}
在此基础上完成修改并给出完整代码
最新发布