2376
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <iostream.h> |
|
32 |
|
33 #include "error.h" |
|
34 #include "ov-struct.h" |
|
35 #include "unwind-prot.h" |
|
36 |
|
37 int octave_struct::t_id = -1; |
|
38 |
|
39 const string octave_struct::t_name ("struct"); |
|
40 |
|
41 |
|
42 octave_value |
2420
|
43 octave_struct::struct_elt_val (const string& nm, bool silent) const |
2376
|
44 { |
|
45 octave_value retval; |
|
46 |
|
47 Pix idx = map.seek (nm); |
|
48 |
|
49 if (idx) |
|
50 retval = map.contents (idx); |
2420
|
51 else if (! silent) |
2376
|
52 error ("structure has no member `%s'", nm.c_str ()); |
|
53 |
|
54 return retval; |
|
55 } |
|
56 |
|
57 octave_value& |
|
58 octave_struct::struct_elt_ref (const string& nm) |
|
59 { |
|
60 return map [nm]; |
|
61 |
|
62 #if 0 |
|
63 static octave_value fooval; |
|
64 |
|
65 Pix idx = map.seek (nm); |
|
66 |
|
67 if (idx) |
|
68 return map.contents (idx); |
|
69 else if (insert) |
|
70 return map [nm]; |
|
71 else |
|
72 error ("structure has no member `%s'", nm.c_str ()); |
|
73 |
|
74 return fooval; |
|
75 #endif |
|
76 } |
|
77 |
|
78 #if 0 |
|
79 octave_value& |
|
80 octave_struct::lookup_map_element (const string& name, bool insert, |
|
81 bool silent) |
|
82 { |
|
83 static octave_value fooval; |
|
84 |
|
85 Pix idx = map.seek (name); |
|
86 |
|
87 if (idx) |
|
88 return map.contents (idx); |
|
89 else if (insert) |
|
90 return map [name]; |
|
91 else if (! silent) |
|
92 error ("structure has no member `%s'", name.c_str ()); |
|
93 |
|
94 return fooval; |
|
95 } |
|
96 |
|
97 octave_value |
|
98 octave_struct::lookup_map_element (const string& ref, bool insert, |
|
99 bool silent) |
|
100 { |
|
101 octave_value retval; |
|
102 |
|
103 if (! ref.empty ()) |
|
104 { |
|
105 SLList<string> list; |
|
106 |
|
107 size_t beg = 0; |
|
108 size_t end; |
|
109 |
|
110 do |
|
111 { |
|
112 end = ref.find ('.', beg); |
|
113 |
|
114 string tmp = (end == NPOS) |
|
115 ? ref.substr (beg) : ref.substr (beg, end - beg); |
|
116 |
|
117 list.append (tmp); |
|
118 } |
|
119 while (end != NPOS && (beg = end + 1)); |
|
120 |
|
121 retval = lookup_map_element (list, insert, silent); |
|
122 } |
|
123 |
|
124 return retval; |
|
125 } |
|
126 |
|
127 octave_value |
|
128 octave_struct::lookup_map_element (SLList<string>& list, bool insert, |
|
129 bool silent) |
|
130 { |
|
131 octave_value retval; |
|
132 |
|
133 Pix p = list.first (); |
|
134 |
|
135 while (p) |
|
136 { |
|
137 string elt = list (p); |
|
138 |
|
139 list.next (p); |
|
140 |
|
141 octave_value tmp = lookup_map_element (elt, insert, silent); |
|
142 |
|
143 if (error_state) |
|
144 break; |
|
145 |
|
146 tmp_rep = tmp.rep; |
|
147 |
|
148 if (! p) |
|
149 retval = tmp; |
|
150 } |
|
151 |
|
152 return retval; |
|
153 } |
|
154 #endif |
|
155 |
|
156 void |
|
157 octave_struct::print (ostream& os) |
|
158 { |
|
159 // XXX FIXME XXX -- would be nice to print the output in some |
|
160 // standard order. Maybe all substructures first, maybe |
|
161 // alphabetize entries, etc. |
|
162 |
|
163 begin_unwind_frame ("octave_struct_print"); |
|
164 |
|
165 unwind_protect_int (struct_indent); |
|
166 unwind_protect_int (Vstruct_levels_to_print); |
|
167 |
|
168 if (Vstruct_levels_to_print-- > 0) |
|
169 { |
|
170 os.form ("\n%*s{\n", struct_indent, ""); |
|
171 |
|
172 increment_struct_indent (); |
|
173 |
|
174 Pix p = map.first (); |
|
175 |
|
176 while (p) |
|
177 { |
|
178 bool pad_after = false; |
|
179 |
|
180 string key = map.key (p); |
|
181 octave_value val = map.contents (p); |
|
182 |
|
183 map.next (p); |
|
184 |
|
185 os.form ("%*s%s =", struct_indent, "", key.c_str ()); |
|
186 |
|
187 if (val.print_as_scalar ()) |
|
188 os << " "; |
|
189 else if (val.print_as_structure ()) |
|
190 { |
|
191 if (p) |
|
192 pad_after = true; |
|
193 } |
|
194 else |
|
195 { |
|
196 if (p) |
|
197 pad_after = true; |
|
198 |
|
199 os << "\n\n"; |
|
200 } |
|
201 |
|
202 val.print (os); |
|
203 |
|
204 if (pad_after) |
|
205 os << "\n"; |
|
206 } |
|
207 |
|
208 decrement_struct_indent (); |
|
209 |
|
210 os.form ("%*s%s", struct_indent, "", "}\n"); |
|
211 } |
|
212 else |
|
213 os << " <structure>\n"; |
|
214 |
|
215 run_unwind_frame ("octave_struct_print"); |
|
216 } |
|
217 |
|
218 /* |
|
219 ;;; Local Variables: *** |
|
220 ;;; mode: C++ *** |
|
221 ;;; End: *** |
|
222 */ |