Mercurial > forge
annotate main/general/src/packfields.cc @ 9687:9df0cf7217ae octave-forge
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
author | carandraug |
---|---|
date | Tue, 13 Mar 2012 22:39:03 +0000 |
parents | 3720f436cb20 |
children | 9227bea976f1 |
rev | line source |
---|---|
9687
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
1 // Copyright (C) 2009 VZLU Prague |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
2 // |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
3 // This program is free software; you can redistribute it and/or modify it under |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
4 // the terms of the GNU General Public License as published by the Free Software |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
5 // Foundation; either version 3 of the License, or (at your option) any later |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
6 // version. |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
7 // |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
8 // This program is distributed in the hope that it will be useful, but WITHOUT |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
11 // details. |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
12 // |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
13 // You should have received a copy of the GNU General Public License along with |
9df0cf7217ae
general: update licenses to GPLv3+ and DESCRIPTION to mention non GPL code
carandraug
parents:
6618
diff
changeset
|
14 // this program; if not, see <http://www.gnu.org/licenses/>. |
6060 | 15 |
16 #include <octave/oct.h> | |
17 #include <octave/utils.h> | |
18 #include <octave/symtab.h> | |
19 #include <octave/oct-map.h> | |
20 | |
21 DEFUN_DLD (packfields, args, , | |
22 "-*- texinfo -*-\n\ | |
23 @deftypefn {Loadable Function} packfields (struct, var1, var2, @dots{})\n\ | |
24 Inserts the named variables @var{var1}, @var{var2}, @dots{} as fields into @var{struct}.\n\ | |
25 @var{struct} can be a scalar structure or user class.\n\ | |
26 This is equivalent to the code:\n\ | |
27 @example\n\ | |
28 struct.var1 = var1;\n\ | |
29 struct.var2 = var2;\n\ | |
30 : \n\ | |
31 @end example\n\ | |
32 but more efficient and more concise.\n\ | |
33 @seealso{unpackfields, struct}\n\ | |
34 @end deftypefn") | |
35 { | |
36 int nargin = args.length (); | |
37 | |
38 if (nargin > 0) | |
39 { | |
40 std::string struct_name = args (0).string_value (); | |
41 string_vector fld_names(nargin-1); | |
6618 | 42 //octave_value_list fld_vals(nargin-1); |
43 // FIXME: workaround for 3.2.4. | |
44 octave_value_list fld_vals (nargin-1, octave_value ()); | |
6060 | 45 |
46 if (! error_state && ! valid_identifier (struct_name)) | |
47 error ("packfields: invalid variable name: %s", struct_name.c_str ()); | |
48 | |
49 for (octave_idx_type i = 0; i < nargin-1; i++) | |
50 { | |
51 if (error_state) | |
52 break; | |
53 | |
54 std::string fld_name = args(i+1).string_value (); | |
55 | |
56 if (error_state) | |
57 break; | |
58 | |
59 if (valid_identifier (fld_name)) | |
60 { | |
61 fld_names(i) = fld_name; | |
62 octave_value fld_val = symbol_table::varval (fld_name); | |
63 if (fld_val.is_defined ()) | |
64 fld_vals(i) = fld_val; | |
65 else | |
66 error ("packfields: variable %s not defined", fld_name.c_str ()); | |
67 } | |
68 else | |
69 error ("packfields: invalid field name: %s", fld_name.c_str ()); | |
70 } | |
71 | |
72 if (! error_state) | |
73 { | |
74 // Force the symbol to be inserted in caller's scope. | |
75 symbol_table::symbol_record& rec = symbol_table::insert (struct_name); | |
76 | |
77 octave_value& struct_ref = rec.varref (); | |
78 | |
79 // If not defined, use struct (). | |
80 if (! struct_ref.is_defined ()) | |
81 struct_ref = Octave_map (dim_vector (1, 1)); | |
82 | |
83 if (struct_ref.is_map ()) | |
84 { | |
85 // Fast code for a built-in struct. | |
86 Octave_map map = struct_ref.map_value (); | |
87 | |
88 if (map.numel () == 1) | |
89 { | |
90 // Do the actual work. | |
91 struct_ref = octave_value (); // Unshare map. | |
92 for (octave_idx_type i = 0; i < nargin-1; i++) | |
93 map.assign (fld_names(i), fld_vals(i)); | |
94 struct_ref = map; | |
95 } | |
96 else | |
97 error ("packfields: structure must have singleton dimensions"); | |
98 } | |
99 else | |
100 { | |
101 // General case. | |
102 struct_ref.make_unique (); | |
103 std::list<octave_value_list> idx (1); | |
104 | |
105 for (octave_idx_type i = 0; i < nargin-1; i++) | |
106 { | |
107 idx.front () = args(i+1); // Save one string->octave_value conversion. | |
108 struct_ref = struct_ref.subsasgn (".", idx, fld_vals (i)); | |
109 | |
110 if (error_state) | |
111 break; | |
112 } | |
113 } | |
114 } | |
115 } | |
116 else | |
117 print_usage (); | |
118 | |
119 return octave_value_list (); | |
120 } |