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, const char *aname, const 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(const void *key1, const void *key2)
230 return(strcmp(static_cast<const char *>(key1), static_cast<const char *>(key2)));
234 cl_options::new_option(class cl_option *opt)
240 cl_options::del_option(class cl_option *opt)
248 cl_options::get_option(const char *the_name)
252 if (search(the_name, idx))
253 return((class cl_option *)(at(idx)));
258 cl_options::get_option(const char *the_name, class cl_base *creator)
263 if (!search(the_name, idx))
268 o= (class cl_option *)(at(idx));
269 while (compare(the_name, key_of(o)) == 0 &&
273 o= (class cl_option *)(at(idx));
275 if (compare(the_name, key_of(o)) != 0)
278 o= (class cl_option *)(at(idx));
279 while (compare(the_name, key_of(o)) == 0 &&
280 o->get_creator() != creator &&
284 o= (class cl_option *)(at(idx));
285 if (compare(the_name, key_of(o)) == 0 &&
286 o->get_creator() == creator)
289 if (compare(the_name, key_of(o)) == 0 &&
290 o->get_creator() == creator)
296 cl_options::get_option(const char *the_name, char *creator)
301 if (!search(the_name, idx))
306 o= (class cl_option *)(at(idx));
307 while (compare(the_name, key_of(o)) == 0 &&
311 o= (class cl_option *)(at(idx));
313 if (compare(the_name, key_of(o)) != 0)
316 o= (class cl_option *)(at(idx));
317 while (compare(the_name, key_of(o)) == 0 &&
318 strcmp(object_name(o->get_creator()), creator) != 0 &&
322 o= (class cl_option *)(at(idx));
323 if (compare(the_name, key_of(o)) == 0 &&
324 strcmp(object_name(o->get_creator()), creator) == 0)
327 if (compare(the_name, key_of(o)) == 0 &&
328 strcmp(object_name(o->get_creator()), creator) == 0)
334 cl_options::get_option(int idx)
338 return((class cl_option *)(at(idx)));
342 cl_options::nuof_options(char *the_name)
346 for (i= 0; i < count; i++)
348 class cl_option *o= (class cl_option *)(at(i));
349 if (strcmp(the_name, o->get_name()) == 0)
356 cl_options::nuof_options(char *the_name, char *creator)
360 for (i= 0; i < count; i++)
362 class cl_option *o= (class cl_option *)(at(i));
363 if (strcmp(the_name, o->get_name()) == 0 &&
364 strcmp(creator, object_name(o->get_creator())) == 0)
371 cl_options::set_value(const char *the_name, cl_base *creator, bool value)
373 class cl_option *o= get_option(the_name, creator);
381 cl_options::set_value(const char *the_name, cl_base *creator, char *value)
383 class cl_option *o= get_option(the_name, creator);
391 cl_options::set_value(const char *the_name, cl_base *creator, void *value)
393 class cl_option *o= get_option(the_name, creator);
401 cl_options::set_value(const char *the_name, cl_base *creator, long value)
403 class cl_option *o= get_option(the_name, creator);
411 cl_options::set_value(const char *the_name, cl_base *creator, double value)
413 class cl_option *o= get_option(the_name, creator);
422 * Reference to an option
425 cl_optref::cl_optref(class cl_base *the_owner)
431 cl_optref::cl_optref(class cl_base *the_owner, class cl_option *new_option)
435 application->options->new_option(option);
438 option->new_reference(this);
439 set_name(option->get_name());
443 cl_optref::~cl_optref(void)
447 option->del_reference(this);
448 if (option->get_creator() == owner)
449 application->options->del_option(option);
454 cl_optref::create(class cl_base *creator,
455 enum option_type type,
456 const char *the_name, const char *help)
459 option->del_reference(this);
466 option= new cl_number_option(creator, the_name, help);
469 option= new cl_float_option(creator, the_name, help);
472 option= new cl_bool_option(creator, the_name, help);
475 option= new cl_string_option(creator, the_name, help);
478 option= new cl_pointer_option(creator, the_name, help);
486 application->options->new_option(option);
487 option->new_reference(this);
488 set_name(option->get_name());
495 cl_optref::default_option(const char *the_name)
497 class cl_option *o= application->options->get_option(the_name, application);
502 //memcpy(option->get_value(), o->get_value(), sizeof(union option_value));
504 option->inform_users();
507 fprintf(stderr,"can not set opt from default, option=%p, o=%p\n",option,o);*/
515 option->del_reference(this);
516 option->new_reference(this);
522 cl_optref::use(const char *the_name)
525 option->del_reference(this);
526 option= application->options->get_option(the_name);
529 option->new_reference(this);
530 set_name(option->get_name());
536 cl_optref::option_removing(void)
542 cl_optref::get_value(bool)
546 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
547 "bool option: %s\n", object_name(owner), get_name());
553 option->get_value(&v);
559 cl_optref::get_value(const char *)
563 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
564 "string option: %s\n", object_name(owner), get_name());
570 option->get_value(&s);
576 cl_optref::get_value(void *)
580 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
581 "pointer option: %s\n", object_name(owner), get_name());
587 option->get_value(&p);
593 cl_optref::get_value(long)
597 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
598 "number option: %s\n", object_name(owner), get_name());
604 option->get_value(&l);
610 cl_optref::get_value(double)
614 fprintf(stderr, "Warning: \"%s\" is sdereferencing a non-existent "
615 "float option: %s\n", object_name(owner), get_name());
621 option->get_value(&d);
628 * BOOL type of option
629 *____________________________________________________________________________
633 cl_bool_option::cl_bool_option(class cl_base *the_creator,
634 const char *aname, const char *Ihelp):
635 cl_option(the_creator, aname, Ihelp)
639 cl_bool_option::print(class cl_console_base *con)
641 if (/**(bool *)option*/value.bval)
642 con->dd_printf("TRUE");
644 con->dd_printf("FALSE");
648 cl_bool_option::set_value(char *s)
658 /**(bool *)option=*/ value.bval= DD_TRUE;
660 /**(bool *)option=*/ value.bval= DD_FALSE;
667 * STRING type of option
668 *____________________________________________________________________________
672 cl_string_option::cl_string_option(class cl_base *the_creator,
673 const char *aname, const char *Ihelp):
674 cl_option(the_creator, aname, Ihelp)
678 cl_string_option::operator=(class cl_option &o)
680 //fprintf(stderr,"string=otheropt%p\"%s\"\nold=%p\"%s\"\n",&o,object_name(&o),value.sval,value.sval);
681 set_value((o.get_value())->sval);
682 //fprintf(stderr,"new=%p\"%s\"\n",value.sval,value.sval);
687 cl_string_option::print(class cl_console_base *con)
689 if (/**(bool *)option*/value.sval)
690 con->dd_printf("\"%s\"", value.sval);
692 con->dd_printf("(null)");
697 * PONITER type of option
698 *____________________________________________________________________________
702 cl_pointer_option::cl_pointer_option(class cl_base *the_creator,
703 const char *aname, const char *Ihelp):
704 cl_option(the_creator, aname, Ihelp)
708 cl_pointer_option::operator=(class cl_option &o)
710 set_value((o.get_value())->pval);
715 cl_pointer_option::print(class cl_console_base *con)
718 con->dd_printf("\"%p\"", value.pval);
720 con->dd_printf("(null)");
728 cl_cons_debug_opt::cl_cons_debug_opt(class cl_app *the_app,
731 cl_option(0, Iid, Ihelp)
737 cl_cons_debug_opt::print(class cl_console_base *con)
739 if (con->flags & CONS_DEBUG)
740 con->dd_printf("TRUE");
742 con->dd_printf("FALSE");
746 cl_cons_debug_opt::get_value(bool *val)
749 *val= app->get_commander()->actual_console?
750 (app->get_commander()->actual_console->flags & CONS_DEBUG):0;
754 cl_cons_debug_opt::set_value(bool opt)
756 if (app->get_commander()->actual_console)
759 app->get_commander()->actual_console->flags|= CONS_DEBUG;
761 app->get_commander()->actual_console->flags&= ~CONS_DEBUG;
767 cl_cons_debug_opt::set_value(char *s)
772 app->get_commander()->actual_console)
786 * NUMBER type of option
787 *____________________________________________________________________________
791 cl_number_option::cl_number_option(class cl_base *the_creator,
792 const char *aname, const char *Ihelp):
793 cl_option(the_creator, aname, Ihelp)
797 cl_number_option::print(class cl_console_base *con)
799 con->dd_printf("%ld", value.ival);
803 cl_number_option::set_value(char *s)
806 value.ival= strtol(s, NIL, 0);
812 * FLOAT type of option
813 *____________________________________________________________________________
817 cl_float_option::cl_float_option(class cl_base *the_creator,
818 const char *aname, const char *Ihelp):
819 cl_option(the_creator, aname, Ihelp)
823 cl_float_option::print(class cl_console_base *con)
825 con->dd_printf("%.3f", value.fval);
829 cl_float_option::set_value(char *s)
832 value.fval= strtod(s, NIL);
837 /* End of option.cc */