1 /* $Id: upnpreplyparse.c,v 1.6 2010/11/11 23:48:13 jmaggard Exp $ */
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
5 * Copyright (c) 2006, Thomas Bernard
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 #include "upnpreplyparse.h"
38 NameValueParserStartElt(void * d
, const char * name
, int l
)
40 struct NameValueParserData
* data
= (struct NameValueParserData
*)d
;
43 memcpy(data
->curelt
, name
, l
);
44 data
->curelt
[l
] = '\0';
46 /* store root element */
47 if(!data
->head
.lh_first
)
49 struct NameValue
* nv
;
50 nv
= malloc(sizeof(struct NameValue
));
51 strcpy(nv
->name
, "rootElement");
52 memcpy(nv
->value
, name
, l
);
54 LIST_INSERT_HEAD( &(data
->head
), nv
, entries
);
59 NameValueParserGetData(void * d
, const char * datas
, int l
)
61 struct NameValueParserData
* data
= (struct NameValueParserData
*)d
;
62 struct NameValue
* nv
;
63 nv
= malloc(sizeof(struct NameValue
));
66 strncpy(nv
->name
, data
->curelt
, 64);
68 memcpy(nv
->value
, datas
, l
);
70 LIST_INSERT_HEAD( &(data
->head
), nv
, entries
);
74 ParseNameValue(const char * buffer
, int bufsize
,
75 struct NameValueParserData
* data
)
77 struct xmlparser parser
;
78 LIST_INIT(&(data
->head
));
79 /* init xmlparser object */
80 parser
.xmlstart
= buffer
;
81 parser
.xmlsize
= bufsize
;
83 parser
.starteltfunc
= NameValueParserStartElt
;
84 parser
.endeltfunc
= 0;
85 parser
.datafunc
= NameValueParserGetData
;
91 ClearNameValueList(struct NameValueParserData
* pdata
)
93 struct NameValue
* nv
;
94 while((nv
= pdata
->head
.lh_first
) != NULL
)
96 LIST_REMOVE(nv
, entries
);
102 GetValueFromNameValueList(struct NameValueParserData
* pdata
,
105 struct NameValue
* nv
;
107 for(nv
= pdata
->head
.lh_first
;
108 (nv
!= NULL
) && (p
== NULL
);
109 nv
= nv
->entries
.le_next
)
111 if(strcmp(nv
->name
, Name
) == 0)
117 /* debug all-in-one function
118 * do parsing then display to stdout */
121 DisplayNameValueList(char * buffer
, int bufsize
)
123 struct NameValueParserData pdata
;
124 struct NameValue
* nv
;
125 ParseNameValue(buffer
, bufsize
, &pdata
);
126 for(nv
= pdata
.head
.lh_first
;
128 nv
= nv
->entries
.le_next
)
130 printf("%s = %s\n", nv
->name
, nv
->value
);
132 ClearNameValueList(&pdata
);