X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fstats%2Fsort-criteria.c;h=b8bdbd06f85b658808e7cfd6126a9350b845ab00;hb=81579d9e9f994fb2908f50af41c3eb033d216e58;hp=fd8c7c535f9e630b88acbfdbafeaa8367374fdcc;hpb=f6b8e421cd29ae0aef4018bbe2e1a06d0bef57df;p=pspp-builds.git diff --git a/src/language/stats/sort-criteria.c b/src/language/stats/sort-criteria.c index fd8c7c53..b8bdbd06 100644 --- a/src/language/stats/sort-criteria.c +++ b/src/language/stats/sort-criteria.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, 2011 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 @@ -16,60 +16,65 @@ #include -#include +#include "language/stats/sort-criteria.h" #include -#include -#include -#include -#include -#include -#include +#include "data/dictionary.h" +#include "data/subcase.h" +#include "data/variable.h" +#include "language/lexer/lexer.h" +#include "language/lexer/variable-parser.h" +#include "libpspp/message.h" #include "gettext.h" #define _(msgid) gettext (msgid) -/* Parses a list of sort keys and returns a struct sort_criteria - based on it. Returns a null pointer on error. +/* Parses a list of sort fields and appends them to ORDERING, + which the caller must already have initialized. + Returns true if successful, false on error. If SAW_DIRECTION is nonnull, sets *SAW_DIRECTION to true if at least one parenthesized sort direction was specified, false otherwise. */ -struct case_ordering * -parse_case_ordering (struct lexer *lexer, const struct dictionary *dict, - bool *saw_direction) +bool +parse_sort_criteria (struct lexer *lexer, const struct dictionary *dict, + struct subcase *ordering, + const struct variable ***vars, bool *saw_direction) { - struct case_ordering *ordering = case_ordering_create (); - const struct variable **vars = NULL; + const struct variable **local_vars = NULL; size_t var_cnt = 0; - if (saw_direction != NULL) + if (vars == NULL) + vars = &local_vars; + *vars = NULL; + + if (saw_direction != NULL) *saw_direction = false; do { - enum sort_direction direction; + size_t prev_var_cnt = var_cnt; + enum subcase_direction direction; size_t i; /* Variables. */ - free (vars); - vars = NULL; - if (!parse_variables_const (lexer, dict, &vars, &var_cnt, PV_NO_SCRATCH)) + if (!parse_variables_const (lexer, dict, vars, &var_cnt, + PV_APPEND | PV_NO_SCRATCH)) goto error; /* Sort direction. */ - if (lex_match (lexer, '(')) + if (lex_match (lexer, T_LPAREN)) { if (lex_match_id (lexer, "D") || lex_match_id (lexer, "DOWN")) - direction = SRT_DESCEND; + direction = SC_DESCEND; else if (lex_match_id (lexer, "A") || lex_match_id (lexer, "UP")) - direction = SRT_ASCEND; + direction = SC_ASCEND; else { msg (SE, _("`A' or `D' expected inside parentheses.")); goto error; } - if (!lex_match (lexer, ')')) + if (!lex_match (lexer, T_RPAREN)) { msg (SE, _("`)' expected.")); goto error; @@ -78,21 +83,25 @@ parse_case_ordering (struct lexer *lexer, const struct dictionary *dict, *saw_direction = true; } else - direction = SRT_ASCEND; - - for (i = 0; i < var_cnt; i++) - if (!case_ordering_add_var (ordering, vars[i], direction)) - msg (SW, _("Variable %s specified twice in sort criteria."), - var_get_name (vars[i])); + direction = SC_ASCEND; + + for (i = prev_var_cnt; i < var_cnt; i++) + { + const struct variable *var = (*vars)[i]; + if (!subcase_add_var (ordering, var, direction)) + msg (SW, _("Variable %s specified twice in sort criteria."), + var_get_name (var)); + } } while (lex_token (lexer) == T_ID - && dict_lookup_var (dict, lex_tokid (lexer)) != NULL); + && dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL); - free (vars); - return ordering; + free (local_vars); + return true; - error: - free (vars); - case_ordering_destroy (ordering); - return NULL; +error: + free (local_vars); + if (vars) + *vars = NULL; + return false; }