Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <expat.h>
#if defined(__amigaos__) && defined(__USE_INLINE__)
#include <proto/expat.h>
#endif
#ifdef XML_LARGE_SIZE
#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
#define XML_FMT_INT_MOD "I64"
#else
#define XML_FMT_INT_MOD "ll"
#endif
#else
#define XML_FMT_INT_MOD "l"
#endif
#define BUFFSIZE 8192
char Buff[BUFFSIZE];
int Depth;
typedef enum {
S_NONE,
S_IFNAME,
S_PROTO,
S_IPADDR,
S_NETMASK,
} state_et;
typedef struct {
state_et state;
char interface[100];
char ifname[100];
char proto[100];
char ipaddr[100];
char netmask[100];
} info_st;
static void XMLCALL
chardata(void *userData, const XML_Char *s, int len)
{
info_st *info = userData;
switch ( info->state ) {
case S_IFNAME:
strncpy(info->ifname,s,len);
info->ifname[len] = '\0';
break;
case S_PROTO:
strncpy(info->proto,s,len);
info->proto[len] = '\0';
break;
case S_IPADDR:
strncpy(info->ipaddr,s,len);
info->ipaddr[len] = '\0';
break;
case S_NETMASK:
strncpy(info->netmask,s,len);
info->netmask[len] = '\0';
break;
}
info->state = S_NONE;
// printf(">-%.*s-<",len,s);
}
static void XMLCALL
start(void *data, const char *el, const char **attr)
{
info_st *info = data;
int i;
{
char temp[100];
if ( sscanf( el, "config_interface_%s", temp) == 1 ) {
strcpy(info->interface,temp);
}
if ( strcmp(el,"ifname")==0) info->state = S_IFNAME;
if ( strcmp(el,"proto")==0) info->state = S_PROTO;
if ( strcmp(el,"ipaddr")==0) info->state = S_IPADDR;
if ( strcmp(el,"netmask")==0) info->state = S_NETMASK;
}
for (i = 0; i < Depth; i++)
printf(" ", data);
printf("%s", el);
for (i = 0; attr[i]; i += 2) {
printf(" %s='%s'", attr[i], attr[i + 1]);
}
printf("\n");
Depth++;
}
static void XMLCALL
end(void *data, const char *el)
{
info_st *info = data;
{
char temp[100];
if ( sscanf( el, "config_interface_%s", temp) == 1 ) {
printf("DATA=%s %s %s %s %s\n",
info->interface,
info->ifname,
info->proto,
info->ipaddr,
info->netmask);
}
}
Depth--;
}
int
main(int argc, char *argv[])
{
info_st info = { 0 };
XML_Parser p = XML_ParserCreate(NULL);
if (! p) {
fprintf(stderr, "Couldn't allocate memory for parser\n");
exit(-1);
}
XML_SetElementHandler(p, start, end);
XML_SetCharacterDataHandler(p,chardata);
XML_SetUserData(p, &info);
for (;;) {
int done;
int len;
len = (int)fread(Buff, 1, BUFFSIZE, stdin);
if (ferror(stdin)) {
fprintf(stderr, "Read error\n");
exit(-1);
}
done = feof(stdin);
if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) {
fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n",
XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p)));
exit(-1);
}
if (done)
break;
}
XML_ParserFree(p);
return 0;
}
The extra information printed looks like this