File: loadxmlhelper.c

package info (click to toggle)
dancer-xml 0.8.2.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 1,644 kB
  • ctags: 109
  • sloc: sh: 8,889; ansic: 1,551; xml: 868; makefile: 115
file content (159 lines) | stat: -rw-r--r-- 3,851 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 *  dancer-XML parser
 *  Copyright (C) 2000,2002 Junichi Uekawa
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/*
 * Helper functions for loaded XML file ... $Id: loadxmlhelper.c,v 1.4 2006/01/24 09:58:32 dancer Exp $
 * 8 Sep MM Junichi Uekawa ... created
 * 11 Sep MM Junichi Uekawa adding get_element_byname and simplepath
 * copyright 2000 Junichi Uekawa
 */

/**
   @file loadxmlhelper.c
   @brief Helper functions for processing the loaded XML file
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dancer-xml.h"

dxml_element * 
dxml_next_notPCDATA(dxml_element * e)
{
  if (!e) return NULL ;
  if (e->element_type == element_type_pcdata)
    return dxml_next_notPCDATA(e->next);
  return e;
}

dxml_element * 
dxml_get_element_byname(dxml_element * e, const char * name)
{
  while (e)
    {
      if ((e->element_type == element_type_element)&&
	  (!strcmp(e->element_name, name)))
	return e;      
      e=e->next;
    }
  return NULL;  
}

static dxml_element * 
dxml_get_element_bysimplepath_internal(dxml_element * e, char*path)
{
  /*
   * search for /asds/asdg/adhahaf/ae  kind of path
   * should never end with a slash.
   */
  char * slash = strchr(path, '/');

  if (slash == path)
    {				/* skip the extra / */
      return dxml_get_element_bysimplepath(e, path + 1 );
    }
  else if (slash)
    {
      *slash=0;
      if (!(e = dxml_get_element_byname(e,path)))
	return NULL;
      if (!(e = e -> child))
	return NULL;      
      
      return dxml_get_element_bysimplepath(e, slash+1);
    }
  else
    return dxml_get_element_byname(e, path);
}

dxml_element * 
dxml_get_element_bysimplepath(dxml_element * e, const char*path)
{
  char * buf=strdup(path);
  dxml_element * e2 ;
  
  if (!buf) return NULL;
  e2 = dxml_get_element_bysimplepath_internal(e, buf);
  free (buf);
  return e2;
}

char * 
dxml_get_PCDATA_bysimplepath(dxml_element * e, const char * path)
{
  /*
   * get PCDATA from simplepath
   */
  dxml_element * e2;
  char * buf = strdup (path);
  if (!buf) return NULL;
  e2 = dxml_get_element_bysimplepath_internal(e, buf);
  free(buf);  
  if (!e2) return NULL;  
  if (!(e2=e2->child)) return NULL;
  while (e2->element_type!= element_type_pcdata && e2)
    {
      e2=e2->next;      
    }  
  if (!e2) return NULL;
  return e2->element_name;  
}

static void
space(int i)
{
  while (i--)
    printf(" ");  
}

static void
recurse_attribute(dxml_attribute * a )
{
  printf(" %s=\"%s\"", a->attribute_name, a->attribute_data);
  if (a->next) 
    recurse_attribute(a->next);  
}

static void
recurse_element(dxml_element * e, int level)
{
  if (e->element_type == element_type_element)
    {
      space(level);
      printf("<%s", e->element_name);
      if (e->element_attribute) 
	recurse_attribute(e->element_attribute);      
      printf(">\n");      
      if (e->child) 
	recurse_element (e->child, level+1);
      space(level);printf("</%s>\n", e->element_name);      
    }
  else
    {
      space(level);      
      printf("%s\n", e->element_name);
    }
  if (e->next) recurse_element(e->next, level);  
}

void
dxml_dump_element (dxml_element * e)
{
  recurse_element(e,0);
}