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

« back to all changes in this revision

Viewing changes to src/xmlread.cc

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2012-10-17 13:40:55 UTC
  • mfrom: (1.1.6)
  • mto: (5.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: package-import@ubuntu.com-20121017134055-e8lrxjd3qgcd3kmt
Tags: upstream-1.2.0
Import upstream version 1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2004 Laurent Mazet
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
16
 
 */
17
 
 
18
 
/* 2004-01-30
19
 
 *   initial release
20
 
 */
21
 
 
22
 
#include <octave/oct.h>
23
 
#include <octave/lo-ieee.h>
24
 
#include <octave/oct-map.h>
25
 
#include <octave/Cell.h>
26
 
 
27
 
extern "C" {
28
 
#include "xmltree.h"
29
 
#include "xmltree_read.h"
30
 
}
31
 
 
32
 
octave_value get_element (element *root) {
33
 
  int length, rows, columns;
34
 
 
35
 
  string_vector tmp_array;
36
 
  ComplexMatrix tmp_matrix;
37
 
  Octave_map tmp_structure;
38
 
  Cell tmp_cell;
39
 
  octave_value_list tmp_list;
40
 
 
41
 
  octave_value retval;
42
 
 
43
 
  if (!root)
44
 
    return retval;
45
 
 
46
 
  switch (root->def_value) {
47
 
  case value_scalar:
48
 
    switch (root->const_value) {
49
 
    case const_true: retval = octave_value(true); break;
50
 
    case const_false: retval = octave_value(false); break;
51
 
    case const_inf: retval = octave_value(octave_Inf); break;
52
 
    case const_neginf: retval = octave_value(-octave_Inf); break;
53
 
    case const_nan:  retval = octave_value(octave_NaN); break;
54
 
    case const_na:  retval = octave_value(octave_NA); break;
55
 
    case const_undef: retval = octave_value(root->scalar_value); break;
56
 
    }
57
 
    break;
58
 
 
59
 
  case value_complex:
60
 
    retval = 
61
 
      octave_value(Complex(get_element(root->child).double_value(),
62
 
                           get_element(root->child->next).double_value()));
63
 
    break;
64
 
 
65
 
  case value_string:
66
 
    retval = octave_value(root->string_value);
67
 
    break;
68
 
 
69
 
  case value_array:
70
 
    rows = root->rows;
71
 
    root = root->child;
72
 
    tmp_array = string_vector(rows);
73
 
    for (int k=0; (k<rows) && (root); k++, root = root->next)
74
 
      tmp_array(k) = get_element(root).string_value();
75
 
    retval = octave_value(tmp_array);
76
 
    break;
77
 
 
78
 
  case value_matrix:
79
 
    rows = root->rows;
80
 
    columns = root->columns;
81
 
    root = root->child;
82
 
    tmp_matrix = ComplexMatrix(rows, columns);
83
 
    for (int k=0; (k<rows) && (root); k++)
84
 
      for (int l=0; (l<columns) && (root); l++, root = root->next)
85
 
        tmp_matrix(k, l) = get_element(root).complex_value();
86
 
    if (tmp_matrix.all_elements_are_real())
87
 
      retval = octave_value(real(tmp_matrix));
88
 
    else
89
 
      retval = octave_value(tmp_matrix);
90
 
    break;
91
 
 
92
 
  case value_structure:
93
 
    root = root->child;
94
 
    for (int k=0; root; root = root->next)
95
 
      if (root->name)
96
 
        tmp_structure.assign(root->name, get_element(root));
97
 
      else {
98
 
        char *name = new char[7];
99
 
        sprintf (name, "__%04d", k++);
100
 
        warning ("no field name in structure.");
101
 
        tmp_structure.assign(name, get_element(root));
102
 
        delete[] name;
103
 
      }
104
 
    retval = octave_value(tmp_structure);
105
 
    break;
106
 
 
107
 
  case value_list:
108
 
    length = root->length;
109
 
    root = root->child;
110
 
    //tmp_list = octave_value_list(length);
111
 
    for (int k=0; (k<length) && (root); k++, root = root->next)
112
 
      tmp_list(k) = get_element(root);
113
 
    retval = octave_value(tmp_list);
114
 
    break;
115
 
    
116
 
  case value_cell:
117
 
    rows = root->rows;
118
 
    columns = root->columns;
119
 
    root = root->child;
120
 
    tmp_cell = Cell(rows, columns);
121
 
    for (int k=0; (k<rows) && (root); k++)
122
 
      for (int l=0; (l<columns) && (root); l++, root = root->next)
123
 
        tmp_cell(k, l) = get_element(root);
124
 
    retval = octave_value(tmp_cell);
125
 
    break;
126
 
 
127
 
  default:
128
 
    warning ("unknown type.\n");
129
 
  }
130
 
 
131
 
  return retval;
132
 
}
133
 
 
134
 
DEFUN_DLD (xmlread, args, nargout,
135
 
           "-*- texinfo -*-\n"
136
 
           "@deftypefn {Function File} {@var{value}} xmlread(@var{filename})\n"
137
 
           "\n"
138
 
           "Read a @var{value} from @var{filename} as an XML file\n"
139
 
           "@end deftypefn") {
140
 
 
141
 
  /* Check argument */
142
 
  if (args.length() != 1) {
143
 
    print_usage ();
144
 
    return octave_value_list();
145
 
  }
146
 
 
147
 
  /* Read file */
148
 
  std::string filename = args(0).string_value();
149
 
  element *root = read_xmltree(filename.c_str());
150
 
  if (!root)
151
 
    return octave_value_list();
152
 
  
153
 
  /* step down into the element tree */
154
 
  octave_value retval = get_element (root->child);
155
 
  free_element (root);
156
 
 
157
 
  return retval;
158
 
}