3998
|
1 #! /usr/bin/perl |
|
2 |
|
3 # Generate option handling code from a simpler input files for |
|
4 # Octave's functions like lsode, dassl, etc. |
|
5 |
3999
|
6 # TODO: |
|
7 # |
|
8 # * Improve default documentation and/or individual documentation |
|
9 # in data files. |
|
10 # |
|
11 # * Fix print/show code to display/return something more informative |
|
12 # for special values (for example, -1 ==> infinite in some cases). |
|
13 # Probably need more information in the data files for this. |
|
14 |
3998
|
15 # Input file format: |
|
16 # |
|
17 # CLASS = string |
|
18 # FCN_NAME = string |
4044
|
19 # INCLUDE = file |
3998
|
20 # DOC_STRING doc END_DOC_STRING |
|
21 # OPTION |
|
22 # NAME = string |
|
23 # TYPE = string |
|
24 # SET_ARG_TYPE = string (optional, defaults to TYPE) |
|
25 # INIT_VALUE = string | INIT_BODY code END_INIT_BODY |
|
26 # SET_EXPR = string | SET_BODY code END_SET_BODY | SET_CODE code END_SET_CODE |
|
27 # END_OPTION |
|
28 # |
|
29 # END_* must appear at beginning of line (whitespace ignored). |
|
30 |
|
31 use Getopt::Long; |
|
32 |
|
33 $opt_emit_opt_class_header = 0; |
|
34 $opt_emit_opt_handler_fcns = 0; |
|
35 $opt_debug = 0; |
|
36 |
|
37 GetOptions ("opt-class-header" => \$opt_emit_opt_class_header, |
|
38 "opt-handler-fcns" => \$opt_emit_opt_handler_fcns, |
|
39 "debug" => \$opt_debug); |
|
40 |
|
41 if (@ARGV == 1) |
|
42 { |
|
43 $INFILE = shift @ARGV; |
|
44 open (INFILE) || die "unable to open input file $INFILE"; |
|
45 } |
|
46 else |
|
47 { |
|
48 die "usage: mk-opts.pl [options] FILE"; |
|
49 } |
|
50 |
|
51 $opt_num = 0; |
|
52 |
|
53 &parse_input; |
|
54 |
|
55 &process_data; |
|
56 |
|
57 FOO: |
|
58 { |
|
59 $opt_emit_opt_class_header && do { &emit_opt_class_header; last FOO; }; |
|
60 |
|
61 $opt_emit_opt_handler_fcns && do { &emit_opt_handler_fcns; last FOO; }; |
|
62 |
|
63 $opt_debug && do { &emit_options_debug; last FOO; }; |
|
64 } |
|
65 |
|
66 sub parse_input |
|
67 { |
|
68 local ($have_doc_string); |
|
69 |
|
70 while (<INFILE>) |
|
71 { |
|
72 next if (/^\s*$/); |
|
73 |
|
74 if (/^\s*OPTION\s*$/) |
|
75 { |
|
76 &parse_option_block; |
|
77 } |
|
78 elsif (/^\s*CLASS\s*=\s*"(\w+)"\s*$/) |
|
79 { |
|
80 die "duplicate CLASS" if ($class ne ""); |
|
81 $CLASS = $1; |
|
82 $class_name = "${CLASS}_options"; |
|
83 $struct_name = "${class_name}_struct"; |
|
84 $static_table_name = "${class_name}_table"; |
|
85 } |
|
86 elsif (/^\s*FCN_NAME\s*=\s*"(\w+)"\s*$/) |
|
87 { |
|
88 die "duplicate FCN_NAME" if ($fcn_name ne ""); |
|
89 $fcn_name = $1; |
|
90 } |
4044
|
91 elsif (/^\s*INCLUDE\s*=\s*"(\S+)"\s*$/) |
|
92 { |
|
93 $include = "${include}#include <$1>\n"; |
|
94 } |
3998
|
95 elsif (/^\s*DOC_STRING\s*$/) |
|
96 { |
|
97 die "duplicate DOC_STRING" if ($have_doc_string); |
|
98 &parse_doc_string; |
|
99 $have_doc_string = 1; |
|
100 } |
4044
|
101 else |
|
102 { |
|
103 die "mk-opts.pl: unknown command: $_\n" |
|
104 } |
3998
|
105 } |
|
106 } |
|
107 |
|
108 sub parse_option_block |
|
109 { |
|
110 local ($have_init_body, $have_set_body, $have_set_code); |
|
111 |
|
112 while (<INFILE>) |
|
113 { |
|
114 next if (/^\s*$/); |
|
115 |
|
116 die "missing END_OPTION" if (/^\s*OPTION\s*$/); |
|
117 |
|
118 last if (/^\s*END_OPTION\s*$/); |
|
119 |
|
120 if (/^\s*NAME\s*=\s*"(.*)"\s*$/) |
|
121 { |
|
122 die "duplicate NAME" if ($name[$opt_num] ne ""); |
|
123 $name[$opt_num] = $1; |
|
124 ($opt[$opt_num] = $name[$opt_num]) =~ s/\s+/_/g; |
|
125 $optvar[$opt_num] = "x_$opt[$opt_num]"; |
|
126 $kw_tok[$opt_num] = [ split (/\s+/, $name[$opt_num]) ]; |
|
127 $n_toks[$opt_num] = @{$kw_tok[$opt_num]}; |
|
128 } |
|
129 elsif (/^\s*TYPE\s*=\s*"(.*)"\s*$/) |
|
130 { |
|
131 die "duplicate TYPE" if ($type[$opt_num] ne ""); |
|
132 $type[$opt_num] = $1; |
|
133 } |
|
134 elsif (/^\s*SET_ARG_TYPE\s*=\s*"(.*)"\s*$/) |
|
135 { |
|
136 die "duplicate SET_ARG_TYPE" if ($set_arg_type[$opt_num] ne ""); |
|
137 $set_arg_type[$opt_num] = $1; |
|
138 } |
|
139 elsif (/^\s*INIT_VALUE\s*=\s*"(.*)"\s*$/) |
|
140 { |
|
141 die "duplicate INIT_VALUE" if ($init_value[$opt_num] ne ""); |
|
142 $init_value[$opt_num] = $1; |
|
143 } |
|
144 elsif (/^\s*SET_EXPR\s*=\s*"(.*)"\s*$/) |
|
145 { |
|
146 die "duplicate SET_EXPR" if ($set_expr[$opt_num] ne ""); |
|
147 $set_expr[$opt_num] = $1; |
|
148 } |
|
149 elsif (/^\s*INIT_BODY\s*$/) |
|
150 { |
|
151 die "duplicate INIT_BODY" if ($have_init_body); |
|
152 &parse_init_body; |
|
153 $have_init_body = 1; |
|
154 } |
|
155 elsif (/^\s*SET_BODY\s*$/) |
|
156 { |
|
157 die "duplicate SET_BODY" if ($have_set_body); |
|
158 &parse_set_body; |
|
159 $have_set_body = 1; |
|
160 } |
|
161 elsif (/^\s*SET_CODE\s*$/) |
|
162 { |
|
163 die "duplicate SET_CODE" if ($have_set_code); |
|
164 &parse_set_code; |
|
165 $have_set_code = 1; |
|
166 } |
|
167 } |
|
168 |
|
169 if ($set_arg_type[$opt_num] eq "") |
|
170 { |
|
171 $set_arg_type[$opt_num] = $type[$opt_num] |
|
172 } |
|
173 else |
|
174 { |
|
175 $set_arg_type[$opt_num] |
|
176 = &substopt ($set_arg_type[$opt_num], $optvar[$opt_num], |
|
177 $opt[$opt_num], $type[$opt_num]); |
|
178 } |
|
179 |
|
180 $opt_num++; |
|
181 } |
|
182 |
|
183 sub process_data |
|
184 { |
|
185 $max_tokens = &max (@n_toks); |
|
186 |
|
187 &get_min_match_len_info ($max_tokens); |
|
188 |
|
189 $fcn_name = lc ($CLASS) if ($fcn_name eq ""); |
|
190 |
|
191 $opt_fcn_name = "${fcn_name}_options" if ($opt_fcn_name eq ""); |
|
192 |
|
193 $static_object_name = "${fcn_name}_opts"; |
|
194 |
|
195 if ($doc_string eq "") |
|
196 { |
|
197 $doc_string = "When called with two arguments, this function\\n\\ |
|
198 allows you set options parameters for the function \@code{$fcn_name}.\\n\\ |
|
199 Given one argument, \@code{$opt_fcn_name} returns the value of the\\n\\ |
|
200 corresponding option. If no arguments are supplied, the names of all\\n\\ |
|
201 the available options and their current values are displayed.\\n\\\n"; |
|
202 } |
|
203 } |
|
204 |
|
205 sub get_min_match_len_info |
|
206 { |
|
207 local ($max_tokens) = @_; |
|
208 |
|
209 local ($i, $j, $k); |
|
210 |
|
211 for ($i = 0; $i < $opt_num; $i++) |
|
212 { |
|
213 for ($j = 0; $j < $max_tokens; $j++) |
|
214 { |
|
215 $min_tok_len_to_match[$i][$j] = 0; |
|
216 } |
|
217 |
|
218 $min_toks_to_match[$i] = 1; |
|
219 |
|
220 L1: for ($k = 0; $k < $opt_num; $k++) |
|
221 { |
|
222 local ($duplicate) = 1; |
|
223 |
|
224 if ($i != $k) |
|
225 { |
|
226 L2: for ($j = 0; $j < $max_tokens; $j++) |
|
227 { |
|
228 if ($j < $n_toks[$i]) |
|
229 { |
|
230 if ($kw_tok[$i][$j] eq $kw_tok[$k][$j]) |
|
231 { |
|
232 if ($min_tok_len_to_match[$i][$j] == 0) |
|
233 { |
|
234 $min_tok_len_to_match[$i][$j] = 1; |
|
235 } |
|
236 |
|
237 $min_toks_to_match[$i]++; |
|
238 } |
|
239 else |
|
240 { |
|
241 $duplicate = 0; |
|
242 |
|
243 if ($min_tok_len_to_match[$i][$j] == 0) |
|
244 { |
|
245 $min_tok_len_to_match[$i][$j] = 1; |
|
246 } |
|
247 |
|
248 local (@s) = split (//, $kw_tok[$i][$j]); |
|
249 local (@t) = split (//, $kw_tok[$k][$j]); |
|
250 |
|
251 local ($n, $ii); |
|
252 $n = scalar (@s); |
|
253 $n = scalar (@t) if (@t < $n); |
|
254 |
|
255 for ($ii = 0; $ii < $n; $ii++) |
|
256 { |
|
257 if ("$s[$ii]" eq "$t[$ii]") |
|
258 { |
|
259 if ($ii + 2 > $min_tok_len_to_match[$i][$j]) |
|
260 { |
|
261 $min_tok_len_to_match[$i][$j]++; |
|
262 } |
|
263 } |
|
264 else |
|
265 { |
|
266 last L2; |
|
267 } |
|
268 } |
|
269 |
|
270 last L1; |
|
271 } |
|
272 } |
|
273 else |
|
274 { |
|
275 die "ambiguous options \"$name[$i]\" and \"$name[$k]\"" if ($duplicate); |
|
276 } |
|
277 } |
|
278 } |
|
279 } |
|
280 } |
|
281 } |
|
282 |
|
283 sub parse_doc_string |
|
284 { |
|
285 while (<INFILE>) |
|
286 { |
|
287 last if (/^\s*END_DOC_STRING\s*$/); |
|
288 |
|
289 $doc_string .= $_; |
|
290 } |
|
291 |
|
292 $doc_string =~ s/\n/\\n\\\n/g; |
|
293 } |
|
294 |
|
295 sub parse_init_body |
|
296 { |
|
297 while (<INFILE>) |
|
298 { |
|
299 last if (/^\s*END_INIT_BODY\s*$/); |
|
300 |
|
301 $init_body[$opt_num] .= $_; |
|
302 } |
|
303 } |
|
304 |
|
305 sub parse_set_body |
|
306 { |
|
307 while (<INFILE>) |
|
308 { |
|
309 last if (/^\s*END_SET_BODY\s*$/); |
|
310 |
|
311 $set_body[$opt_num] .= $_; |
|
312 } |
|
313 } |
|
314 |
|
315 sub parse_set_code |
|
316 { |
|
317 while (<INFILE>) |
|
318 { |
|
319 last if (/^\s*END_SET_CODE\s*$/); |
|
320 |
|
321 $set_code[$opt_num] .= $_; |
|
322 } |
|
323 } |
|
324 |
|
325 sub emit_opt_class_header |
|
326 { |
|
327 local ($i, $s); |
|
328 |
|
329 print "// DO NOT EDIT! |
|
330 // Generated automatically from $INFILE. |
|
331 |
|
332 #if !defined (octave_${class_name}_h) |
|
333 #define octave_${class_name}_h 1 |
|
334 |
|
335 #include <cfloat> |
|
336 #include <cmath> |
|
337 |
4044
|
338 ${include} |
|
339 |
3998
|
340 class |
|
341 ${class_name} |
|
342 { |
|
343 public: |
|
344 |
|
345 ${class_name} (void) { init (); } |
|
346 |
|
347 ${class_name} (const ${class_name}& opt) { copy (opt); } |
|
348 |
|
349 ${class_name}& operator = (const ${class_name}& opt) |
|
350 { |
|
351 if (this != &opt) |
|
352 copy (opt); |
|
353 |
|
354 return *this; |
|
355 } |
|
356 |
|
357 ~${class_name} (void) { }\n"; |
|
358 |
|
359 print "\n void init (void)\n {\n"; |
|
360 |
|
361 for ($i = 0; $i < $opt_num; $i++) |
|
362 { |
|
363 if ($init_value[$i]) |
|
364 { |
|
365 print " $optvar[$i] = $init_value[$i];\n"; |
|
366 } |
|
367 elsif ($init_body[$i]) |
|
368 { |
|
369 $s = &substopt ($init_body[$i], $optvar[$i], $opt[$i], $type[$i]); |
|
370 chop ($s); |
|
371 $s =~ s/^\s*/ /g; |
|
372 $s =~ s/\n\s*/\n /g; |
|
373 print "$s\n"; |
|
374 } |
|
375 } |
|
376 |
|
377 print " }\n"; |
|
378 |
|
379 print "\n void copy (const ${class_name}& opt)\n {\n"; |
|
380 |
|
381 for ($i = 0; $i < $opt_num; $i++) |
|
382 { |
|
383 print " $optvar[$i] = opt.$optvar[$i];\n"; |
|
384 } |
|
385 |
|
386 print " }\n"; |
|
387 |
|
388 print "\n void set_default_options (void) { init (); }\n"; |
|
389 |
|
390 for ($i = 0; $i < $opt_num; $i++) |
|
391 { |
|
392 if ($set_expr[$i]) |
|
393 { |
|
394 &emit_set_decl ($i); |
|
395 |
|
396 print "\n { $optvar[$i] = $set_expr[$i]; }\n"; |
|
397 } |
|
398 elsif ($set_body[$i]) |
|
399 { |
|
400 &emit_set_decl ($i); |
|
401 |
|
402 $s = &substopt ($set_body[$i], $optvar[$i], $opt[$i], $type[$i]); |
|
403 chop ($s); |
|
404 $s =~ s/^/ /g; |
|
405 $s =~ s/\n/\n /g; |
|
406 print "\n {\n$s\n }\n"; |
|
407 } |
|
408 elsif ($set_code[$i]) |
|
409 { |
|
410 $s = &substopt ($set_code[$i], $optvar[$i], $opt[$i], $type[$i]); |
|
411 chop ($s); |
|
412 $s =~ s/^ //g; |
|
413 $s =~ s/\n /\n/g; |
|
414 print "\n$s\n"; |
|
415 } |
|
416 } |
|
417 |
|
418 for ($i = 0; $i < $opt_num; $i++) |
|
419 { |
|
420 print " $type[$i] $opt[$i] (void) const\n { return $optvar[$i]; }\n\n"; |
|
421 } |
|
422 |
|
423 print "private:\n\n"; |
|
424 |
|
425 for ($i = 0; $i < $opt_num; $i++) |
|
426 { |
|
427 print " $type[$i] $optvar[$i];\n"; |
|
428 } |
|
429 |
|
430 print "};\n\n#endif\n"; |
|
431 } |
|
432 |
|
433 sub emit_set_decl |
|
434 { |
|
435 local ($i) = @_; |
|
436 |
|
437 print " |
|
438 void set_$opt[$i] ($set_arg_type[$i] val)"; |
|
439 } |
|
440 |
|
441 sub emit_opt_handler_fcns |
|
442 { |
|
443 local ($i); |
4044
|
444 my $header = $INFILE; |
|
445 $header =~ s/[.]\w*$/.h/; # replace .in with .h |
|
446 $header =~ s|^.*/([^/]*)$|$1|; # strip directory part |
3998
|
447 |
|
448 print "// DO NOT EDIT!\n// Generated automatically from $INFILE.\n\n"; |
|
449 |
|
450 print "#ifdef HAVE_CONFIG_H |
|
451 #include <config.h> |
|
452 #endif |
|
453 |
|
454 #include <iomanip> |
|
455 #include <iostream> |
|
456 |
4044
|
457 #include \"$header\" |
|
458 |
3998
|
459 #include \"defun-dld.h\" |
|
460 #include \"pr-output.h\" |
|
461 |
4044
|
462 #include \"oct-obj.h\" |
|
463 #include \"utils.h\" |
|
464 #include \"pager.h\" |
|
465 |
3998
|
466 static ${class_name} ${static_object_name};\n\n"; |
|
467 |
|
468 &emit_struct_decl; |
|
469 |
|
470 &emit_struct_def; |
|
471 |
|
472 &emit_print_function; |
|
473 |
|
474 &emit_set_functions; |
|
475 |
|
476 &emit_show_function; |
|
477 |
|
478 &emit_options_function; |
|
479 } |
|
480 |
|
481 sub emit_struct_decl |
|
482 { |
|
483 local ($i); |
|
484 |
|
485 print "#define MAX_TOKENS $max_tokens\n\n"; |
|
486 |
|
487 print "struct ${struct_name}\n{\n"; |
|
488 |
|
489 print " const char *keyword;\n"; |
|
490 print " const char *kw_tok[MAX_TOKENS + 1];\n"; |
|
491 print " int min_len[MAX_TOKENS + 1];\n"; |
|
492 print " int min_toks_to_match;\n"; |
|
493 |
|
494 print "};\n\n"; |
|
495 } |
|
496 |
|
497 sub emit_struct_def |
|
498 { |
|
499 local ($i); |
|
500 |
|
501 print "#define NUM_OPTIONS $opt_num\n\n"; |
|
502 |
|
503 print "static ${struct_name} ${static_table_name} [] =\n{\n"; |
|
504 |
|
505 for ($i = 0; $i < $opt_num; $i++) |
|
506 { |
|
507 &emit_option_table_entry ($i, 0); |
|
508 |
|
509 if ($i < $opt_num - 1) |
|
510 { |
|
511 print "\n"; |
|
512 } |
|
513 } |
|
514 |
|
515 print "};\n\n"; |
|
516 } |
|
517 |
|
518 sub emit_option_table_entry |
|
519 { |
|
520 local ($i, $empty) = @_; |
|
521 |
|
522 local ($k); |
|
523 |
|
524 if ($empty) |
|
525 { |
|
526 print " { 0,\n"; |
|
527 } |
|
528 else |
|
529 { |
|
530 print " { \"$name[$i]\",\n"; |
|
531 } |
|
532 |
|
533 local ($n) = scalar $#{$kw_tok[$i]}; |
|
534 print " {"; |
|
535 for $k (0 .. $max_tokens) |
|
536 { |
|
537 if ($empty || $k > $n) |
|
538 { |
|
539 print " 0,"; |
|
540 } |
|
541 else |
|
542 { |
|
543 print " \"$kw_tok[$i][$k]\","; |
|
544 } |
|
545 } |
|
546 print " },\n"; |
|
547 |
|
548 print " {"; |
|
549 for $k (0 .. $max_tokens) |
|
550 { |
|
551 if ($empty || $k > $n) |
|
552 { |
|
553 print " 0,"; |
|
554 } |
|
555 else |
|
556 { |
|
557 print " $min_tok_len_to_match[$i][$k],"; |
|
558 } |
|
559 } |
|
560 print " }, $min_toks_to_match[$i], "; |
|
561 |
|
562 print "},\n"; |
|
563 } |
|
564 |
|
565 sub emit_print_function |
|
566 { |
|
567 local ($i); |
|
568 |
4047
|
569 ## XXX FIXME XXX -- determine the width of the table automatically. |
|
570 |
3998
|
571 print "static void |
|
572 print_${class_name} (std::ostream& os) |
|
573 { |
|
574 print_usage (\"$opt_fcn_name\", 1); |
|
575 |
|
576 os << \"\\n\" |
|
577 << \"Options for $CLASS include:\\n\\n\" |
4047
|
578 << \" keyword value\\n\" |
|
579 << \" ------- -----\\n\"; |
3998
|
580 |
|
581 $struct_name *list = $static_table_name;\n\n"; |
|
582 |
|
583 for ($i = 0; $i < $opt_num; $i++) |
|
584 { |
|
585 print " {\n os << \" \" |
4047
|
586 << std::setiosflags (std::ios::left) << std::setw (50) |
3998
|
587 << list[$i].keyword |
|
588 << std::resetiosflags (std::ios::left) |
|
589 << \" \";\n\n"; |
|
590 |
|
591 if ($type[$i] eq "double") |
|
592 { |
|
593 print " double val = $static_object_name.$opt[$i] ();\n\n"; |
|
594 print " os << val << \"\\n\";\n"; |
|
595 } |
|
596 elsif ($type[$i] eq "int") |
|
597 { |
|
598 print " int val = $static_object_name.$opt[$i] ();\n\n"; |
|
599 print " os << val << \"\\n\";\n"; |
|
600 } |
|
601 elsif ($type[$i] eq "std::string") |
|
602 { |
|
603 print " os << $static_object_name.$opt[$i] () << \"\\n\";\n"; |
|
604 } |
4044
|
605 elsif ($type[$i] eq "Array<int>") |
|
606 { |
|
607 print " Array<int> val = $static_object_name.$opt[$i] ();\n\n"; |
|
608 print " if (val.length () == 1) |
|
609 { |
|
610 os << val(0) << \"\\n\"; |
|
611 } |
|
612 else |
|
613 { |
|
614 os << \"\\n\\n\"; |
|
615 int len = val.length (); |
|
616 Matrix tmp (len, 1); |
|
617 for (int i = 0; i < len; i++) |
|
618 tmp(i,0) = val(i); |
|
619 octave_print_internal (os, tmp, false, 2); |
|
620 os << \"\\n\\n\"; |
|
621 }\n"; |
|
622 } |
3998
|
623 elsif ($type[$i] eq "Array<double>") |
|
624 { |
|
625 print " Array<double> val = $static_object_name.$opt[$i] ();\n\n"; |
|
626 print " if (val.length () == 1) |
|
627 { |
|
628 os << val(0) << \"\\n\"; |
|
629 } |
|
630 else |
|
631 { |
|
632 os << \"\\n\\n\"; |
|
633 Matrix tmp = Matrix (ColumnVector (val)); |
|
634 octave_print_internal (os, tmp, false, 2); |
|
635 os << \"\\n\\n\"; |
|
636 }\n"; |
|
637 } |
|
638 else |
|
639 { |
|
640 die ("unknown type $type[$i]"); |
|
641 } |
|
642 |
|
643 print " }\n\n"; |
|
644 } |
|
645 |
|
646 print " os << \"\\n\";\n}\n\n"; |
|
647 } |
|
648 |
|
649 sub emit_set_functions |
|
650 { |
|
651 print "static void |
|
652 set_${class_name} (const std::string& keyword, const octave_value& val) |
|
653 { |
|
654 $struct_name *list = $static_table_name;\n\n"; |
|
655 |
|
656 $iftok = "if"; |
|
657 |
|
658 for ($i = 0; $i < $opt_num; $i++) |
|
659 { |
|
660 $iftok = "else if" if ($i > 0); |
|
661 |
|
662 print " $iftok (keyword_almost_match (list[$i].kw_tok, list[$i].min_len, |
|
663 keyword, list[$i].min_toks_to_match, MAX_TOKENS)) |
|
664 {\n"; |
|
665 |
|
666 if ($type[$i] eq "double") |
|
667 { |
|
668 print " double tmp = val.double_value ();\n\n"; |
|
669 print " if (! error_state) |
|
670 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
671 } |
|
672 elsif ($type[$i] eq "int") |
|
673 { |
|
674 print " int tmp = val.int_value ();\n\n"; |
|
675 print " if (! error_state) |
|
676 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
677 } |
|
678 elsif ($type[$i] eq "std::string") |
|
679 { |
|
680 print " std::string tmp = val.string_value ();\n\n"; |
|
681 print " if (! error_state) |
|
682 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
683 } |
4044
|
684 elsif ($type[$i] eq "Array<int>") |
|
685 { |
|
686 print " Array<int> tmp = val.int_vector_value ();\n\n"; |
|
687 print " if (! error_state) |
|
688 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
689 } |
3998
|
690 elsif ($type[$i] eq "Array<double>") |
|
691 { |
|
692 print " Array<double> tmp = val.vector_value ();\n\n"; |
|
693 print " if (! error_state) |
|
694 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
695 } |
|
696 else |
|
697 { |
|
698 die ("unknown type $type[$i]"); |
|
699 } |
|
700 |
|
701 print " }\n"; |
|
702 } |
|
703 |
|
704 print " else |
|
705 { |
|
706 warning (\"$opt_fcn_name: no match for `%s'\", keyword.c_str ()); |
|
707 } |
|
708 }\n\n"; |
|
709 } |
|
710 |
|
711 sub emit_show_function |
|
712 { |
|
713 local ($i, $iftok); |
|
714 |
|
715 print "static octave_value_list |
|
716 show_${class_name} (const std::string& keyword) |
|
717 { |
|
718 octave_value retval; |
|
719 |
|
720 $struct_name *list = $static_table_name;\n\n"; |
|
721 |
|
722 $iftok = "if"; |
|
723 |
|
724 for ($i = 0; $i < $opt_num; $i++) |
|
725 { |
|
726 $iftok = "else if" if ($i > 0); |
|
727 |
|
728 print " $iftok (keyword_almost_match (list[$i].kw_tok, list[$i].min_len, |
|
729 keyword, list[$i].min_toks_to_match, MAX_TOKENS)) |
|
730 {\n"; |
|
731 |
|
732 if ($type[$i] eq "double") |
|
733 { |
|
734 print " double val = $static_object_name.$opt[$i] ();\n\n"; |
|
735 print " retval = val;\n"; |
|
736 } |
|
737 elsif ($type[$i] eq "int") |
|
738 { |
|
739 print " int val = $static_object_name.$opt[$i] ();\n\n"; |
|
740 print " retval = static_cast<double> (val);\n"; |
|
741 } |
|
742 elsif ($type[$i] eq "std::string") |
|
743 { |
|
744 print " retval = $static_object_name.$opt[$i] ();\n"; |
|
745 } |
4044
|
746 elsif ($type[$i] eq "Array<int>") |
|
747 { |
|
748 print " Array<int> val = $static_object_name.$opt[$i] ();\n\n"; |
|
749 print " if (val.length () == 1) |
|
750 { |
|
751 retval = static_cast<double> (val(0)); |
|
752 } |
|
753 else |
|
754 { |
|
755 int len = val.length (); |
|
756 ColumnVector tmp (len); |
|
757 for (int i = 0; i < len; i++) |
|
758 tmp(i) = val(i); |
|
759 retval = tmp; |
|
760 }\n"; |
|
761 } |
3998
|
762 elsif ($type[$i] eq "Array<double>") |
|
763 { |
|
764 print " Array<double> val = $static_object_name.$opt[$i] ();\n\n"; |
|
765 print " if (val.length () == 1) |
|
766 { |
|
767 retval = val(0); |
|
768 } |
|
769 else |
|
770 { |
|
771 retval = ColumnVector (val); |
|
772 }\n"; |
|
773 } |
|
774 else |
|
775 { |
|
776 die ("unknown type $type[$i]"); |
|
777 } |
|
778 |
|
779 print " }\n"; |
|
780 } |
|
781 |
|
782 print " else |
|
783 { |
|
784 warning (\"$opt_fcn_name: no match for `%s'\", keyword.c_str ()); |
|
785 } |
|
786 |
|
787 return retval;\n}\n\n"; |
|
788 } |
|
789 |
|
790 sub emit_options_function |
|
791 { |
|
792 print "DEFUN_DLD ($opt_fcn_name, args, , |
|
793 \"-*- texinfo -*-\\n\\ |
|
794 \@deftypefn {Loadable Function} {} $opt_fcn_name (\@var{opt}, \@var{val})\\n\\ |
|
795 $doc_string\@end deftypefn\") |
|
796 { |
|
797 octave_value_list retval; |
|
798 |
|
799 int nargin = args.length (); |
|
800 |
|
801 if (nargin == 0) |
|
802 { |
|
803 print_${class_name} (octave_stdout); |
|
804 } |
|
805 else if (nargin == 1 || nargin == 2) |
|
806 { |
|
807 std::string keyword = args(0).string_value (); |
|
808 |
|
809 if (! error_state) |
|
810 { |
|
811 if (nargin == 1) |
|
812 retval = show_${class_name} (keyword); |
|
813 else |
|
814 set_${class_name} (keyword, args(1)); |
|
815 } |
|
816 else |
|
817 error (\"$opt_fcn_name: expecting keyword as first argument\"); |
|
818 } |
|
819 else |
|
820 print_usage (\"$opt_fcn_name\"); |
|
821 |
|
822 return retval; |
4035
|
823 }\n"; |
3998
|
824 } |
|
825 |
|
826 sub emit_options_debug |
|
827 { |
|
828 print "CLASS = \"$class\"\n"; |
|
829 |
|
830 for ($i = 0; $i < $opt_num; $i++) |
|
831 { |
|
832 $NAME = $name[$i]; |
|
833 ($OPT = $NAME) =~ s/\s+/_/g; |
|
834 $OPTVAR = "x_$OPT"; |
|
835 $TYPE = $type[$i]; |
|
836 print "\n"; |
|
837 print "OPTION\n"; |
|
838 print " NAME = \"$NAME\"\n"; |
|
839 print " TYPE = \"$TYPE\"\n"; |
|
840 if ($set_arg_type[$i]) |
|
841 { |
|
842 print eval ("\" SET_ARG_TYPE = \\\"$set_arg_type[$i]\\\"\"") . "\n"; |
|
843 } |
|
844 if ($init_value[$i]) |
|
845 { |
|
846 print " INIT_VALUE = \"$init_value[$i]\"\n"; |
|
847 } |
|
848 if ($init_body[$i]) |
|
849 { |
|
850 print " INIT_BODY\n"; |
|
851 print &substopt ($init_body[$i]); |
|
852 print " END_INIT_BODY\n"; |
|
853 } |
|
854 if ($set_expr[$i]) |
|
855 { |
|
856 print " SET_EXPR = \"$set_expr[$i]\"\n"; |
|
857 } |
|
858 if ($set_body[$i]) |
|
859 { |
|
860 print " SET_BODY\n"; |
|
861 print &substopt ($set_body[$i]); |
|
862 print " END_SET_BODY\n"; |
|
863 } |
|
864 if ($set_code[$i]) |
|
865 { |
|
866 print " SET_CODE\n"; |
|
867 print &substopt ($set_code[$i]); |
|
868 print " END_SET_CODE\n"; |
|
869 } |
|
870 print "END_OPTION\n"; |
|
871 } |
|
872 } |
|
873 |
|
874 sub substopt |
|
875 { |
|
876 local ($string, $OPTVAR, $OPT, $TYPE) = @_; |
|
877 |
|
878 $string =~ s/\$OPTVAR/$OPTVAR/g; |
|
879 $string =~ s/\$OPT/$OPT/g; |
|
880 $string =~ s/\$TYPE/$TYPE/g; |
|
881 |
|
882 $string; |
|
883 } |
|
884 |
|
885 sub print_assoc_array |
|
886 { |
|
887 local (%t) = @_; |
|
888 |
|
889 local ($k); |
|
890 |
|
891 foreach $k (keys (%t)) |
|
892 { |
|
893 print "$k: $t{$k}\n"; |
|
894 } |
|
895 } |
|
896 |
|
897 sub max |
|
898 { |
|
899 local ($max) = shift; |
|
900 |
|
901 foreach (@_) |
|
902 { |
|
903 $max = $_ if $max < $_; |
|
904 } |
|
905 |
|
906 $max; |
|
907 } |