]> Creatis software - CreaPhase.git/blob - octave_packages/io-1.0.19/xmlwrite.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / io-1.0.19 / xmlwrite.m
1 ## Copyright (C) 2004 Laurent Mazet <mazet@crm.mot.com>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{nb} =} xmlwrite (@var{filename}, @var{value})
18 ## @deftypefnx {Function File} {@var{nb} =} xmlwrite (@var{fd}, @var{value}, [@var{name}])
19 ##
20 ## Write a @var{value} into @var{filename} (@var{fd}) as an XML file.
21 ##
22 ##The number of elements (@var{nb}) or 0 is returned.
23 ## @end deftypefn
24
25 function nb = xmlwrite (filename, value, name)
26   persistent indent = "";
27   persistent separator = "\n";
28
29   ## Check argument number
30   nb = 0;
31   if (nargin < 2) || (nargin > 3)
32     print_usage;
33   endif
34   
35   ## Get the file identificator
36   isopen = false;
37   if ischar(filename)
38
39     ## Check file name
40     sn = char (strsplit (filename, "."));
41     if !strcmp(tolower(deblank(sn(end,:))), "xml")
42       filename = [filename, ".xml"];
43     endif
44
45     ## Open file
46     fd = fopen (filename, "w");
47     if fd <= 0
48       error("xmlwrite: error opening file \"%s\"\n", filename);
49     endif
50
51     ## XML header
52     fprintf (fd, "<?xml version=\"1.0\"?>\n");
53     fprintf (fd, "<!DOCTYPE octave SYSTEM \"octave.dtd\">\n");
54     fprintf (fd, "<octave>\n");
55     indent = "  ";
56   else
57     isopen = true;
58     fd = filename;
59   endif
60   
61   ## Store name in optional argument
62   opt = "";
63   if nargin == 3
64     opt = sprintf(" name=\"%s\"", name);
65   endif
66   
67   ## Process by type
68
69   if ischar(value) && (rows(value) <= 1)
70     ## String type
71     
72     fprintf (fd, "%s<string%s length=\"%d\">%s</string>%s",
73              indent, opt, length(value), value, separator);
74     
75   elseif ischar(value)
76     ## String array type
77     
78     fprintf (fd, "%s<array%s rows=\"%d\">\n", indent, opt, rows(value));
79     _indent = indent; indent = [indent, "  "];
80     for k=1:rows(value),
81       nb += xmlwrite (fd, deblank(value(k, :)));
82     endfor
83     indent = _indent;
84     fprintf (fd, "%s</array>\n", indent);
85     
86   elseif isscalar(value)
87     ## Scalar type
88     
89     if iscomplex(value)
90       ## Complex type
91
92       fprintf (fd, "%s<complex%s>", indent, opt);
93       _indent = indent; indent = ""; _separator = separator; separator = "";
94       nb += xmlwrite (fd, real(value));
95       nb += xmlwrite (fd, imag(value));
96       indent = _indent; separator = _separator;
97       fprintf (fd, "</complex>%s", separator);
98
99     elseif isbool(value)
100       ## Boolean type
101     
102       if value
103         fprintf (fd, "%s<scalar%s value=\"true\"/>%s", indent, opt, separator);
104       else
105         fprintf (fd, "%s<scalar%s value=\"false\"/>%s", indent, opt, separator);
106       endif
107     
108     elseif isinf(value)
109       ## Infinite type
110     
111       if value > 0
112         fprintf (fd, "%s<scalar%s value=\"inf\"/>%s",
113                  indent, opt, separator);
114       else
115         fprintf (fd, "%s<scalar%s value=\"neginf\"/>%s",
116                  indent, opt, separator);
117       endif
118     
119     elseif isnan(value)
120       ## Not-A-Number type
121       
122       fprintf (fd, "%s<scalar%s value=\"nan\"/>%s", indent, opt, separator);
123       
124     elseif isna(value)
125       ## Not-Avaliable
126       
127       fprintf (fd, "%s<scalar%s value=\"na\"/>%s", indent, opt, separator);
128       
129     else
130       sc = sprintf(sprintf("%%.%dg", save_precision), value);
131       fprintf (fd, "%s<scalar%s>%s</scalar>%s", indent, opt, sc, ...
132                separator);
133     endif
134     
135   elseif ismatrix(value) && isnumeric(value) && (length(size(value)) <= 2)
136     ## Matrix type
137     
138     fprintf (fd, "%s<matrix%s rows=\"%d\" columns=\"%d\">\n",
139              indent, opt, rows(value), columns(value));
140     _indent = indent; indent = ""; separator = "";
141     for k=1:rows(value),
142       fprintf (fd, "%s  ", _indent);
143       for l=1:columns(value)-1,
144         nb += xmlwrite (fd, value(k, l));
145         fprintf (fd, " ");
146       endfor
147       nb += xmlwrite (fd, value(k, end));
148       fprintf (fd, "\n");
149     endfor
150     indent = _indent; separator = "\n";
151     fprintf (fd, "%s</matrix>\n", indent);
152     
153   elseif isstruct(value)
154     ## Structure type
155
156     st = fieldnames(value);
157     fprintf (fd, "%s<structure%s>\n", indent, opt);
158     _indent = indent; indent = [indent, "  "];
159     for k=1:length(st),
160       eval(sprintf("nb += xmlwrite (fd, value.%s, \"%s\");", st{k}, st{k}));
161     endfor
162     indent = _indent;
163     fprintf (fd, "%s</structure>\n", indent);
164     
165   elseif iscell(value)
166     ## Cell type
167     
168     fprintf (fd, "%s<cell%s rows=\"%d\" columns=\"%d\">\n",
169              indent, opt, rows(value), columns(value));
170     _indent = indent; indent = [indent, "  "];
171     for k=1:rows(value),
172       for l=1:columns(value),
173         nb += xmlwrite (fd, value{k, l});
174       endfor
175     endfor
176     indent = _indent;
177     fprintf (fd, "%s</cell>\n", indent);
178     
179   elseif islist(value)
180     ## List type
181     
182     fprintf (fd, "%s<list%s length=\"%d\">\n", indent, opt, length(value));
183     _indent = indent; indent = [indent, "  "];
184     for k=1:length(value),
185       nb += xmlwrite (fd, value{k});
186     endfor
187     indent = _indent;
188     fprintf (fd, "%s</list>\n", indent);
189     
190   else
191     ## Unknown type
192     error("xmlwrite: unknown type\n");
193   endif
194   nb++;
195   
196   if !isopen
197     fprintf (fd, "</octave>\n");
198     fclose(fd);
199   endif
200   
201 endfunction