- lex_match (lexer, T_COMMA);
- }
- while (!lex_match (lexer, T_RBRACK));
- cats_end_ofs = lex_ofs (lexer) - 1;
- }
-
- struct ctables_category cat = {
- .type = CCT_VALUE,
- .include_missing = false,
- .sort_ascending = true,
- };
- bool show_totals = false;
- char *total_label = NULL;
- bool totals_before = false;
- while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
- {
- if (!c->n_cats && lex_match_id (lexer, "ORDER"))
- {
- lex_match (lexer, T_EQUALS);
- if (lex_match_id (lexer, "A"))
- cat.sort_ascending = true;
- else if (lex_match_id (lexer, "D"))
- cat.sort_ascending = false;
- else
- {
- lex_error_expecting (lexer, "A", "D");
- goto error;
- }
- }
- else if (!c->n_cats && lex_match_id (lexer, "KEY"))
- {
- int start_ofs = lex_ofs (lexer) - 1;
- lex_match (lexer, T_EQUALS);
- if (lex_match_id (lexer, "VALUE"))
- cat.type = CCT_VALUE;
- else if (lex_match_id (lexer, "LABEL"))
- cat.type = CCT_LABEL;
- else
- {
- cat.type = CCT_FUNCTION;
- if (!parse_ctables_summary_function (lexer, &cat.sort_function,
- &cat.weighting, &cat.area))
- goto error;
-
- if (lex_match (lexer, T_LPAREN))
- {
- cat.sort_var = parse_variable (lexer, dict);
- if (!cat.sort_var)
- goto error;
-
- if (cat.sort_function == CTSF_PTILE)
- {
- lex_match (lexer, T_COMMA);
- if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
- goto error;
- cat.percentile = lex_number (lexer);
- lex_get (lexer);
- }
-
- if (!lex_force_match (lexer, T_RPAREN))
- goto error;
- }
- else if (ctables_function_availability (cat.sort_function)
- == CTFA_SCALE)
- {
- bool UNUSED b = lex_force_match (lexer, T_LPAREN);
- goto error;
- }
-
- lex_ofs_error (lexer, start_ofs, lex_ofs (lexer) - 1,
- _("Data-dependent sorting is not implemented."));
- goto error;
- }
- }
- else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
- {
- lex_match (lexer, T_EQUALS);
- if (lex_match_id (lexer, "INCLUDE"))
- cat.include_missing = true;
- else if (lex_match_id (lexer, "EXCLUDE"))
- cat.include_missing = false;
- else
- {
- lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
- goto error;
- }
- }
- else if (lex_match_id (lexer, "TOTAL"))
- {
- lex_match (lexer, T_EQUALS);
- if (!parse_bool (lexer, &show_totals))
- goto error;
- }
- else if (lex_match_id (lexer, "LABEL"))
- {
- lex_match (lexer, T_EQUALS);
- if (!lex_force_string (lexer))
- goto error;
- free (total_label);
- total_label = ss_xstrdup (lex_tokss (lexer));
- lex_get (lexer);
- }
- else if (lex_match_id (lexer, "POSITION"))
- {
- lex_match (lexer, T_EQUALS);
- if (lex_match_id (lexer, "BEFORE"))
- totals_before = true;
- else if (lex_match_id (lexer, "AFTER"))
- totals_before = false;
- else
- {
- lex_error_expecting (lexer, "BEFORE", "AFTER");
- goto error;
- }
- }
- else if (lex_match_id (lexer, "EMPTY"))
- {
- lex_match (lexer, T_EQUALS);
- if (lex_match_id (lexer, "INCLUDE"))
- c->show_empty = true;
- else if (lex_match_id (lexer, "EXCLUDE"))
- c->show_empty = false;
- else
- {
- lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
- goto error;
- }
- }
- else
- {
- if (!c->n_cats)
- lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
- "TOTAL", "LABEL", "POSITION", "EMPTY");
- else
- lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
- goto error;
- }
- }
-
- if (!c->n_cats)
- {
- if (c->n_cats >= allocated_cats)
- c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
- c->cats[c->n_cats++] = cat;
- }
-
- if (show_totals)
- {
- if (c->n_cats >= allocated_cats)
- c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
-
- struct ctables_category *totals;
- if (totals_before)
- {
- insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
- totals = &c->cats[0];
- }
- else
- totals = &c->cats[c->n_cats];
- c->n_cats++;
-
- *totals = (struct ctables_category) {
- .type = CCT_TOTAL,
- .total_label = total_label ? total_label : xstrdup (_("Total")),
- };
- }
-
- struct ctables_category *subtotal = NULL;
- for (size_t i = totals_before ? 0 : c->n_cats;
- totals_before ? i < c->n_cats : i-- > 0;
- totals_before ? i++ : 0)
- {
- struct ctables_category *cat = &c->cats[i];
- switch (cat->type)
- {
- case CCT_NUMBER:
- case CCT_STRING:
- case CCT_NRANGE:
- case CCT_SRANGE:
- case CCT_MISSING:
- case CCT_OTHERNM:
- cat->subtotal = subtotal;
- break;
-
- case CCT_POSTCOMPUTE:
- break;
-
- case CCT_SUBTOTAL:
- subtotal = cat;
- break;
-
- case CCT_TOTAL:
- case CCT_VALUE:
- case CCT_LABEL:
- case CCT_FUNCTION:
- case CCT_EXCLUDED_MISSING:
- break;
- }
- }
-
- if (cats_start_ofs != -1)
- {
- for (size_t i = 0; i < c->n_cats; i++)
- {
- struct ctables_category *cat = &c->cats[i];
- switch (cat->type)
- {
- case CCT_POSTCOMPUTE:
- cat->parse_format = parse_strings ? common_format->type : FMT_F;
- struct msg_location *cats_location
- = lex_ofs_location (lexer, cats_start_ofs, cats_end_ofs);
- bool ok = ctables_recursive_check_postcompute (
- dict, cat->pc->expr, cat, c, cats_location);
- msg_location_destroy (cats_location);
- if (!ok)
- goto error;
- break;
-
- case CCT_NUMBER:
- case CCT_NRANGE:
- for (size_t j = 0; j < n_vars; j++)
- if (var_is_alpha (vars[j]))
- {
- msg_at (SE, cat->location,
- _("This category specification may be applied "
- "only to numeric variables, but this "
- "subcommand tries to apply it to string "
- "variable %s."),
- var_get_name (vars[j]));
- goto error;
- }
- break;
-
- case CCT_STRING:
- if (parse_strings)
- {
- double n;
- if (!parse_category_string (cat->location, cat->string, dict,
- common_format->type, &n))
- goto error;
-
- ss_dealloc (&cat->string);
-
- cat->type = CCT_NUMBER;
- cat->number = n;
- }
- else if (!all_strings (vars, n_vars, cat))
- goto error;
- break;
-
- case CCT_SRANGE:
- if (parse_strings)
- {
- double n[2];
-
- if (!cat->srange[0].string)
- n[0] = -DBL_MAX;
- else if (!parse_category_string (cat->location,
- cat->srange[0], dict,
- common_format->type, &n[0]))
- goto error;
-
- if (!cat->srange[1].string)
- n[1] = DBL_MAX;
- else if (!parse_category_string (cat->location,
- cat->srange[1], dict,
- common_format->type, &n[1]))
- goto error;
-
- ss_dealloc (&cat->srange[0]);
- ss_dealloc (&cat->srange[1]);
-
- cat->type = CCT_NRANGE;
- cat->nrange[0] = n[0];
- cat->nrange[1] = n[1];
- }
- else if (!all_strings (vars, n_vars, cat))
- goto error;
- break;
-
- case CCT_MISSING:
- case CCT_OTHERNM:
- case CCT_SUBTOTAL:
- case CCT_TOTAL:
- case CCT_VALUE:
- case CCT_LABEL:
- case CCT_FUNCTION:
- case CCT_EXCLUDED_MISSING:
- break;
- }
- }
- }
-
- free (vars);
- return true;
-
-error:
- free (vars);
- return false;
-}
-\f
-union ctables_summary
- {
- /* COUNT, VALIDN, TOTALN. */
- double count;
-
- /* MINIMUM, MAXIMUM, RANGE. */
- struct
- {
- double min;
- double max;
- };
-
- /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
- struct moments1 *moments;
-
- /* MEDIAN, MODE, PTILE. */
- struct
- {
- struct casewriter *writer;
- double ovalid;
- double ovalue;
- };
- };
-
-static void
-ctables_summary_init (union ctables_summary *s,
- const struct ctables_summary_spec *ss)
-{
- switch (ss->function)
- {
- case CTSF_COUNT:
- case CTSF_areaPCT_COUNT:
- case CTSF_areaPCT_VALIDN:
- case CTSF_areaPCT_TOTALN:
- case CTSF_MISSING:
- case CTSF_TOTALN:
- case CTSF_VALIDN:
- s->count = 0;
- break;
-
- case CTSF_areaID:
- break;
-
- case CTSF_MAXIMUM:
- case CTSF_MINIMUM:
- case CTSF_RANGE:
- s->min = s->max = SYSMIS;
- break;
-
- case CTSF_MEAN:
- case CTSF_SUM:
- case CTSF_areaPCT_SUM:
- s->moments = moments1_create (MOMENT_MEAN);
- break;