]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/src/XML/src/FMI1/fmi1_xml_vendor_annotations.c
Merge remote-tracking branch 'origin/master' into release/1.35.1
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / src / XML / src / FMI1 / fmi1_xml_vendor_annotations.c
1 /*
2     Copyright (C) 2012 Modelon AB
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the BSD style license.
6
7      This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10     FMILIB_License.txt file for more details.
11
12     You should have received a copy of the FMILIB_License.txt file
13     along with this program. If not, contact Modelon AB <http://www.modelon.com>.
14 */
15
16 #include <string.h>
17
18 #include "fmi1_xml_parser.h"
19 #include "fmi1_xml_model_description_impl.h"
20 #include "fmi1_xml_vendor_annotations_impl.h"
21
22 static const char* module = "FMI1XML";
23
24 void fmi1_xml_vendor_free(fmi1_xml_vendor_t* v) {
25     jm_named_vector_free_data(&v->annotations);
26     v->annotations.callbacks->free(v);
27 }
28
29 const char* fmi1_xml_get_vendor_name(fmi1_xml_vendor_t* v) {
30     return v->name;
31 }
32
33 unsigned int  fmi1_xml_get_number_of_vendor_annotations(fmi1_xml_vendor_t* v) {
34     return (unsigned int)jm_vector_get_size(jm_named_ptr)(&v->annotations);
35 }
36
37 fmi1_xml_annotation_t* fmi1_xml_get_vendor_annotation(fmi1_xml_vendor_t* v, unsigned int  index) {
38     if(index >= fmi1_xml_get_number_of_vendor_annotations(v)) return 0;
39     return jm_vector_get_item(jm_named_ptr)(&v->annotations, index).ptr;
40 }
41
42 const char* fmi1_xml_get_annotation_name(fmi1_xml_annotation_t* a) {
43     return a->name;
44 }
45
46 const char* fmi1_xml_get_annotation_value(fmi1_xml_annotation_t* a) {
47     return a->value;
48 }
49
50 int fmi1_xml_handle_VendorAnnotations(fmi1_xml_parser_context_t *context, const char* data) {
51     if(!data) {
52                 jm_log_verbose(context->callbacks, module, "Parsing XML element VendorAnnotations");
53         }
54     else {
55         /* might give out a warning if(data[0] != 0) */
56     }
57     return 0;
58 }
59
60 int fmi1_xml_handle_Tool(fmi1_xml_parser_context_t *context, const char* data) {
61     if(!data) {
62             fmi1_xml_model_description_t* md = context->modelDescription;
63             jm_vector(char)* bufName = fmi1_xml_reserve_parse_buffer(context,1,100);
64             fmi1_xml_vendor_t* vendor = 0;
65             fmi1_xml_vendor_t dummyV;
66             jm_voidp *pvendor;
67
68             if(!bufName) return -1;
69             /* <xs:attribute name="name" type="xs:normalizedString" use="required"> */
70             if( fmi1_xml_set_attr_string(context, fmi1_xml_elmID_Tool, fmi_attr_id_name, 1, bufName)) return -1;
71             pvendor = jm_vector_push_back(jm_voidp)(&md->vendorList, vendor);
72             if(pvendor )
73                 *pvendor = vendor = jm_named_alloc_v(bufName,sizeof(fmi1_xml_vendor_t), dummyV.name - (char*)&dummyV, context->callbacks).ptr;
74             if(!pvendor || !vendor) {
75                 fmi1_xml_parse_fatal(context, "Could not allocate memory");
76                 return -1;
77             }
78             jm_vector_init(jm_named_ptr)(&vendor->annotations,0, context->callbacks);
79     }
80     else {
81         /* don't do anything. might give out a warning if(data[0] != 0) */
82         return 0;
83     }
84     return 0;
85 }
86 int fmi1_xml_handle_Annotation(fmi1_xml_parser_context_t *context, const char* data) {
87     if(!data) {
88             fmi1_xml_model_description_t* md = context->modelDescription;
89             size_t numVendors = jm_vector_get_size(jm_voidp)(&(md->vendorList));
90             fmi1_xml_vendor_t* vendor =(fmi1_xml_vendor_t*)jm_vector_get_item(jm_voidp)(&(md->vendorList), numVendors-1);
91             jm_vector(char)* bufName = fmi1_xml_reserve_parse_buffer(context,1,100);
92             jm_vector(char)* bufValue = fmi1_xml_reserve_parse_buffer(context,2,100);
93             jm_named_ptr named, *pnamed;
94             fmi1_xml_annotation_t* annotation = 0;
95             size_t vallen;
96
97             if(!bufName || !bufValue ||
98             /*     <xs:attribute name="name" type="xs:normalizedString" use="required"/> */
99                 fmi1_xml_set_attr_string(context, fmi1_xml_elmID_Annotation, fmi_attr_id_name, 1, bufName) ||
100             /* <xs:attribute name="value" type="xs:string" use="required"/> */
101                 fmi1_xml_set_attr_string(context, fmi1_xml_elmID_Annotation, fmi_attr_id_value, 1, bufValue)
102                     )
103                 return -1;
104             vallen = jm_vector_get_size(char)(bufValue);
105             named.ptr = 0;
106                         named.name = 0;
107             pnamed = jm_vector_push_back(jm_named_ptr)(&vendor->annotations, named);
108
109             if(pnamed) *pnamed = named = jm_named_alloc_v(bufName,sizeof(fmi1_xml_annotation_t)+vallen+1,sizeof(fmi1_xml_annotation_t)+vallen,context->callbacks);
110             annotation = named.ptr;
111             if( !pnamed || !annotation ) {
112                 fmi1_xml_parse_fatal(context, "Could not allocate memory");
113                 return -1;
114             }
115             annotation->name = named.name;
116             if(vallen)
117                 memcpy(annotation->value,jm_vector_get_itemp(char)(bufValue,0), vallen);
118             annotation->value[vallen] = 0;
119     }
120     else {
121         /* don't do anything. might give out a warning if(data[0] != 0) */
122         return 0;
123     }
124     return 0;
125 }
126