|
From: Melchior F. <mf...@fl...> - 2007-06-30 11:08:20
|
Update of /var/cvs/FlightGear-0.9/data/Nasal
In directory baron:/tmp/cvs-serv25414
Modified Files:
io.nas
Log Message:
- make io.readxml() a lot faster
- documentation update
Index: io.nas
===================================================================
RCS file: /var/cvs/FlightGear-0.9/data/Nasal/io.nas,v
retrieving revision 1.6
retrieving revision 1.7
diff -C 2 -r1.6 -r1.7
*** io.nas 29 Jun 2007 15:49:08 -0000 1.6
--- io.nas 30 Jun 2007 11:06:36 -0000 1.7
***************
*** 28,50 ****
# Reads an XML file from an absolute path and returns it as property
! # tree. All nodes are of type STRING, attributes are written as regular
! # nodes with the optional prefix prepended to the name. If the prefix
! # is nil, then attributes are ignored. Returns nil on error.
var readxml = func(file, prefix = "") {
! var stack = [[0, ""]];
var node = props.Node.new();
var tree = node; # prevent GC
var start = func(name, attr) {
! stack[-1][0] += 1; # count children
! append(stack, [0, ""]);
! var index = size(node.getChildren(name));
! node = node.getChild(name, index, 1);
if(prefix != nil)
foreach(var n; keys(attr))
node.getNode(prefix ~ n, 1).setValue(attr[n]);
}
var end = func(name) {
var buf = pop(stack);
! if(!buf[0] and size(buf[1]))
node.setValue(buf[1]);
node = node.getParent();
--- 28,62 ----
# Reads an XML file from an absolute path and returns it as property
! # tree. All nodes are of type STRING. Data are only written to leafs.
! # Attributes are written as regular nodes with the optional prefix
! # prepended to the name. If the prefix is nil, then attributes are
! # ignored. Returns nil on error.
! #
! # NOTE: this function is meant for importing 'foreign' XML files, *not*
! # for reading FlighGear's standard "PropertyList" XML files. For those
! # we have better means, like the loadxml/savexml fgcommands. This
! # function does neither interpret the "type" nor the "n" (index) or
! # "archive" attribute.
! #
var readxml = func(file, prefix = "") {
! var stack = [[{}, ""]];
var node = props.Node.new();
var tree = node; # prevent GC
var start = func(name, attr) {
! var index = stack[-1][0];
! if (!contains(index, name))
! index[name] = 0;
!
! node = node.getChild(name, index[name], 1);
if(prefix != nil)
foreach(var n; keys(attr))
node.getNode(prefix ~ n, 1).setValue(attr[n]);
+
+ index[name] += 1;
+ append(stack, [{}, ""]);
}
var end = func(name) {
var buf = pop(stack);
! if(!size(buf[0]) and size(buf[1]))
node.setValue(buf[1]);
node = node.getParent();
|