1 /* $Id: upnpreplyparse.c,v 1.12 2012/03/05 19:42:48 nanard Exp $ */
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2006-2011 Thomas Bernard
5 * This software is subject to the conditions detailed
6 * in the LICENCE file provided within the distribution */
12 #include "upnpreplyparse.h"
16 NameValueParserStartElt(void * d
, const char * name
, int l
)
18 struct NameValueParserData
* data
= (struct NameValueParserData
*)d
;
21 memcpy(data
->curelt
, name
, l
);
22 data
->curelt
[l
] = '\0';
26 NameValueParserGetData(void * d
, const char * datas
, int l
)
28 struct NameValueParserData
* data
= (struct NameValueParserData
*)d
;
29 struct NameValue
* nv
;
30 if(strcmp(data
->curelt
, "NewPortListing") == 0)
32 /* specific case for NewPortListing which is a XML Document */
33 data
->portListing
= malloc(l
+ 1);
34 if(!data
->portListing
)
39 memcpy(data
->portListing
, datas
, l
);
40 data
->portListing
[l
] = '\0';
41 data
->portListingLength
= l
;
45 /* standard case. Limited to 63 chars strings */
46 nv
= malloc(sizeof(struct NameValue
));
49 strncpy(nv
->name
, data
->curelt
, 64);
51 memcpy(nv
->value
, datas
, l
);
53 LIST_INSERT_HEAD( &(data
->head
), nv
, entries
);
58 ParseNameValue(const char * buffer
, int bufsize
,
59 struct NameValueParserData
* data
)
61 struct xmlparser parser
;
62 LIST_INIT(&(data
->head
));
63 data
->portListing
= NULL
;
64 data
->portListingLength
= 0;
65 /* init xmlparser object */
66 parser
.xmlstart
= buffer
;
67 parser
.xmlsize
= bufsize
;
69 parser
.starteltfunc
= NameValueParserStartElt
;
70 parser
.endeltfunc
= 0;
71 parser
.datafunc
= NameValueParserGetData
;
77 ClearNameValueList(struct NameValueParserData
* pdata
)
79 struct NameValue
* nv
;
80 if(pdata
->portListing
)
82 free(pdata
->portListing
);
83 pdata
->portListing
= NULL
;
84 pdata
->portListingLength
= 0;
86 while((nv
= pdata
->head
.lh_first
) != NULL
)
88 LIST_REMOVE(nv
, entries
);
94 GetValueFromNameValueList(struct NameValueParserData
* pdata
,
97 struct NameValue
* nv
;
99 for(nv
= pdata
->head
.lh_first
;
100 (nv
!= NULL
) && (p
== NULL
);
101 nv
= nv
->entries
.le_next
)
103 if(strcmp(nv
->name
, Name
) == 0)
110 /* useless now that minixml ignores namespaces by itself */
112 GetValueFromNameValueListIgnoreNS(struct NameValueParserData
* pdata
,
115 struct NameValue
* nv
;
118 for(nv
= pdata
->head
.lh_first
;
119 (nv
!= NULL
) && (p
== NULL
);
120 nv
= nv
->entries
.le_next
)
122 pname
= strrchr(nv
->name
, ':');
127 if(strcmp(pname
, Name
)==0)
134 /* debug all-in-one function
135 * do parsing then display to stdout */
138 DisplayNameValueList(char * buffer
, int bufsize
)
140 struct NameValueParserData pdata
;
141 struct NameValue
* nv
;
142 ParseNameValue(buffer
, bufsize
, &pdata
);
143 for(nv
= pdata
.head
.lh_first
;
145 nv
= nv
->entries
.le_next
)
147 printf("%s = %s\n", nv
->name
, nv
->value
);
149 ClearNameValueList(&pdata
);