diff --git a/Makefile b/Makefile index 6d48a60..be38283 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,8 @@ # along with this program. If not, see . # #=----------------------------------------------------------------------------=# -CCOPTS=-pthread -Wall -g -Os -LDFLAGS= -lc -lpthread +CCOPTS=-pthread -Wall -g -Os -I/usr/include/libxml2 +LDFLAGS= -lc -lpthread -lxml2 BINDIR=bin INCDIR=include diff --git a/include/base.h b/include/base.h index 078d89e..51ae1ee 100644 --- a/include/base.h +++ b/include/base.h @@ -170,6 +170,13 @@ struct { Supervisor_t *supervisor; } typedef Model_t; +struct ModelField_t { + char id[25]; + char *value; + struct ModelField_t *son; + struct ModelField_t *next; +} typedef ModelField_t; + /* -------------------------------------------------------------------------- */ // diff --git a/include/xml.h b/include/xml.h index 6e9c8f2..0c3fb7f 100644 --- a/include/xml.h +++ b/include/xml.h @@ -31,7 +31,7 @@ static xmlDocPtr currentDocument = NULL; static xmlNodePtr currentDocumentRoot = NULL; -void setCurrentDocument(char *filename) +void openCurrentDocument(const char *filename) { currentDocument = xmlParseFile(filename); @@ -39,6 +39,7 @@ void setCurrentDocument(char *filename) printLog("%s not parsed successfully.\n", filename); xmlFreeDoc(currentDocument); currentDocument = NULL; + return; } currentDocumentRoot = xmlDocGetRootElement(currentDocument); @@ -48,13 +49,71 @@ void setCurrentDocument(char *filename) xmlFreeDoc(currentDocument); currentDocument = NULL; currentDocumentRoot = NULL; + return; } } -void delCurrentDocument(void) +void closeCurrentDocument(void) { - currentDocument = xmlFreeDoc(currentDocument); + xmlFreeDoc(currentDocument); currentDocumentRoot = NULL; currentDocument = NULL; } +static void resetDocumentRoot(void) +{ + currentDocumentRoot = xmlDocGetRootElement(currentDocument); +} + +void parseTag(const char* tagName, char *destination, char destSize) +{ + char *curString; + xmlNodePtr cur = currentDocumentRoot; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))) { + curString = (char*)xmlNodeListGetString(currentDocument, + cur->xmlChildrenNode, 1); + if (curString[0] != '\n' && destination) + snprintf(destination, destSize, "%s\n", curString); + free(curString); + } + cur = cur->next; + }; +} + +void findRoot(const char *tagName, xmlNodePtr curRoot) +{ + xmlNodePtr cur = curRoot; + + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))){ + currentDocumentRoot = cur; + } + + if (cur->xmlChildrenNode) + findRoot(tagName, cur->xmlChildrenNode); + + cur = cur->next; + } +} + +void parseTree(ModelField_t *tree) +{ + ModelField_t *curTree; + char tempString[50]; + + curTree = tree; + + while (curTree) { + findRoot(curTree->id, currentDocumentRoot); + parseTag(curTree->id, tempString, 50); + + if (!(tempString[0] == '\0')) + printLog("Field %s: %s", curTree->id, tempString); + + if (curTree->son) + parseTree(curTree->son); + + curTree = curTree->next; + } +} diff --git a/src/main.c b/src/main.c index 3f80f55..be21337 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,7 @@ static Server_t *server; -static void SigIntTermHandler(int signum) +static void ExitingHandler(int signum) { server->pleaseStop = true; printLog("Server stopping\n"); @@ -45,21 +45,21 @@ 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") + 1); - strcpy(parameters->configDir, "debian/etc"); + parameters->configDir = (char*) malloc(255 * sizeof(char)); + realpath("./debian/etc", parameters->configDir); - parameters->modelDir = (char*) malloc(strlen("debian/var/models") + 1); - strcpy(parameters->modelDir, "debian/var/models"); + parameters->modelDir = (char*) malloc(255 * sizeof(char)); + realpath("./debian/var/models", parameters->modelDir); - parameters->userDir = (char*) malloc(strlen("debian/var/users") + 1); - strcpy(parameters->userDir, "debian/var/users"); + parameters->userDir = (char*) malloc(255 * sizeof(char)); + realpath("./debian/var/users", parameters->userDir); // Go! printLog("Starting gem-graph-server...\n"); // Register new interrupt handler - signal(SIGINT, SigIntTermHandler); - signal(SIGTERM, SigIntTermHandler); + signal(SIGINT, ExitingHandler); + signal(SIGTERM, ExitingHandler); // Initializing random generator t = time(&t); diff --git a/src/model.c b/src/model.c index 4c02922..d459daa 100644 --- a/src/model.c +++ b/src/model.c @@ -211,7 +211,6 @@ void ModelSystemInit(Parameters_t *parameters) return; } - printLog("Detected models : "); while ((modelDirEntry = readdir(modelDir)) != NULL) { if ((extensionPosition = strstr(modelDirEntry->d_name, ".xml"))) { @@ -219,7 +218,17 @@ void ModelSystemInit(Parameters_t *parameters) ModelCreate(&newModel); // Write filename - strncpy(newModel->filename, modelDirEntry->d_name, + /* strncpy(newModel->filename, modelDirEntry->d_name, */ + /* strlen(modelDirEntry->d_name)); // XXX get full path */ + strncpy(newModel->filename, parameters->modelDir, + strlen(parameters->modelDir)); + strcat(newModel->filename + + strlen(parameters->modelDir), + "/"); + strncpy(newModel->filename + + strlen(parameters->modelDir) + + 1, + modelDirEntry->d_name, strlen(modelDirEntry->d_name)); // Write name @@ -234,10 +243,9 @@ void ModelSystemInit(Parameters_t *parameters) continue; } - printf("%s ", newModel->name); + printLog("Loaded model %s\n", newModel->name); } } - printf("\n"); } void ModelSystemDestroy(void) @@ -255,6 +263,44 @@ void ModelSystemDestroy(void) static void ModelParseFile(Model_t *model) { + ModelField_t identityField; + ModelField_t authorField; + ModelField_t nameField; + ModelField_t dateField; + ModelField_t versionField; + + strcpy(identityField.id, "identity"); + identityField.value = NULL; + identityField.son = &nameField; + identityField.next = NULL; + + strcpy(nameField.id, "name"); + nameField.value = NULL; + nameField.son = NULL; + nameField.next = &authorField; + + strcpy(authorField.id, "author"); + authorField.value = NULL; + authorField.son = NULL; + authorField.next = &dateField; + + strcpy(dateField.id, "date"); + dateField.value = NULL; + dateField.son = NULL; + dateField.next = &versionField; + + strcpy(versionField.id, "version"); + versionField.value = NULL; + versionField.son = NULL; + versionField.next = NULL; + // TODO modify model according to things in file - ; + + printLog("Parsing model %s\n", model->name); + openCurrentDocument(model->filename); + + resetDocumentRoot(); + parseTree(&identityField); + + closeCurrentDocument(); }