Skip to content

Commit

Permalink
First steps; getting principle parsing for valid example in place
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Meisrimel authored and Peter Meisrimel committed Aug 12, 2024
1 parent 0b928f4 commit 9b8fb8b
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<fmiTerminalsAndIcons fmiVersion="3.0-alpha">
<GraphicalRepresentation>
<CoordinateSystem x1="0.1" y1="2.3" x2="4.5" y2="6.7" suggestedScalingFactorTo_mm="0.2"/>
<Icon x1="-0.1" y1="-2.3" x2="-4.5" y2="-6.7"/>
</GraphicalRepresentation>
<Terminals>
<Terminal name="terminalA" matchingRule="bus" description="some description" terminalKind="abc">
<!-- These are all dummys for now -->
Expand Down
6 changes: 6 additions & 0 deletions src/XML/include/FMI/fmi_xml_terminals_and_icons.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ extern "C" {

/* Structure encapsulating terminals and icons information */
typedef struct fmi_xml_terminals_and_icons_t fmi_xml_terminals_and_icons_t;
/* Structure encapsulating graphicalRepresentation information */
typedef struct fmi_xml_graphicalRepresentation_t fmi_xml_graphicalRepresentation_t;
/* Structure encapsulating coordinateSystem information */
typedef struct fmi_xml_coordinateSystem_t fmi_xml_coordinateSystem_t;
/* Structure encapsulating icon information */
typedef struct fmi_xml_icon_t fmi_xml_icon_t;
/* Structure encapsulating terminal information */
typedef struct fmi_xml_terminal_t fmi_xml_terminal_t;

Expand Down
31 changes: 22 additions & 9 deletions src/XML/include/FMI/fmi_xml_terminals_and_icons_scheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ extern "C" {
#define FMI_XML_ATTRLIST_TERM_ICON(EXPAND_XML_ATTRNAME) \
EXPAND_XML_ATTRNAME(fmiVersion) \
EXPAND_XML_ATTRNAME(name) \
EXPAND_XML_ATTRNAME(description)
EXPAND_XML_ATTRNAME(description) \
EXPAND_XML_ATTRNAME(x1) \
EXPAND_XML_ATTRNAME(y1) \
EXPAND_XML_ATTRNAME(x2) \
EXPAND_XML_ATTRNAME(y2) \
EXPAND_XML_ATTRNAME(suggestedScalingFactorTo_mm)


/* Element names found in terminalsAndIcons.xml */
#define FMI_XML_ELMLIST_TERM_ICON(EXPAND_XML_ELMNAME) \
EXPAND_XML_ELMNAME(fmiTerminalsAndIcons) \
EXPAND_XML_ELMNAME(GraphicalRepresentation) \
EXPAND_XML_ELMNAME(CoordinateSystem) \
EXPAND_XML_ELMNAME(Icon) \
EXPAND_XML_ELMNAME(Terminals) \
EXPAND_XML_ELMNAME(Terminal) \
EXPAND_XML_ELMNAME(TerminalMemberVariable) \
Expand All @@ -48,14 +57,18 @@ extern "C" {
@multi_elem:
if the parent can have multiple elements of this type
*/
/* scheme_ID, super_type, parent_ID, sib_idx, multi_elem */
#define fmi_xml_scheme_termIcon_fmiTerminalsAndIcons {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_none, 0, 0}

#define fmi_xml_scheme_termIcon_Terminals {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_fmiTerminalsAndIcons, 1, 0}
#define fmi_xml_scheme_termIcon_Terminal {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_Terminals, 0, 1}
#define fmi_xml_scheme_termIcon_TerminalMemberVariable {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_Terminal, 0, 1}
#define fmi_xml_scheme_termIcon_TerminalStreamMemberVariable {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_Terminal, 1, 1}
#define fmi_xml_scheme_termIcon_TerminalGraphicalRepresentation {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_Terminal, 3, 0}
/* scheme_ID, super_type, parent_ID, sib_idx, multi_elem */
#define fmi_xml_scheme_termIcon_fmiTerminalsAndIcons {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_none, 0, 0}

#define fmi_xml_scheme_termIcon_GraphicalRepresentation {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_fmiTerminalsAndIcons, 0, 0}
#define fmi_xml_scheme_termIcon_CoordinateSystem {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_GraphicalRepresentation, 0, 0}
#define fmi_xml_scheme_termIcon_Icon {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_GraphicalRepresentation, 1, 0}

#define fmi_xml_scheme_termIcon_Terminals {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_fmiTerminalsAndIcons, 1, 0}
#define fmi_xml_scheme_termIcon_Terminal {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_Terminals, 0, 1}
#define fmi_xml_scheme_termIcon_TerminalMemberVariable {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_Terminal, 0, 1}
#define fmi_xml_scheme_termIcon_TerminalStreamMemberVariable {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_Terminal, 1, 1}
#define fmi_xml_scheme_termIcon_TerminalGraphicalRepresentation {fmi_xml_elmID_termIcon_none, fmi_xml_elmID_termIcon_Terminal, 3, 0}

// Attribute enum
#define FMI_TERMICON_XML_ATTR_ID(attr) fmi_termIcon_attr_id_##attr,
Expand Down
92 changes: 91 additions & 1 deletion src/XML/src/FMI/fmi_xml_terminals_and_icons.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ fmi_xml_terminals_and_icons_t* fmi_xml_allocate_terminals_and_icons(jm_callbacks

jm_vector_init(char)(&termIcon->fmi3_xml_standard_version, 0, cb);

termIcon->graphicalRepresentation = NULL;

jm_vector_init(jm_voidp)(&termIcon->terminalsOrigOrder, 0, cb);
jm_vector_init(jm_named_ptr)(&termIcon->terminalsByName, 0, cb);

Expand All @@ -61,6 +63,13 @@ void fmi_xml_free_terminals_and_icons(fmi_xml_terminals_and_icons_t* termIcon) {
jm_callbacks* callbacks = termIcon->callbacks;
if (termIcon) {
jm_vector_free_data(char)(&(termIcon->fmi3_xml_standard_version));

// free graphicalDescription
if (termIcon->graphicalRepresentation) {
callbacks->free(termIcon->graphicalRepresentation->coordinateSystem);
callbacks->free(termIcon->graphicalRepresentation->icon);
}
callbacks->free(termIcon->graphicalRepresentation);

// free terminals
for (size_t i = 0; i < jm_vector_get_size(jm_voidp)(&(termIcon->terminalsOrigOrder)); i++) {
Expand Down Expand Up @@ -139,6 +148,87 @@ int fmi_xml_handle_fmiTerminalsAndIcons(fmi3_xml_parser_context_t* context, cons
return 0;
}

int fmi_xml_handle_GraphicalRepresentation(fmi3_xml_parser_context_t* context, const char* data) {
fmi_xml_terminals_and_icons_t* termIcon = context->termIcon;
if (!data) {
termIcon->graphicalRepresentation = (fmi_xml_graphicalRepresentation_t*)context->callbacks->calloc(1, sizeof(fmi_xml_graphicalRepresentation_t));
if (!termIcon->graphicalRepresentation) {
fmi3_xml_parse_fatal(context, "Could not allocate memory");
return 1;
}
// GraphicalRepresentation has a default coordinateSystem, allocate here directly
termIcon->graphicalRepresentation->coordinateSystem = (fmi_xml_coordinateSystem_t*)context->callbacks->calloc(1, sizeof(fmi_xml_coordinateSystem_t));
if (!termIcon->graphicalRepresentation->coordinateSystem) {
fmi3_xml_parse_fatal(context, "Could not allocate memory");
return 1;
}
termIcon->graphicalRepresentation->coordinateSystem->x1 = -100;
termIcon->graphicalRepresentation->coordinateSystem->y1 = -100;
termIcon->graphicalRepresentation->coordinateSystem->x2 = 100;
termIcon->graphicalRepresentation->coordinateSystem->y2 = 100;
termIcon->graphicalRepresentation->coordinateSystem->suggestedScalingFactorTo_mm = 0.1;
} else {
;
}
return 0;
}

int fmi_xml_handle_CoordinateSystem(fmi3_xml_parser_context_t* context, const char* data) {
fmi_xml_terminals_and_icons_t* termIcon = context->termIcon;
fmi_xml_coordinateSystem_t* coordSys = termIcon->graphicalRepresentation->coordinateSystem;
if (!data) {
// Allocation in GraphicalRepresentation
int ret = 0;

ret |= fmi3_xml_parse_attr_as_float64(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_CoordinateSystem),
FMI_ATTR_TERMICON(fmi_termIcon_attr_id_x1), 1, &(coordSys->x1), -100);
ret |= fmi3_xml_parse_attr_as_float64(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_CoordinateSystem),
FMI_ATTR_TERMICON(fmi_termIcon_attr_id_y1), 1, &(coordSys->y1), -100);
ret |= fmi3_xml_parse_attr_as_float64(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_CoordinateSystem),
FMI_ATTR_TERMICON(fmi_termIcon_attr_id_x2), 1, &(coordSys->x2), 100);
ret |= fmi3_xml_parse_attr_as_float64(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_CoordinateSystem),
FMI_ATTR_TERMICON(fmi_termIcon_attr_id_y2), 1, &(coordSys->y2), 100);
ret |= fmi3_xml_parse_attr_as_float64(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_CoordinateSystem),
FMI_ATTR_TERMICON(fmi_termIcon_attr_id_suggestedScalingFactorTo_mm), 1, &(coordSys->suggestedScalingFactorTo_mm), 100);
if (ret) {
// TODO: Log; clearly state that full coordinate system is discarded and default is taken instead?
return 1;
}
} else {
// TODO: Error check; warning is
}
return 0;
}

int fmi_xml_handle_Icon(fmi3_xml_parser_context_t* context, const char* data) {
fmi_xml_terminals_and_icons_t* termIcon = context->termIcon;
fmi_xml_graphicalRepresentation_t* graphRepr = termIcon->graphicalRepresentation;
if (!data) {
int ret;
graphRepr->icon = (fmi_xml_icon_t*)context->callbacks->calloc(1, sizeof(fmi_xml_icon_t));
if (!(graphRepr->icon)) {
fmi3_xml_parse_fatal(context, "Could not allocate memory");
return 1;
}
ret = 0;
ret |= fmi3_xml_parse_attr_as_float64(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_Icon),
FMI_ATTR_TERMICON(fmi_termIcon_attr_id_x1), 1, &(graphRepr->icon->x1), 0);
ret |= fmi3_xml_parse_attr_as_float64(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_Icon),
FMI_ATTR_TERMICON(fmi_termIcon_attr_id_y1), 1, &(graphRepr->icon->y1), 0);
ret |= fmi3_xml_parse_attr_as_float64(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_Icon),
FMI_ATTR_TERMICON(fmi_termIcon_attr_id_x2), 1, &(graphRepr->icon->x2), 0);
ret |= fmi3_xml_parse_attr_as_float64(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_Icon),
FMI_ATTR_TERMICON(fmi_termIcon_attr_id_y2), 1, &(graphRepr->icon->y2), 0);
if (ret) {
// TODO: Error message?
return 1;
}
} else {
;
}
return 0;
}

int fmi_xml_handle_Terminals(fmi3_xml_parser_context_t* context, const char* data) {
fmi_xml_terminals_and_icons_t* termIcon = context->termIcon;
if (!data) {
Expand Down Expand Up @@ -192,7 +282,7 @@ int fmi_xml_handle_Terminal(fmi3_xml_parser_context_t* context, const char* data
// parse name
jm_vector(char)* bufName = fmi3_xml_reserve_parse_buffer(context, bufIdx++, 100);
if (!bufName) {return -1;}
if (fmi3_xml_parse_attr_as_string(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_Terminal), FMI_ATTR_TERMICON(fmi_attr_id_name), 1 /* required */, bufName)) {return -1;}
if (fmi3_xml_parse_attr_as_string(context, FMI_ELM_TERMICON(fmi_xml_elmID_termIcon_Terminal), FMI_ATTR_TERMICON(fmi_termIcon_attr_id_name), 1 /* required */, bufName)) {return -1;}

/* Add the name to the terminalsAndIcons-wide set and retrieve the pointer */
if (jm_vector_get_size(char)(bufName)) {
Expand Down
22 changes: 22 additions & 0 deletions src/XML/src/FMI/fmi_xml_terminals_and_icons_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,34 @@ struct fmi_xml_terminals_and_icons_t {

jm_vector(char) fmi3_xml_standard_version;

fmi_xml_graphicalRepresentation_t* graphicalRepresentation;

jm_vector(jm_voidp) terminalsOrigOrder;
jm_vector(jm_named_ptr) terminalsByName;

jm_string_set names; // storage for all terminal names
};

struct fmi_xml_graphicalRepresentation_t {
fmi_xml_coordinateSystem_t* coordinateSystem;
fmi_xml_icon_t* icon;
};

struct fmi_xml_coordinateSystem_t {
double x1;
double y1;
double x2;
double y2;
double suggestedScalingFactorTo_mm;
};

struct fmi_xml_icon_t {
double x1;
double y1;
double x2;
double y2;
};

struct fmi_xml_terminal_t {
const char* name;
// TODO: Currently only supports name; add remaining attributes.
Expand Down

0 comments on commit 9b8fb8b

Please sign in to comment.