Changeset d536e72 for variable.c
- Timestamp:
- Jan 10, 2008, 1:02:06 AM (17 years ago)
- Branches:
- master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
- Children:
- 44f32fb
- Parents:
- b10f340
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
variable.c
rad15610 rd536e72 455 455 456 456 int owl_variable_dict_setup(owl_vardict *vd) { 457 owl_variable * cur;457 owl_variable *var, *cur; 458 458 if (owl_dict_create(vd)) return(-1); 459 for (cur = variables_to_init; cur->name != NULL; cur++) { 459 for (var = variables_to_init; var->name != NULL; var++) { 460 cur = owl_malloc(sizeof(owl_variable)); 461 memcpy(cur, var, sizeof(owl_variable)); 460 462 switch (cur->type) { 461 463 case OWL_VARIABLE_OTHER: … … 520 522 void owl_variable_dict_add_variable(owl_vardict * vardict, 521 523 owl_variable * var) { 522 owl_dict_insert_element(vardict, var->name, (void*)var, NULL);524 owl_dict_insert_element(vardict, var->name, (void*)var, (void(*)(void*))owl_variable_free); 523 525 } 524 526 … … 532 534 } 533 535 536 void owl_variable_update(owl_variable *var, char *summary, char *desc) { 537 if(var->summary) owl_free(var->summary); 538 var->summary = owl_strdup(summary); 539 if(var->description) owl_free(var->description); 540 var->description = owl_strdup(desc); 541 } 542 534 543 void owl_variable_dict_newvar_string(owl_vardict * vd, char *name, char *summ, char * desc, char * initval) { 535 owl_variable * var = owl_variable_newvar(name, summ, desc); 536 var->type = OWL_VARIABLE_STRING; 537 var->pval_default = owl_strdup(initval); 538 var->set_fn = owl_variable_string_set_default; 539 var->set_fromstring_fn = owl_variable_string_set_fromstring_default; 540 var->get_fn = owl_variable_get_default; 541 var->get_tostring_fn = owl_variable_string_get_tostring_default; 542 var->free_fn = owl_variable_free_default; 543 var->set_fn(var, initval); 544 owl_variable_dict_add_variable(vd, var); 544 owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_STRING); 545 if(old) { 546 owl_variable_update(old, summ, desc); 547 if(old->pval_default) owl_free(old->pval_default); 548 old->pval_default = owl_strdup(initval); 549 } else { 550 owl_variable * var = owl_variable_newvar(name, summ, desc); 551 var->type = OWL_VARIABLE_STRING; 552 var->pval_default = owl_strdup(initval); 553 var->set_fn = owl_variable_string_set_default; 554 var->set_fromstring_fn = owl_variable_string_set_fromstring_default; 555 var->get_fn = owl_variable_get_default; 556 var->get_tostring_fn = owl_variable_string_get_tostring_default; 557 var->free_fn = owl_variable_free_default; 558 var->set_fn(var, initval); 559 owl_variable_dict_add_variable(vd, var); 560 } 545 561 } 546 562 547 563 void owl_variable_dict_newvar_int(owl_vardict * vd, char *name, char *summ, char * desc, int initval) { 548 owl_variable * var = owl_variable_newvar(name, summ, desc); 549 var->type = OWL_VARIABLE_INT; 550 var->ival_default = initval; 551 var->validate_fn = owl_variable_int_validate_default; 552 var->set_fn = owl_variable_int_set_default; 553 var->set_fromstring_fn = owl_variable_int_set_fromstring_default; 554 var->get_fn = owl_variable_get_default; 555 var->get_tostring_fn = owl_variable_int_get_tostring_default; 556 var->free_fn = owl_variable_free_default; 557 var->val = owl_malloc(sizeof(int)); 558 var->set_fn(var, &initval); 559 owl_variable_dict_add_variable(vd, var); 564 owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_INT); 565 if(old) { 566 owl_variable_update(old, summ, desc); 567 old->ival_default = initval; 568 } else { 569 owl_variable * var = owl_variable_newvar(name, summ, desc); 570 var->type = OWL_VARIABLE_INT; 571 var->ival_default = initval; 572 var->validate_fn = owl_variable_int_validate_default; 573 var->set_fn = owl_variable_int_set_default; 574 var->set_fromstring_fn = owl_variable_int_set_fromstring_default; 575 var->get_fn = owl_variable_get_default; 576 var->get_tostring_fn = owl_variable_int_get_tostring_default; 577 var->free_fn = owl_variable_free_default; 578 var->val = owl_malloc(sizeof(int)); 579 var->set_fn(var, &initval); 580 owl_variable_dict_add_variable(vd, var); 581 } 560 582 } 561 583 562 584 void owl_variable_dict_newvar_bool(owl_vardict * vd, char *name, char *summ, char * desc, int initval) { 563 owl_variable * var = owl_variable_newvar(name, summ, desc); 564 var->type = OWL_VARIABLE_BOOL; 565 var->ival_default = initval; 566 var->validate_fn = owl_variable_bool_validate_default; 567 var->set_fn = owl_variable_bool_set_default; 568 var->set_fromstring_fn = owl_variable_bool_set_fromstring_default; 569 var->get_fn = owl_variable_get_default; 570 var->get_tostring_fn = owl_variable_bool_get_tostring_default; 571 var->free_fn = owl_variable_free_default; 572 var->val = owl_malloc(sizeof(int)); 573 var->set_fn(var, &initval); 574 owl_variable_dict_add_variable(vd, var); 585 owl_variable *old = owl_variable_get_var(vd, name, OWL_VARIABLE_BOOL); 586 if(old) { 587 owl_variable_update(old, summ, desc); 588 old->ival_default = initval; 589 } else { 590 owl_variable * var = owl_variable_newvar(name, summ, desc); 591 var->type = OWL_VARIABLE_BOOL; 592 var->ival_default = initval; 593 var->validate_fn = owl_variable_bool_validate_default; 594 var->set_fn = owl_variable_bool_set_default; 595 var->set_fromstring_fn = owl_variable_bool_set_fromstring_default; 596 var->get_fn = owl_variable_get_default; 597 var->get_tostring_fn = owl_variable_bool_get_tostring_default; 598 var->free_fn = owl_variable_free_default; 599 var->val = owl_malloc(sizeof(int)); 600 var->set_fn(var, &initval); 601 owl_variable_dict_add_variable(vd, var); 602 } 575 603 } 576 604 … … 590 618 void owl_variable_free(owl_variable *v) { 591 619 if (v->free_fn) v->free_fn(v); 620 owl_free(v); 592 621 } 593 622 … … 687 716 } 688 717 689 /* returns a reference */ 690 void *owl_variable_get(owl_vardict *d, char *name, int require_type) { 718 owl_variable *owl_variable_get_var(owl_vardict *d, char *name, int require_type) { 691 719 owl_variable *v; 692 720 if (!name) return(NULL); 693 721 v = owl_dict_find_element(d, name); 694 722 if (v == NULL || !v->get_fn || v->type != require_type) return(NULL); 723 return v; 724 } 725 726 /* returns a reference */ 727 void *owl_variable_get(owl_vardict *d, char *name, int require_type) { 728 owl_variable *v = owl_variable_get_var(d, name, require_type); 729 if(v == NULL) return NULL; 695 730 return v->get_fn(v); 696 731 }
Note: See TracChangeset
for help on using the changeset viewer.