diff --git a/src/cmds.c b/src/cmds.c index 5a5451b..2df88f7 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -95,14 +95,17 @@ char *CmdModel(char **argv, Server_t *args) id = (int) strtol(argv[2] + 3, NULL, 10); - if ((eid = ModelLoad(id)) < 0) { - strcat(buf, "Failed to load model id "); + if (id == 0 && (eid = ModelLoad(id)) <= 0) { + strcat(buf, "Failed to load model id"); tempsize = sprintf(temp, " %d\n", id); strcat(buf, temp); goto CmdModelEnd; } - strcat(buf, "Model loaded with eid "); + strcat(buf, "Model id"); + tempsize = sprintf(temp, " %d ", id); + strcat(buf, temp); + strcat(buf, "loaded with eid "); tempsize = sprintf(temp, " %d\n", eid); strcat(buf, temp); goto CmdModelEnd; diff --git a/src/model.c b/src/model.c index bef2ed5..14ffb23 100644 --- a/src/model.c +++ b/src/model.c @@ -46,7 +46,7 @@ static void ModelPrepareArrows(Space_t *globalDrawingSpace, Model_t *model); /* -------------------------------------------------------------------------- */ - +//XXX problem with index and struct void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots { knownModel = @@ -67,13 +67,17 @@ void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots int ModelLoad(int id) // TODO unload ! { - if (id > knownModelIndex) return -1; + if (id <= 0 || id >= knownModelIndex) { + return -1; + } + + printLog("Loading model id %d (/%d models)...\n", id, knownModelIndex); // Creating structure for the Scheduler knownModel[id]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); knownModel[id]->scheduler->globalDrawingSpace = - (Space_t*) calloc(1, sizeof(Space_t)); + (Space_t*) calloc(1, sizeof(Space_t)); // TODO free this knownModel[id]->scheduler->nMaxThread = knownModel[id]->nmaxThread; knownModel[id]->scheduler->nMaxCycles = knownModel[id]->nmaxCycles; @@ -83,13 +87,16 @@ int ModelLoad(int id) // TODO unload ! knownModel[id]); knownModel[id]->scheduler->arrowArray = - (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); + (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); // TODO free this ModelPrepareArrows(knownModel[id]->scheduler->globalDrawingSpace, knownModel[id]->scheduler->arrowArray, knownModel[id]); + loadedModel = + (Model_t**) realloc(loadedModel, ++loadedModelIndex * sizeof(Model_t*)); + loadedModel[loadedModelIndex] = knownModel[id]; - return loadedModelIndex++; + return loadedModelIndex; } void ModelRun(int id) @@ -121,9 +128,9 @@ void ModelShutdown(void) void ModelSystemInit(void) { - loadedModel = (Model_t**) calloc(MAX_MODEL_NUMBER, sizeof(Model_t*)); + loadedModel = (Model_t**) calloc(1, sizeof(Model_t*)); // XXX read known models from files - knownModel = (Model_t**) calloc(MAX_MODEL_NUMBER, sizeof(Model_t*)); + knownModel = (Model_t**) calloc(1, sizeof(Model_t*)); } void ModelSystemDestroy(void)