X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=octave_packages%2Fio-1.0.19%2Fxmlwrite.m;fp=octave_packages%2Fio-1.0.19%2Fxmlwrite.m;h=a29b030f42ea6fb9071f1a78fe2226a599b3d77a;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hp=0000000000000000000000000000000000000000;hpb=1705066eceaaea976f010f669ce8e972f3734b05;p=CreaPhase.git diff --git a/octave_packages/io-1.0.19/xmlwrite.m b/octave_packages/io-1.0.19/xmlwrite.m new file mode 100644 index 0000000..a29b030 --- /dev/null +++ b/octave_packages/io-1.0.19/xmlwrite.m @@ -0,0 +1,201 @@ +## Copyright (C) 2004 Laurent Mazet +## +## 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 3 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, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{nb} =} xmlwrite (@var{filename}, @var{value}) +## @deftypefnx {Function File} {@var{nb} =} xmlwrite (@var{fd}, @var{value}, [@var{name}]) +## +## Write a @var{value} into @var{filename} (@var{fd}) as an XML file. +## +##The number of elements (@var{nb}) or 0 is returned. +## @end deftypefn + +function nb = xmlwrite (filename, value, name) + persistent indent = ""; + persistent separator = "\n"; + + ## Check argument number + nb = 0; + if (nargin < 2) || (nargin > 3) + print_usage; + endif + + ## Get the file identificator + isopen = false; + if ischar(filename) + + ## Check file name + sn = char (strsplit (filename, ".")); + if !strcmp(tolower(deblank(sn(end,:))), "xml") + filename = [filename, ".xml"]; + endif + + ## Open file + fd = fopen (filename, "w"); + if fd <= 0 + error("xmlwrite: error opening file \"%s\"\n", filename); + endif + + ## XML header + fprintf (fd, "\n"); + fprintf (fd, "\n"); + fprintf (fd, "\n"); + indent = " "; + else + isopen = true; + fd = filename; + endif + + ## Store name in optional argument + opt = ""; + if nargin == 3 + opt = sprintf(" name=\"%s\"", name); + endif + + ## Process by type + + if ischar(value) && (rows(value) <= 1) + ## String type + + fprintf (fd, "%s%s%s", + indent, opt, length(value), value, separator); + + elseif ischar(value) + ## String array type + + fprintf (fd, "%s\n", indent, opt, rows(value)); + _indent = indent; indent = [indent, " "]; + for k=1:rows(value), + nb += xmlwrite (fd, deblank(value(k, :))); + endfor + indent = _indent; + fprintf (fd, "%s\n", indent); + + elseif isscalar(value) + ## Scalar type + + if iscomplex(value) + ## Complex type + + fprintf (fd, "%s", indent, opt); + _indent = indent; indent = ""; _separator = separator; separator = ""; + nb += xmlwrite (fd, real(value)); + nb += xmlwrite (fd, imag(value)); + indent = _indent; separator = _separator; + fprintf (fd, "%s", separator); + + elseif isbool(value) + ## Boolean type + + if value + fprintf (fd, "%s%s", indent, opt, separator); + else + fprintf (fd, "%s%s", indent, opt, separator); + endif + + elseif isinf(value) + ## Infinite type + + if value > 0 + fprintf (fd, "%s%s", + indent, opt, separator); + else + fprintf (fd, "%s%s", + indent, opt, separator); + endif + + elseif isnan(value) + ## Not-A-Number type + + fprintf (fd, "%s%s", indent, opt, separator); + + elseif isna(value) + ## Not-Avaliable + + fprintf (fd, "%s%s", indent, opt, separator); + + else + sc = sprintf(sprintf("%%.%dg", save_precision), value); + fprintf (fd, "%s%s%s", indent, opt, sc, ... + separator); + endif + + elseif ismatrix(value) && isnumeric(value) && (length(size(value)) <= 2) + ## Matrix type + + fprintf (fd, "%s\n", + indent, opt, rows(value), columns(value)); + _indent = indent; indent = ""; separator = ""; + for k=1:rows(value), + fprintf (fd, "%s ", _indent); + for l=1:columns(value)-1, + nb += xmlwrite (fd, value(k, l)); + fprintf (fd, " "); + endfor + nb += xmlwrite (fd, value(k, end)); + fprintf (fd, "\n"); + endfor + indent = _indent; separator = "\n"; + fprintf (fd, "%s\n", indent); + + elseif isstruct(value) + ## Structure type + + st = fieldnames(value); + fprintf (fd, "%s\n", indent, opt); + _indent = indent; indent = [indent, " "]; + for k=1:length(st), + eval(sprintf("nb += xmlwrite (fd, value.%s, \"%s\");", st{k}, st{k})); + endfor + indent = _indent; + fprintf (fd, "%s\n", indent); + + elseif iscell(value) + ## Cell type + + fprintf (fd, "%s\n", + indent, opt, rows(value), columns(value)); + _indent = indent; indent = [indent, " "]; + for k=1:rows(value), + for l=1:columns(value), + nb += xmlwrite (fd, value{k, l}); + endfor + endfor + indent = _indent; + fprintf (fd, "%s\n", indent); + + elseif islist(value) + ## List type + + fprintf (fd, "%s\n", indent, opt, length(value)); + _indent = indent; indent = [indent, " "]; + for k=1:length(value), + nb += xmlwrite (fd, value{k}); + endfor + indent = _indent; + fprintf (fd, "%s\n", indent); + + else + ## Unknown type + error("xmlwrite: unknown type\n"); + endif + nb++; + + if !isopen + fprintf (fd, "\n"); + fclose(fd); + endif + +endfunction