WIP: simple opening of XML file

This commit is contained in:
Adrien Bourmault 2024-01-11 18:05:50 +01:00
parent 52e6c59d76
commit d50ce9afe1
Signed by: neox
GPG Key ID: 2974E1D5F25DFCC8
3 changed files with 369 additions and 333 deletions

View File

@ -1,25 +1,32 @@
#include <stdbool.h>
#include <libxml/parser.h>
// #define LIBXML_TEST_VERSION
// xmlCheckVersion(20912)
bool model_init(char *docname);
bool model_shutdown(char *docname);
char model_get_dim(xmlChar *reste);
char model_get_dim_X(xmlChar *path);
char model_get_dim_Y(xmlChar *path);
char model_get_dim_Z(xmlChar *path);
char model_get_dim_value(xmlChar *path, xmlChar *axis);
char model_get_cond (xmlChar *path, xmlChar *node_id);
char model_get_trans (xmlChar *path, xmlChar *node_id);
char model_get_cond_tree (xmlChar *path);
char model_get_multiplicity (xmlChar *path);
char model_get_objects_list(xmlChar *path);
char model_get_states_list(xmlChar *path);
long model_get_state_arrows_count(xmlChar *path, long n);
int model_get_state(xmlChar *path, xmlChar *state_id);
char model_get_conditions_list(xmlChar *path);
char model_get_transitions_list(xmlChar *path);
/*
* Gem-graph OpenGL experiments
*
* Desc: Model parsing header
*
* Copyright (C) 2023 Arthur Menges <arthur.menges@a-lec.org>
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
*
* This file is part of Gem-graph.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <unistd.h>
#include "../include/base.h"
bool model_init(const char *content, size_t length, const char *basename);

View File

@ -1,3 +1,27 @@
/*
* Gem-graph client
*
* Desc: Model parsing functions
*
* Copyright (C) 2023 Arthur Menges <arthur.menges@a-lec.org>
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
*
* This file is part of Gem-graph.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@ -7,49 +31,47 @@
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlreader.h> // http://xmlsoft.org/examples/#parse1.c
#define VRB0 0
#define VRB1 0
#define VRB2 0
#define VRB3 0
#define VRB4 1
// https://gnome.pages.gitlab.gnome.org/libxml2/devhelp/general.html
// https://gnome.pages.gitlab.gnome.org/libxml2/devhelp/general.html
// make && bin/learning ./data/dimers\ random\ walk.xml
#include "../../include/base.h"
/****** WARNING ! VARIABLES GLOBALES !! ÇA PAS BON !!! > TODO < **********/
xmlDocPtr doc; // ! WARNING ! I TAKE FOR GRANTED THAT xmlDocPtr doc != NULL
xmlHashTable *my_table;
/******************************************************************************/
bool model_init(char *docname)
bool model_init(char *content, size_t length, char *basename)
{
xmlNodePtr node;
/*
* this initialize the library and check potential ABI mismatches
* between the version it was compiled for and the actual shared
* library used.
*/
LIBXML_TEST_VERSION
doc = xmlParseFile(docname);
xmlDocPtr doc;
doc = xmlReadMemory(content, length, basename, NULL, 0);
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully.\n");
return false;
}
node = xmlDocGetRootElement(doc);
g_free(basename);
g_free(content);
return true;
if (node == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return false;
}
/* node = xmlDocGetRootElement(doc); */
if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) {
fprintf(stderr,"document of the wrong type, root node != gem-graph-model\n");
xmlFreeDoc(doc);
return false;
}
/* if (node == NULL) { */
/* fprintf(stderr,"empty document\n"); */
/* xmlFreeDoc(doc); */
/* return false; */
/* } */
my_table = xmlHashCreate(10);
printf("-------------------- model_init(%s) -----------------------\n", docname);
/* if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) { */
/* fprintf(stderr,"document of the wrong type, root node != gem-graph-model\n"); */
/* xmlFreeDoc(doc); */
/* return false; */
/* } */
/* my_table = xmlHashCreate(10); */
/* xmlDocPtr my_tree = xmlReadIO, */
/* xmlInputReadCallback ioread, */
@ -58,345 +80,345 @@ bool model_init(char *docname)
/* const char * URL, */
/* const char * encoding, */
/* int options); */
return true;
}
/******************************************************************************/
bool model_shutdown(char *docname){
/* bool model_shutdown(char *docname){ */
xmlFreeDoc(doc);
/* * Cleanup function for the XML library. */
/* */
/* xmlCleanupParser(); */
/* * this is to debug memory for regression tests */
/* */
/* xmlMemoryDump(); */
printf("-------------------- model_shutdown(%s) -------------------\n", docname);
return false;
}
/* return true; */
/* } */
/******************************************************************************/
static xmlNodePtr getNextChild(xmlNodePtr node, xmlChar *last){
while (node != NULL && xmlStrcmp(node->name, last)) {
// printf(" <>--- line n°%lu <%s>\n", xmlGetLineNo(node), node->name);
node = node->next;
}
return node;
}
/* static xmlNodePtr getNextChild(xmlNodePtr node, xmlChar *last){ */
/* while (node != NULL && xmlStrcmp(node->name, last)) { */
/* // printf(" <>--- line n°%lu <%s>\n", xmlGetLineNo(node), node->name); */
/* node = node->next; */
/* } */
/* return node; */
/* } */
static xmlChar* splitStrAtSlash(xmlChar *toSplit){
toSplit = (xmlChar *)xmlStrchr(toSplit, '/');
toSplit = xmlStrsub (toSplit, 1, xmlStrlen(toSplit));
return toSplit;
}
/* static xmlChar* splitStrAtSlash(xmlChar *toSplit){ */
/* toSplit = (xmlChar *)xmlStrchr(toSplit, '/'); */
/* toSplit = xmlStrsub (toSplit, 1, xmlStrlen(toSplit)); */
/* return toSplit; */
/* } */
static xmlChar* getFirstTag(xmlChar *path){
xmlChar *preop = path;
path = (xmlChar *)xmlStrchr(path, '/');
path = xmlStrsub (path, 1, xmlStrlen(path));
/* static xmlChar* getFirstTag(xmlChar *path){ */
/* xmlChar *preop = path; */
/* path = (xmlChar *)xmlStrchr(path, '/'); */
/* path = xmlStrsub (path, 1, xmlStrlen(path)); */
/* printf("%s = %s + / + %s\n", preop,\ */
/* xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1), path); */
return xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1);
}
/* return xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1); */
/* } */
static xmlChar* getLastTag(xmlChar *path){
while ((ulong)xmlStrchr (path, '/')) path = splitStrAtSlash((xmlChar *)path);
// printf("last tag in the path = <%s>\n", path);
return path; // which is no more the given path but only its last tag !
}
/* static xmlChar* getLastTag(xmlChar *path){ */
/* while ((ulong)xmlStrchr (path, '/')) path = splitStrAtSlash((xmlChar *)path); */
/* // printf("last tag in the path = <%s>\n", path); */
/* return path; // which is no more the given path but only its last tag ! */
/* } */
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
static xmlNodePtr model_get_node(xmlChar *path)
{
xmlNodePtr node;
/* static xmlNodePtr model_get_node(xmlChar *path) */
/* { */
/* xmlNodePtr node; */
// Lookup for node from path in hash table
node = xmlHashLookup(my_table, path);
/* // Lookup for node from path in hash table */
/* node = xmlHashLookup(my_table, path); */
// Found a node in hash table
if(node){
if (VRB0) printf("xmlHashLookup (my_table, path: <%s>, tag: <%s> @>%p)\n",
path, node->name, node);
/* // Found a node in hash table */
/* if(node){ */
/* if (VRB0) printf("xmlHashLookup (my_table, path: <%s>, tag: <%s> @>%p)\n", */
/* path, node->name, node); */
assert(node > (xmlNodePtr)2048);
/* assert(node > (xmlNodePtr)2048); */
return node;
/* return node; */
// no node found in hash table
} else {
xmlChar *reste = path, *last = reste, *affich = reste;
last = getLastTag(last);
xmlNodePtr node = xmlDocGetRootElement(doc);
/* // no node found in hash table */
/* } else { */
/* xmlChar *reste = path, *last = reste, *affich = reste; */
/* last = getLastTag(last); */
/* xmlNodePtr node = xmlDocGetRootElement(doc); */
if (VRB1) printf("line n° %lu : tag <%s> reste = %s\n",\
xmlGetLineNo(node), (xmlChar *)node->name, (xmlChar *)affich);
/* if (VRB1) printf("line n° %lu : tag <%s> reste = %s\n",\ */
/* xmlGetLineNo(node), (xmlChar *)node->name, (xmlChar *)affich); */
xmlChar *extrait;
/* xmlChar *extrait; */
while ((ulong)xmlStrchr (reste, '/')){
extrait = getFirstTag(reste); //printf("extrait = %s\n", extrait);
reste = splitStrAtSlash((xmlChar *)reste);//printf("reste = %s\n", reste);
node = node->xmlChildrenNode;
node = getNextChild(node, extrait);
/* while ((ulong)xmlStrchr (reste, '/')){ */
/* extrait = getFirstTag(reste); //printf("extrait = %s\n", extrait); */
/* reste = splitStrAtSlash((xmlChar *)reste);//printf("reste = %s\n", reste); */
/* node = node->xmlChildrenNode; */
/* node = getNextChild(node, extrait); */
if (VRB1) printf("line n°%lu : tag <%s> reste = %s\n",\
xmlGetLineNo(node), (xmlChar *)extrait, (xmlChar *)reste);
}
/* if (VRB1) printf("line n°%lu : tag <%s> reste = %s\n",\ */
/* xmlGetLineNo(node), (xmlChar *)extrait, (xmlChar *)reste); */
/* } */
if(node != NULL && xmlStrcmp(node->name, last)) {
node = node->xmlChildrenNode;
/* if(node != NULL && xmlStrcmp(node->name, last)) { */
/* node = node->xmlChildrenNode; */
while (node != NULL && xmlStrcmp(node->name, last)) {
node = node->next;
}
/* while (node != NULL && xmlStrcmp(node->name, last)) { */
/* node = node->next; */
/* } */
if (VRB1) printf("line n°%lu : tag <%s>\n",\
xmlGetLineNo(node), (xmlChar *)node->name);
/* if (VRB1) printf("line n°%lu : tag <%s>\n",\ */
/* xmlGetLineNo(node), (xmlChar *)node->name); */
xmlHashAddEntry (my_table, path, node);
}
/* xmlHashAddEntry (my_table, path, node); */
/* } */
if (VRB0) printf("VRB0 xmlHashAddEntry (my_table, path: <%s>, tag: <%s> @>%p)\n",\
path, node->name, node);
/* if (VRB0) printf("VRB0 xmlHashAddEntry (my_table, path: <%s>, tag: <%s> @>%p)\n",\ */
/* path, node->name, node); */
return node;
}
/* return node; */
/* } */
return NULL; // ?
}
/* return NULL; // ? */
/* } */
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
static long model_get_node_attrib(xmlNodePtr node)
{
long value = 0;
if (node && node->properties) {
xmlAttr* attribute = node->properties;
xmlChar* value_txt = NULL;
char *ptr;
while(attribute && attribute->name && attribute->children)
{
value_txt = xmlNodeListGetString(node->doc, attribute->children, 1);
value = strtol((char *)value_txt, &ptr, 10);
if (VRB3) printf ("model_get_node_attrib <%s> = %s = %ld\n",\
attribute->name, value_txt, value);
attribute = attribute->next;
}
}
return value;
}
/* static long model_get_node_attrib(xmlNodePtr node) */
/* { */
/* long value = 0; */
/* if (node && node->properties) { */
/* xmlAttr* attribute = node->properties; */
/* xmlChar* value_txt = NULL; */
/* char *ptr; */
/* while(attribute && attribute->name && attribute->children) */
/* { */
/* value_txt = xmlNodeListGetString(node->doc, attribute->children, 1); */
/* value = strtol((char *)value_txt, &ptr, 10); */
/* if (VRB3) printf ("model_get_node_attrib <%s> = %s = %ld\n",\ */
/* attribute->name, value_txt, value); */
/* attribute = attribute->next; */
/* } */
/* } */
/* return value; */
/* } */
static long model_get_state_arrows_nb(xmlNodePtr node)
{
long arrows_nb= 0;
if (VRB0) printf ("VRB0 model_get_state_arrows_nb <%s> start at line %ld\n",\
node->name, xmlGetLineNo(node));
while (node && node->next){
if (VRB2) printf ("%s %ld >", (xmlChar *)node->name, arrows_nb);
if (!xmlStrcmp(node->name, (xmlChar *)"arrow")) ++arrows_nb;
if (VRB2) printf (" %ld ", arrows_nb);
node = node->next;
}
if (VRB2) printf ("\n");
if (VRB0) printf ("VRB0 model_get_state_arrows_nb = %ld end at line %ld\n",\
arrows_nb, xmlGetLineNo(node));
return arrows_nb;
}
/* static long model_get_state_arrows_nb(xmlNodePtr node) */
/* { */
/* long arrows_nb= 0; */
/* if (VRB0) printf ("VRB0 model_get_state_arrows_nb <%s> start at line %ld\n",\ */
/* node->name, xmlGetLineNo(node)); */
/* while (node && node->next){ */
/* if (VRB2) printf ("%s %ld >", (xmlChar *)node->name, arrows_nb); */
/* if (!xmlStrcmp(node->name, (xmlChar *)"arrow")) ++arrows_nb; */
/* if (VRB2) printf (" %ld ", arrows_nb); */
/* node = node->next; */
/* } */
/* if (VRB2) printf ("\n"); */
/* if (VRB0) printf ("VRB0 model_get_state_arrows_nb = %ld end at line %ld\n",\ */
/* arrows_nb, xmlGetLineNo(node)); */
/* return arrows_nb; */
/* } */
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
char model_get_dim(xmlChar *path){
/* char model_get_dim(xmlChar *path){ */
xmlNodePtr node = model_get_node(path); if (VRB2) printf("@node = %p ", node);
/* xmlNodePtr node = model_get_node(path); if (VRB2) printf("@node = %p ", node); */
if (node && node->properties) {
xmlAttr* attribute = node->properties;
while(attribute && attribute->name && attribute->children)
{
xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
// printf ("model_get_dim %s = %s\n", attribute->name, value);
xmlFree(value);
attribute = attribute->next;
}
}
/* if (node && node->properties) { */
/* xmlAttr* attribute = node->properties; */
/* while(attribute && attribute->name && attribute->children) */
/* { */
/* xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1); */
/* // printf ("model_get_dim %s = %s\n", attribute->name, value); */
/* xmlFree(value); */
/* attribute = attribute->next; */
/* } */
/* } */
if (xmlHasProp (node, (xmlChar *) "z")) return 3;
if (xmlHasProp (node, (xmlChar *) "y")) return 2;
if (xmlHasProp (node, (xmlChar *) "x")) return 1;
return 0;
}
/* if (xmlHasProp (node, (xmlChar *) "z")) return 3; */
/* if (xmlHasProp (node, (xmlChar *) "y")) return 2; */
/* if (xmlHasProp (node, (xmlChar *) "x")) return 1; */
/* return 0; */
/* } */
int model_get_dim_value(xmlChar *path, xmlChar *axis){
xmlNodePtr node = model_get_node(path);
//xmlNodePtr node= xmlTextReadernoderentNode(reader);
if (node && node->properties) {
xmlAttr* attribute = node->properties;
while(attribute && attribute->name && attribute->children)
{
xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
if (!xmlStrcmp(attribute->name, axis))
return (int)strtol((char *)value, NULL, 0);
}
}
/* int model_get_dim_value(xmlChar *path, xmlChar *axis){ */
/* xmlNodePtr node = model_get_node(path); */
/* //xmlNodePtr node= xmlTextReadernoderentNode(reader); */
/* if (node && node->properties) { */
/* xmlAttr* attribute = node->properties; */
/* while(attribute && attribute->name && attribute->children) */
/* { */
/* xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1); */
/* if (!xmlStrcmp(attribute->name, axis)) */
/* return (int)strtol((char *)value, NULL, 0); */
/* } */
/* } */
return 0;
}
/* return 0; */
/* } */
char model_get_cond_tree (xmlChar *path){
xmlNodePtr node = model_get_node(path);
node = node->xmlChildrenNode;
node = getNextChild(node, (xmlChar *)"condition");
node = xmlCopyNodeList (node->xmlChildrenNode);
printf("model_get_cond_tree(\"%s\") > line n°%ld\n", path, xmlGetLineNo(node));
return 0;
}
/* char model_get_cond_tree (xmlChar *path){ */
/* xmlNodePtr node = model_get_node(path); */
/* node = node->xmlChildrenNode; */
/* node = getNextChild(node, (xmlChar *)"condition"); */
/* node = xmlCopyNodeList (node->xmlChildrenNode); */
/* printf("model_get_cond_tree(\"%s\") > line n°%ld\n", path, xmlGetLineNo(node)); */
/* return 0; */
/* } */
int model_get_cond (xmlChar *path, xmlChar *node_id){
xmlNodePtr node = model_get_node(path);
printf("model_get_cond(...) {...node->name = %s...}\n", node->name);
node = getNextChild(node->next, (xmlChar *)"condition");
node = getNextChild(node->next, (xmlChar *)"condition");
if (xmlHasProp (node, (xmlChar *) "site")) {
printf(" node %s has attribute site\n", node->name);
}
if (xmlHasProp (node, (xmlChar *) "node_id")) {
printf(" node %s has attribute %s with value %hhn %s %s\n",\
node->name,\
xmlHasProp (node, (xmlChar *) "node_id")->name,\
xmlGetProp (node, (xmlChar *) "node_id"),\
(xmlChar *)"?! xmlGetProp is deprecated !",\
(xmlChar *)"");
}
node = getNextChild(node->next, (xmlChar *)"condition");
return 0;
}
/* int model_get_cond (xmlChar *path, xmlChar *node_id){ */
/* xmlNodePtr node = model_get_node(path); */
/* printf("model_get_cond(...) {...node->name = %s...}\n", node->name); */
/* node = getNextChild(node->next, (xmlChar *)"condition"); */
/* node = getNextChild(node->next, (xmlChar *)"condition"); */
/* if (xmlHasProp (node, (xmlChar *) "site")) { */
/* printf(" node %s has attribute site\n", node->name); */
/* } */
/* if (xmlHasProp (node, (xmlChar *) "node_id")) { */
/* printf(" node %s has attribute %s with value %hhn %s %s\n",\ */
/* node->name,\ */
/* xmlHasProp (node, (xmlChar *) "node_id")->name,\ */
/* xmlGetProp (node, (xmlChar *) "node_id"),\ */
/* (xmlChar *)"?! xmlGetProp is deprecated !",\ */
/* (xmlChar *)""); */
/* } */
/* node = getNextChild(node->next, (xmlChar *)"condition"); */
/* return 0; */
/* } */
long model_get_dim_X (xmlChar *path){
xmlNodePtr node = xmlHashLookup(my_table, (xmlChar *)"get_X");
if(node){
//if (VRB1){
printf("get_X > tag <%s> found in hash table\n",\
node->name); // node->name = "x"
//}
return (long)xmlHashLookup(my_table, (xmlChar *)"get_X"); //TODO
/* long model_get_dim_X (xmlChar *path){ */
/* xmlNodePtr node = xmlHashLookup(my_table, (xmlChar *)"get_X"); */
/* if(node){ */
/* //if (VRB1){ */
/* printf("get_X > tag <%s> found in hash table\n",\ */
/* node->name); // node->name = "x" */
/* //} */
/* return (long)xmlHashLookup(my_table, (xmlChar *)"get_X"); //TODO */
}else{
xmlNodePtr node = model_get_node(path); // node = dimension
if (node && node->properties) {
xmlAttr* attribute = node->properties;
while(attribute && attribute->name && attribute->children)
{
xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
if (!xmlStrcmp(attribute->name, (xmlChar *)"x")) {
xmlHashAddEntry (my_table, (xmlChar *)"get_X", attribute);
return (int)strtol((char *)value, NULL, 0);
}
}
}
}
return 0;
}
/* }else{ */
/* xmlNodePtr node = model_get_node(path); // node = dimension */
/* if (node && node->properties) { */
/* xmlAttr* attribute = node->properties; */
/* while(attribute && attribute->name && attribute->children) */
/* { */
/* xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1); */
/* if (!xmlStrcmp(attribute->name, (xmlChar *)"x")) { */
/* xmlHashAddEntry (my_table, (xmlChar *)"get_X", attribute); */
/* return (int)strtol((char *)value, NULL, 0); */
/* } */
/* } */
/* } */
/* } */
/* return 0; */
/* } */
int model_get_dim_Y (xmlChar *path){
printf("model_get_dim_Y(%s)\n", path);
return 0;
}
/* int model_get_dim_Y (xmlChar *path){ */
/* printf("model_get_dim_Y(%s)\n", path); */
/* return 0; */
/* } */
int model_get_dim_Z (xmlChar *path){
printf("model_get_dim_Z(%s)\n", path);
return 0;
}
/* int model_get_dim_Z (xmlChar *path){ */
/* printf("model_get_dim_Z(%s)\n", path); */
/* return 0; */
/* } */
int model_get_multiplicity (xmlChar *path){
xmlNodePtr node = model_get_node(path);
if(node) return (int)strtol((char *)xmlNodeGetContent(node), NULL, 0);
return 0;
}
/* int model_get_multiplicity (xmlChar *path){ */
/* xmlNodePtr node = model_get_node(path); */
/* if(node) return (int)strtol((char *)xmlNodeGetContent(node), NULL, 0); */
/* return 0; */
/* } */
int model_get_objects_list(xmlChar *path){
return xmlGetLineNo(model_get_node(path));
}
/* int model_get_objects_list(xmlChar *path){ */
/* return xmlGetLineNo(model_get_node(path)); */
/* } */
xmlChar *model_get_states_list(xmlChar *path){
xmlNodePtr node = model_get_node(path);
return (xmlChar *)xmlNodeGetContent(node);
}
/* xmlChar *model_get_states_list(xmlChar *path){ */
/* xmlNodePtr node = model_get_node(path); */
/* return (xmlChar *)xmlNodeGetContent(node); */
/* } */
long model_get_state_arrows_count(xmlChar *path, long n){
// Ici, je sais que c'est le 'PREMIER' état qui est demandé car n == 0
char compl[] = "/state/arrow";
char *new_p = calloc(1, (sizeof(compl) + strlen((char *)path)) * sizeof(char));
strcat(new_p, (char *)path);
strcat(new_p + strlen((char *)path), compl); // printf("%s\n", new_p);
/* long model_get_state_arrows_count(xmlChar *path, long n){ */
/* // Ici, je sais que c'est le 'PREMIER' état qui est demandé car n == 0 */
/* char compl[] = "/state/arrow"; */
/* char *new_p = calloc(1, (sizeof(compl) + strlen((char *)path)) * sizeof(char)); */
/* strcat(new_p, (char *)path); */
/* strcat(new_p + strlen((char *)path), compl); // printf("%s\n", new_p); */
xmlNodePtr node = model_get_node((xmlChar *)new_p);
long size = model_get_state_arrows_nb(node);
int dim = model_get_dim((xmlChar *)"parameters/space-param/dimension");
if (VRB0) printf("VRB0 model_get_saved_state(size = %ld)->%p<-attrib dim = %d size = %ld\n",\
size, (xmlNodePtr)model_get_node_attrib(node), dim, size * ( 2 + dim ) * sizeof(int));
/* xmlNodePtr node = model_get_node((xmlChar *)new_p); */
/* long size = model_get_state_arrows_nb(node); */
/* int dim = model_get_dim((xmlChar *)"parameters/space-param/dimension"); */
/* if (VRB0) printf("VRB0 model_get_saved_state(size = %ld)->%p<-attrib dim = %d size = %ld\n",\ */
/* size, (xmlNodePtr)model_get_node_attrib(node), dim, size * ( 2 + dim ) * sizeof(int)); */
int c = 0;
int *t = calloc(1, (size * ( 2 + dim ) * sizeof(int)));
// int rk = 0;
// int *s = calloc(1, ( 2 + dim ) * sizeof(int));
/* int c = 0; */
/* int *t = calloc(1, (size * ( 2 + dim ) * sizeof(int))); */
/* // int rk = 0; */
/* // int *s = calloc(1, ( 2 + dim ) * sizeof(int)); */
if(1)
{
long arrows_nb= 0;
if (VRB4) printf ("VRB4 model_get_state_arrows_nb <%s> start at line %ld\n",\
node->name, xmlGetLineNo(node));
while (node && node->next){
if (!xmlStrcmp(node->name, (xmlChar *)"arrow")) ++arrows_nb;
if (VRB4 && !xmlStrcmp(node->name, (xmlChar *)"arrow")) printf ("VRB4 %s-%ld ",\
(xmlChar *)node->name, arrows_nb);
if (node && node->properties) {
xmlAttr* attribute = node->properties;
while(attribute)
{
xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
if (VRB4) printf("v=%s ", value);
*(t + c) = strtol((char*)value, NULL, 0);
++c;
xmlFree(value);
attribute = attribute->next;
}
if (VRB4) printf("\n");
}
node = node->next;
}
if (VRB4) printf ("VRB4 model_get_state_arrows_nb = %ld end at line %ld\n",\
arrows_nb, xmlGetLineNo(node));
if (VRB4) for(int k = 0; k < c; k++) printf("%d ", *(t + k)); printf("\n");
return arrows_nb;
}
/* if(1) */
/* { */
/* long arrows_nb= 0; */
/* if (VRB4) printf ("VRB4 model_get_state_arrows_nb <%s> start at line %ld\n",\ */
/* node->name, xmlGetLineNo(node)); */
/* while (node && node->next){ */
/* if (!xmlStrcmp(node->name, (xmlChar *)"arrow")) ++arrows_nb; */
/* if (VRB4 && !xmlStrcmp(node->name, (xmlChar *)"arrow")) printf ("VRB4 %s-%ld ",\ */
/* (xmlChar *)node->name, arrows_nb); */
/* if (node && node->properties) { */
/* xmlAttr* attribute = node->properties; */
/* while(attribute) */
/* { */
/* xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1); */
/* if (VRB4) printf("v=%s ", value); */
/* *(t + c) = strtol((char*)value, NULL, 0); */
/* ++c; */
/* xmlFree(value); */
/* attribute = attribute->next; */
/* } */
/* if (VRB4) printf("\n"); */
/* } */
/* node = node->next; */
/* } */
/* if (VRB4) printf ("VRB4 model_get_state_arrows_nb = %ld end at line %ld\n",\ */
/* arrows_nb, xmlGetLineNo(node)); */
/* if (VRB4) for(int k = 0; k < c; k++) printf("%d ", *(t + k)); printf("\n"); */
/* return arrows_nb; */
/* } */
return (long) size * ( 2 + dim ) * sizeof(int);
}
/* return (long) size * ( 2 + dim ) * sizeof(int); */
/* } */
int model_get_state(xmlChar *path, xmlChar *state_id){
printf("model_get_state (%s -> %s state)\n", path, state_id);
return 0;
}
/* int model_get_state(xmlChar *path, xmlChar *state_id){ */
/* printf("model_get_state (%s -> %s state)\n", path, state_id); */
/* return 0; */
/* } */
int model_get_conditions_list(xmlChar *path){
printf("model_get_conditions_list(%s)\n", path);
return 0;
}
/* int model_get_conditions_list(xmlChar *path){ */
/* printf("model_get_conditions_list(%s)\n", path); */
/* return 0; */
/* } */
int model_get_transitions_list(xmlChar *path){
printf("model_get_transitions_list(%s)\n", path);
return 0;
}
/* int model_get_transitions_list(xmlChar *path){ */
/* printf("model_get_transitions_list(%s)\n", path); */
/* return 0; */
/* } */

View File

@ -26,6 +26,7 @@
#include "../../include/base.h"
#include "../../include/graphics.h"
#include "../../include/parsing.h"
#include "../../include/ui.h"
@ -318,16 +319,17 @@ void on_openfile_response_complete(GObject *source_object,
{
GFile *file = G_FILE(source_object);
g_autofree char *contents = NULL;
gsize length = 0;
char *content = NULL;
size_t length = 0;
g_autoptr(GError) error = NULL;
GError *error = NULL;
char *basename = g_file_get_basename(file);
// Gives you the contents of the file as a byte array, or
// set the error argument
g_file_load_contents_finish(file,
result,
&contents,
&content,
&length,
NULL,
&error);
@ -338,10 +340,15 @@ void on_openfile_response_complete(GObject *source_object,
g_printerr("Unable to open “%s”: %s\n",
g_file_peek_path(file),
error->message);
g_free(error);
return;
}
//g_print("File content is :\n%s\n", contents);
if (model_init(content, length, basename) == false) {
ui_send_internal_notification("Error while loading the model");
g_free(basename);
return;
}
ui_enable_action("closefile");
ui_enable_action("savefile");