Refactor config parsing to use a switch statement for improved readability and maintainability

This commit is contained in:
2025-10-02 21:46:42 +00:00
parent 8b87e77db8
commit e93e65f882

View File

@@ -5,6 +5,20 @@
#include <ctype.h>
#include "server_config.h"
typedef enum {
CONFIG_PORT,
CONFIG_USE_HTTPS,
CONFIG_LOG_FILE,
CONFIG_MAX_THREADS,
CONFIG_RUNNING,
CONFIG_SERVER_NAME,
CONFIG_VERBOSE,
CONFIG_ENABLE_HTTP2,
CONFIG_ENABLE_WEBSOCKET,
CONFIG_UNKNOWN
} ConfigKey;
// Trim whitespace from both ends of a string
static char* trim_whitespace(char *str) {
char *end;
@@ -32,6 +46,32 @@ static bool parse_bool(const char *value) {
}
return false;
}
// Map string to enum
static ConfigKey get_config_key(const char *key) {
static const struct
{
const char *name;
ConfigKey key;
} key_map[] = {
{"port", CONFIG_PORT},
{"use_https", CONFIG_USE_HTTPS},
{"log_file", CONFIG_LOG_FILE},
{"max_threads", CONFIG_MAX_THREADS},
{"running", CONFIG_RUNNING},
{"server_name", CONFIG_SERVER_NAME},
{"verbose", CONFIG_VERBOSE},
{"enable_http2", CONFIG_ENABLE_HTTP2},
{"enable_websocket",CONFIG_ENABLE_WEBSOCKET},
{NULL, CONFIG_UNKNOWN}
};
for (int i = 0;key_map[i].name != NULL; i++) {
if (strcasecmp(key, key_map[i].name) == 0) {
return key_map[i].key;
}
}
return CONFIG_UNKNOWN;
}
int load_config(const char *filename, ServerConfig *config) {
FILE *fp = fopen(filename, "r");
@@ -80,54 +120,65 @@ int load_config(const char *filename, ServerConfig *config) {
value[strlen(value) - 1] = '\0';
value++;
}
// Parse configuration options
if (strcasecmp(key, "port") == 0) {
switch (get_config_key(key)) {
case CONFIG_PORT:
config->port = atoi(value);
printf("load_config: port = %d\n", config->port);
}
else if (strcasecmp(key, "use_https") == 0) {
break;
case CONFIG_USE_HTTPS:
config->use_https = parse_bool(value);
printf("load_config: use_https = %d\n", config->use_https);
}
else if (strcasecmp(key, "log_file") == 0) {
break;
case CONFIG_LOG_FILE:
strncpy(config->log_file, value, sizeof(config->log_file) - 1);
config->log_file[sizeof(config->log_file) - 1] = '\0';
printf("load_config: log_file = %s\n", config->log_file);
}
else if (strcasecmp(key, "max_threads") == 0) {
break;
case CONFIG_MAX_THREADS:
config->max_threads = atoi(value);
printf("load_config: max_threads = %d\n", config->max_threads);
}
else if (strcasecmp(key, "running") == 0) {
break;
case CONFIG_RUNNING:
config->running = parse_bool(value);
printf("load_config: running = %d\n", config->running);
}
else if (strcasecmp(key, "server_name") == 0) {
break;
case CONFIG_SERVER_NAME:
strncpy(config->server_name, value, sizeof(config->server_name) - 1);
config->server_name[sizeof(config->server_name) - 1] = '\0';
printf("load_config: server_name = %s\n", config->server_name);
if (strcmp(config->server_name, "Your_domain/IP") == 0) {
fprintf(stderr, "WARNING: server_name is set to default\nPlease set server_name in server.conf to the server's IP address or domain name for proper operation.\n");
fprintf(stderr, "WARNING: server_name is set to default\n"
"Please set server_name in server.conf to the server's IP address or domain name for proper operation.\n");
}
}
else if (strcasecmp(key, "verbose") == 0) {
break;
case CONFIG_VERBOSE:
config->verbose = parse_bool(value);
printf("load_config: verbose = %d\n", config->verbose);
}
else if (strcasecmp(key, "enable_http2") == 0) {
break;
case CONFIG_ENABLE_HTTP2:
config->enable_http2 = parse_bool(value);
printf("load_config: enable_http2 = %d\n", config->enable_http2);
}
else if (strcasecmp(key, "enable_websocket") == 0) {
break;
case CONFIG_ENABLE_WEBSOCKET:
config->enable_websocket = parse_bool(value);
printf("load_config: enable_websocket = %d\n", config->enable_websocket);
}
else {
fprintf(stderr, "Warning: Unknown config option '%s' on line %d\n", key, line_number);
}
}
break;
case CONFIG_UNKNOWN:
default:
fprintf(stderr, "Warning: Unknown config option '%s' on line %d\n", key, line_number);
break;
}
}
fclose(fp);
return 0;
}