1 /* PSPP - RANK. -*-c-*-
3 Copyright (C) 2005 Free Software Foundation, Inc.
4 Author: John Darrington 2005
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "dictionary.h"
28 #define _(msgid) gettext (msgid)
44 +missing=miss:!exclude/include.
67 struct variable **destvars;
68 struct variable *srcvar;
72 static struct rank_spec *rank_specs;
73 static size_t n_rank_specs;
75 static struct sort_criteria *sc;
77 static struct variable **group_vars;
78 static size_t n_group_vars;
80 static struct cmd_rank cmd;
92 if ( !parse_rank(&cmd) )
96 for (i = 0 ; i < sc->crit_cnt ; ++i )
98 struct sort_criterion *crit = &sc->crits[i];
100 printf("Dir: %d; Index: %d\n", crit->dir, crit->fv);
103 for (i = 0 ; i < n_group_vars ; ++i )
104 printf("Group var: %s\n",group_vars[0]->name);
106 for (i = 0 ; i < n_rank_specs ; ++i )
109 printf("Ranks spec %d; Func: %d\n",i, rank_specs[i].rfunc);
111 for (j=0; j < sc->crit_cnt ; ++j )
112 printf("Dest var is \"%s\"\n", rank_specs[i].destvars[j]->name);
119 for (i = 0 ; i < n_rank_specs ; ++i )
121 free(rank_specs[i].destvars);
126 sort_destroy_criteria(sc);
133 /* Parser for the variables sub command
134 Returns 1 on success */
136 rank_custom_variables(struct cmd_rank *cmd UNUSED)
138 static const int terminators[2] = {T_BY, 0};
142 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
146 sc = sort_parse_criteria (default_dict, 0, 0, 0, terminators);
148 if ( lex_match(T_BY) )
150 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL))
155 if (!parse_variables (default_dict, &group_vars, &n_group_vars,
156 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
167 /* Return a name for a new variable which ranks the variable VAR_NAME,
168 according to the ranking function F.
169 If IDX is non zero, then IDX is used as a disambiguating number.
170 FIXME: This is not very robust.
173 new_variable_name(const char *ranked_var_name, enum RANK_FUNC f, int idx)
175 static char new_name[SHORT_NAME_LEN + 1];
176 char temp[SHORT_NAME_LEN + 1];
184 strcpy(new_name,"R");
190 strcpy(new_name,"N");
195 strcpy(new_name,"P");
199 strcpy(new_name,"S");
207 strncat(new_name, ranked_var_name, 7);
211 strncpy(temp, ranked_var_name, 3);
212 snprintf(new_name, SHORT_NAME_LEN, "%s%03d", temp, idx);
218 /* Parse the [/rank INTO var1 var2 ... varN ] clause */
220 parse_rank_function(struct cmd_rank *cmd UNUSED, enum RANK_FUNC f)
222 static const struct fmt_spec f8_2 = {FMT_F, 8, 2};
226 rank_specs = xnrealloc(rank_specs, n_rank_specs, sizeof *rank_specs);
227 rank_specs[n_rank_specs - 1].rfunc = f;
229 rank_specs[n_rank_specs - 1].destvars =
230 xcalloc (sc->crit_cnt, sizeof (struct variable *));
232 if (lex_match_id("INTO"))
234 struct variable *destvar;
236 while( token == T_ID )
239 if ( dict_lookup_var (default_dict, tokid) != NULL )
241 msg(ME, _("Variable %s already exists."), tokid);
244 if ( var_count > sc->crit_cnt )
246 msg(ME, _("Too many variables in INTO clause."));
250 destvar = dict_create_var (default_dict, tokid, 0);
253 destvar->print = destvar->write = f8_2;
256 rank_specs[n_rank_specs - 1].destvars[var_count - 1] = destvar ;
263 /* Allocate rank variable names to all those which haven't had INTO
264 variables assigned */
265 while (var_count < sc->crit_cnt)
268 struct variable *destvar ;
269 const struct variable *v = dict_get_var(default_dict,
270 sc->crits[var_count].fv);
275 new_name = new_variable_name(v->name, f, idx);
277 destvar = dict_create_var (default_dict, new_name, 0);
281 } while( !destvar ) ;
283 destvar->print = destvar->write = f8_2;
285 rank_specs[n_rank_specs - 1].destvars[var_count] = destvar ;
295 rank_custom_rank(struct cmd_rank *cmd )
297 return parse_rank_function(cmd, RANK);
301 rank_custom_normal(struct cmd_rank *cmd )
303 return parse_rank_function(cmd, NORMAL);
307 rank_custom_percent(struct cmd_rank *cmd )
309 return parse_rank_function(cmd, NORMAL);
313 rank_custom_rfraction(struct cmd_rank *cmd )
315 return parse_rank_function(cmd, RFRACTION);
319 rank_custom_proportion(struct cmd_rank *cmd )
321 return parse_rank_function(cmd, PROPORTION);
325 rank_custom_n(struct cmd_rank *cmd )
327 return parse_rank_function(cmd, N);
331 rank_custom_savage(struct cmd_rank *cmd )
333 return parse_rank_function(cmd, SAVAGE);
338 rank_custom_ntiles(struct cmd_rank *cmd )
340 if ( lex_force_match('(') )
342 if ( lex_force_int() )
345 lex_force_match(')');
353 return parse_rank_function(cmd, NTILES);