2 * Simulator of microcontrollers (option.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
11 This file is part of microcontroller simulator: ucsim.
13 UCSIM is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 UCSIM is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with UCSIM; see the file COPYING. If not, write to the Free
25 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
47 /*struct id_element option_type_names[]= {
49 { integer_opt , "integer" },
50 { float_opt , "float" },
51 { bool_opt , "boolean" },
52 { string_opt , "string" },
53 { pointer_opt , "pointer" },
58 * Base class for option's objects
59 *____________________________________________________________________________
63 cl_option::cl_option(class cl_base *the_creator, char *aname, char *Ihelp):
69 char *s= (char*)malloc(strlen(aname)+100);
70 sprintf(s, "users of option \"%s\"", aname);
71 users= new cl_list(2, 2, s);
73 memset(&value, 0, sizeof(value));
78 cl_option::operator=(class cl_option &o)
80 //memcpy(&value, &(o.value), sizeof(union option_value));
81 //fprintf(stderr,"opt%p\"%s\"=%p\"%s\"\nold=%p\n",this,object_name(this),&o,object_name(&o),value.sval);
83 //fprintf(stderr,"new=%p\n",value.sval);
88 cl_option::~cl_option(void)
96 cl_option::pre_remove(void)
100 for (i= 0; i < users->count; i++)
102 class cl_optref *user= (class cl_optref *)(users->at(i));
103 user->option_removing();
108 cl_option::new_reference(class cl_optref *ref)
114 cl_option::del_reference(class cl_optref *ref)
120 cl_option::inform_users(void)
124 for (i= 0; i < users->count; i++)
126 class cl_optref *user= (class cl_optref *)(users->at(i));
127 //fprintf(stderr,"%p\"%s\" informs user %p\"%s\"\n",this,object_name(this),user,object_name(user));
128 user->option_changed();
134 cl_option::get_value(bool *val)
141 cl_option::set_value(bool opt)
149 cl_option::get_value(char **val)
156 cl_option::set_value(char *opt)
158 //fprintf(stderr,"set_string_value (%s) to %p\"%s\"\n",opt,this,object_name(this));
159 //fprintf(stderr,"old value=%p\"%s\"\n",value.sval,value.sval);
164 value.sval= strdup(opt);
166 value.sval= strdup("");
167 //fprintf(stderr,"new value=%p\"%s\"\n",value.sval,value.sval);
173 cl_option::get_value(void **val)
180 cl_option::set_value(void *opt)
188 cl_option::get_value(long *val)
195 cl_option::set_value(long opt)
203 cl_option::get_value(double *val)
210 cl_option::set_value(double opt)
222 cl_options::key_of(void *item)
224 return(((class cl_base *)item)->get_name());
228 cl_options::compare(void *key1, void *key2)
230 //class cl_option *k1, *k2;
234 k1= /*(class cl_option *)*/(char *)key1;
235 k2= /*(class cl_option *)*/(char *)key2;
236 if ((i= strcmp(k1, k2)) != 0)
242 cl_options::new_option(class cl_option *opt)
248 cl_options::del_option(class cl_option *opt)
256 cl_options::get_option(char *the_name)
260 if (search(the_name, idx))
261 return((class cl_option *)(at(idx)));
266 cl_options::get_option(char *the_name, class cl_base *creator)
271 if (!search(the_name, idx))
276 o= (class cl_option *)(at(idx));
277 while (compare(the_name, key_of(o)) == 0 &&
281 o= (class cl_option *)(at(idx));
283 if (compare(the_name, key_of(o)) != 0)
286 o= (class cl_option *)(at(idx));
287 while (compare(the_name, key_of(o)) == 0 &&
288 o->get_creator() != creator &&
292 o= (class cl_option *)(at(idx));
293 if (compare(the_name, key_of(o)) == 0 &&
294 o->get_creator() == creator)
297 if (compare(the_name, key_of(o)) == 0 &&
298 o->get_creator() == creator)
304 cl_options::get_option(char *the_name, char *creator)
309 if (!search(the_name, idx))
314 o= (class cl_option *)(at(idx));
315 while (compare(the_name, key_of(o)) == 0 &&
319 o= (class cl_option *)(at(idx));
321 if (compare(the_name, key_of(o)) != 0)
324 o= (class cl_option *)(at(idx));
325 while (compare(the_name, key_of(o)) == 0 &&
326 strcmp(object_name(o->get_creator()), creator) != 0 &&
330 o= (class cl_option *)(at(idx));
331 if (compare(the_name, key_of(o)) == 0 &&
332 strcmp(object_name(o->get_creator()), creator) == 0)
335 if (compare(the_name, key_of(o)) == 0 &&
336 strcmp(object_name(o->get_creator()), creator) == 0)
342 cl_options::get_option(int idx)
346 return((class cl_option *)(at(idx)));
350 cl_options::nuof_options(char *the_name)
354 for (i= 0; i < count; i++)
356 class cl_option *o= (class cl_option *)(at(i));
357 if (strcmp(the_name, o->get_name()) == 0)
364 cl_options::nuof_options(char *the_name, char *creator)
368 for (i= 0; i < count; i++)
370 class cl_option *o= (class cl_option *)(at(i));
371 if (strcmp(the_name, o->get_name()) == 0 &&
372 strcmp(creator, object_name(o->get_creator())) == 0)
379 cl_options::set_value(char *the_name, cl_base *creator, bool value)
381 class cl_option *o= get_option(the_name, creator);
389 cl_options::set_value(char *the_name, cl_base *creator, char *value)
391 class cl_option *o= get_option(the_name, creator);
399 cl_options::set_value(char *the_name, cl_base *creator, void *value)
401 class cl_option *o= get_option(the_name, creator);
409 cl_options::set_value(char *the_name, cl_base *creator, long value)
411 class cl_option *o= get_option(the_name, creator);
419 cl_options::set_value(char *the_name, cl_base *creator, double value)
421 class cl_option *o= get_option(the_name, creator);
430 * Reference to an option
433 cl_optref::cl_optref(class cl_base *the_owner)
439 cl_optref::cl_optref(class cl_base *the_owner, class cl_option *new_option)
443 application->options->new_option(option);
446 option->new_reference(this);
447 set_name(option->get_name());
451 cl_optref::~cl_optref(void)
455 option->del_reference(this);
456 if (option->get_creator() == owner)
457 application->options->del_option(option);
462 cl_optref::create(class cl_base *creator,
463 enum option_type type,
464 char *the_name, char *help)
467 option->del_reference(this);
474 option= new cl_number_option(creator, the_name, help);
477 option= new cl_float_option(creator, the_name, help);
480 option= new cl_bool_option(creator, the_name, help);
483 option= new cl_string_option(creator, the_name, help);
486 option= new cl_pointer_option(creator, the_name, help);
494 application->options->new_option(option);
495 option->new_reference(this);
496 set_name(option->get_name());
503 cl_optref::default_option(char *the_name)
505 class cl_option *o= application->options->get_option(the_name, application);
510 //memcpy(option->get_value(), o->get_value(), sizeof(union option_value));
512 option->inform_users();
515 fprintf(stderr,"can not set opt from default, option=%p, o=%p\n",option,o);*/
523 option->del_reference(this);
524 option->new_reference(this);
530 cl_optref::use(char *the_name)
533 option->del_reference(this);
534 option= application->options->get_option(the_name);
537 option->new_reference(this);
538 set_name(option->get_name());
544 cl_optref::option_removing(void)
550 cl_optref::get_value(bool)
554 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
555 "bool option: %s\n", object_name(owner), get_name());
561 option->get_value(&v);
567 cl_optref::get_value(char *)
571 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
572 "string option: %s\n", object_name(owner), get_name());
578 option->get_value(&s);
584 cl_optref::get_value(void *)
588 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
589 "pointer option: %s\n", object_name(owner), get_name());
595 option->get_value(&p);
601 cl_optref::get_value(long)
605 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
606 "number option: %s\n", object_name(owner), get_name());
612 option->get_value(&l);
618 cl_optref::get_value(double)
622 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
623 "float option: %s\n", object_name(owner), get_name());
629 option->get_value(&d);
636 * BOOL type of option
637 *____________________________________________________________________________
641 cl_bool_option::cl_bool_option(class cl_base *the_creator,
642 char *aname, char *Ihelp):
643 cl_option(the_creator, aname, Ihelp)
647 cl_bool_option::print(class cl_console *con)
649 if (/**(bool *)option*/value.bval)
650 con->dd_printf("TRUE");
652 con->dd_printf("FALSE");
656 cl_bool_option::set_value(char *s)
666 /**(bool *)option=*/ value.bval= DD_TRUE;
668 /**(bool *)option=*/ value.bval= DD_FALSE;
675 * STRING type of option
676 *____________________________________________________________________________
680 cl_string_option::cl_string_option(class cl_base *the_creator,
681 char *aname, char *Ihelp):
682 cl_option(the_creator, aname, Ihelp)
686 cl_string_option::operator=(class cl_option &o)
688 //fprintf(stderr,"string=otheropt%p\"%s\"\nold=%p\"%s\"\n",&o,object_name(&o),value.sval,value.sval);
689 set_value((o.get_value())->sval);
690 //fprintf(stderr,"new=%p\"%s\"\n",value.sval,value.sval);
695 cl_string_option::print(class cl_console *con)
697 if (/**(bool *)option*/value.sval)
698 con->dd_printf("\"%s\"", value.sval);
700 con->dd_printf("(null)");
705 * PONITER type of option
706 *____________________________________________________________________________
710 cl_pointer_option::cl_pointer_option(class cl_base *the_creator,
711 char *aname, char *Ihelp):
712 cl_option(the_creator, aname, Ihelp)
716 cl_pointer_option::operator=(class cl_option &o)
718 set_value((o.get_value())->pval);
723 cl_pointer_option::print(class cl_console *con)
726 con->dd_printf("\"%p\"", value.pval);
728 con->dd_printf("(null)");
736 cl_cons_debug_opt::cl_cons_debug_opt(class cl_app *the_app,
739 cl_option(0, Iid, Ihelp)
745 cl_cons_debug_opt::print(class cl_console *con)
747 if (con->flags & CONS_DEBUG)
748 con->dd_printf("TRUE");
750 con->dd_printf("FALSE");
754 cl_cons_debug_opt::get_value(bool *val)
757 *val= app->get_commander()->actual_console?
758 (app->get_commander()->actual_console->flags & CONS_DEBUG):0;
762 cl_cons_debug_opt::set_value(bool opt)
764 if (app->get_commander()->actual_console)
767 app->get_commander()->actual_console->flags|= CONS_DEBUG;
769 app->get_commander()->actual_console->flags&= ~CONS_DEBUG;
775 cl_cons_debug_opt::set_value(char *s)
780 app->get_commander()->actual_console)
794 * NUMBER type of option
795 *____________________________________________________________________________
799 cl_number_option::cl_number_option(class cl_base *the_creator,
800 char *aname, char *Ihelp):
801 cl_option(the_creator, aname, Ihelp)
805 cl_number_option::print(class cl_console *con)
807 con->dd_printf("%ld", value.ival);
811 cl_number_option::set_value(char *s)
814 value.ival= strtol(s, NIL, 0);
820 * FLOAT type of option
821 *____________________________________________________________________________
825 cl_float_option::cl_float_option(class cl_base *the_creator,
826 char *aname, char *Ihelp):
827 cl_option(the_creator, aname, Ihelp)
831 cl_float_option::print(class cl_console *con)
833 con->dd_printf("%.3f", value.fval);
837 cl_float_option::set_value(char *s)
840 value.fval= strtod(s, NIL);
845 /* End of option.cc */