From 0a18c32ad55044070812325dd5e569259fac6a37 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 24 Mar 2010 21:18:34 -0700 Subject: [PATCH] dict: Make dict_clone_var(), dict_clone_var_assert() not rename variables. Most uses of dict_clone_var() and dict_clone_var_assert() do not involve renaming variables, but the interface assumes that renaming is the common case. This commit changes their interfaces to not support renaming and adds new functions dict_clone_var_as() and dict_clone_var_as_assert() to support renaming during cloning. --- doc/dev/concepts.texi | 18 ++++++---- src/data/dictionary.c | 49 +++++++++++++++++++--------- src/data/dictionary.h | 12 ++++--- src/language/data-io/combine-files.c | 2 +- src/language/stats/aggregate.c | 7 ++-- src/ui/gui/psppire-data-editor.c | 7 ++-- src/ui/gui/text-data-import-dialog.c | 4 +-- 7 files changed, 62 insertions(+), 37 deletions(-) diff --git a/doc/dev/concepts.texi b/doc/dev/concepts.texi index 06652d6265..f134737934 100644 --- a/doc/dev/concepts.texi +++ b/doc/dev/concepts.texi @@ -2106,17 +2106,23 @@ if through a call to @code{var_create} with those arguments of variables, and returns the new variable. @end deftypefun -@deftypefun {struct variable *} dict_clone_var (struct dictionary *@var{dict}, const struct variable *@var{old_var}, const char *@var{name}) -@deftypefunx {struct variable *} dict_clone_var_assert (struct dictionary *@var{dict}, const struct variable *@var{old_var}, const char *@var{name}) +@deftypefun {struct variable *} dict_clone_var (struct dictionary *@var{dict}, const struct variable *@var{old_var}) +@deftypefunx {struct variable *} dict_clone_var_assert (struct dictionary *@var{dict}, const struct variable *@var{old_var}) Creates a new variable as a clone of @var{var}, inserts the new -variable into @var{dict}, and returns the new variable. The new -variable is named @var{name}. Other properties of the new variable -are copied from @var{old_var}, except for those not copied by -@code{var_clone} (@pxref{var_clone}). +variable into @var{dict}, and returns the new variable. Other +properties of the new variable are copied from @var{old_var}, except +for those not copied by @code{var_clone} (@pxref{var_clone}). @var{var} does not need to be a member of any dictionary. @end deftypefun +@deftypefun {struct variable *} dict_clone_var_as (struct dictionary *@var{dict}, const struct variable *@var{old_var}, const char *@var{name}) +@deftypefunx {struct variable *} dict_clone_var_as_assert (struct dictionary *@var{dict}, const struct variable *@var{old_var}, const char *@var{name}) +These functions are similar to @code{dict_clone_var} and +@code{dict_clone_var_assert}, respectively, except that the new +variable is named @var{name} instead of keeping @var{old_var}'s name. +@end deftypefun + @node Dictionary Deleting Variables @subsection Deleting Variables diff --git a/src/data/dictionary.c b/src/data/dictionary.c index 1ddf5575aa..43df1eb0e6 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 @@ -185,7 +185,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 +410,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); diff --git a/src/data/dictionary.h b/src/data/dictionary.h index 02fd5cd4c0..780d5a9412 100644 --- a/src/data/dictionary.h +++ b/src/data/dictionary.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc. + Copyright (C) 2004, 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 @@ -56,10 +56,14 @@ struct variable *dict_create_var (struct dictionary *, const char *, int width); struct variable *dict_create_var_assert (struct dictionary *, const char *, int width); -struct variable *dict_clone_var (struct dictionary *, const struct variable *, - const char *); +struct variable *dict_clone_var (struct dictionary *, const struct variable *); struct variable *dict_clone_var_assert (struct dictionary *, - const struct variable *, const char *); + const struct variable *); +struct variable *dict_clone_var_as (struct dictionary *, + const struct variable *, const char *); +struct variable *dict_clone_var_as_assert (struct dictionary *, + const struct variable *, + const char *); /* Deleting variables. */ void dict_delete_var (struct dictionary *, struct variable *); diff --git a/src/language/data-io/combine-files.c b/src/language/data-io/combine-files.c index 15566792a3..fd57beced0 100644 --- a/src/language/data-io/combine-files.c +++ b/src/language/data-io/combine-files.c @@ -575,7 +575,7 @@ merge_dictionary (struct dictionary *const m, struct comb_file *f) var_set_label (mv, var_get_label (dv)); } else - mv = dict_clone_var_assert (m, dv, var_get_name (dv)); + mv = dict_clone_var_assert (m, dv); } return true; diff --git a/src/language/stats/aggregate.c b/src/language/stats/aggregate.c index b3d02e1413..ba0650937d 100644 --- a/src/language/stats/aggregate.c +++ b/src/language/stats/aggregate.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006, 2008, 2009 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2008, 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 @@ -237,8 +237,7 @@ cmd_aggregate (struct lexer *lexer, struct dataset *ds) agr.break_var_cnt = subcase_get_n_fields (&agr.sort); for (i = 0; i < agr.break_var_cnt; i++) - dict_clone_var_assert (agr.dict, agr.break_vars[i], - var_get_name (agr.break_vars[i])); + dict_clone_var_assert (agr.dict, agr.break_vars[i]); /* BREAK must follow the options. */ break; @@ -582,7 +581,7 @@ parse_aggregate_functions (struct lexer *lexer, const struct dictionary *dict, } if (function->alpha_type == VAL_STRING) - destvar = dict_clone_var (agr->dict, v->src, dest[i]); + destvar = dict_clone_var_as (agr->dict, v->src, dest[i]); else { assert (var_is_numeric (v->src) diff --git a/src/ui/gui/psppire-data-editor.c b/src/ui/gui/psppire-data-editor.c index 58aa0b8f91..8a882e2c2d 100644 --- a/src/ui/gui/psppire-data-editor.c +++ b/src/ui/gui/psppire-data-editor.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2008, 2009 Free Software Foundation, Inc. + Copyright (C) 2008, 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 @@ -1624,10 +1624,7 @@ data_sheet_set_clip (PsppireSheet *sheet) clip_dict = dict_create (); dict_set_encoding (clip_dict, dict_get_encoding (ds->dict->dict)); for (i = col0; i <= coli; i++) - { - const struct variable *old = dict_get_var (ds->dict->dict, i); - dict_clone_var_assert (clip_dict, old, var_get_name (old)); - } + dict_clone_var_assert (clip_dict, dict_get_var (ds->dict->dict, i)); /* Construct clip data. */ map = case_map_by_name (ds->dict->dict, clip_dict); diff --git a/src/ui/gui/text-data-import-dialog.c b/src/ui/gui/text-data-import-dialog.c index 6f33ff6d8b..b32573c362 100644 --- a/src/ui/gui/text-data-import-dialog.c +++ b/src/ui/gui/text-data-import-dialog.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2008, 2009 Free Software Foundation + Copyright (C) 2008, 2009, 2010 Free Software Foundation 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 @@ -1615,7 +1615,7 @@ prepare_formats_page (struct import_assistant *ia) if (!dict_make_unique_var_name (dict, var_get_name (modified_var), &number, name)) NOT_REACHED (); - dict_clone_var_assert (dict, modified_var, name); + dict_clone_var_as_assert (dict, modified_var, name); } } fmt_guesser_destroy (fg); -- 2.30.2