finish up the documentation page

Signed-off-by: ngn <ngn@ngn.tf>
This commit is contained in:
ngn
2025-01-17 02:42:32 +03:00
parent e87764a4c2
commit 6f7263dd84
28 changed files with 1141 additions and 234 deletions

View File

@ -1,72 +1,53 @@
#include <linux/limits.h>
#include <cjson/cJSON.h>
#include <ctorm/ctorm.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include "util.h"
#include "routes.h"
#include "config.h"
#include "docs.h"
void route_list(ctorm_req_t *req, ctorm_res_t *res) {
config_t *conf = REQ_LOCAL("config");
char *docs_dir = config_get(conf, "docs_dir");
char *docs_dir = config_get(conf, "docs_dir"), *doc_data = NULL;
cJSON *array = NULL, *json = NULL, *doc_json = NULL;
docs_t docs;
cJSON *array = NULL, *json = NULL;
DIR *docs_dir_fd = NULL;
if (NULL == (array = cJSON_CreateArray())) {
ctorm_fail("failed to create cJSON array");
return util_send(res, 500, NULL);
if (!docs_init(&docs, docs_dir)) {
ctorm_fail("docs_init failed: %s", ctorm_geterror());
util_send(res, 500, NULL);
goto end;
}
if (NULL == (docs_dir_fd = opendir(docs_dir))) {
ctorm_fail("failed to open the docs dir (%s): %s", docs_dir, ctorm_geterror());
return util_send(res, 500, NULL);
}
char doc_path[PATH_MAX + 1], doc_name[NAME_MAX + 1];
util_file_t *doc_file = NULL;
struct dirent *doc = NULL;
uint64_t ext_indx = 0;
while (NULL != (doc = readdir(docs_dir_fd))) {
if ((ext_indx = util_endswith(doc->d_name, ".json")) == 0)
continue;
snprintf(doc_path, sizeof(doc_path), "%s/%s", docs_dir, doc->d_name);
if (NULL == (doc_file = util_file_load(doc_path))) {
ctorm_fail("failed to load the JSON file: %s", doc_path);
goto next;
}
if (NULL == (json = cJSON_Parse(doc_file->content))) {
ctorm_fail("failed to parse the JSON file: %s", doc_path);
goto next;
}
strcpy(doc_name, doc->d_name);
util_tolower(doc_name);
doc_name[ext_indx] = 0;
cJSON_AddStringToObject(json, "name", doc_name);
cJSON_AddItemToArray(array, json);
next:
if (NULL != doc_file)
util_file_free(doc_file);
doc_file = NULL;
}
closedir(docs_dir_fd);
if (NULL == (json = cJSON_CreateObject())) {
ctorm_fail("failed to create cJSON object");
return util_send(res, 500, NULL);
util_send(res, 500, NULL);
goto end;
}
while (NULL != (doc_data = docs_next(&docs, NULL, false))) {
if (NULL == (array = cJSON_GetObjectItem(json, docs.lang)) &&
NULL == (array = cJSON_AddArrayToObject(json, docs.lang))) {
ctorm_fail("failed to create an array object for the language %s", docs.lang);
continue;
}
if (NULL == (doc_json = cJSON_Parse(doc_data))) {
ctorm_fail("failed to parse JSON: %s (%s)", docs.name, docs.lang);
continue;
}
cJSON_AddStringToObject(doc_json, "name", docs.name);
cJSON_AddItemToArray(array, doc_json);
}
cJSON_AddItemToObject(json, "list", array);
util_send(res, 200, json);
cJSON_free(json);
end:
docs_free(&docs);
if (NULL != json)
cJSON_Delete(json);
}