# HG changeset patch # User Markus Mützel # Date 1604779314 -3600 # Sat Nov 07 21:01:54 2020 +0100 # Node ID a09536755449a45d985ffc6187ba2588afef2bcb # Parent 64c5e64894648c2a09184d04403a75d27b2308ed WIP: readstruct (bug #59245). diff -r 64c5e6489464 -r a09536755449 scripts/io/readstruct.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/io/readstruct.m Sat Nov 07 21:01:54 2020 +0100 @@ -0,0 +1,97 @@ +######################################################################## +## +## Copyright (C) 2020 The Octave Project Developers +## +## See the file COPYRIGHT.md in the top-level directory of this +## distribution or . +## +## This file is part of Octave. +## +## Octave 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 3 of the License, or +## (at your option) any later version. +## +## Octave 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 Octave; see the file COPYING. If not, see +## . +## +######################################################################## + +## -*- texinfo -*- +## @deftypefn {} {@var{out} =} readstruct (@var{filename}) +## Read XML file content into structure. +## +## @seealso{xmlread} +## @end deftypefn + +function out = readstruct (filename) + +if nargout > 1 + print_usage (); +endif + +if (! ischar (filename)) + error ("Octave:readstruct:invalid-filename", ... + "readstruct: FILENAME must be a character vector."); +endif + +if (! exist (filename, "file")) + error ("Octave:readstruct:filename-not-exists", ... + "readstruct: FILENAME '%s' does not exist.", filename); +endif + +dom = xmlread (filename); + +out = struct (); + +out = node2struct (dom, out, 0); + +endfunction + + +function struc = node2struct (node, struc, extend_existing) + +i_struc = numel (struc) + extend_existing; + +attributes = node.getAttributes (); +for i_attr = 1:numel (attributes) + name = attributes(i_attr).getName (); + % FIXME: Replace invalid chars (e.g. ":") + struc(i_struc).([name "Attribute"]) = node.getAttribute (name); +endfor + +kid_nodes = node.getChildNodes (); +for i_kid = 1:numel(kid_nodes) + name = kid_nodes(i_kid).getNodeName (); + if isempty (name) + if isstruct (struc) + struc = node.getNodeValue (); + else + struc(i_struc) = node.getNodeValue (); + endif + else + if isfield (struc, name) && ! isempty (struc(i_struc).(name)) + kid_struc = struc(i_struc).(name); + else + kid_struc = struct (); + endif + + struc(i_struc).(name) = ... + node2struct (kid_nodes(i_kid), kid_struc, ... + isfield (struc, name) && ! isempty (struc(i_struc).(name))); + endif +endfor + +%if (isempty (fieldnames (struc))) +% struc = node.getNodeValue (); +%else + +%endif + +endfunction