517
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
517
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
517
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
517
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
517
|
29 #endif |
|
30 |
1968
|
31 #include "error.h" |
1742
|
32 #include "oct-obj.h" |
517
|
33 |
2970
|
34 octave_allocator |
|
35 octave_value_list::allocator (sizeof (octave_value_list)); |
|
36 |
2872
|
37 octave_value_list& |
|
38 octave_value_list::prepend (const octave_value& val) |
|
39 { |
|
40 int n = length (); |
|
41 |
|
42 resize (n + 1); |
|
43 |
|
44 while (n > 0) |
|
45 { |
|
46 elem (n) = elem (n - 1); |
|
47 n--; |
|
48 } |
|
49 |
|
50 elem (0) = val; |
|
51 |
|
52 return *this; |
|
53 } |
|
54 |
|
55 octave_value_list& |
|
56 octave_value_list::append (const octave_value& val) |
|
57 { |
|
58 int n = length (); |
|
59 |
|
60 resize (n + 1); |
|
61 |
|
62 elem (n) = val; |
|
63 |
|
64 return *this; |
|
65 } |
|
66 |
|
67 octave_value_list& |
|
68 octave_value_list::append (const octave_value_list& lst) |
|
69 { |
|
70 int len = length (); |
|
71 int lst_len = lst.length (); |
|
72 |
|
73 resize (len + lst_len); |
|
74 |
|
75 for (int i = 0; i < lst_len; i++) |
|
76 elem (len + i) = lst (i); |
|
77 |
|
78 return *this; |
|
79 } |
|
80 |
|
81 octave_value_list& |
|
82 octave_value_list::reverse (void) |
|
83 { |
|
84 int n = length (); |
|
85 |
|
86 for (int i = 0; i < n / 2; i++) |
|
87 { |
|
88 octave_value tmp = elem (i); |
|
89 elem (i) = elem (n - i - 1); |
|
90 elem (n - i - 1) = tmp; |
|
91 } |
|
92 |
|
93 return *this; |
|
94 } |
|
95 |
3195
|
96 octave_value_list |
|
97 octave_value_list::splice (int offset, int rep_length, |
|
98 const octave_value_list& lst) const |
|
99 { |
|
100 octave_value_list retval; |
|
101 |
|
102 int len = length (); |
|
103 |
|
104 if (offset < 0 || offset >= len) |
|
105 { |
|
106 error ("octave_value_list::splice: invalid OFFSET"); |
|
107 return retval; |
|
108 } |
|
109 |
|
110 if (rep_length < 0 || rep_length + offset > len) |
|
111 { |
|
112 error ("octave_value_list::splice: invalid LENGTH"); |
|
113 return retval; |
|
114 } |
|
115 |
|
116 int lst_len = lst.length (); |
|
117 |
|
118 int new_len = len - rep_length + lst_len; |
|
119 |
|
120 retval.resize (new_len); |
|
121 |
|
122 int k = 0; |
|
123 |
|
124 for (int i = 0; i < offset; i++) |
|
125 retval(k++) = elem (i); |
|
126 |
|
127 for (int i = 0; i < lst_len; i++) |
|
128 retval(k++) = lst(i); |
|
129 |
|
130 for (int i = offset + rep_length; i < len; i++) |
|
131 retval(k++) = elem (i); |
|
132 |
|
133 return retval; |
|
134 } |
|
135 |
2872
|
136 bool |
|
137 octave_value_list::all_strings_p (void) const |
1968
|
138 { |
|
139 int n = length (); |
517
|
140 |
1968
|
141 for (int i = 0; i < n; i++) |
|
142 if (! elem(i).is_string ()) |
|
143 return 0; |
1746
|
144 |
1968
|
145 return 1; |
517
|
146 } |
|
147 |
1968
|
148 string_vector |
2086
|
149 octave_value_list::make_argv (const string& fcn_name) const |
517
|
150 { |
1968
|
151 string_vector argv; |
|
152 |
2872
|
153 if (all_strings_p ()) |
1968
|
154 { |
3180
|
155 int len = length (); |
|
156 |
|
157 int total_nr = 0; |
|
158 |
|
159 for (int i = 0; i < len; i++) |
|
160 total_nr += elem(i).rows (); |
|
161 |
|
162 argv.resize (total_nr+1); |
|
163 |
1968
|
164 argv[0] = fcn_name; |
517
|
165 |
3180
|
166 int k = 1; |
|
167 for (int i = 0; i < len; i++) |
|
168 { |
|
169 int nr = elem(i).rows (); |
|
170 |
|
171 if (nr == 1) |
|
172 argv[k++] = elem(i).string_value (); |
|
173 else |
|
174 { |
|
175 string_vector tmp = elem(i).all_strings (); |
|
176 |
|
177 for (int j = 0; j < nr; j++) |
|
178 argv[k++] = tmp[j]; |
|
179 } |
|
180 } |
1968
|
181 } |
|
182 else |
|
183 error ("%s: expecting all arguments to be strings", fcn_name.c_str ()); |
517
|
184 |
1968
|
185 return argv; |
517
|
186 } |
|
187 |
|
188 /* |
|
189 ;;; Local Variables: *** |
|
190 ;;; mode: C++ *** |
|
191 ;;; End: *** |
|
192 */ |