1 #ifdef HAVE_CONFIG_H
2 #include "config.h" /* HAVE_LIBMAGICS */
3 #endif
4 
5 #include "cdo_output.h"
6 #include "compare.h"
7 #include "magics_template_parser.h"
8 #include "string_utilities.h"
9 
10 #ifdef HAVE_LIBXML2
11 #include <libxml/parser.h>
12 #include <libxml/tree.h>
13 #endif
14 
15 #ifdef HAVE_LIBMAGICS
16 #include "magics_api.h"
17 #endif
18 
19 #define DBG 0
20 
21 #ifdef HAVE_LIBXML2
22 extern void *magics_node;
23 #endif
24 
25 /* Recursive function that sets the Magics parameters from the XML structure */
26 
27 #ifdef HAVE_LIBXML2
28 int
magics_template_parser(void * node)29 magics_template_parser(void *node)
30 {
31   xmlNode *a_node = (xmlNode *) node;
32   int param_set_flag;
33   xmlNode *cur_node = nullptr;
34   const char *param_name, *param_type, *param_value;
35 
36   if (a_node == nullptr) return 0;
37 
38 #if 0
39     fprintf( stdout,"Parsing the magics Node \n");
40 #endif
41 
42   if (cdo_cmpstr((const char *) a_node->name, "magics"))
43     {
44       const char *value = (const char *) xmlGetProp(a_node, (const xmlChar *) "version");
45 
46       if (value)
47         {
48           if (DBG) printf("Version %s \n", value);
49 
50           if (atof(value) > 3.0f)
51             {
52               return 1;
53             }
54         }
55     }
56 
57   for (cur_node = a_node->children; cur_node; cur_node = cur_node->next)
58     {
59       param_name = nullptr;
60       param_type = nullptr;
61       param_value = nullptr;
62 
63       if (cur_node->type == XML_ELEMENT_NODE)
64         {
65 
66           if (DBG) printf("Node Name: %s \n", cur_node->name);
67 
68 #if 0
69             fprintf( stdout,"Node Name: %s \n", cur_node->name );
70 #endif
71 
72           if (cur_node->properties == nullptr)
73             {
74               if (cur_node->children == nullptr)
75                 {
76                   printf("NO ATTRIBUTES!!!\n");
77                 }
78             }
79           else
80             {
81 
82               param_name = (const char *) xmlGetProp(cur_node, (const xmlChar *) "parameter");
83               param_type = (const char *) xmlGetProp(cur_node, (const xmlChar *) "type");
84               param_value = (const char *) xmlGetProp(cur_node, (const xmlChar *) "value");
85 #if 0
86     		printf( "\t\tAttr name: %s Type: %s Value: %s \n", param_name,param_type,param_value);
87 #endif
88 
89               param_set_flag = _set_magics_parameter_value(param_name, param_type, param_value);
90 
91               if (param_set_flag) printf(" Error in Setting the Parameter %s\n", param_name);
92             }
93         }
94     }
95 
96   return 0;
97 }
98 #else
99 
100 int
magics_template_parser(void * node)101 magics_template_parser(void *node)
102 {
103   (void) node;
104   cdo_abort("XML2 support not compiled in!");
105   return 0;
106 }
107 
108 #endif
109 
110 #ifdef HAVE_LIBMAGICS
111 int
_set_magics_parameter_value(const char * param_name,const char * param_type,const char * param_value)112 _set_magics_parameter_value(const char *param_name, const char *param_type, const char *param_value)
113 #else
114 int
115 _set_magics_parameter_value(const char *, const char *, const char *)
116 #endif
117 {
118   int ret_flag = 0;
119 #ifdef HAVE_LIBMAGICS
120   const char *sep_char = ",";
121   const char *search_char = ";";
122 
123   if (param_name == nullptr)
124     {
125       ret_flag = 1;
126       return ret_flag;
127     }
128 
129   if (param_value == nullptr) ret_flag = 2;
130 
131   // MAGICS++ ENV RELATED PARAMETERS
132   if (cdo_cmpstr(param_type, "environvar"))
133     {
134       if (cdo_cmpstr(param_name, "quiet_option"))
135         {
136           if (cdo_cmpstr(param_value, "off") || cdo_cmpstr(param_value, "OFF"))
137             {
138 #if 0
139               printf( "Quiet Option %s \n", param_value );
140 #endif
141               if (!unsetenv("MAGPLUS_QUIET"))
142                 {
143                   if (DBG) fprintf(stderr, "Quiet Option %s is un-set successfully!!! \n", param_value);
144                 }
145               else
146                 fprintf(stderr, "Quiet Option %s COULDN'T be UNSET!!!\n", param_value);
147             }
148 
149           if (cdo_cmpstr(param_value, "on") || cdo_cmpstr(param_value, "ON"))
150             {
151 #if 0
152               printf( "Quiet Option %s \n", param_value );
153 #endif
154               if (!setenv("MAGPLUS_QUIET", "1", 1))
155                 {
156                   if (DBG) fprintf(stderr, "Quiet Option %s is set successfully!!! \n", param_value);
157                 }
158               else
159                 fprintf(stderr, "Quiet Option %s COULDN'T be SET!!!\n", param_value);
160             }
161         }
162     }
163 
164   // MAGICS++ FLOAT TYPE PARAMETERS
165   else if (cdo_cmpstr(param_type, "float"))
166     {
167       mag_setr(param_name, atof(param_value));
168     }
169 
170   // MAGICS++ FLOAT ARRAY  TYPE    PARAMETERS
171   else if (cdo_cmpstr(param_type, "floatarray"))
172     {
173 
174 #if 0
175       fprintf(stderr, "param_name : %s\tparam_value: %s\n", param_name, param_value);
176 #endif
177       if (strchr(param_value, ';')) sep_char = ";";
178       const auto splitStrings = cstr_split_with_seperator(param_value, sep_char);
179       if (splitStrings.size())
180         {
181           std::vector<double> float_param_list(splitStrings.size());
182           for (int i = 0; i < (int)splitStrings.size(); i++)
183             {
184 #if 0
185 	      fprintf(stderr, "%d %d %s\n", i, (int)splitStrings.size(), splitStrings[i].c_str());
186 #endif
187               float_param_list[i] = std::stod(splitStrings[i]);
188             }
189           mag_set1r(param_name, float_param_list.data(), (int)splitStrings.size());
190         }
191     }
192 
193   // MAGICS++ INT TYPE    PARAMETERS
194   else if (cdo_cmpstr(param_type, "int"))
195     {
196       mag_seti(param_name, atoi(param_value));
197     }
198 
199   // MAGICS++ INT ARRAY  TYPE    PARAMETERS
200   else if (cdo_cmpstr(param_type, "intarray"))
201     {
202       if (strchr(param_value, ';')) sep_char = ";";
203       const auto splitStrings = cstr_split_with_seperator(param_value, sep_char);
204       if (splitStrings.size())
205         {
206           std::vector<int> int_param_list(splitStrings.size());
207           for (int i = 0; i < (int)splitStrings.size(); i++)
208             {
209               int_param_list[i] = std::stoi(splitStrings[i]);
210             }
211           mag_set1i(param_name, int_param_list.data(), (int)splitStrings.size());
212         }
213     }
214 
215   // MAGICS++ STRING TYPE    PARAMETERS
216   else if (cdo_cmpstr(param_type, "string"))
217     {
218       mag_setc(param_name, param_value);
219     }
220 
221   // MAGICS++ STRINGARRAY  TYPE    PARAMETERS
222   else if (cdo_cmpstr(param_type, "stringarray"))
223     {
224       if (DBG) fprintf(stderr, "Input strarr is %s  Sep char is %s Search char is %s\n", param_value, sep_char, search_char);
225       if (strstr(param_value, ";")) sep_char = ";";
226 
227       if (DBG) fprintf(stderr, "Input strarr is %s  Sep char is %s\n", param_value, sep_char);
228       const auto splitStrings = cstr_split_with_seperator(param_value, sep_char);
229 
230       if (DBG) fprintf(stderr, "Input strarr is %s split str count is %d Sep char is %s\n", param_value, (int)splitStrings.size(), sep_char);
231 
232       const char **split_str = (const char **) malloc(splitStrings.size() * sizeof(char*));
233       for (size_t k = 0; k < splitStrings.size(); ++k) split_str[k] = splitStrings[k].c_str();
234       mag_set1c(param_name, split_str, (int)splitStrings.size());
235       free(split_str);
236     }
237   else
238     {
239       ret_flag = 3;
240       fprintf(stderr, "Unknown Parameter Type\n");
241     }
242 #endif
243 
244   return ret_flag;
245 }
246