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
22 #include <language/command.h>
23 #include <libpspp/compiler.h>
24 #include <data/dictionary.h>
25 #include <math/sort.h>
26 #include <data/variable.h>
28 #include "sort-criteria.h"
31 #define _(msgid) gettext (msgid)
47 +missing=miss:!exclude/include.
70 struct variable **destvars;
71 struct variable *srcvar;
75 static struct rank_spec *rank_specs;
76 static size_t n_rank_specs;
78 static struct sort_criteria *sc;
80 static struct variable **group_vars;
81 static size_t n_group_vars;
83 static struct cmd_rank cmd;
95 if ( !parse_rank(&cmd) )
99 for (i = 0 ; i < sc->crit_cnt ; ++i )
101 struct sort_criterion *crit = &sc->crits[i];
103 printf("Dir: %d; Index: %d\n", crit->dir, crit->fv);
106 for (i = 0 ; i < n_group_vars ; ++i )
107 printf("Group var: %s\n",group_vars[0]->name);
109 for (i = 0 ; i < n_rank_specs ; ++i )
112 printf("Ranks spec %d; Func: %d\n",i, rank_specs[i].rfunc);
114 for (j=0; j < sc->crit_cnt ; ++j )
115 printf("Dest var is \"%s\"\n", rank_specs[i].destvars[j]->name);
122 for (i = 0 ; i < n_rank_specs ; ++i )
124 free(rank_specs[i].destvars);
129 sort_destroy_criteria(sc);
136 /* Parser for the variables sub command
137 Returns 1 on success */
139 rank_custom_variables(struct cmd_rank *cmd UNUSED)
141 static const int terminators[2] = {T_BY, 0};
145 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
149 sc = sort_parse_criteria (default_dict, 0, 0, 0, terminators);
151 if ( lex_match(T_BY) )
153 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL))
158 if (!parse_variables (default_dict, &group_vars, &n_group_vars,
159 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
170 /* Return a name for a new variable which ranks the variable VAR_NAME,
171 according to the ranking function F.
172 If IDX is non zero, then IDX is used as a disambiguating number.
173 FIXME: This is not very robust.
176 new_variable_name(const char *ranked_var_name, enum RANK_FUNC f, int idx)
178 static char new_name[SHORT_NAME_LEN + 1];
179 char temp[SHORT_NAME_LEN + 1];
187 strcpy(new_name,"R");
193 strcpy(new_name,"N");
198 strcpy(new_name,"P");
202 strcpy(new_name,"S");
210 strncat(new_name, ranked_var_name, 7);
214 strncpy(temp, ranked_var_name, 3);
215 snprintf(new_name, SHORT_NAME_LEN, "%s%03d", temp, idx);
221 /* Parse the [/rank INTO var1 var2 ... varN ] clause */
223 parse_rank_function(struct cmd_rank *cmd UNUSED, enum RANK_FUNC f)
225 static const struct fmt_spec f8_2 = {FMT_F, 8, 2};
229 rank_specs = xnrealloc(rank_specs, n_rank_specs, sizeof *rank_specs);
230 rank_specs[n_rank_specs - 1].rfunc = f;
232 rank_specs[n_rank_specs - 1].destvars =
233 xcalloc (sc->crit_cnt, sizeof (struct variable *));
235 if (lex_match_id("INTO"))
237 struct variable *destvar;
239 while( token == T_ID )
242 if ( dict_lookup_var (default_dict, tokid) != NULL )
244 msg(ME, _("Variable %s already exists."), tokid);
247 if ( var_count > sc->crit_cnt )
249 msg(ME, _("Too many variables in INTO clause."));
253 destvar = dict_create_var (default_dict, tokid, 0);
256 destvar->print = destvar->write = f8_2;
259 rank_specs[n_rank_specs - 1].destvars[var_count - 1] = destvar ;
266 /* Allocate rank variable names to all those which haven't had INTO
267 variables assigned */
268 while (var_count < sc->crit_cnt)
271 struct variable *destvar ;
272 const struct variable *v = dict_get_var(default_dict,
273 sc->crits[var_count].fv);
278 new_name = new_variable_name(v->name, f, idx);
280 destvar = dict_create_var (default_dict, new_name, 0);
284 } while( !destvar ) ;
286 destvar->print = destvar->write = f8_2;
288 rank_specs[n_rank_specs - 1].destvars[var_count] = destvar ;
298 rank_custom_rank(struct cmd_rank *cmd )
300 return parse_rank_function(cmd, RANK);
304 rank_custom_normal(struct cmd_rank *cmd )
306 return parse_rank_function(cmd, NORMAL);
310 rank_custom_percent(struct cmd_rank *cmd )
312 return parse_rank_function(cmd, NORMAL);
316 rank_custom_rfraction(struct cmd_rank *cmd )
318 return parse_rank_function(cmd, RFRACTION);
322 rank_custom_proportion(struct cmd_rank *cmd )
324 return parse_rank_function(cmd, PROPORTION);
328 rank_custom_n(struct cmd_rank *cmd )
330 return parse_rank_function(cmd, N);
334 rank_custom_savage(struct cmd_rank *cmd )
336 return parse_rank_function(cmd, SAVAGE);
341 rank_custom_ntiles(struct cmd_rank *cmd )
343 if ( lex_force_match('(') )
345 if ( lex_force_int() )
348 lex_force_match(')');
356 return parse_rank_function(cmd, NTILES);