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"
42 +missing=miss:!exclude/include.
65 struct variable **destvars;
66 struct variable *srcvar;
70 static struct rank_spec *rank_specs;
71 static int n_rank_specs;
73 static struct sort_criteria *sc;
75 static struct variable **group_vars;
76 static int n_group_vars;
78 static struct cmd_rank cmd;
90 if ( !parse_rank(&cmd) )
94 for (i = 0 ; i < sc->crit_cnt ; ++i )
96 struct sort_criterion *crit = &sc->crits[i];
98 printf("Dir: %d; Index: %d\n", crit->dir, crit->fv);
101 for (i = 0 ; i < n_group_vars ; ++i )
102 printf("Group var: %s\n",group_vars[0]->name);
104 for (i = 0 ; i < n_rank_specs ; ++i )
107 printf("Ranks spec %d; Func: %d\n",i, rank_specs[i].rfunc);
109 for (j=0; j < sc->crit_cnt ; ++j )
110 printf("Dest var is \"%s\"\n", rank_specs[i].destvars[j]->name);
117 for (i = 0 ; i < n_rank_specs ; ++i )
119 free(rank_specs[i].destvars);
124 sort_destroy_criteria(sc);
131 /* Parser for the variables sub command
132 Returns 1 on success */
134 rank_custom_variables(struct cmd_rank *cmd UNUSED)
136 static const int terminators[2] = {T_BY, 0};
140 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
144 sc = sort_parse_criteria (default_dict, 0, 0, 0, terminators);
146 if ( lex_match(T_BY) )
148 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL))
153 if (!parse_variables (default_dict, &group_vars, &n_group_vars,
154 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
165 /* Return a name for a new variable which ranks the variable VAR_NAME,
166 according to the ranking function F.
167 If IDX is non zero, then IDX is used as a disambiguating number.
168 FIXME: This is not very robust.
171 new_variable_name(const char *ranked_var_name, enum RANK_FUNC f, int idx)
173 static char new_name[SHORT_NAME_LEN + 1];
174 char temp[SHORT_NAME_LEN + 1];
182 strcpy(new_name,"R");
188 strcpy(new_name,"N");
193 strcpy(new_name,"P");
197 strcpy(new_name,"S");
205 strncat(new_name, ranked_var_name, 7);
209 strncpy(temp, ranked_var_name, 3);
210 snprintf(new_name, SHORT_NAME_LEN, "%s%03d", temp, idx);
216 /* Parse the [/rank INTO var1 var2 ... varN ] clause */
218 parse_rank_function(struct cmd_rank *cmd UNUSED, enum RANK_FUNC f)
220 static const struct fmt_spec f8_2 = {FMT_F, 8, 2};
224 rank_specs = xrealloc(rank_specs, n_rank_specs * sizeof *rank_specs);
225 rank_specs[n_rank_specs - 1].rfunc = f;
227 rank_specs[n_rank_specs - 1].destvars =
228 xcalloc(sc->crit_cnt ,sizeof (struct variable *));
230 if (lex_match_id("INTO"))
232 struct variable *destvar;
234 while( token == T_ID )
237 if ( dict_lookup_var (default_dict, tokid) != NULL )
239 msg(ME, _("Variable %s already exists."), tokid);
242 if ( var_count > sc->crit_cnt )
244 msg(ME, _("Too many variables in INTO clause."), tokid);
248 destvar = dict_create_var (default_dict, tokid, 0);
251 destvar->print = destvar->write = f8_2;
254 rank_specs[n_rank_specs - 1].destvars[var_count - 1] = destvar ;
261 /* Allocate rank variable names to all those which haven't had INTO
262 variables assigned */
263 while (var_count < sc->crit_cnt)
266 struct variable *destvar ;
267 const struct variable *v = dict_get_var(default_dict,
268 sc->crits[var_count].fv);
273 new_name = new_variable_name(v->name, f, idx);
275 destvar = dict_create_var (default_dict, new_name, 0);
279 } while( !destvar ) ;
281 destvar->print = destvar->write = f8_2;
283 rank_specs[n_rank_specs - 1].destvars[var_count] = destvar ;
293 rank_custom_rank(struct cmd_rank *cmd )
295 return parse_rank_function(cmd, RANK);
299 rank_custom_normal(struct cmd_rank *cmd )
301 return parse_rank_function(cmd, NORMAL);
305 rank_custom_percent(struct cmd_rank *cmd )
307 return parse_rank_function(cmd, NORMAL);
311 rank_custom_rfraction(struct cmd_rank *cmd )
313 return parse_rank_function(cmd, RFRACTION);
317 rank_custom_proportion(struct cmd_rank *cmd )
319 return parse_rank_function(cmd, PROPORTION);
323 rank_custom_n(struct cmd_rank *cmd )
325 return parse_rank_function(cmd, N);
329 rank_custom_savage(struct cmd_rank *cmd )
331 return parse_rank_function(cmd, SAVAGE);
336 rank_custom_ntiles(struct cmd_rank *cmd )
338 if ( lex_force_match('(') )
340 if ( lex_force_int() )
343 lex_force_match(')');
351 return parse_rank_function(cmd, NTILES);