X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fdictionary.c;h=bca92dabe300b4c34439845c47abf485c900a778;hb=fd674641880b82597fa35492207f189136e83fbb;hp=67af049b24fee7a72e3dd6b6561a6784f87a70b7;hpb=5c3291dc396b795696e94f47780308fd7ace6fc4;p=pspp diff --git a/src/data/dictionary.c b/src/data/dictionary.c index 67af049b24..bca92dabe3 100644 --- a/src/data/dictionary.c +++ b/src/data/dictionary.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006, 2007, 2009 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -81,7 +81,10 @@ void dict_set_encoding (struct dictionary *d, const char *enc) { if (enc) - d->encoding = xstrdup (enc); + { + free (d->encoding); + d->encoding = xstrdup (enc); + } } const char * @@ -119,7 +122,7 @@ dict_dump (const struct dictionary *d) { const struct variable *v = d->var[i]; - printf ("Name: %s;\tdict_idx: %d; case_idx: %d\n", + printf ("Name: %s;\tdict_idx: %zu; case_idx: %zu\n", var_get_name (v), var_get_dict_index (v), var_get_case_index (v)); @@ -185,7 +188,7 @@ dict_clone (const struct dictionary *s) const struct vardict_info *svdi; struct vardict_info dvdi; struct variable *sv = s->var[i]; - struct variable *dv = dict_clone_var_assert (d, sv, var_get_name (sv)); + struct variable *dv = dict_clone_var_assert (d, sv); size_t i; for (i = 0; i < var_get_short_name_cnt (sv); i++) @@ -410,27 +413,46 @@ dict_create_var_assert (struct dictionary *d, const char *name, int width) return add_var (d, var_create (name, width)); } -/* Creates and returns a new variable in D with name NAME, as a - copy of existing variable OLD_VAR, which need not be in D or - in any dictionary. Returns a null pointer if the given NAME - would duplicate that of an existing variable in the +/* Creates and returns a new variable in D, as a copy of existing variable + OLD_VAR, which need not be in D or in any dictionary. Returns a null + pointer if OLD_VAR's name would duplicate that of an existing variable in + the dictionary. */ +struct variable * +dict_clone_var (struct dictionary *d, const struct variable *old_var) +{ + return dict_clone_var_as (d, old_var, var_get_name (old_var)); +} + +/* Creates and returns a new variable in D, as a copy of existing variable + OLD_VAR, which need not be in D or in any dictionary. Assert-fails if + OLD_VAR's name would duplicate that of an existing variable in the dictionary. */ struct variable * -dict_clone_var (struct dictionary *d, const struct variable *old_var, - const char *name) +dict_clone_var_assert (struct dictionary *d, const struct variable *old_var) +{ + return dict_clone_var_as_assert (d, old_var, var_get_name (old_var)); +} + +/* Creates and returns a new variable in D with name NAME, as a copy of + existing variable OLD_VAR, which need not be in D or in any dictionary. + Returns a null pointer if the given NAME would duplicate that of an existing + variable in the dictionary. */ +struct variable * +dict_clone_var_as (struct dictionary *d, const struct variable *old_var, + const char *name) { return (dict_lookup_var (d, name) == NULL - ? dict_clone_var_assert (d, old_var, name) + ? dict_clone_var_as_assert (d, old_var, name) : NULL); } -/* Creates and returns a new variable in D with name NAME, as a - copy of existing variable OLD_VAR, which need not be in D or - in any dictionary. Assert-fails if the given NAME would - duplicate that of an existing variable in the dictionary. */ +/* Creates and returns a new variable in D with name NAME, as a copy of + existing variable OLD_VAR, which need not be in D or in any dictionary. + Assert-fails if the given NAME would duplicate that of an existing variable + in the dictionary. */ struct variable * -dict_clone_var_assert (struct dictionary *d, const struct variable *old_var, - const char *name) +dict_clone_var_as_assert (struct dictionary *d, const struct variable *old_var, + const char *name) { struct variable *new_var = var_clone (old_var); assert (dict_lookup_var (d, name) == NULL); @@ -664,7 +686,7 @@ dict_reorder_vars (struct dictionary *d, assert (count == 0 || order != NULL); assert (count <= d->var_cnt); - new_var = xnmalloc (d->var_cnt, sizeof *new_var); + new_var = xnmalloc (d->var_cap, sizeof *new_var); memcpy (new_var, order, count * sizeof *new_var); for (i = 0; i < count; i++) { @@ -1016,7 +1038,7 @@ dict_set_case_limit (struct dictionary *d, casenumber case_limit) const struct caseproto * dict_get_proto (const struct dictionary *d_) { - struct dictionary *d = (struct dictionary *) d_; + struct dictionary *d = CONST_CAST (struct dictionary *, d_); if (d->proto == NULL) { size_t i; @@ -1375,7 +1397,7 @@ dict_clear_vectors (struct dictionary *d) struct attrset * dict_get_attributes (const struct dictionary *d) { - return (struct attrset *) &d->attributes; + return CONST_CAST (struct attrset *, &d->attributes); } /* Replaces D's attributes set by a copy of ATTRS. */