/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
/* Default encoding for reading syntax files. */
char *syntax_encoding;
-}; /* struct dataset */
-
+};
static void add_case_limit_trns (struct dataset *ds);
static void add_filter_trns (struct dataset *ds);
if (ds->callback) ds->callback (ds->cb_data);
}
+static void
+dict_callback (struct dictionary *d UNUSED, void *ds_)
+{
+ struct dataset *ds = ds_;
+ dataset_set_unsaved (ds);
+}
\f
-/* Public functions. */
+/* Creates and returns a new dataset. The dataset initially has an empty
+ dictionary and no data source. */
+struct dataset *
+dataset_create (void)
+{
+ struct dataset *ds;
+
+ ds = xzalloc (sizeof *ds);
+ ds->dict = dict_create ();
+ dict_set_change_callback (ds->dict, dict_callback, ds);
+ dict_set_encoding (ds->dict, get_default_encoding ());
+
+ ds->caseinit = caseinit_create ();
+ proc_cancel_all_transformations (ds);
+ ds->syntax_encoding = xstrdup ("Auto");
+ return ds;
+}
+/* Destroys DS. */
+void
+dataset_destroy (struct dataset *ds)
+{
+ if (ds != NULL)
+ {
+ dataset_clear (ds);
+ dict_destroy (ds->dict);
+ caseinit_destroy (ds->caseinit);
+ trns_chain_destroy (ds->permanent_trns_chain);
+
+ if ( ds->xform_callback)
+ ds->xform_callback (false, ds->xform_callback_aux);
+ free (ds->syntax_encoding);
+ free (ds);
+ }
+}
+
+/* Discards the active file dictionary, data, and transformations. */
+void
+dataset_clear (struct dataset *ds)
+{
+ assert (ds->proc_state == PROC_COMMITTED);
+
+ dict_clear (ds->dict);
+ fh_set_default_handle (NULL);
+
+ ds->n_lag = 0;
+
+ casereader_destroy (ds->source);
+ ds->source = NULL;
+
+ proc_cancel_all_transformations (ds);
+}
+
+/* Returns the dictionary within DS. This is always nonnull, although it
+ might not contain any variables. */
+struct dictionary *
+dataset_dict (const struct dataset *ds)
+{
+ return ds->dict;
+}
+
+/* Replaces DS's dictionary by DICT, discarding any source and
+ transformations. */
+void
+dataset_set_dict (struct dataset *ds, struct dictionary *dict)
+{
+ assert (ds->proc_state == PROC_COMMITTED);
+ assert (ds->dict != dict);
+
+ dataset_clear (ds);
+
+ dict_destroy (ds->dict);
+ ds->dict = dict;
+ dict_set_change_callback (ds->dict, dict_callback, ds);
+}
+
+/* Returns the casereader that will be read when a procedure is executed on
+ DS. This can be NULL if none has been set up yet. */
+const struct casereader *
+dataset_source (const struct dataset *ds)
+{
+ return ds->source;
+}
+
+/* Returns true if DS has a data source, false otherwise. */
+bool
+dataset_has_source (const struct dataset *ds)
+{
+ return dataset_source (ds) != NULL;
+}
+
+/* Replaces the active file's data by READER. READER's cases must have an
+ appropriate format for DS's dictionary. */
+bool
+dataset_set_source (struct dataset *ds, struct casereader *reader)
+{
+ casereader_destroy (ds->source);
+ ds->source = reader;
+
+ caseinit_clear (ds->caseinit);
+ caseinit_mark_as_preinited (ds->caseinit, ds->dict);
+
+ return reader == NULL || !casereader_error (reader);
+}
+
+/* Returns the data source from DS and removes it from DS. Returns a null
+ pointer if DS has no data source. */
+struct casereader *
+dataset_steal_source (struct dataset *ds)
+{
+ struct casereader *reader = ds->source;
+ ds->source = NULL;
+
+ return reader;
+}
+\f
void
dataset_set_callback (struct dataset *ds, void (*cb) (void *), void *cb_data)
{
ds->proc_state = PROC_CLOSED;
ds->ok = casereader_destroy (ds->source) && ds->ok;
ds->source = NULL;
- proc_set_active_file_data (ds, NULL);
+ dataset_set_source (ds, NULL);
}
/* Must return false if the source casereader, a transformation,
}
\f
-static void
-dict_callback (struct dictionary *d UNUSED, void *ds_)
-{
- struct dataset *ds = ds_;
- dataset_set_unsaved (ds);
-}
-
-/* Initializes procedure handling. */
-struct dataset *
-create_dataset (void)
-{
- struct dataset *ds = xzalloc (sizeof(*ds));
- ds->dict = dict_create ();
-
- dict_set_change_callback (ds->dict, dict_callback, ds);
-
- dict_set_encoding (ds->dict, get_default_encoding ());
-
- ds->caseinit = caseinit_create ();
- proc_cancel_all_transformations (ds);
-
- ds->syntax_encoding = xstrdup ("Auto");
-
- return ds;
-}
-
-
void
dataset_add_transform_change_callback (struct dataset *ds,
transformation_change_callback_func *cb,
ds->xform_callback_aux = aux;
}
-/* Finishes up procedure handling. */
-void
-destroy_dataset (struct dataset *ds)
-{
- proc_discard_active_file (ds);
- dict_destroy (ds->dict);
- caseinit_destroy (ds->caseinit);
- trns_chain_destroy (ds->permanent_trns_chain);
-
- if ( ds->xform_callback)
- ds->xform_callback (false, ds->xform_callback_aux);
-
- free (ds->syntax_encoding);
- free (ds);
-}
-
/* Causes output from the next procedure to be discarded, instead
of being preserved for use as input for the next procedure. */
void
ds->discard_output = true;
}
-/* Discards the active file dictionary, data, and
- transformations. */
-void
-proc_discard_active_file (struct dataset *ds)
-{
- assert (ds->proc_state == PROC_COMMITTED);
-
- dict_clear (ds->dict);
- fh_set_default_handle (NULL);
-
- ds->n_lag = 0;
-
- casereader_destroy (ds->source);
- ds->source = NULL;
-
- proc_cancel_all_transformations (ds);
-}
-
-/* Sets SOURCE as the source for procedure input for the next
- procedure. */
-void
-proc_set_active_file (struct dataset *ds,
- struct casereader *source,
- struct dictionary *dict)
-{
- assert (ds->proc_state == PROC_COMMITTED);
- assert (ds->dict != dict);
-
- proc_discard_active_file (ds);
-
- dict_destroy (ds->dict);
- ds->dict = dict;
- dict_set_change_callback (ds->dict, dict_callback, ds);
-
- proc_set_active_file_data (ds, source);
-}
-
-/* Replaces the active file's data by READER without replacing
- the associated dictionary. */
-bool
-proc_set_active_file_data (struct dataset *ds, struct casereader *reader)
-{
- casereader_destroy (ds->source);
- ds->source = reader;
-
- caseinit_clear (ds->caseinit);
- caseinit_mark_as_preinited (ds->caseinit, ds->dict);
-
- return reader == NULL || !casereader_error (reader);
-}
-
-/* Returns true if an active file data source is available, false
- otherwise. */
-bool
-proc_has_active_file (const struct dataset *ds)
-{
- return ds->source != NULL;
-}
-
-/* Returns the active file data source from DS, or a null pointer
- if DS has no data source, and removes it from DS. */
-struct casereader *
-proc_extract_active_file_data (struct dataset *ds)
-{
- struct casereader *reader = ds->source;
- ds->source = NULL;
-
- return reader;
-}
/* Checks whether DS has a corrupted active file. If so,
discards it and returns false. If not, returns true without
{
if (casereader_error (ds->source))
{
- proc_discard_active_file (ds);
+ dataset_clear (ds);
return false;
}
else
}
-struct dictionary *
-dataset_dict (const struct dataset *ds)
-{
- return ds->dict;
-}
-
-const struct casereader *
-dataset_source (const struct dataset *ds)
-{
- return ds->source;
-}
-
void
dataset_need_lag (struct dataset *ds, int n_before)
{
struct dataset;
struct dictionary;
\f
+struct dataset *dataset_create (void);
+void dataset_destroy (struct dataset *);
+
+void dataset_clear (struct dataset *);
+
+struct dictionary *dataset_dict (const struct dataset *);
+void dataset_set_dict (struct dataset *, struct dictionary *);
+
+const struct casereader *dataset_source (const struct dataset *);
+bool dataset_has_source (const struct dataset *ds);
+bool dataset_set_source (struct dataset *, struct casereader *);
+struct casereader *dataset_steal_source (struct dataset *);
+
+void dataset_set_callback (struct dataset *, void (*cb) (void *), void *);
+
+void dataset_set_default_syntax_encoding (struct dataset *, const char *);
+const char *dataset_get_default_syntax_encoding (const struct dataset *);
+\f
/* Transformations. */
void add_transformation (struct dataset *ds,
typedef void transformation_change_callback_func (bool non_empty, void *aux);
-struct dataset * create_dataset (void);
-
-void destroy_dataset (struct dataset *);
void dataset_add_transform_change_callback (struct dataset *,
transformation_change_callback_func *, void *);
-void proc_discard_active_file (struct dataset *);
-void proc_set_active_file (struct dataset *,
- struct casereader *, struct dictionary *);
-bool proc_set_active_file_data (struct dataset *, struct casereader *);
-bool proc_has_active_file (const struct dataset *ds);
-struct casereader *proc_extract_active_file_data (struct dataset *);
-
void proc_discard_output (struct dataset *ds);
bool proc_execute (struct dataset *ds);
bool dataset_end_of_command (struct dataset *);
\f
-struct dictionary *dataset_dict (const struct dataset *ds);
-const struct casereader *dataset_source (const struct dataset *ds);
-
const struct ccase *lagged_case (const struct dataset *ds, int n_before);
void dataset_need_lag (struct dataset *ds, int n_before);
-void dataset_set_callback (struct dataset *ds, void (*cb) (void *), void *);
-
-void dataset_set_default_syntax_encoding (struct dataset *, const char *);
-const char *dataset_get_default_syntax_encoding (const struct dataset *);
-
#endif /* dataset.h */
{
const struct dictionary *dict = dataset_dict (ds);
return cmd_parse_in_state (lexer, ds,
- proc_has_active_file (ds) &&
+ dataset_has_source (ds) &&
dict_get_var_cnt (dict) > 0 ?
CMD_STATE_DATA : CMD_STATE_INITIAL);
}
int
cmd_new_file (struct lexer *lexer UNUSED, struct dataset *ds)
{
- proc_discard_active_file (ds);
+ dataset_clear (ds);
return CMD_SUCCESS;
}
if (lex_match (lexer, T_ASTERISK))
{
- if (!proc_has_active_file (ds))
+ if (!dataset_has_source (ds))
{
msg (SE, _("Cannot specify the active file since no active "
"file has been defined."));
if (active_file != NULL)
proc_commit (ds);
- proc_set_active_file (ds, casewriter_make_reader (proc.output), proc.dict);
+ dataset_set_dict (ds, proc.dict);
+ dataset_set_source (ds, casewriter_make_reader (proc.output));
proc.dict = NULL;
proc.output = NULL;
casereader = casereader_create_sequential (NULL, r->proto,
CASENUMBER_MAX,
&data_parser_casereader_class, r);
- proc_set_active_file (ds, casereader, dict);
+ dataset_set_dict (ds, dict);
+ dataset_set_source (ds, casereader);
}
static struct ccase *
struct casereader *reader = psql_open_reader (&psql, &dict);
if ( reader )
- proc_set_active_file (ds, reader, dict);
+ {
+ dataset_set_dict (ds, dict);
+ dataset_set_source (ds, reader);
+ }
}
ds_destroy (&psql.sql);
struct casereader *reader = gnumeric_open_reader (&gri, &dict);
if ( reader )
- proc_set_active_file (ds, reader, dict);
+ {
+ dataset_set_dict (ds, dict);
+ dataset_set_source (ds, reader);
+ }
}
free (gri.file_name);
if (map != NULL)
reader = case_map_create_input_translator (map, reader);
- proc_set_active_file (ds, reader, dict);
+ dataset_set_dict (ds, dict);
+ dataset_set_source (ds, reader);
fh_unref (fh);
return CMD_SUCCESS;
struct input_program_pgm *inp;
bool saw_END_CASE = false;
- proc_discard_active_file (ds);
+ dataset_clear (ds);
if (!lex_match (lexer, T_ENDCMD))
return lex_end_of_command (lexer);
if (result == CMD_EOF)
msg (SE, _("Unexpected end-of-file within INPUT PROGRAM."));
inside_input_program = false;
- proc_discard_active_file (ds);
+ dataset_clear (ds);
destroy_input_program (inp);
return result;
}
if (dict_get_next_value_idx (dataset_dict (ds)) == 0)
{
msg (SE, _("Input program did not create any variables."));
- proc_discard_active_file (ds);
+ dataset_clear (ds);
destroy_input_program (inp);
return CMD_FAILURE;
}
caseinit_mark_for_init (inp->init, dataset_dict (ds));
inp->proto = caseproto_ref (dict_get_proto (dataset_dict (ds)));
- proc_set_active_file_data (
+ dataset_set_source (
ds, casereader_create_sequential (NULL, inp->proto, CASENUMBER_MAX,
&input_program_casereader_class, inp));
if ( ds == NULL )
{
- ds = create_dataset ();
+ ds = dataset_create ();
d = dataset_dict (ds);
}
retval = CMD_SUCCESS;
done:
- if (ds)
- destroy_dataset (ds);
+ dataset_destroy (ds);
case_unref (c);
if (next_input == NULL)
goto error;
- proc_set_active_file (ds, next_input, agr.dict);
+ dataset_set_dict (ds, agr.dict);
+ dataset_set_source (ds, next_input);
agr.dict = NULL;
}
else
/* Flip the data we read. */
if (!ok || !flip_file (flip))
{
- proc_discard_active_file (ds);
+ dataset_clear (ds);
goto error;
}
reader = casereader_create_sequential (NULL, dict_get_proto (dict),
flip->n_vars,
&flip_casereader_class, flip);
- proc_set_active_file_data (ds, reader);
+ dataset_set_source (ds, reader);
return CMD_SUCCESS;
error:
}
ok = casegrouper_destroy (split_grouper);
ok = proc_commit (ds) && ok;
- ok = (proc_set_active_file_data (ds, casewriter_make_reader (output))
+ ok = (dataset_set_source (ds, casewriter_make_reader (output))
&& ok);
if (!ok)
break;
result = proc_commit (ds) && result;
dict_delete_var (dataset_dict (ds), order);
- result = proc_set_active_file_data (ds, sorted) && result;
+ result = dataset_set_source (ds, sorted) && result;
}
rank_cleanup();
proc_discard_output (ds);
output = sort_execute (proc_open (ds), &ordering);
ok = proc_commit (ds);
- ok = proc_set_active_file_data (ds, output) && ok;
+ ok = dataset_set_source (ds, output) && ok;
done:
min_buffers = 64;
/* PSPPIRE - a graphical user interface for PSPP.
- Copyright (C) 2007, 2009, 2010 Free Software Foundation
+ Copyright (C) 2007, 2009, 2010, 2011 Free Software Foundation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
reader = lazy_casereader_create (proto, case_cnt,
create_casereader_from_data_store,
the_data_store, &lazy_serial);
- proc_set_active_file_data (the_dataset, reader);
+ dataset_set_source (the_dataset, reader);
- g_return_val_if_fail (proc_has_active_file (the_dataset), FALSE);
+ g_return_val_if_fail (dataset_has_source (the_dataset), FALSE);
lexer = lex_create ();
psppire_set_lexer (lexer);
psppire_dict_replace_dictionary (the_data_store->dict,
dataset_dict (the_dataset));
- reader = proc_extract_active_file_data (the_dataset);
+ reader = dataset_steal_source (the_dataset);
if (!lazy_casereader_destroy (reader, lazy_serial))
psppire_data_store_set_reader (the_data_store, reader);
/* PSPPIRE - a graphical user interface for PSPP.
- Copyright (C) 2004, 2005, 2006, 2009, 2010 Free Software Foundation
+ Copyright (C) 2004, 2005, 2006, 2009, 2010, 2011 Free Software Foundation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
settings_init ();
fh_init ();
- the_dataset = create_dataset ();
+ the_dataset = dataset_create ();
psppire_set_lexer (NULL);
dictionary = psppire_dict_new_from_dict (dataset_dict (the_dataset));
random_init ();
lexer = lex_create ();
- the_dataset = create_dataset ();
+ the_dataset = dataset_create ();
parser = argv_parser_create ();
terminal_opts = terminal_opts_init (parser, &syntax_mode, &process_statrc,
}
- destroy_dataset (the_dataset);
+ dataset_destroy (the_dataset);
random_done ();
settings_done ();