diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-07-17 20:26:24 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-07-17 20:26:24 +0000 |
commit | 2245d37ca9068fe9021c9f271d80fb0102107fbc (patch) | |
tree | 4eb75241bbbae0c67498b1c090a97c928e4a266f /src/util/configuration.c | |
parent | aabe756fa2d764481c337060d27ff03c74b9e320 (diff) | |
download | gnunet-2245d37ca9068fe9021c9f271d80fb0102107fbc.tar.gz gnunet-2245d37ca9068fe9021c9f271d80fb0102107fbc.zip |
-code cleanup, tolerate e->vals of NULL
Diffstat (limited to 'src/util/configuration.c')
-rw-r--r-- | src/util/configuration.c | 108 |
1 files changed, 47 insertions, 61 deletions
diff --git a/src/util/configuration.c b/src/util/configuration.c index c762e3b9c..531b24fe0 100644 --- a/src/util/configuration.c +++ b/src/util/configuration.c | |||
@@ -119,7 +119,7 @@ struct DiffHandle | |||
119 | struct GNUNET_CONFIGURATION_Handle * | 119 | struct GNUNET_CONFIGURATION_Handle * |
120 | GNUNET_CONFIGURATION_create () | 120 | GNUNET_CONFIGURATION_create () |
121 | { | 121 | { |
122 | return GNUNET_malloc (sizeof (struct GNUNET_CONFIGURATION_Handle)); | 122 | return GNUNET_new (struct GNUNET_CONFIGURATION_Handle); |
123 | } | 123 | } |
124 | 124 | ||
125 | 125 | ||
@@ -400,13 +400,11 @@ GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
400 | 400 | ||
401 | /* Pass1 : calculate the buffer size required */ | 401 | /* Pass1 : calculate the buffer size required */ |
402 | m_size = 0; | 402 | m_size = 0; |
403 | sec = cfg->sections; | 403 | for (sec = cfg->sections; NULL != sec; sec = sec->next) |
404 | while (NULL != sec) | ||
405 | { | 404 | { |
406 | /* For each section we need to add 3 charaters: {'[',']','\n'} */ | 405 | /* For each section we need to add 3 charaters: {'[',']','\n'} */ |
407 | m_size += strlen (sec->name) + 3; | 406 | m_size += strlen (sec->name) + 3; |
408 | ent = sec->entries; | 407 | for (ent = sec->entries; NULL != ent; ent = ent->next) |
409 | while (NULL != ent) | ||
410 | { | 408 | { |
411 | if (NULL != ent->val) | 409 | if (NULL != ent->val) |
412 | { | 410 | { |
@@ -421,11 +419,9 @@ GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
421 | spaces and 1 equal-to character and 1 new line) */ | 419 | spaces and 1 equal-to character and 1 new line) */ |
422 | m_size += strlen (ent->key) + strlen (ent->val) + 4; | 420 | m_size += strlen (ent->key) + strlen (ent->val) + 4; |
423 | } | 421 | } |
424 | ent = ent->next; | ||
425 | } | 422 | } |
426 | /* A new line after section end */ | 423 | /* A new line after section end */ |
427 | m_size++; | 424 | m_size++; |
428 | sec = sec->next; | ||
429 | } | 425 | } |
430 | 426 | ||
431 | /* Pass2: Allocate memory and write the configuration to it */ | 427 | /* Pass2: Allocate memory and write the configuration to it */ |
@@ -440,8 +436,7 @@ GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
440 | memcpy (mem + c_size, cbuf, len); | 436 | memcpy (mem + c_size, cbuf, len); |
441 | c_size += len; | 437 | c_size += len; |
442 | GNUNET_free (cbuf); | 438 | GNUNET_free (cbuf); |
443 | ent = sec->entries; | 439 | for (ent = sec->entries; NULL != ent; ent = ent->next) |
444 | while (NULL != ent) | ||
445 | { | 440 | { |
446 | if (NULL != ent->val) | 441 | if (NULL != ent->val) |
447 | { | 442 | { |
@@ -459,7 +454,6 @@ GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
459 | c_size += len; | 454 | c_size += len; |
460 | GNUNET_free (cbuf); | 455 | GNUNET_free (cbuf); |
461 | } | 456 | } |
462 | ent = ent->next; | ||
463 | } | 457 | } |
464 | memcpy (mem + c_size, "\n", 1); | 458 | memcpy (mem + c_size, "\n", 1); |
465 | c_size ++; | 459 | c_size ++; |
@@ -530,17 +524,10 @@ GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
530 | struct ConfigSection *spos; | 524 | struct ConfigSection *spos; |
531 | struct ConfigEntry *epos; | 525 | struct ConfigEntry *epos; |
532 | 526 | ||
533 | spos = cfg->sections; | 527 | for (spos = cfg->sections; NULL != spos; spos = spos->next) |
534 | while (spos != NULL) | 528 | for (epos = spos->entries; NULL != epos; epos = epos->next) |
535 | { | 529 | if (NULL != epos->val) |
536 | epos = spos->entries; | 530 | iter (iter_cls, spos->name, epos->key, epos->val); |
537 | while (epos != NULL) | ||
538 | { | ||
539 | iter (iter_cls, spos->name, epos->key, epos->val); | ||
540 | epos = epos->next; | ||
541 | } | ||
542 | spos = spos->next; | ||
543 | } | ||
544 | } | 531 | } |
545 | 532 | ||
546 | 533 | ||
@@ -565,16 +552,11 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct | |||
565 | spos = cfg->sections; | 552 | spos = cfg->sections; |
566 | while ((spos != NULL) && (0 != strcasecmp (spos->name, section))) | 553 | while ((spos != NULL) && (0 != strcasecmp (spos->name, section))) |
567 | spos = spos->next; | 554 | spos = spos->next; |
568 | 555 | if (NULL == spos) | |
569 | if (spos == NULL) | ||
570 | return; | 556 | return; |
571 | 557 | for (epos = spos->entries; NULL != epos; epos = epos->next) | |
572 | epos = spos->entries; | 558 | if (NULL != epos->val) |
573 | while (epos != NULL) | 559 | iter (iter_cls, spos->name, epos->key, epos->val); |
574 | { | ||
575 | iter (iter_cls, spos->name, epos->key, epos->val); | ||
576 | epos = epos->next; | ||
577 | } | ||
578 | } | 560 | } |
579 | 561 | ||
580 | 562 | ||
@@ -619,11 +601,11 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg, | |||
619 | 601 | ||
620 | prev = NULL; | 602 | prev = NULL; |
621 | spos = cfg->sections; | 603 | spos = cfg->sections; |
622 | while (spos != NULL) | 604 | while (NULL != spos) |
623 | { | 605 | { |
624 | if (0 == strcasecmp (section, spos->name)) | 606 | if (0 == strcasecmp (section, spos->name)) |
625 | { | 607 | { |
626 | if (prev == NULL) | 608 | if (NULL == prev) |
627 | cfg->sections = spos->next; | 609 | cfg->sections = spos->next; |
628 | else | 610 | else |
629 | prev->next = spos->next; | 611 | prev->next = spos->next; |
@@ -682,7 +664,7 @@ GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg) | |||
682 | 664 | ||
683 | 665 | ||
684 | /** | 666 | /** |
685 | * FIXME. | 667 | * Find a section entry from a configuration. |
686 | * | 668 | * |
687 | * @param cfg FIXME | 669 | * @param cfg FIXME |
688 | * @param section FIXME | 670 | * @param section FIXME |
@@ -715,8 +697,7 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, | |||
715 | struct ConfigSection *sec; | 697 | struct ConfigSection *sec; |
716 | struct ConfigEntry *pos; | 698 | struct ConfigEntry *pos; |
717 | 699 | ||
718 | sec = findSection (cfg, section); | 700 | if (NULL == (sec = findSection (cfg, section))) |
719 | if (sec == NULL) | ||
720 | return NULL; | 701 | return NULL; |
721 | pos = sec->entries; | 702 | pos = sec->entries; |
722 | while ((pos != NULL) && (0 != strcasecmp (key, pos->key))) | 703 | while ((pos != NULL) && (0 != strcasecmp (key, pos->key))) |
@@ -743,7 +724,9 @@ compare_entries (void *cls, const char *section, const char *option, | |||
743 | struct ConfigEntry *entNew; | 724 | struct ConfigEntry *entNew; |
744 | 725 | ||
745 | entNew = findEntry (dh->cfgDefault, section, option); | 726 | entNew = findEntry (dh->cfgDefault, section, option); |
746 | if ((entNew != NULL) && (strcmp (entNew->val, value) == 0)) | 727 | if ( (NULL != entNew) && |
728 | (NULL != entNew->val) && | ||
729 | (0 == strcmp (entNew->val, value)) ) | ||
747 | return; | 730 | return; |
748 | GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value); | 731 | GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value); |
749 | } | 732 | } |
@@ -812,11 +795,19 @@ GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg, | |||
812 | char *nv; | 795 | char *nv; |
813 | 796 | ||
814 | e = findEntry (cfg, section, option); | 797 | e = findEntry (cfg, section, option); |
815 | if (e != NULL) | 798 | if (NULL != e) |
816 | { | 799 | { |
817 | nv = GNUNET_strdup (value); | 800 | if (NULL == value) |
818 | GNUNET_free_non_null (e->val); | 801 | { |
819 | e->val = nv; | 802 | GNUNET_free_non_null (e->val); |
803 | e->val = NULL; | ||
804 | } | ||
805 | else | ||
806 | { | ||
807 | nv = GNUNET_strdup (value); | ||
808 | GNUNET_free_non_null (e->val); | ||
809 | e->val = nv; | ||
810 | } | ||
820 | return; | 811 | return; |
821 | } | 812 | } |
822 | sec = findSection (cfg, section); | 813 | sec = findSection (cfg, section); |
@@ -872,8 +863,9 @@ GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle | |||
872 | { | 863 | { |
873 | struct ConfigEntry *e; | 864 | struct ConfigEntry *e; |
874 | 865 | ||
875 | e = findEntry (cfg, section, option); | 866 | if (NULL == (e = findEntry (cfg, section, option))) |
876 | if (e == NULL) | 867 | return GNUNET_SYSERR; |
868 | if (NULL == e->val) | ||
877 | return GNUNET_SYSERR; | 869 | return GNUNET_SYSERR; |
878 | if (1 != SSCANF (e->val, "%llu", number)) | 870 | if (1 != SSCANF (e->val, "%llu", number)) |
879 | return GNUNET_SYSERR; | 871 | return GNUNET_SYSERR; |
@@ -898,10 +890,10 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle | |||
898 | { | 890 | { |
899 | struct ConfigEntry *e; | 891 | struct ConfigEntry *e; |
900 | 892 | ||
901 | e = findEntry (cfg, section, option); | 893 | if (NULL == (e = findEntry (cfg, section, option))) |
902 | if (e == NULL) | 894 | return GNUNET_SYSERR; |
895 | if (NULL == e->val) | ||
903 | return GNUNET_SYSERR; | 896 | return GNUNET_SYSERR; |
904 | |||
905 | return GNUNET_STRINGS_fancy_time_to_relative (e->val, time); | 897 | return GNUNET_STRINGS_fancy_time_to_relative (e->val, time); |
906 | } | 898 | } |
907 | 899 | ||
@@ -923,8 +915,9 @@ GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle | |||
923 | { | 915 | { |
924 | struct ConfigEntry *e; | 916 | struct ConfigEntry *e; |
925 | 917 | ||
926 | e = findEntry (cfg, section, option); | 918 | if (NULL == (e = findEntry (cfg, section, option))) |
927 | if (e == NULL) | 919 | return GNUNET_SYSERR; |
920 | if (NULL == e->val) | ||
928 | return GNUNET_SYSERR; | 921 | return GNUNET_SYSERR; |
929 | return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size); | 922 | return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size); |
930 | } | 923 | } |
@@ -948,15 +941,13 @@ GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle | |||
948 | struct ConfigEntry *e; | 941 | struct ConfigEntry *e; |
949 | 942 | ||
950 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section); | 943 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section); |
951 | e = findEntry (cfg, section, option); | 944 | if ( (NULL == (e = findEntry (cfg, section, option))) || |
952 | if ((e == NULL) || (e->val == NULL)) | 945 | (NULL == e->val) ) |
953 | { | 946 | { |
954 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve the string\n"); | ||
955 | *value = NULL; | 947 | *value = NULL; |
956 | return GNUNET_SYSERR; | 948 | return GNUNET_SYSERR; |
957 | } | 949 | } |
958 | *value = GNUNET_strdup (e->val); | 950 | *value = GNUNET_strdup (e->val); |
959 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved string `%s'\n", e->val); | ||
960 | return GNUNET_OK; | 951 | return GNUNET_OK; |
961 | } | 952 | } |
962 | 953 | ||
@@ -980,19 +971,14 @@ GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle | |||
980 | const char **value) | 971 | const char **value) |
981 | { | 972 | { |
982 | struct ConfigEntry *e; | 973 | struct ConfigEntry *e; |
983 | int i; | 974 | unsigned int i; |
984 | 975 | ||
985 | e = findEntry (cfg, section, option); | 976 | if (NULL == (e = findEntry (cfg, section, option))) |
986 | if (e == NULL) | ||
987 | return GNUNET_SYSERR; | 977 | return GNUNET_SYSERR; |
988 | i = 0; | 978 | for (i = 0; NULL != choices[i]; i++) |
989 | while (choices[i] != NULL) | ||
990 | { | ||
991 | if (0 == strcasecmp (choices[i], e->val)) | 979 | if (0 == strcasecmp (choices[i], e->val)) |
992 | break; | 980 | break; |
993 | i++; | 981 | if (NULL == choices[i]) |
994 | } | ||
995 | if (choices[i] == NULL) | ||
996 | { | 982 | { |
997 | LOG (GNUNET_ERROR_TYPE_ERROR, | 983 | LOG (GNUNET_ERROR_TYPE_ERROR, |
998 | _("Configuration value '%s' for '%s'" | 984 | _("Configuration value '%s' for '%s'" |
@@ -1018,7 +1004,7 @@ GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1018 | { | 1004 | { |
1019 | struct ConfigEntry *e; | 1005 | struct ConfigEntry *e; |
1020 | 1006 | ||
1021 | if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL)) | 1007 | if ((NULL == (e = findEntry (cfg, section, option))) || (NULL == e->val)) |
1022 | return GNUNET_NO; | 1008 | return GNUNET_NO; |
1023 | return GNUNET_YES; | 1009 | return GNUNET_YES; |
1024 | } | 1010 | } |