First XML parsing

This commit is contained in:
Adrien Bourmault 2021-08-24 00:41:49 +02:00
parent 459da14781
commit d36242933d
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
5 changed files with 131 additions and 19 deletions

View File

@ -19,8 +19,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # # along with this program. If not, see <https://www.gnu.org/licenses/>. #
#=----------------------------------------------------------------------------=# #=----------------------------------------------------------------------------=#
CCOPTS=-pthread -Wall -g -Os CCOPTS=-pthread -Wall -g -Os -I/usr/include/libxml2
LDFLAGS= -lc -lpthread LDFLAGS= -lc -lpthread -lxml2
BINDIR=bin BINDIR=bin
INCDIR=include INCDIR=include

View File

@ -170,6 +170,13 @@ struct {
Supervisor_t *supervisor; Supervisor_t *supervisor;
} typedef Model_t; } typedef Model_t;
struct ModelField_t {
char id[25];
char *value;
struct ModelField_t *son;
struct ModelField_t *next;
} typedef ModelField_t;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
// //

View File

@ -31,7 +31,7 @@
static xmlDocPtr currentDocument = NULL; static xmlDocPtr currentDocument = NULL;
static xmlNodePtr currentDocumentRoot = NULL; static xmlNodePtr currentDocumentRoot = NULL;
void setCurrentDocument(char *filename) void openCurrentDocument(const char *filename)
{ {
currentDocument = xmlParseFile(filename); currentDocument = xmlParseFile(filename);
@ -39,6 +39,7 @@ void setCurrentDocument(char *filename)
printLog("%s not parsed successfully.\n", filename); printLog("%s not parsed successfully.\n", filename);
xmlFreeDoc(currentDocument); xmlFreeDoc(currentDocument);
currentDocument = NULL; currentDocument = NULL;
return;
} }
currentDocumentRoot = xmlDocGetRootElement(currentDocument); currentDocumentRoot = xmlDocGetRootElement(currentDocument);
@ -48,13 +49,71 @@ void setCurrentDocument(char *filename)
xmlFreeDoc(currentDocument); xmlFreeDoc(currentDocument);
currentDocument = NULL; currentDocument = NULL;
currentDocumentRoot = NULL; currentDocumentRoot = NULL;
return;
} }
} }
void delCurrentDocument(void) void closeCurrentDocument(void)
{ {
currentDocument = xmlFreeDoc(currentDocument); xmlFreeDoc(currentDocument);
currentDocumentRoot = NULL; currentDocumentRoot = NULL;
currentDocument = 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;
}
}

View File

@ -25,7 +25,7 @@
static Server_t *server; static Server_t *server;
static void SigIntTermHandler(int signum) static void ExitingHandler(int signum)
{ {
server->pleaseStop = true; server->pleaseStop = true;
printLog("Server stopping\n"); printLog("Server stopping\n");
@ -45,21 +45,21 @@ int main(int argc, char **argv)
// Get parameters TODO from args // Get parameters TODO from args
Parameters_t *parameters = (Parameters_t*) calloc(sizeof(Parameters_t), 1); Parameters_t *parameters = (Parameters_t*) calloc(sizeof(Parameters_t), 1);
parameters->configDir = (char*) malloc(strlen("debian/etc") + 1); parameters->configDir = (char*) malloc(255 * sizeof(char));
strcpy(parameters->configDir, "debian/etc"); realpath("./debian/etc", parameters->configDir);
parameters->modelDir = (char*) malloc(strlen("debian/var/models") + 1); parameters->modelDir = (char*) malloc(255 * sizeof(char));
strcpy(parameters->modelDir, "debian/var/models"); realpath("./debian/var/models", parameters->modelDir);
parameters->userDir = (char*) malloc(strlen("debian/var/users") + 1); parameters->userDir = (char*) malloc(255 * sizeof(char));
strcpy(parameters->userDir, "debian/var/users"); realpath("./debian/var/users", parameters->userDir);
// Go! // Go!
printLog("Starting gem-graph-server...\n"); printLog("Starting gem-graph-server...\n");
// Register new interrupt handler // Register new interrupt handler
signal(SIGINT, SigIntTermHandler); signal(SIGINT, ExitingHandler);
signal(SIGTERM, SigIntTermHandler); signal(SIGTERM, ExitingHandler);
// Initializing random generator // Initializing random generator
t = time(&t); t = time(&t);

View File

@ -211,7 +211,6 @@ void ModelSystemInit(Parameters_t *parameters)
return; return;
} }
printLog("Detected models : ");
while ((modelDirEntry = readdir(modelDir)) != NULL) { while ((modelDirEntry = readdir(modelDir)) != NULL) {
if ((extensionPosition = strstr(modelDirEntry->d_name, ".xml"))) { if ((extensionPosition = strstr(modelDirEntry->d_name, ".xml"))) {
@ -219,7 +218,17 @@ void ModelSystemInit(Parameters_t *parameters)
ModelCreate(&newModel); ModelCreate(&newModel);
// Write filename // 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)); strlen(modelDirEntry->d_name));
// Write name // Write name
@ -234,10 +243,9 @@ void ModelSystemInit(Parameters_t *parameters)
continue; continue;
} }
printf("%s ", newModel->name); printLog("Loaded model %s\n", newModel->name);
} }
} }
printf("\n");
} }
void ModelSystemDestroy(void) void ModelSystemDestroy(void)
@ -255,6 +263,44 @@ void ModelSystemDestroy(void)
static void ModelParseFile(Model_t *model) 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 // TODO modify model according to things in file
;
printLog("Parsing model %s\n", model->name);
openCurrentDocument(model->filename);
resetDocumentRoot();
parseTree(&identityField);
closeCurrentDocument();
} }