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"
29 #define _(msgid) gettext (msgid)
45 +missing=miss:!exclude/include.
68 struct variable **destvars;
69 struct variable *srcvar;
73 static struct rank_spec *rank_specs;
74 static int n_rank_specs;
76 static struct sort_criteria *sc;
78 static struct variable **group_vars;
79 static int n_group_vars;
81 static struct cmd_rank cmd;
93 if ( !parse_rank(&cmd) )
97 for (i = 0 ; i < sc->crit_cnt ; ++i )
99 struct sort_criterion *crit = &sc->crits[i];
101 printf("Dir: %d; Index: %d\n", crit->dir, crit->fv);
104 for (i = 0 ; i < n_group_vars ; ++i )
105 printf("Group var: %s\n",group_vars[0]->name);
107 for (i = 0 ; i < n_rank_specs ; ++i )
110 printf("Ranks spec %d; Func: %d\n",i, rank_specs[i].rfunc);
112 for (j=0; j < sc->crit_cnt ; ++j )
113 printf("Dest var is \"%s\"\n", rank_specs[i].destvars[j]->name);
120 for (i = 0 ; i < n_rank_specs ; ++i )
122 free(rank_specs[i].destvars);
127 sort_destroy_criteria(sc);
134 /* Parser for the variables sub command
135 Returns 1 on success */
137 rank_custom_variables(struct cmd_rank *cmd UNUSED)
139 static const int terminators[2] = {T_BY, 0};
143 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
147 sc = sort_parse_criteria (default_dict, 0, 0, 0, terminators);
149 if ( lex_match(T_BY) )
151 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL))
156 if (!parse_variables (default_dict, &group_vars, &n_group_vars,
157 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
168 /* Return a name for a new variable which ranks the variable VAR_NAME,
169 according to the ranking function F.
170 If IDX is non zero, then IDX is used as a disambiguating number.
171 FIXME: This is not very robust.
174 new_variable_name(const char *ranked_var_name, enum RANK_FUNC f, int idx)
176 static char new_name[SHORT_NAME_LEN + 1];
177 char temp[SHORT_NAME_LEN + 1];
185 strcpy(new_name,"R");
191 strcpy(new_name,"N");
196 strcpy(new_name,"P");
200 strcpy(new_name,"S");
208 strncat(new_name, ranked_var_name, 7);
212 strncpy(temp, ranked_var_name, 3);
213 snprintf(new_name, SHORT_NAME_LEN, "%s%03d", temp, idx);
219 /* Parse the [/rank INTO var1 var2 ... varN ] clause */
221 parse_rank_function(struct cmd_rank *cmd UNUSED, enum RANK_FUNC f)
223 static const struct fmt_spec f8_2 = {FMT_F, 8, 2};
227 rank_specs = xrealloc(rank_specs, n_rank_specs * sizeof *rank_specs);
228 rank_specs[n_rank_specs - 1].rfunc = f;
230 rank_specs[n_rank_specs - 1].destvars =
231 xcalloc(sc->crit_cnt ,sizeof (struct variable *));
233 if (lex_match_id("INTO"))
235 struct variable *destvar;
237 while( token == T_ID )
240 if ( dict_lookup_var (default_dict, tokid) != NULL )
242 msg(ME, _("Variable %s already exists."), tokid);
245 if ( var_count > sc->crit_cnt )
247 msg(ME, _("Too many variables in INTO clause."));
251 destvar = dict_create_var (default_dict, tokid, 0);
254 destvar->print = destvar->write = f8_2;
257 rank_specs[n_rank_specs - 1].destvars[var_count - 1] = destvar ;
264 /* Allocate rank variable names to all those which haven't had INTO
265 variables assigned */
266 while (var_count < sc->crit_cnt)
269 struct variable *destvar ;
270 const struct variable *v = dict_get_var(default_dict,
271 sc->crits[var_count].fv);
276 new_name = new_variable_name(v->name, f, idx);
278 destvar = dict_create_var (default_dict, new_name, 0);
282 } while( !destvar ) ;
284 destvar->print = destvar->write = f8_2;
286 rank_specs[n_rank_specs - 1].destvars[var_count] = destvar ;
296 rank_custom_rank(struct cmd_rank *cmd )
298 return parse_rank_function(cmd, RANK);
302 rank_custom_normal(struct cmd_rank *cmd )
304 return parse_rank_function(cmd, NORMAL);
308 rank_custom_percent(struct cmd_rank *cmd )
310 return parse_rank_function(cmd, NORMAL);
314 rank_custom_rfraction(struct cmd_rank *cmd )
316 return parse_rank_function(cmd, RFRACTION);
320 rank_custom_proportion(struct cmd_rank *cmd )
322 return parse_rank_function(cmd, PROPORTION);
326 rank_custom_n(struct cmd_rank *cmd )
328 return parse_rank_function(cmd, N);
332 rank_custom_savage(struct cmd_rank *cmd )
334 return parse_rank_function(cmd, SAVAGE);
339 rank_custom_ntiles(struct cmd_rank *cmd )
341 if ( lex_force_match('(') )
343 if ( lex_force_int() )
346 lex_force_match(')');
354 return parse_rank_function(cmd, NTILES);