1 |
/* xmlparser.cpp |
---|
2 |
* Copyright (C) 2005,06 Pieter Palmers, Daniel Wagner |
---|
3 |
* |
---|
4 |
* This file is part of FreeBoB |
---|
5 |
* |
---|
6 |
* This library is free software; you can redistribute it and/or |
---|
7 |
* modify it under the terms of the GNU Lesser General Public |
---|
8 |
* License as published by the Free Software Foundation; either |
---|
9 |
* version 2.1 of the License, or (at your option) any later version. |
---|
10 |
* |
---|
11 |
* This library is distributed in the hope that it will be useful, |
---|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 |
* Lesser General Public License for more details. |
---|
15 |
* |
---|
16 |
* You should have received a copy of the GNU Lesser General Public |
---|
17 |
* License along with this library; if not, write to the Free Software |
---|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
---|
19 |
* MA 02110-1301 USA |
---|
20 |
*/ |
---|
21 |
|
---|
22 |
#include "libfreebob/xmlparser.h" |
---|
23 |
#include "libfreebob/freebob.h" |
---|
24 |
|
---|
25 |
#include <libxml/xmlmemory.h> |
---|
26 |
#include <libxml/parser.h> |
---|
27 |
|
---|
28 |
#include <stdio.h> |
---|
29 |
#include <stdlib.h> |
---|
30 |
#include <string.h> |
---|
31 |
#include <assert.h> |
---|
32 |
|
---|
33 |
#undef DEBUG |
---|
34 |
#ifdef DEBUG |
---|
35 |
#define debugPrint( format, args... ) printf( format, ##args ) |
---|
36 |
#else |
---|
37 |
#define debugPrint( format, args... ) |
---|
38 |
#endif |
---|
39 |
|
---|
40 |
/* XML parsing functions |
---|
41 |
*/ |
---|
42 |
freebob_stream_spec_t* |
---|
43 |
freebob_xmlparse_stream( xmlDocPtr doc, xmlNodePtr cur ) |
---|
44 |
{ |
---|
45 |
freebob_stream_spec_t *stream_spec; |
---|
46 |
|
---|
47 |
// allocate memory |
---|
48 |
stream_spec = malloc( sizeof(freebob_stream_spec_t) ); |
---|
49 |
if ( !stream_spec ) { |
---|
50 |
fprintf( stderr, "Could not allocate memory for stream_spec" ); |
---|
51 |
return 0; |
---|
52 |
} |
---|
53 |
|
---|
54 |
#define StreamSpecParseNode( NodeName, Member ) \ |
---|
55 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) NodeName ) ) { \ |
---|
56 |
xmlChar* key = \ |
---|
57 |
xmlNodeListGetString( doc, cur->xmlChildrenNode, 1 ); \ |
---|
58 |
debugPrint( "\t\t"#NodeName": %s\n", key ); \ |
---|
59 |
stream_spec->Member = strtol( (const char*) key, \ |
---|
60 |
(char**) 0, 10 ); \ |
---|
61 |
xmlFree( key ); \ |
---|
62 |
} |
---|
63 |
|
---|
64 |
cur = cur->xmlChildrenNode; |
---|
65 |
while ( cur ) { |
---|
66 |
StreamSpecParseNode( "Position", position ); |
---|
67 |
StreamSpecParseNode( "Location", location ); |
---|
68 |
StreamSpecParseNode( "Format", format ); |
---|
69 |
StreamSpecParseNode( "Type", type ); |
---|
70 |
StreamSpecParseNode( "DestinationPort", destination_port ); |
---|
71 |
|
---|
72 |
if ( !xmlStrcmp( cur->name, (const xmlChar *) "Name" ) ) { |
---|
73 |
xmlChar* key = |
---|
74 |
xmlNodeListGetString( doc, cur->xmlChildrenNode, 1 ); |
---|
75 |
debugPrint( "\t\tname: %s\n", key ); |
---|
76 |
strncpy( stream_spec->name, (const char*) key, |
---|
77 |
FREEBOB_MAX_NAME_LEN ); |
---|
78 |
xmlFree( key ); |
---|
79 |
} |
---|
80 |
cur = cur->next; |
---|
81 |
} |
---|
82 |
#undef StreamSpecParseNode |
---|
83 |
|
---|
84 |
return stream_spec; |
---|
85 |
} |
---|
86 |
|
---|
87 |
freebob_stream_info_t * |
---|
88 |
freebob_xmlparse_streams( xmlDocPtr doc, xmlNodePtr node ) |
---|
89 |
{ |
---|
90 |
freebob_stream_info_t* stream_info; |
---|
91 |
|
---|
92 |
// allocate memory |
---|
93 |
stream_info = malloc( sizeof(freebob_stream_info_t) ); |
---|
94 |
if ( !stream_info ) { |
---|
95 |
fprintf( stderr, "Could not allocate memory for stream_info" ); |
---|
96 |
return 0; |
---|
97 |
} |
---|
98 |
|
---|
99 |
// count number of child streams |
---|
100 |
stream_info->nb_streams=0; |
---|
101 |
xmlNodePtr cur = node->xmlChildrenNode; |
---|
102 |
while (cur != NULL) { |
---|
103 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Stream" ) ) { |
---|
104 |
stream_info->nb_streams++; |
---|
105 |
} |
---|
106 |
cur = cur->next; |
---|
107 |
} |
---|
108 |
|
---|
109 |
if ( stream_info->nb_streams ) { |
---|
110 |
// allocate memory for the stream_spec pointer array |
---|
111 |
stream_info->streams = |
---|
112 |
(freebob_stream_spec_t**) calloc( stream_info->nb_streams, |
---|
113 |
sizeof(freebob_stream_spec_t*) ); |
---|
114 |
|
---|
115 |
if ( !stream_info->streams ) { |
---|
116 |
fprintf( stderr, "Could not allocate memory for stream specs" ); |
---|
117 |
free( stream_info ); |
---|
118 |
return 0; |
---|
119 |
} |
---|
120 |
|
---|
121 |
// parse the child stream_specs |
---|
122 |
cur = node->xmlChildrenNode; |
---|
123 |
int i = 0; |
---|
124 |
while ( cur ) { |
---|
125 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Stream" ) ) { |
---|
126 |
stream_info->streams[i] = |
---|
127 |
freebob_xmlparse_stream( doc, cur ); |
---|
128 |
|
---|
129 |
if ( !stream_info->streams[i] ) { |
---|
130 |
// invalid XML or memory problem, clean up. |
---|
131 |
while ( --i ) { |
---|
132 |
free( stream_info->streams[i] ); |
---|
133 |
} |
---|
134 |
free( stream_info->streams ); |
---|
135 |
stream_info->streams = 0; |
---|
136 |
free( stream_info ); |
---|
137 |
return 0; |
---|
138 |
} |
---|
139 |
i++; |
---|
140 |
} |
---|
141 |
cur = cur->next; |
---|
142 |
} |
---|
143 |
} |
---|
144 |
return stream_info; |
---|
145 |
} |
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|
150 |
freebob_connection_spec_t* |
---|
151 |
freebob_xmlparse_connection( xmlDocPtr doc, xmlNodePtr cur ) |
---|
152 |
{ |
---|
153 |
#define ConnectionSpecParseNode( NodeName, Member ) \ |
---|
154 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) NodeName ) ) { \ |
---|
155 |
xmlChar* key = \ |
---|
156 |
xmlNodeListGetString( doc, cur->xmlChildrenNode, 1 ); \ |
---|
157 |
debugPrint( "\t"#NodeName": %s\n", key ); \ |
---|
158 |
connection_spec->Member = strtol( (const char*) key, \ |
---|
159 |
(char**) 0, 10 ); \ |
---|
160 |
xmlFree( key ); \ |
---|
161 |
} |
---|
162 |
|
---|
163 |
// allocate memory |
---|
164 |
freebob_connection_spec_t* connection_spec = |
---|
165 |
calloc( 1, sizeof(freebob_connection_spec_t) ); |
---|
166 |
if ( !connection_spec ) { |
---|
167 |
fprintf( stderr, "Could not allocate memory for connection_spec" ); |
---|
168 |
return 0; |
---|
169 |
} |
---|
170 |
|
---|
171 |
cur = cur->xmlChildrenNode; |
---|
172 |
while ( cur ) { |
---|
173 |
ConnectionSpecParseNode( "Id", id ); |
---|
174 |
ConnectionSpecParseNode( "Node", node ); |
---|
175 |
ConnectionSpecParseNode( "Port", port ); |
---|
176 |
ConnectionSpecParseNode( "Plug", plug ); |
---|
177 |
ConnectionSpecParseNode( "Dimension", dimension ); |
---|
178 |
ConnectionSpecParseNode( "Samplerate", samplerate ); |
---|
179 |
ConnectionSpecParseNode( "IsoChannel", iso_channel ); |
---|
180 |
ConnectionSpecParseNode( "Master", is_master ); |
---|
181 |
|
---|
182 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Streams" ) ) { |
---|
183 |
connection_spec->stream_info |
---|
184 |
= freebob_xmlparse_streams( doc, cur ); |
---|
185 |
if ( !connection_spec->stream_info ) { |
---|
186 |
free( connection_spec ); |
---|
187 |
return 0; |
---|
188 |
} |
---|
189 |
} |
---|
190 |
cur = cur->next; |
---|
191 |
} |
---|
192 |
#undef ConnectionSpecParseNode |
---|
193 |
|
---|
194 |
return connection_spec; |
---|
195 |
} |
---|
196 |
|
---|
197 |
freebob_supported_stream_format_spec_t* |
---|
198 |
freebob_xmlparse_supported_stream_format_node( xmlDocPtr doc, xmlNodePtr cur ) |
---|
199 |
{ |
---|
200 |
#define FormatSpecParseNode( NodeName, Member ) \ |
---|
201 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) NodeName ) ) { \ |
---|
202 |
xmlChar* key = \ |
---|
203 |
xmlNodeListGetString( doc, cur->xmlChildrenNode, 1 ); \ |
---|
204 |
debugPrint( "\t"#NodeName": %s\n", key ); \ |
---|
205 |
format_spec->Member = strtol( (const char*) key, \ |
---|
206 |
(char**) 0, 10 ); \ |
---|
207 |
xmlFree( key ); \ |
---|
208 |
} |
---|
209 |
|
---|
210 |
// allocate memory |
---|
211 |
freebob_supported_stream_format_spec_t* format_spec = |
---|
212 |
calloc( 1, sizeof(freebob_supported_stream_format_spec_t) ); |
---|
213 |
if ( !format_spec ) { |
---|
214 |
fprintf( stderr, "Could not allocate memory for format_spec" ); |
---|
215 |
return 0; |
---|
216 |
} |
---|
217 |
|
---|
218 |
cur = cur->xmlChildrenNode; |
---|
219 |
while ( cur ) { |
---|
220 |
FormatSpecParseNode( "Samplerate", samplerate ); |
---|
221 |
FormatSpecParseNode( "AudioChannels", nb_audio_channels ); |
---|
222 |
FormatSpecParseNode( "MidiChannels", nb_midi_channels ); |
---|
223 |
cur = cur->next; |
---|
224 |
} |
---|
225 |
#undef FormatSpecParseNode |
---|
226 |
|
---|
227 |
return format_spec; |
---|
228 |
} |
---|
229 |
|
---|
230 |
freebob_connection_info_t* |
---|
231 |
freebob_xmlparse_connectionset( xmlDocPtr doc, xmlNodePtr node ) |
---|
232 |
{ |
---|
233 |
assert( node ); |
---|
234 |
|
---|
235 |
// allocate memory |
---|
236 |
freebob_connection_info_t* connection_info = |
---|
237 |
malloc( sizeof(freebob_connection_info_t) ); |
---|
238 |
if ( !connection_info ) { |
---|
239 |
fprintf( stderr, "Could not allocate memory for connection_info" ); |
---|
240 |
return 0; |
---|
241 |
} |
---|
242 |
|
---|
243 |
// count number of child streams |
---|
244 |
connection_info->nb_connections = 0; |
---|
245 |
xmlNodePtr cur = node->xmlChildrenNode; |
---|
246 |
while ( cur ) { |
---|
247 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Connection" ) ) { |
---|
248 |
connection_info->nb_connections = |
---|
249 |
connection_info->nb_connections + 1; |
---|
250 |
debugPrint( "nb_connections: %d\n", |
---|
251 |
connection_info->nb_connections ); |
---|
252 |
} |
---|
253 |
|
---|
254 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Direction" ) ) { |
---|
255 |
xmlChar* key = |
---|
256 |
xmlNodeListGetString( doc, cur->xmlChildrenNode, 1 ); |
---|
257 |
debugPrint( "\tdirection: %s\n", key ); |
---|
258 |
connection_info->direction = strtol( (const char*) key, |
---|
259 |
(char**) 0, 10); |
---|
260 |
xmlFree( key ); |
---|
261 |
} |
---|
262 |
cur = cur->next; |
---|
263 |
} |
---|
264 |
|
---|
265 |
debugPrint( "ConnectionInfo contains %d connection_specs\n", |
---|
266 |
connection_info->nb_connections ); |
---|
267 |
|
---|
268 |
if ( connection_info->nb_connections ) { |
---|
269 |
// allocate memory for the connection_spec pointer array |
---|
270 |
connection_info->connections = |
---|
271 |
(freebob_connection_spec_t**) |
---|
272 |
calloc( connection_info->nb_connections, |
---|
273 |
sizeof(freebob_connection_spec_t*) ); |
---|
274 |
|
---|
275 |
if ( !connection_info->connections ) { |
---|
276 |
fprintf( stderr, |
---|
277 |
"Could not allocate memory for connection specs" ); |
---|
278 |
free( connection_info ); |
---|
279 |
return 0; |
---|
280 |
} |
---|
281 |
|
---|
282 |
// parse the child stream_specs |
---|
283 |
cur = node->xmlChildrenNode; |
---|
284 |
int i = 0; |
---|
285 |
while ( cur ) { |
---|
286 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Connection" ) ) { |
---|
287 |
connection_info->connections[i] = |
---|
288 |
freebob_xmlparse_connection( doc, cur ); |
---|
289 |
|
---|
290 |
if ( !connection_info->connections[i] ) { |
---|
291 |
// invalid XML or memory problem, clean up. |
---|
292 |
while ( --i ) { |
---|
293 |
freebob_free_connection_spec( connection_info->connections[i] ); |
---|
294 |
} |
---|
295 |
free( connection_info->connections ); |
---|
296 |
connection_info->connections = 0; |
---|
297 |
free( connection_info ); |
---|
298 |
return 0; |
---|
299 |
} |
---|
300 |
i++; |
---|
301 |
} |
---|
302 |
cur = cur->next; |
---|
303 |
} |
---|
304 |
} |
---|
305 |
|
---|
306 |
return connection_info; |
---|
307 |
} |
---|
308 |
|
---|
309 |
freebob_supported_stream_format_info_t* |
---|
310 |
freebob_xmlparse_supported_stream_format( xmlDocPtr doc, xmlNodePtr node ) |
---|
311 |
{ |
---|
312 |
assert( node ); |
---|
313 |
|
---|
314 |
// allocate memory |
---|
315 |
freebob_supported_stream_format_info_t* stream_info = |
---|
316 |
malloc( sizeof(freebob_supported_stream_format_info_t) ); |
---|
317 |
if ( !stream_info ) { |
---|
318 |
fprintf( stderr, "Could not allocate memory for stream_info" ); |
---|
319 |
return 0; |
---|
320 |
} |
---|
321 |
|
---|
322 |
// count number of child streams |
---|
323 |
stream_info->nb_formats = 0; |
---|
324 |
xmlNodePtr cur = node->xmlChildrenNode; |
---|
325 |
while ( cur ) { |
---|
326 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Format" ) ) { |
---|
327 |
stream_info->nb_formats += 1; |
---|
328 |
debugPrint( "\tnb_formats: %d\n", |
---|
329 |
stream_info->nb_formats ); |
---|
330 |
} |
---|
331 |
|
---|
332 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Direction" ) ) { |
---|
333 |
xmlChar* key = |
---|
334 |
xmlNodeListGetString( doc, cur->xmlChildrenNode, 1 ); |
---|
335 |
debugPrint( "\tdirection: %s\n", key ); |
---|
336 |
stream_info->direction = strtol( (const char*) key, |
---|
337 |
(char**) 0, 10); |
---|
338 |
xmlFree( key ); |
---|
339 |
} |
---|
340 |
cur = cur->next; |
---|
341 |
} |
---|
342 |
|
---|
343 |
debugPrint( "StreamFormatInfo contains %d stream_format_specs\n", |
---|
344 |
stream_info->nb_formats ); |
---|
345 |
|
---|
346 |
if ( stream_info->nb_formats ) { |
---|
347 |
// allocate memory for the connection_spec pointer array |
---|
348 |
stream_info->formats = |
---|
349 |
(freebob_supported_stream_format_spec_t**) |
---|
350 |
calloc( stream_info->nb_formats, |
---|
351 |
sizeof(freebob_supported_stream_format_spec_t*) ); |
---|
352 |
|
---|
353 |
if ( !stream_info->formats ) { |
---|
354 |
fprintf( stderr, |
---|
355 |
"Could not allocate memory for stream format specs" ); |
---|
356 |
free( stream_info ); |
---|
357 |
return 0; |
---|
358 |
} |
---|
359 |
|
---|
360 |
// parse the child stream_specs |
---|
361 |
cur = node->xmlChildrenNode; |
---|
362 |
int i = 0; |
---|
363 |
while ( cur ) { |
---|
364 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Format" ) ) { |
---|
365 |
stream_info->formats[i] = |
---|
366 |
freebob_xmlparse_supported_stream_format_node( doc, cur ); |
---|
367 |
|
---|
368 |
if ( !stream_info->formats[i] ) { |
---|
369 |
// invalid XML or memory problem, clean up. |
---|
370 |
while ( --i ) { |
---|
371 |
freebob_free_supported_stream_format_spec( stream_info->formats[i] ); |
---|
372 |
} |
---|
373 |
free( stream_info->formats ); |
---|
374 |
stream_info->formats = 0; |
---|
375 |
free( stream_info ); |
---|
376 |
return 0; |
---|
377 |
} |
---|
378 |
i++; |
---|
379 |
} |
---|
380 |
cur = cur->next; |
---|
381 |
} |
---|
382 |
} |
---|
383 |
|
---|
384 |
return stream_info; |
---|
385 |
} |
---|
386 |
|
---|
387 |
|
---|
388 |
xmlNodePtr |
---|
389 |
freebob_xmlparse_get_connectionset_node( xmlDocPtr doc, |
---|
390 |
xmlNodePtr cur, |
---|
391 |
int direction ) |
---|
392 |
{ |
---|
393 |
while ( cur ) { |
---|
394 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "ConnectionSet" ) ) { |
---|
395 |
xmlNodePtr cur2 = cur->xmlChildrenNode; |
---|
396 |
while ( cur2 ) { |
---|
397 |
if ( !xmlStrcmp( cur2->name, (const xmlChar*) "Direction" ) ) { |
---|
398 |
xmlChar* key = xmlNodeListGetString( doc, |
---|
399 |
cur2->xmlChildrenNode, |
---|
400 |
1 ); |
---|
401 |
int tmp_direction = strtol( (const char*) key, |
---|
402 |
(char**) 0, 10 ); |
---|
403 |
xmlFree( key ); |
---|
404 |
if( tmp_direction == direction) { |
---|
405 |
return cur; |
---|
406 |
} |
---|
407 |
} |
---|
408 |
cur2 = cur2->next; |
---|
409 |
} |
---|
410 |
} |
---|
411 |
cur = cur->next; |
---|
412 |
} |
---|
413 |
return 0; |
---|
414 |
} |
---|
415 |
|
---|
416 |
xmlNodePtr |
---|
417 |
freebob_xmlparse_get_supported_stream_format_node( xmlDocPtr doc, |
---|
418 |
xmlNodePtr cur, |
---|
419 |
int direction ) |
---|
420 |
{ |
---|
421 |
while ( cur ) { |
---|
422 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "StreamFormats" ) ) { |
---|
423 |
xmlNodePtr cur2 = cur->xmlChildrenNode; |
---|
424 |
while ( cur2 ) { |
---|
425 |
if ( !xmlStrcmp( cur2->name, (const xmlChar*) "Direction" ) ) { |
---|
426 |
xmlChar* key = xmlNodeListGetString( doc, |
---|
427 |
cur2->xmlChildrenNode, |
---|
428 |
1 ); |
---|
429 |
int tmp_direction = strtol( (const char*) key, |
---|
430 |
(char**) 0, 10 ); |
---|
431 |
xmlFree( key ); |
---|
432 |
if( tmp_direction == direction) { |
---|
433 |
return cur; |
---|
434 |
} |
---|
435 |
} |
---|
436 |
cur2 = cur2->next; |
---|
437 |
} |
---|
438 |
} |
---|
439 |
cur = cur->next; |
---|
440 |
} |
---|
441 |
return 0; |
---|
442 |
} |
---|
443 |
|
---|
444 |
xmlNodePtr |
---|
445 |
freebob_xmlparse_get_connection_set_by_node_id( xmlDocPtr doc, |
---|
446 |
xmlNodePtr cur, |
---|
447 |
int nodeId ) |
---|
448 |
{ |
---|
449 |
while ( cur ) { |
---|
450 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Device" ) ) { |
---|
451 |
xmlNodePtr cur2 = cur->xmlChildrenNode; |
---|
452 |
while ( cur2 ) { |
---|
453 |
if ( !xmlStrcmp( cur2->name, |
---|
454 |
(const xmlChar*) "NodeId" ) ) |
---|
455 |
{ |
---|
456 |
xmlChar* key = xmlNodeListGetString( doc, |
---|
457 |
cur2->xmlChildrenNode, |
---|
458 |
1 ); |
---|
459 |
int tmp_node_id = strtol( (const char*) key, |
---|
460 |
(char**) 0, 10 ); |
---|
461 |
xmlFree( key ); |
---|
462 |
|
---|
463 |
if ( tmp_node_id == nodeId ) { |
---|
464 |
xmlNodePtr cur3 = cur->xmlChildrenNode; |
---|
465 |
while ( cur3 ) { |
---|
466 |
if ( !xmlStrcmp( cur3->name, |
---|
467 |
(const xmlChar*) "ConnectionSet" ) ) |
---|
468 |
{ |
---|
469 |
return cur3; |
---|
470 |
} |
---|
471 |
cur3 = cur3->next; |
---|
472 |
} |
---|
473 |
} |
---|
474 |
} |
---|
475 |
cur2 = cur2->next; |
---|
476 |
} |
---|
477 |
} |
---|
478 |
cur = cur->next; |
---|
479 |
} |
---|
480 |
|
---|
481 |
return 0; |
---|
482 |
} |
---|
483 |
|
---|
484 |
xmlNodePtr |
---|
485 |
freebob_xmlparse_get_supported_stream_format_set_by_node_id( xmlDocPtr doc, |
---|
486 |
xmlNodePtr cur, |
---|
487 |
int nodeId ) |
---|
488 |
{ |
---|
489 |
while ( cur ) { |
---|
490 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Device" ) ) { |
---|
491 |
xmlNodePtr cur2 = cur->xmlChildrenNode; |
---|
492 |
while ( cur2 ) { |
---|
493 |
if ( !xmlStrcmp( cur2->name, |
---|
494 |
(const xmlChar*) "NodeId" ) ) |
---|
495 |
{ |
---|
496 |
xmlChar* key = xmlNodeListGetString( doc, |
---|
497 |
cur2->xmlChildrenNode, |
---|
498 |
1 ); |
---|
499 |
int tmp_node_id = strtol( (const char*) key, |
---|
500 |
(char**) 0, 10 ); |
---|
501 |
xmlFree( key ); |
---|
502 |
|
---|
503 |
if ( tmp_node_id == nodeId ) { |
---|
504 |
xmlNodePtr cur3 = cur->xmlChildrenNode; |
---|
505 |
while ( cur3 ) { |
---|
506 |
if ( !xmlStrcmp( cur3->name, |
---|
507 |
(const xmlChar*) "StreamFormats" ) ) |
---|
508 |
{ |
---|
509 |
return cur3; |
---|
510 |
} |
---|
511 |
cur3 = cur3->next; |
---|
512 |
} |
---|
513 |
} |
---|
514 |
} |
---|
515 |
cur2 = cur2->next; |
---|
516 |
} |
---|
517 |
} |
---|
518 |
cur = cur->next; |
---|
519 |
} |
---|
520 |
|
---|
521 |
return 0; |
---|
522 |
} |
---|
523 |
|
---|
524 |
xmlNodePtr |
---|
525 |
freebob_xmlparse_get_connection_set_by_device( xmlDocPtr doc, |
---|
526 |
xmlNodePtr cur, |
---|
527 |
int device ) |
---|
528 |
{ |
---|
529 |
int count=0; |
---|
530 |
while ( count < device ) { |
---|
531 |
if(!cur) { |
---|
532 |
return NULL; |
---|
533 |
} |
---|
534 |
|
---|
535 |
if(!xmlStrcmp( cur->name, (const xmlChar*) "Device" )) { |
---|
536 |
count++; |
---|
537 |
} |
---|
538 |
cur=cur->next; |
---|
539 |
} |
---|
540 |
|
---|
541 |
if ( !xmlStrcmp( cur->name, (const xmlChar*) "Device" ) ) { |
---|
542 |
xmlNodePtr cur3 = cur->xmlChildrenNode; |
---|
543 |
while ( cur3 ) { |
---|
544 |
if ( !xmlStrcmp( cur3->name, |
---|
545 |
(const xmlChar*) "ConnectionSet" ) ) |
---|
546 |
{ |
---|
547 |
return cur3; |
---|
548 |
} |
---|
549 |
cur3 = cur3->next; |
---|
550 |
} |
---|
551 |
} else { |
---|
552 |
return NULL; |
---|
553 |
} |
---|
554 |
|
---|
555 |
return NULL; |
---|
556 |
} |
---|
557 |
|
---|
558 |
// a side effect is that both old connection info's are freed, unless NULL is returned! |
---|
559 |
freebob_connection_info_t* |
---|
560 |
freebob_xmlparse_append_connectionset(freebob_connection_info_t* connection_info, freebob_connection_info_t* cinfo) |
---|
561 |
{ |
---|
562 |
if(connection_info==NULL) return cinfo; |
---|
563 |
if(cinfo==NULL) return connection_info; |
---|
564 |
|
---|
565 |
if (connection_info->direction != cinfo->direction) { |
---|
566 |
// direction mismatch |
---|
567 |
return NULL; |
---|
568 |
} |
---|
569 |
|
---|
570 |
freebob_connection_info_t *tmpci=calloc(1,sizeof(freebob_connection_info_t)); |
---|
571 |
if (!tmpci) { |
---|
572 |
return NULL; |
---|
573 |
} |
---|
574 |
|
---|
575 |
tmpci->nb_connections=connection_info->nb_connections+cinfo->nb_connections; |
---|
576 |
tmpci->connections = calloc( tmpci->nb_connections, sizeof(freebob_connection_spec_t*)); |
---|
577 |
|
---|
578 |
int i=0; |
---|
579 |
int c=0; |
---|
580 |
for(i=0;i<connection_info->nb_connections;i++) { |
---|
581 |
tmpci->connections[c]=connection_info->connections[i]; // these are pointers |
---|
582 |
c++; |
---|
583 |
} |
---|
584 |
for(i=0;i<cinfo->nb_connections;i++) { |
---|
585 |
tmpci->connections[c]=cinfo->connections[i]; // these are pointers |
---|
586 |
c++; |
---|
587 |
} |
---|
588 |
|
---|
589 |
free(connection_info->connections); |
---|
590 |
free(cinfo->connections); |
---|
591 |
|
---|
592 |
free(connection_info); |
---|
593 |
free(cinfo); |
---|
594 |
|
---|
595 |
connection_info=NULL; |
---|
596 |
cinfo=NULL; |
---|
597 |
|
---|
598 |
return tmpci; |
---|
599 |
} |
---|
600 |
|
---|
601 |
// a side effect is that both old connection info's are freed, unless NULL is returned! |
---|
602 |
freebob_supported_stream_format_info_t* |
---|
603 |
freebob_xmlparse_append_stream_format(freebob_supported_stream_format_info_t* stream_info, |
---|
604 |
freebob_supported_stream_format_info_t* cinfo) |
---|
605 |
{ |
---|
606 |
if ( !stream_info ) { |
---|
607 |
return cinfo; |
---|
608 |
} |
---|
609 |
if ( !cinfo ) { |
---|
610 |
return stream_info; |
---|
611 |
} |
---|
612 |
|
---|
613 |
if ( stream_info->direction != cinfo->direction ) { |
---|
614 |
return 0; |
---|
615 |
} |
---|
616 |
|
---|
617 |
freebob_supported_stream_format_info_t* tmpci = |
---|
618 |
calloc( 1, sizeof( freebob_supported_stream_format_info_t ) ); |
---|
619 |
if ( !tmpci ) { |
---|
620 |
return 0; |
---|
621 |
} |
---|
622 |
|
---|
623 |
tmpci->nb_formats = |
---|
624 |
stream_info->nb_formats + cinfo->nb_formats; |
---|
625 |
tmpci->formats = |
---|
626 |
calloc( tmpci->nb_formats, |
---|
627 |
sizeof( freebob_supported_stream_format_spec_t* ) ); |
---|
628 |
|
---|
629 |
int c = 0; |
---|
630 |
for( int i = 0; i < stream_info->nb_formats; i++, c++ ) { |
---|
631 |
tmpci->formats[c] = stream_info->formats[i]; |
---|
632 |
} |
---|
633 |
for ( int i = 0; i < cinfo->nb_formats; i++, c++ ) { |
---|
634 |
tmpci->formats[c] = cinfo->formats[i]; |
---|
635 |
} |
---|
636 |
|
---|
637 |
free( stream_info->formats ); |
---|
638 |
free( cinfo->formats ); |
---|
639 |
|
---|
640 |
free( stream_info ); |
---|
641 |
free( cinfo ); |
---|
642 |
|
---|
643 |
stream_info = 0; |
---|
644 |
cinfo = 0; |
---|
645 |
|
---|
646 |
return tmpci; |
---|
647 |
} |
---|
648 |
|
---|
649 |
int |
---|
650 |
freebob_xmlparse_get_nb_devices( xmlDocPtr doc, |
---|
651 |
xmlNodePtr cur) |
---|
652 |
{ |
---|
653 |
int count=0; |
---|
654 |
while (cur != NULL) { |
---|
655 |
if ((!xmlStrcmp(cur->name, (const xmlChar *)"Device"))) { |
---|
656 |
count++; |
---|
657 |
} |
---|
658 |
cur = cur->next; |
---|
659 |
} |
---|
660 |
return count; |
---|
661 |
} |
---|
662 |
|
---|
663 |
freebob_connection_info_t* |
---|
664 |
freebob_xmlparse_get_connection_info( xmlDocPtr doc, |
---|
665 |
int node_id, |
---|
666 |
int direction ) |
---|
667 |
{ |
---|
668 |
xmlNodePtr base = xmlDocGetRootElement(doc); |
---|
669 |
xmlNodePtr cur; |
---|
670 |
|
---|
671 |
if ( !base ) { |
---|
672 |
fprintf( stderr, "empty document\n"); |
---|
673 |
return 0; |
---|
674 |
} |
---|
675 |
|
---|
676 |
if ( xmlStrcmp( base->name, (const xmlChar*) "FreeBoBConnectionInfo") ) { |
---|
677 |
fprintf( stderr, |
---|
678 |
"document of the wrong type, root node " |
---|
679 |
"!= FreeBoBConnectionInfo\n" ); |
---|
680 |
return 0; |
---|
681 |
} |
---|
682 |
|
---|
683 |
base = base->xmlChildrenNode; |
---|
684 |
if ( !base ) { |
---|
685 |
fprintf( stderr, "Root node has no children!\n" ); |
---|
686 |
return 0; |
---|
687 |
} |
---|
688 |
|
---|
689 |
if( node_id > -1) { |
---|
690 |
cur = freebob_xmlparse_get_connection_set_by_node_id( doc, base, node_id ); |
---|
691 |
if ( !cur ) { |
---|
692 |
fprintf( stderr, |
---|
693 |
"Could not get description for node id %d\n", node_id ); |
---|
694 |
return 0; |
---|
695 |
} |
---|
696 |
cur = freebob_xmlparse_get_connectionset_node( doc, cur, direction ); |
---|
697 |
if ( !cur ) { |
---|
698 |
fprintf( stderr, |
---|
699 |
"Could not get a connection set for direction %d\n", |
---|
700 |
direction ); |
---|
701 |
return 0; |
---|
702 |
} |
---|
703 |
|
---|
704 |
freebob_connection_info_t* connection_info = |
---|
705 |
freebob_xmlparse_connectionset( doc, cur ); |
---|
706 |
|
---|
707 |
return connection_info; |
---|
708 |
} else { |
---|
709 |
freebob_connection_info_t* connection_info=NULL; |
---|
710 |
|
---|
711 |
int device_nr=0; |
---|
712 |
|
---|
713 |
int nb_devices=freebob_xmlparse_get_nb_devices(doc, base); |
---|
714 |
|
---|
715 |
/* fprintf( stderr, |
---|
716 |
"Nb devices %d\n", |
---|
717 |
nb_devices );*/ |
---|
718 |
|
---|
719 |
for(device_nr=0;device_nr<nb_devices;device_nr++) { |
---|
720 |
|
---|
721 |
cur = freebob_xmlparse_get_connection_set_by_device( doc, base, device_nr ); |
---|
722 |
if ( !cur ) { |
---|
723 |
fprintf( stderr, |
---|
724 |
"Could not get description for device %d\n", device_nr ); |
---|
725 |
return 0; |
---|
726 |
} |
---|
727 |
|
---|
728 |
cur = freebob_xmlparse_get_connectionset_node( doc, cur, direction ); |
---|
729 |
if ( !cur ) { |
---|
730 |
fprintf( stderr, |
---|
731 |
"Could not get a connection set for direction %d\n", |
---|
732 |
direction ); |
---|
733 |
return 0; |
---|
734 |
} |
---|
735 |
|
---|
736 |
freebob_connection_info_t* cinfo = freebob_xmlparse_connectionset( doc, cur ); |
---|
737 |
connection_info=freebob_xmlparse_append_connectionset(connection_info,cinfo); |
---|
738 |
} |
---|
739 |
|
---|
740 |
return connection_info; |
---|
741 |
} |
---|
742 |
} |
---|
743 |
|
---|
744 |
freebob_supported_stream_format_info_t* |
---|
745 |
freebob_xmlparse_get_stream_formats( xmlDocPtr doc, |
---|
746 |
int node_id, |
---|
747 |
int direction ) |
---|
748 |
{ |
---|
749 |
xmlNodePtr base = xmlDocGetRootElement(doc); |
---|
750 |
xmlNodePtr cur; |
---|
751 |
|
---|
752 |
if ( !base ) { |
---|
753 |
fprintf( stderr, "empty document\n"); |
---|
754 |
return 0; |
---|
755 |
} |
---|
756 |
|
---|
757 |
if ( xmlStrcmp( base->name, (const xmlChar*) "FreeBoBConnectionInfo") ) { |
---|
758 |
fprintf( stderr, |
---|
759 |
"document of the wrong type, root node " |
---|
760 |
"!= FreeBoBConnectionInfo\n" ); |
---|
761 |
return 0; |
---|
762 |
} |
---|
763 |
|
---|
764 |
base = base->xmlChildrenNode; |
---|
765 |
if ( !base ) { |
---|
766 |
fprintf( stderr, "Root node has no children!\n" ); |
---|
767 |
return 0; |
---|
768 |
} |
---|
769 |
|
---|
770 |
cur = freebob_xmlparse_get_supported_stream_format_set_by_node_id( doc, base, node_id ); |
---|
771 |
if ( !cur ) { |
---|
772 |
fprintf( stderr, |
---|
773 |
"Could not get description for node id %d\n", node_id ); |
---|
774 |
return 0; |
---|
775 |
} |
---|
776 |
cur = freebob_xmlparse_get_supported_stream_format_node( doc, cur, direction ); |
---|
777 |
if ( !cur ) { |
---|
778 |
fprintf( stderr, |
---|
779 |
"Could not get a connection set for direction %d\n", |
---|
780 |
direction ); |
---|
781 |
return 0; |
---|
782 |
} |
---|
783 |
|
---|
784 |
freebob_supported_stream_format_info_t* stream_info = |
---|
785 |
freebob_xmlparse_supported_stream_format( doc, cur ); |
---|
786 |
|
---|
787 |
return stream_info; return 0; |
---|
788 |
} |
---|