From: Ben Pfaff Date: Sat, 3 Apr 2010 04:26:52 +0000 (-0700) Subject: variable-parser: Implement PV_NO_DUPLICATE. X-Git-Tag: sav-api~312 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pspp;a=commitdiff_plain;h=726b2e7b2b7a79f0cf860efe43e349762bf3f44e variable-parser: Implement PV_NO_DUPLICATE. Now that we have string_set, PV_NO_DUPLICATE is easy to implement, so we might as well. --- diff --git a/src/language/data-io/trim.c b/src/language/data-io/trim.c index 7a14c996b8..921b9c655e 100644 --- a/src/language/data-io/trim.c +++ b/src/language/data-io/trim.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006, 2007, 2008 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2007, 2008, 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 @@ -114,7 +114,8 @@ parse_dict_rename (struct lexer *lexer, struct dictionary *dict) msg (SE, _("`=' expected after variable list.")); goto done; } - if (!parse_DATA_LIST_vars (lexer, &new_names, &nn, PV_APPEND | PV_NO_SCRATCH)) + if (!parse_DATA_LIST_vars (lexer, &new_names, &nn, + PV_APPEND | PV_NO_SCRATCH | PV_NO_DUPLICATE)) goto done; if (nn != nv) { diff --git a/src/language/dictionary/numeric.c b/src/language/dictionary/numeric.c index 5fa77541b2..41f3c79a67 100644 --- a/src/language/dictionary/numeric.c +++ b/src/language/dictionary/numeric.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 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 @@ -49,7 +49,7 @@ cmd_numeric (struct lexer *lexer, struct dataset *ds) be used. */ struct fmt_spec f; - if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NONE)) + if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NO_DUPLICATE)) return CMD_FAILURE; /* Get the optional format specification. */ @@ -127,7 +127,7 @@ cmd_string (struct lexer *lexer, struct dataset *ds) do { - if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NONE)) + if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NO_DUPLICATE)) return CMD_FAILURE; if (!lex_force_match (lexer, '(') diff --git a/src/language/dictionary/rename-variables.c b/src/language/dictionary/rename-variables.c index 90117ff69e..c175831d8d 100644 --- a/src/language/dictionary/rename-variables.c +++ b/src/language/dictionary/rename-variables.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 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 @@ -67,7 +67,8 @@ cmd_rename_variables (struct lexer *lexer, struct dataset *ds) msg (SE, _("`=' expected between lists of new and old variable names.")); goto lossage; } - if (!parse_DATA_LIST_vars (lexer, &rename_new_names, &prev_nv_1, PV_APPEND)) + if (!parse_DATA_LIST_vars (lexer, &rename_new_names, &prev_nv_1, + PV_APPEND | PV_NO_DUPLICATE)) goto lossage; if (prev_nv_1 != rename_cnt) { diff --git a/src/language/lexer/variable-parser.c b/src/language/lexer/variable-parser.c index c6a2d5378b..b9d67523ee 100644 --- a/src/language/lexer/variable-parser.c +++ b/src/language/lexer/variable-parser.c @@ -34,6 +34,7 @@ #include "libpspp/misc.h" #include "libpspp/pool.h" #include "libpspp/str.h" +#include "libpspp/stringi-set.h" #include "gl/xalloc.h" @@ -400,7 +401,8 @@ extract_num (char *s, char *r, int *n, int *d) /* Parses a list of variable names according to the DATA LIST version of the TO convention. */ bool -parse_DATA_LIST_vars (struct lexer *lexer, char ***names, size_t *nnames, int pv_opts) +parse_DATA_LIST_vars (struct lexer *lexer, char ***names, + size_t *nnames, int pv_opts) { int n1, n2; int d1, d2; @@ -408,16 +410,27 @@ parse_DATA_LIST_vars (struct lexer *lexer, char ***names, size_t *nnames, int pv size_t nvar, mvar; char name1[VAR_NAME_LEN + 1], name2[VAR_NAME_LEN + 1]; char root1[VAR_NAME_LEN + 1], root2[VAR_NAME_LEN + 1]; + struct stringi_set set; int success = 0; assert (names != NULL); assert (nnames != NULL); assert ((pv_opts & ~(PV_APPEND | PV_SINGLE | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0); - /* FIXME: PV_NO_DUPLICATE is not implemented. */ + stringi_set_init (&set); if (pv_opts & PV_APPEND) - nvar = mvar = *nnames; + { + nvar = mvar = *nnames; + + if (pv_opts & PV_NO_DUPLICATE) + { + size_t i; + + for (i = 0; i < nvar; i++) + stringi_set_insert (&set, (*names)[i]); + } + } else { nvar = mvar = 0; @@ -477,6 +490,13 @@ parse_DATA_LIST_vars (struct lexer *lexer, char ***names, size_t *nnames, int pv { char name[VAR_NAME_LEN + 1]; sprintf (name, "%s%0*d", root1, d1, n); + + if (pv_opts & PV_NO_DUPLICATE && !stringi_set_insert (&set, name)) + { + msg (SE, _("Variable %s appears twice in variable list."), + name); + goto fail; + } (*names)[nvar] = xstrdup (name); nvar++; } @@ -501,6 +521,7 @@ parse_DATA_LIST_vars (struct lexer *lexer, char ***names, size_t *nnames, int pv fail: *nnames = nvar; + stringi_set_destroy (&set); if (!success) { int i; diff --git a/src/language/stats/aggregate.c b/src/language/stats/aggregate.c index b54ebf6376..42f330d5d2 100644 --- a/src/language/stats/aggregate.c +++ b/src/language/stats/aggregate.c @@ -391,7 +391,8 @@ parse_aggregate_functions (struct lexer *lexer, const struct dictionary *dict, size_t n_dest_prev = n_dest; if (!parse_DATA_LIST_vars (lexer, &dest, &n_dest, - PV_APPEND | PV_SINGLE | PV_NO_SCRATCH)) + (PV_APPEND | PV_SINGLE | PV_NO_SCRATCH + | PV_NO_DUPLICATE))) goto error; /* Assign empty labels. */