From 876b0deb8b5b50677e3b410e1a3a3e63c5182789 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 20 Aug 2021 16:10:18 +0200 Subject: [PATCH] Detect xml models --- include/base.h | 1 + src/cmds.c | 4 ++++ src/main.c | 15 +++++++-------- src/model.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/include/base.h b/include/base.h index 417687f..078d89e 100644 --- a/include/base.h +++ b/include/base.h @@ -158,6 +158,7 @@ struct { struct { int id; char *name; + char *filename; int space_xMax; int space_yMax; int space_zMax; diff --git a/src/cmds.c b/src/cmds.c index bb76061..48610db 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -57,8 +57,12 @@ char *CmdModel(char *buf, char **argv, Server_t *args) // TODO modify model according to things in file + // Write name strcpy(newModel->name, argv[2] + 5); + // Write filename + strcpy(newModel->filename, argv[3] + 5); + sprintf(buf + strlen(buf), "Model %s created with id %d\n", newModel->name, newModel->id); goto CmdModelEnd; diff --git a/src/main.c b/src/main.c index e1972a2..fca50c2 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,7 @@ static Server_t *server; -void SigIntTermHandler(int signum) +static void SigIntTermHandler(int signum) { server->pleaseStop = true; printLog("Server stopping\n"); @@ -34,7 +34,6 @@ void SigIntTermHandler(int signum) printLog("All model shutted down\n"); } - // TODO parse args to discover : // - config files directory // - models & users directories @@ -46,14 +45,14 @@ int main(int argc, char **argv) // Get parameters TODO from args Parameters_t *parameters = (Parameters_t*) calloc(sizeof(Parameters_t), 1); - parameters->configDir = (char*) malloc(strlen("../debian/etc")); - strcpy(parameters->configDir, "../debian/etc"); + parameters->configDir = (char*) malloc(strlen("debian/etc")); + strcpy(parameters->configDir, "debian/etc"); - parameters->modelDir = (char*) malloc(strlen("../debian/var/models")); - strcpy(parameters->modelDir, "../debian/etc"); + parameters->modelDir = (char*) malloc(strlen("debian/var/models")); + strcpy(parameters->modelDir, "debian/var/models"); - parameters->userDir = (char*) malloc(strlen("../debian/var/users")); - strcpy(parameters->userDir, "../debian/etc"); + parameters->userDir = (char*) malloc(strlen("debian/var/users")); + strcpy(parameters->userDir, "debian/var/users"); // Go! printLog("Starting gem-graph-server...\n"); diff --git a/src/model.c b/src/model.c index 8f093d5..96bdb14 100644 --- a/src/model.c +++ b/src/model.c @@ -23,6 +23,9 @@ #include "../include/arrows.h" #include "../include/scheduler.h" +#include +#include + #define MAX_MODEL_NUMBER 1 #define MAX_MODEL_NAME_SIZE 255 #define ARROW_NUMBER 6 @@ -48,6 +51,9 @@ static void ModelPrepareArrows(Space_t *globalDrawingSpace, ArrowArray_t *arrowArray, Model_t *model); + +void ModelSystemDestroy(void); + /* -------------------------------------------------------------------------- */ void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots { @@ -68,6 +74,8 @@ void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots // cont. model population knownModel[knownModelSize-1]->name = (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); + knownModel[knownModelSize-1]->filename = + (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); knownModel[knownModelSize-1]->space_xMax = XMAX; knownModel[knownModelSize-1]->space_yMax = YMAX; knownModel[knownModelSize-1]->space_zMax = ZMAX; @@ -184,6 +192,11 @@ void ModelShutdown(void) void ModelSystemInit(Parameters_t *parameters) { + struct dirent *modelDirEntry = NULL; + DIR *modelDir = NULL; + Model_t *newModel; + char *extensionPosition; + loadedModel = (Model_t**) calloc(1, sizeof(Model_t*)); knownModel = (Model_t**) calloc(1, sizeof(Model_t*)); @@ -192,6 +205,35 @@ void ModelSystemInit(Parameters_t *parameters) loadedModelSize = 0; printLog("Model system initiated with folder : %s\n", parameters->modelDir); + + if ((modelDir = opendir(parameters->modelDir)) <= 0) { + printLog("Could not open %s\n", parameters->modelDir); + ModelSystemDestroy(); + kill(getpid(), SIGTERM); + return; + } + + printLog("Detected models : "); + while ((modelDirEntry = readdir(modelDir)) != NULL) { + if ((extensionPosition = strstr(modelDirEntry->d_name, ".xml"))) { + + // Creating model + ModelCreate(&newModel); + + // TODO modify model according to things in file + + // Write filename + strncpy(newModel->filename, modelDirEntry->d_name, + strlen(modelDirEntry->d_name)); + + // Write name + strncpy(newModel->name, modelDirEntry->d_name, + extensionPosition - modelDirEntry->d_name); + + printf("%s ", newModel->name); + } + } + printf("\n"); } void ModelSystemDestroy(void)