XML parsing sucessful
This commit is contained in:
parent
d6538985b8
commit
88f0c6a7b9
|
@ -6,11 +6,11 @@
|
|||
|
||||
<!-- Model identity informations -->
|
||||
<identity>
|
||||
<name>Modèle de test</name>
|
||||
<author>Adrien Bourmault</author>
|
||||
<author_id>1</author_id>
|
||||
<date>2021-08-21-21h34m44s</date> <!-- TODO timestamp ? -->
|
||||
<version>1.0</version>
|
||||
<name>Modèle de test</name>
|
||||
|
||||
</identity>
|
||||
|
||||
|
@ -75,6 +75,6 @@
|
|||
|
||||
<!-- Non context-specific tags -->
|
||||
<ref id="" date="">https://www.a-lec.org</ref>
|
||||
<quote id="" date="" author="" lang="">Comment</quote>
|
||||
<quote id="" date="" author="" lang="">Quote</quote>
|
||||
|
||||
</gem-graph-model>
|
||||
|
|
|
@ -157,9 +157,12 @@ struct {
|
|||
//
|
||||
struct {
|
||||
int id;
|
||||
bool validated;
|
||||
char *name;
|
||||
char *author;
|
||||
time_t date;
|
||||
char *version;
|
||||
char *filename;
|
||||
bool validated;
|
||||
int space_xMax;
|
||||
int space_yMax;
|
||||
int space_zMax;
|
||||
|
@ -172,7 +175,8 @@ struct {
|
|||
} typedef Model_t;
|
||||
|
||||
struct ModelField_t {
|
||||
char id[25];
|
||||
bool mandatory;
|
||||
char tag[25];
|
||||
char *value;
|
||||
size_t valueSize;
|
||||
struct ModelField_t *son;
|
||||
|
|
|
@ -72,15 +72,21 @@ static inline int parseTag(const char* tagName, char *destination, char destSize
|
|||
|
||||
xmlNodePtr cur = currentDocumentRoot;
|
||||
|
||||
printLog("Asked for %s\n", tagName);
|
||||
//printLog("Asked for %s\n", tagName);
|
||||
|
||||
// Enumerate all brothers in tree from current root
|
||||
while (cur != NULL) {
|
||||
printLog("Got %s\n", cur->name);
|
||||
//printLog("\tGot %s (res = %d)\n", cur->name, res);
|
||||
|
||||
// Check if current tag is the asked tag
|
||||
if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))) {
|
||||
//printLog("\tFound %s\n", cur->name);
|
||||
curString = (char*)xmlNodeListGetString(currentDocument,
|
||||
cur->xmlChildrenNode, 1);
|
||||
//printLog("\t\tString : %s\n", curString);
|
||||
|
||||
if (curString[0] != '\n' && destination) {
|
||||
// Check there is text in that tag (and that is asked)
|
||||
if (destination && curString[0] != '\n') {
|
||||
snprintf(destination, destSize, "%s\n", curString);
|
||||
res++;
|
||||
}
|
||||
|
@ -96,7 +102,7 @@ static inline xmlNodePtr findRoot(const char *tagName, xmlNodePtr curRoot)
|
|||
xmlNodePtr cur = curRoot;
|
||||
xmlNodePtr recur;
|
||||
|
||||
printLog("Asked for %s\n", tagName);
|
||||
//printLog("Asked for %s\n", tagName);
|
||||
|
||||
while (cur != NULL) {
|
||||
if ((!xmlStrcmp(cur->name, (const xmlChar *)tagName))){
|
||||
|
@ -115,30 +121,41 @@ static inline xmlNodePtr findRoot(const char *tagName, xmlNodePtr curRoot)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static inline int parseTree(ModelField_t *tree)
|
||||
static inline int parseTree(ModelField_t *curTree)
|
||||
{
|
||||
ModelField_t *curTree;
|
||||
|
||||
curTree = tree;
|
||||
|
||||
if ((findRoot(curTree->id, currentDocumentRoot)) == NULL)
|
||||
return EBADF;
|
||||
int currentOrSonCorrectlyParsed = 0;
|
||||
|
||||
while (curTree) {
|
||||
printLog("Asked for %s\n", curTree->id);
|
||||
|
||||
if ((parseTag(curTree->id, curTree->value, curTree->valueSize) == 0))
|
||||
if (findRoot(
|
||||
curTree->tag,
|
||||
currentDocumentRoot->parent
|
||||
) == NULL) {
|
||||
printLog("Error : can't find a new root\n");
|
||||
return EBADF;
|
||||
}
|
||||
|
||||
//printLog("Asked for %s\n", curTree->tag);
|
||||
|
||||
// Try to parse current tag
|
||||
if (parseTag(curTree->tag, curTree->value, curTree->valueSize) != 0) {
|
||||
// There are values in that tag
|
||||
if (curTree->value[0])
|
||||
if (!(curTree->value[0] == '\0')) {
|
||||
printLog("Field %s: %s", curTree->id, curTree->value);
|
||||
printLog("Field %s: %s", curTree->tag, curTree->value);
|
||||
}
|
||||
} else {
|
||||
// There aren't values in that tag
|
||||
if (curTree->mandatory) {
|
||||
// But it was mandatory
|
||||
printLog("Error : can't find %s\n", curTree->tag);
|
||||
return EBADF;
|
||||
}
|
||||
}
|
||||
|
||||
if (curTree->son)
|
||||
parseTree(curTree->son);
|
||||
currentOrSonCorrectlyParsed |= parseTree(curTree->son);
|
||||
|
||||
curTree = curTree->next;
|
||||
}
|
||||
return 0;
|
||||
return currentOrSonCorrectlyParsed;
|
||||
}
|
||||
|
|
50
src/model.c
50
src/model.c
|
@ -73,8 +73,16 @@ 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]->author =
|
||||
(char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE);
|
||||
|
||||
knownModel[knownModelSize-1]->version =
|
||||
(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;
|
||||
|
@ -175,10 +183,19 @@ void ModelDelete(int id) //XXX
|
|||
{
|
||||
free(knownModel[id-1]->name);
|
||||
knownModel[id-1]->name = NULL;
|
||||
|
||||
free(knownModel[id-1]->filename);
|
||||
knownModel[id-1]->name = NULL;
|
||||
knownModel[id-1]->filename = NULL;
|
||||
|
||||
free(knownModel[id-1]->author);
|
||||
knownModel[id-1]->author = NULL;
|
||||
|
||||
free(knownModel[id-1]->version);
|
||||
knownModel[id-1]->version = NULL;
|
||||
|
||||
free(knownModel[id-1]);
|
||||
knownModel[id-1] = NULL;
|
||||
|
||||
knownModelSize--;
|
||||
}
|
||||
|
||||
|
@ -273,32 +290,39 @@ static int ModelParseFile(Model_t *model)
|
|||
ModelField_t dateField;
|
||||
ModelField_t versionField;
|
||||
|
||||
strcpy(identityField.id, "identity");
|
||||
char date[25];
|
||||
|
||||
strcpy(identityField.tag, "identity");
|
||||
identityField.mandatory = false;
|
||||
identityField.value = NULL;
|
||||
identityField.valueSize = 0;
|
||||
identityField.son = &authorField;
|
||||
identityField.son = &nameField;
|
||||
identityField.next = NULL;
|
||||
|
||||
strcpy(nameField.id, "name");
|
||||
strcpy(nameField.tag, "name");
|
||||
nameField.mandatory = true;
|
||||
nameField.value = model->name;
|
||||
nameField.valueSize = 255;
|
||||
nameField.son = NULL;
|
||||
nameField.next = &dateField;
|
||||
nameField.next = &authorField;
|
||||
|
||||
strcpy(authorField.id, "author");
|
||||
authorField.value = NULL;
|
||||
strcpy(authorField.tag, "author");
|
||||
authorField.mandatory = true;
|
||||
authorField.value = model->author;
|
||||
authorField.valueSize = 25;
|
||||
authorField.son = NULL;
|
||||
authorField.next = &nameField;
|
||||
authorField.next = &dateField;
|
||||
|
||||
strcpy(dateField.id, "date");
|
||||
dateField.value = NULL;
|
||||
dateField.valueSize = 12;
|
||||
strcpy(dateField.tag, "date");
|
||||
dateField.mandatory = true;
|
||||
dateField.value = date;
|
||||
dateField.valueSize = 25;
|
||||
dateField.son = NULL;
|
||||
dateField.next = &versionField;
|
||||
|
||||
strcpy(versionField.id, "version");
|
||||
versionField.value = NULL;
|
||||
strcpy(versionField.tag, "version");
|
||||
versionField.mandatory = true;
|
||||
versionField.value = model->version;
|
||||
versionField.valueSize = 15;
|
||||
versionField.son = NULL;
|
||||
versionField.next = NULL;
|
||||
|
|
Loading…
Reference in New Issue