File: trparse_xml.c

package info (click to toggle)
pcb-rnd 3.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 29,624 kB
  • sloc: ansic: 197,571; yacc: 6,153; sh: 5,808; awk: 2,708; makefile: 2,139; lex: 1,107; python: 519; xml: 261; lisp: 169; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (154 lines) | stat: -rw-r--r-- 3,671 bytes parent folder | download | duplicates (4)
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
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 2017 Tibor 'Igor2' Palinkas
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Contact:
 *    Project page: http://repo.hu/projects/pcb-rnd
 *    lead developer: http://repo.hu/projects/pcb-rnd/contact.html
 *    mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
 *
 */

#include "config.h"

#include <stdlib.h>
#include <assert.h>
#include <libxml/tree.h>
#include <libxml/parser.h>

#include <librnd/core/error.h>
#include <librnd/core/safe_fs.h>

#include "trparse.h"
#include "trparse_xml.h"

static int eagle_xml_load(trparse_t *pst, const char *fn)
{
	xmlDoc *doc;
	xmlNode *root;
	FILE *f;
	char *efn;

	f = rnd_fopen_fn(NULL, fn, "r", &efn);
	if (f == NULL) {
		rnd_message(RND_MSG_ERROR, "can't open '%s'\n", fn);
		return -1;
	}
	fclose(f);

	doc = xmlReadFile(efn, NULL, 0);
	if (doc == NULL) {
		rnd_message(RND_MSG_ERROR, "xml parsing error on file %s (%s)\n", fn, efn);
		free(efn);
		return -1;
	}
	free(efn);

	root = xmlDocGetRootElement(doc);
	if (xmlStrcmp(root->name, (xmlChar *)"eagle") != 0) {
		rnd_message(RND_MSG_ERROR, "xml error: root is not <eagle>\n");
		xmlFreeDoc(doc);
		return -1;
	}

	pst->doc = doc;
	pst->root = root;

	return 0;
}

static int eagle_xml_unload(trparse_t *pst)
{
	xmlFreeDoc((xmlDoc *)pst->doc);
	return 0;
}

static trnode_t *eagle_xml_children(trparse_t *pst, trnode_t *node)
{
	xmlNode *nd = (xmlNode *)node;
	return (trnode_t *)nd->children;
}

static trnode_t *eagle_xml_parent(trparse_t *pst, trnode_t *node)
{
	xmlNode *nd = (xmlNode *)node;
	return (trnode_t *)nd->parent;
}

static trnode_t *eagle_xml_next(trparse_t *pst, trnode_t *node)
{
	xmlNode *nd = (xmlNode *)node;
	return (trnode_t *)nd->next;
}

const char *eagle_xml_nodename(trnode_t *node)
{
	xmlNode *nd = (xmlNode *)node;
	return (const char *)nd->name;
}

static const char *eagle_xml_prop(trparse_t *pst, trnode_t *node, const char *key)
{
	xmlNode *nd = (xmlNode *)node;
	return (const char *)xmlGetProp(nd, (const xmlChar *)key);
}

static const char *eagle_xml_text(trparse_t *pst, trnode_t *node)
{
	xmlNode *nd = (xmlNode *)node;
	return (const char *)nd->content;
}

static int eagle_xml_strcmp(const char *s1, const char *s2)
{
	return xmlStrcmp((const xmlChar *)s1, (const xmlChar *)s2);
}

static int eagle_xml_is_text(trparse_t *pst, trnode_t *node)
{
	return ((xmlNode *)node)->type == XML_TEXT_NODE;
}

static void *eagle_xml_get_user_data(trnode_t *node)
{
	xmlNode *nd = (xmlNode *)node;
	return nd->_private;
}

static void eagle_xml_set_user_data(trnode_t *node, void *data)
{
	xmlNode *nd = (xmlNode *)node;
	nd->_private = data;
}


trparse_calls_t trparse_xml_calls = {
	eagle_xml_load,
	eagle_xml_unload,
	eagle_xml_parent,
	eagle_xml_children,
	eagle_xml_next,
	eagle_xml_nodename,
	eagle_xml_prop,
	eagle_xml_text,
	eagle_xml_strcmp,
	eagle_xml_is_text,
	eagle_xml_get_user_data,
	eagle_xml_set_user_data
};