Changeset 5f784ec for variable.c
- Timestamp:
- Sep 30, 2011, 8:12:24 AM (13 years ago)
- Children:
- c0e728a
- Parents:
- 7803326
- git-author:
- Jason Gross <jgross@mit.edu> (07/12/11 18:29:38)
- git-committer:
- Jason Gross <jgross@mit.edu> (09/30/11 08:12:24)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
variable.c
rf271129 r5f784ec 568 568 int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable *variables_to_init) 569 569 { 570 int ret; 570 571 owl_variable *var, *cur; 571 572 for (var = variables_to_init; var->name != NULL; var++) { … … 576 577 cur->summary = g_strdup(var->summary); 577 578 cur->description = g_strdup(var->description); 578 switch (cur->type) { 579 if (cur->type == OWL_VARIABLE_STRING) 580 cur->pval_default = g_strdup(var->pval_default); 581 ret = owl_variable_init_defaults(cur); 582 if (ret != 0) { 583 fprintf(stderr, "owl_variable_setup: invalid variable type\n"); 584 return ret; 585 } 586 owl_dict_insert_element(vd, cur->name, cur, NULL); 587 } 588 return 0; 589 } 590 591 int owl_variable_init_defaults(owl_variable *cur) 592 { 593 switch (cur->type) { 579 594 case OWL_VARIABLE_OTHER: 580 595 cur->set_fn(cur, cur->pval_default); … … 593 608 if (!cur->delete_fn) 594 609 cur->delete_fn = owl_variable_delete_default; 595 cur->pval_default = g_strdup(var->pval_default);596 610 cur->set_fn(cur, cur->pval_default); 597 611 break; … … 629 643 break; 630 644 default: 631 fprintf(stderr, "owl_variable_setup: invalid variable type\n"); 632 return(-2); 633 } 634 owl_dict_insert_element(vd, cur->name, cur, NULL); 645 return -2; 635 646 } 636 647 return 0; … … 642 653 } 643 654 644 CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description )655 CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description, const char *validsettings, int type) 645 656 { 646 657 owl_variable * var = g_new0(owl_variable, 1); … … 648 659 var->summary = g_strdup(summary); 649 660 var->description = g_strdup(description); 661 var->validsettings = validsettings; 662 var->type = type; 650 663 return var; 651 664 } 652 665 653 void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {654 g_free(var->summary);655 var->summary = g_strdup(summary);656 g_free(var->description);657 var->description = g_strdup(desc);658 }659 660 666 void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval) 661 667 { 662 668 owl_variable *old = owl_variable_get_var(vd, name); 663 if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) { 664 owl_variable_update(old, summ, desc); 665 g_free(old->pval_default); 666 old->pval_default = g_strdup(initval); 667 } else { 668 owl_variable * var = owl_variable_newvar(name, summ, desc); 669 var->type = OWL_VARIABLE_STRING; 670 var->validsettings = "<string>"; 671 var->pval_default = g_strdup(initval); 672 var->set_fn = owl_variable_string_set_default; 673 var->set_fromstring_fn = owl_variable_string_set_fromstring_default; 674 var->get_fn = owl_variable_get_default; 675 var->get_tostring_fn = owl_variable_string_get_tostring_default; 676 var->delete_fn = owl_variable_delete_default; 677 var->set_fn(var, initval); 678 owl_variable_dict_add_variable(vd, var); 679 } 669 char *oldval = NULL; 670 if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) 671 oldval = owl_variable_get_tostring(old); 672 owl_variable *var = owl_variable_newvar(name, summ, desc, "<string>", OWL_VARIABLE_STRING); 673 var->pval_default = g_strdup(initval); 674 owl_variable_init_defaults(var); 675 if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) 676 var->set_fn(var, oldval); 677 g_free(oldval); 678 owl_variable_dict_add_variable(vd, var); 680 679 } 681 680 … … 683 682 { 684 683 owl_variable *old = owl_variable_get_var(vd, name); 685 if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) { 686 owl_variable_update(old, summ, desc); 687 old->ival_default = initval; 688 } else { 689 owl_variable * var = owl_variable_newvar(name, summ, desc); 690 var->type = OWL_VARIABLE_INT; 691 var->validsettings = "<int>"; 692 var->ival_default = initval; 693 var->validate_fn = owl_variable_int_validate_default; 694 var->set_fn = owl_variable_int_set_default; 695 var->set_fromstring_fn = owl_variable_int_set_fromstring_default; 696 var->get_fn = owl_variable_get_default; 697 var->get_tostring_fn = owl_variable_int_get_tostring_default; 698 var->delete_fn = owl_variable_delete_default; 699 var->val = g_new(int, 1); 700 var->set_fn(var, &initval); 701 owl_variable_dict_add_variable(vd, var); 702 } 684 int oldval; 685 if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) 686 oldval = owl_variable_get_int(old); 687 owl_variable *var = owl_variable_newvar(name, summ, desc, "<int>", OWL_VARIABLE_INT); 688 var->ival_default = initval; 689 owl_variable_init_defaults(var); 690 if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) 691 var->set_fn(var, &oldval); 692 owl_variable_dict_add_variable(vd, var); 703 693 } 704 694 … … 706 696 { 707 697 owl_variable *old = owl_variable_get_var(vd, name); 708 if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) { 709 owl_variable_update(old, summ, desc); 710 old->ival_default = initval; 711 } else { 712 owl_variable * var = owl_variable_newvar(name, summ, desc); 713 var->type = OWL_VARIABLE_BOOL; 714 var->validsettings = "on,off"; 715 var->ival_default = initval; 716 var->validate_fn = owl_variable_bool_validate_default; 717 var->set_fn = owl_variable_bool_set_default; 718 var->set_fromstring_fn = owl_variable_bool_set_fromstring_default; 719 var->get_fn = owl_variable_get_default; 720 var->get_tostring_fn = owl_variable_bool_get_tostring_default; 721 var->delete_fn = owl_variable_delete_default; 722 var->val = g_new(int, 1); 723 var->set_fn(var, &initval); 724 owl_variable_dict_add_variable(vd, var); 725 } 698 bool oldval; 699 if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) 700 oldval = owl_variable_get_bool(old); 701 owl_variable *var = owl_variable_newvar(name, summ, desc, "on,off", OWL_VARIABLE_BOOL); 702 var->ival_default = initval; 703 owl_variable_init_defaults(var); 704 if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) 705 var->set_fn(var, &oldval); 706 owl_variable_dict_add_variable(vd, var); 726 707 } 727 708
Note: See TracChangeset
for help on using the changeset viewer.