Update config_parser.c
Added server_name
This commit is contained in:
@@ -41,48 +41,58 @@ int load_config(const char *filename, ServerConfig *config) {
|
|||||||
cJSON *port = cJSON_GetObjectItemCaseSensitive(root, "port");
|
cJSON *port = cJSON_GetObjectItemCaseSensitive(root, "port");
|
||||||
if (cJSON_IsNumber(port)) {
|
if (cJSON_IsNumber(port)) {
|
||||||
config->port = port->valueint;
|
config->port = port->valueint;
|
||||||
printf("load_config: port = %d\n", config->port); // debug
|
printf("load_config: port = %d\n", config->port);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "load_config: port not found or not a number. Using default.\n");
|
fprintf(stderr, "load_config: port not found or not a number. Using default.\n");
|
||||||
config->port = 8080; // Default value
|
config->port = 80;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *use_https = cJSON_GetObjectItemCaseSensitive(root, "use_https");
|
cJSON *use_https = cJSON_GetObjectItemCaseSensitive(root, "use_https");
|
||||||
if (cJSON_IsBool(use_https)) {
|
if (cJSON_IsBool(use_https)) {
|
||||||
config->use_https = cJSON_IsTrue(use_https);
|
config->use_https = cJSON_IsTrue(use_https);
|
||||||
printf("load_config: use_https = %d\n", config->use_https); // debug
|
printf("load_config: use_https = %d\n", config->use_https);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "load_config: use_https not found or not a boolean. Using default.\n");
|
fprintf(stderr, "load_config: use_https not found or not a boolean. Using default.\n");
|
||||||
config->use_https = false; // Default value
|
config->use_https = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *log_file = cJSON_GetObjectItemCaseSensitive(root, "log_file");
|
cJSON *log_file = cJSON_GetObjectItemCaseSensitive(root, "log_file");
|
||||||
if (cJSON_IsString(log_file) && (log_file->valuestring != NULL)) {
|
if (cJSON_IsString(log_file) && (log_file->valuestring != NULL)) {
|
||||||
strncpy(config->log_file, log_file->valuestring, sizeof(config->log_file) - 1);
|
strncpy(config->log_file, log_file->valuestring, sizeof(config->log_file) - 1);
|
||||||
config->log_file[sizeof(config->log_file) - 1] = '\0'; // Ensure null termination
|
config->log_file[sizeof(config->log_file) - 1] = '\0';
|
||||||
printf("load_config: log_file = %s\n", config->log_file); // debug
|
printf("load_config: log_file = %s\n", config->log_file);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "load_config: log_file not found or not a string. Using default.\n");
|
fprintf(stderr, "load_config: log_file not found or not a string. Using default.\n");
|
||||||
strcpy(config->log_file, "server.log"); // Default value
|
strcpy(config->log_file, "server.log");
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *max_threads = cJSON_GetObjectItemCaseSensitive(root, "max_threads");
|
cJSON *max_threads = cJSON_GetObjectItemCaseSensitive(root, "max_threads");
|
||||||
if (cJSON_IsNumber(max_threads)) {
|
if (cJSON_IsNumber(max_threads)) {
|
||||||
config->max_threads = max_threads->valueint;
|
config->max_threads = max_threads->valueint;
|
||||||
printf("load_config: max_threads = %d\n", config->max_threads); // debug
|
printf("load_config: max_threads = %d\n", config->max_threads);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "load_config: max_threads not found or not a number. Using default.\n");
|
fprintf(stderr, "load_config: max_threads not found or not a number. Using default.\n");
|
||||||
config->max_threads = 4; // Default value
|
config->max_threads = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *running = cJSON_GetObjectItemCaseSensitive(root, "running");
|
cJSON *running = cJSON_GetObjectItemCaseSensitive(root, "running");
|
||||||
if (cJSON_IsBool(running)) {
|
if (cJSON_IsBool(running)) {
|
||||||
config->running = cJSON_IsTrue(running);
|
config->running = cJSON_IsTrue(running);
|
||||||
printf("load_config: running = %d\n", config->running); // debug
|
printf("load_config: running = %d\n", config->running);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "load_config: running not found or not a boolean. Using default.\n");
|
fprintf(stderr, "load_config: running not found or not a boolean. Using default.\n");
|
||||||
config->running = true; // Default value
|
config->running = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cJSON *server_name = cJSON_GetObjectItemCaseSensitive(root, "server_name");
|
||||||
|
if (cJSON_IsString(server_name) && (server_name->valuestring != NULL)) {
|
||||||
|
strncpy(config->server_name, server_name->valuestring, sizeof(config->server_name) - 1);
|
||||||
|
config->server_name[sizeof(config->server_name) - 1] = '\0';
|
||||||
|
printf("load_config: server_name = %s\n", config->server_name);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "load_config: server_name not found or not a string. Using default.\n");
|
||||||
|
strcpy(config->server_name, "192.168.1.1"); // Default IP address
|
||||||
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
cJSON_Delete(root);
|
cJSON_Delete(root);
|
||||||
|
|||||||
Reference in New Issue
Block a user