Help save net neutrality! Learn more.
Close

[60bb2c]: / miscellaneous / inst / xmlwrite.m  Maximize  Restore  History

Download this file

202 lines (165 with data), 5.8 kB

  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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
## Copyright (C) 2004 Laurent Mazet <mazet@crm.mot.com>
##
## 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 <http://www.gnu.org/licenses/>.
## -*- 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 a 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("error opening file \"%s\"\n", filename);
endif
## XML header
fprintf (fd, "<?xml version=\"1.0\"?>\n");
fprintf (fd, "<!DOCTYPE octave SYSTEM \"octave.dtd\">\n");
fprintf (fd, "<octave>\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<string%s length=\"%d\">%s</string>%s",
indent, opt, length(value), value, separator);
elseif ischar(value)
## String array type
fprintf (fd, "%s<array%s rows=\"%d\">\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</array>\n", indent);
elseif isscalar(value)
## Scalar type
if iscomplex(value)
## Complex type
fprintf (fd, "%s<complex%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, "</complex>%s", separator);
elseif isbool(value)
## Boolean type
if value
fprintf (fd, "%s<scalar%s value=\"true\"/>%s", indent, opt, separator);
else
fprintf (fd, "%s<scalar%s value=\"false\"/>%s", indent, opt, separator);
endif
elseif isinf(value)
## Infinite type
if value > 0
fprintf (fd, "%s<scalar%s value=\"inf\"/>%s",
indent, opt, separator);
else
fprintf (fd, "%s<scalar%s value=\"neginf\"/>%s",
indent, opt, separator);
endif
elseif isnan(value)
## Not-A-Number type
fprintf (fd, "%s<scalar%s value=\"nan\"/>%s", indent, opt, separator);
elseif isna(value)
## Not-Avaliable
fprintf (fd, "%s<scalar%s value=\"na\"/>%s", indent, opt, separator);
else
sc = sprintf(sprintf("%%.%dg", save_precision), value);
fprintf (fd, "%s<scalar%s>%s</scalar>%s", indent, opt, sc, ...
separator);
endif
elseif ismatrix(value) && isnumeric(value) && (length(size(value)) <= 2)
## Matrix type
fprintf (fd, "%s<matrix%s rows=\"%d\" columns=\"%d\">\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</matrix>\n", indent);
elseif isstruct(value)
## Structure type
st = fieldnames(value);
fprintf (fd, "%s<structure%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</structure>\n", indent);
elseif iscell(value)
## Cell type
fprintf (fd, "%s<cell%s rows=\"%d\" columns=\"%d\">\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</cell>\n", indent);
elseif islist(value)
## List type
fprintf (fd, "%s<list%s length=\"%d\">\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</list>\n", indent);
else
## Unknown type
error("unknown type\n");
endif
nb++;
if !isopen
fprintf (fd, "</octave>\n");
fclose(fd);
endif
endfunction