1 /* $Id: upnpreplyparse.c,v 1.19 2015/07/15 10:29:11 nanard Exp $ */
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2006-2015 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
;
22 memcpy(data
->curelt
, name
, l
);
23 data
->curelt
[l
] = '\0';
29 NameValueParserEndElt(void * d
, const char * name
, int l
)
31 struct NameValueParserData
* data
= (struct NameValueParserData
*)d
;
32 struct NameValue
* nv
;
37 if(strcmp(data
->curelt
, "NewPortListing") != 0)
40 /* standard case. Limited to n chars strings */
42 nv
= malloc(sizeof(struct NameValue
));
47 fprintf(stderr
, "%s: error allocating memory",
48 "NameValueParserEndElt");
52 if(l
>=(int)sizeof(nv
->value
))
53 l
= sizeof(nv
->value
) - 1;
54 strncpy(nv
->name
, data
->curelt
, 64);
56 if(data
->cdata
!= NULL
)
58 memcpy(nv
->value
, data
->cdata
, l
);
65 nv
->l_next
= data
->l_head
; /* insert in list */
74 NameValueParserGetData(void * d
, const char * datas
, int l
)
76 struct NameValueParserData
* data
= (struct NameValueParserData
*)d
;
77 if(strcmp(data
->curelt
, "NewPortListing") == 0)
79 /* specific case for NewPortListing which is a XML Document */
80 data
->portListing
= malloc(l
+ 1);
81 if(!data
->portListing
)
85 fprintf(stderr
, "%s: error allocating memory",
86 "NameValueParserGetData");
90 memcpy(data
->portListing
, datas
, l
);
91 data
->portListing
[l
] = '\0';
92 data
->portListingLength
= l
;
103 ParseNameValue(const char * buffer
, int bufsize
,
104 struct NameValueParserData
* data
)
106 struct xmlparser parser
;
108 data
->portListing
= NULL
;
109 data
->portListingLength
= 0;
110 /* init xmlparser object */
111 parser
.xmlstart
= buffer
;
112 parser
.xmlsize
= bufsize
;
114 parser
.starteltfunc
= NameValueParserStartElt
;
115 parser
.endeltfunc
= NameValueParserEndElt
;
116 parser
.datafunc
= NameValueParserGetData
;
122 ClearNameValueList(struct NameValueParserData
* pdata
)
124 struct NameValue
* nv
;
125 if(pdata
->portListing
)
127 free(pdata
->portListing
);
128 pdata
->portListing
= NULL
;
129 pdata
->portListingLength
= 0;
131 while((nv
= pdata
->l_head
) != NULL
)
133 pdata
->l_head
= nv
->l_next
;
139 GetValueFromNameValueList(struct NameValueParserData
* pdata
,
142 struct NameValue
* nv
;
144 for(nv
= pdata
->l_head
;
145 (nv
!= NULL
) && (p
== NULL
);
148 if(strcmp(nv
->name
, Name
) == 0)
155 /* useless now that minixml ignores namespaces by itself */
157 GetValueFromNameValueListIgnoreNS(struct NameValueParserData
* pdata
,
160 struct NameValue
* nv
;
163 for(nv
= pdata
->head
.lh_first
;
164 (nv
!= NULL
) && (p
== NULL
);
165 nv
= nv
->entries
.le_next
)
167 pname
= strrchr(nv
->name
, ':');
172 if(strcmp(pname
, Name
)==0)
179 /* debug all-in-one function
180 * do parsing then display to stdout */
183 DisplayNameValueList(char * buffer
, int bufsize
)
185 struct NameValueParserData pdata
;
186 struct NameValue
* nv
;
187 ParseNameValue(buffer
, bufsize
, &pdata
);
188 for(nv
= pdata
.l_head
;
192 printf("%s = %s\n", nv
->name
, nv
->value
);
194 ClearNameValueList(&pdata
);