1: /* $Id: upnpreplyparse.c,v 1.1.1.1 2012/02/21 23:16:02 misho Exp $ */
2: /* MiniUPnP project
3: * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4: * (c) 2006 Thomas Bernard
5: * This software is subject to the conditions detailed
6: * in the LICENCE file provided within the distribution */
7:
8: #include <stdlib.h>
9: #include <string.h>
10: #include <stdio.h>
11:
12: #include "upnpreplyparse.h"
13: #include "minixml.h"
14:
15: static void
16: NameValueParserStartElt(void * d, const char * name, int l)
17: {
18: struct NameValueParserData * data = (struct NameValueParserData *)d;
19: if(l>63)
20: l = 63;
21: memcpy(data->curelt, name, l);
22: data->curelt[l] = '\0';
23: }
24:
25: static void
26: NameValueParserGetData(void * d, const char * datas, int l)
27: {
28: struct NameValueParserData * data = (struct NameValueParserData *)d;
29: struct NameValue * nv;
30: nv = malloc(sizeof(struct NameValue));
31: if(l>63)
32: l = 63;
33: strncpy(nv->name, data->curelt, 64);
34: nv->name[63] = '\0';
35: memcpy(nv->value, datas, l);
36: nv->value[l] = '\0';
37: LIST_INSERT_HEAD( &(data->head), nv, entries);
38: }
39:
40: void
41: ParseNameValue(const char * buffer, int bufsize,
42: struct NameValueParserData * data)
43: {
44: struct xmlparser parser;
45: LIST_INIT(&(data->head));
46: /* init xmlparser object */
47: parser.xmlstart = buffer;
48: parser.xmlsize = bufsize;
49: parser.data = data;
50: parser.starteltfunc = NameValueParserStartElt;
51: parser.endeltfunc = 0;
52: parser.datafunc = NameValueParserGetData;
53: parser.attfunc = 0;
54: parsexml(&parser);
55: }
56:
57: void
58: ClearNameValueList(struct NameValueParserData * pdata)
59: {
60: struct NameValue * nv;
61: while((nv = pdata->head.lh_first) != NULL)
62: {
63: LIST_REMOVE(nv, entries);
64: free(nv);
65: }
66: }
67:
68: char *
69: GetValueFromNameValueList(struct NameValueParserData * pdata,
70: const char * Name)
71: {
72: struct NameValue * nv;
73: char * p = NULL;
74: for(nv = pdata->head.lh_first;
75: (nv != NULL) && (p == NULL);
76: nv = nv->entries.le_next)
77: {
78: if(strcmp(nv->name, Name) == 0)
79: p = nv->value;
80: }
81: return p;
82: }
83:
84: #if 0
85: /* useless now that minixml ignores namespaces by itself */
86: char *
87: GetValueFromNameValueListIgnoreNS(struct NameValueParserData * pdata,
88: const char * Name)
89: {
90: struct NameValue * nv;
91: char * p = NULL;
92: char * pname;
93: for(nv = pdata->head.lh_first;
94: (nv != NULL) && (p == NULL);
95: nv = nv->entries.le_next)
96: {
97: pname = strrchr(nv->name, ':');
98: if(pname)
99: pname++;
100: else
101: pname = nv->name;
102: if(strcmp(pname, Name)==0)
103: p = nv->value;
104: }
105: return p;
106: }
107: #endif
108:
109: /* debug all-in-one function
110: * do parsing then display to stdout */
111: #ifdef DEBUG
112: void
113: DisplayNameValueList(char * buffer, int bufsize)
114: {
115: struct NameValueParserData pdata;
116: struct NameValue * nv;
117: ParseNameValue(buffer, bufsize, &pdata);
118: for(nv = pdata.head.lh_first;
119: nv != NULL;
120: nv = nv->entries.le_next)
121: {
122: printf("%s = %s\n", nv->name, nv->value);
123: }
124: ClearNameValueList(&pdata);
125: }
126: #endif
127:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>