~ubuntu-branches/ubuntu/wily/octave-miscellaneous/wily

« back to all changes in this revision

Viewing changes to inst/xmlwrite.m

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot, Rafael Laboissiere, Sébastien Villemot
  • Date: 2012-10-17 13:40:55 UTC
  • mfrom: (1.2.1) (12 sid)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: package-import@ubuntu.com-20121017134055-vatltexghy77fnv7
Tags: 1.2.0-1
[ Rafael Laboissiere ]
* Imported Upstream version 1.2.0
* Bump Standards-Version to 3.9.4 (no changes needed)
* Refresh for new upstream release
* Use Sébastien Villemot's @debian.org email address
* Remove obsolete DM-Upload-Allowed flag
* Add patch autoload-yes.patch
* Add copyright info for file lauchli.m (included in a Debian patch)
* Add patch partcnt-test-succeeds.patch
* Build-depends on octave-pkg-dev >= 1.0.3

[ Sébastien Villemot ]
* debian/control: fix versioned dependency on debhelper
* Add lintian override for false positive on hardening (fortify)

Show diffs side-by-side

added added

removed removed

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