Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / ui / gui / psppire-case-file.c
index cadcffd758b920dc17788de68d216109b94c7ee8..2edfec89ae0a036c3791309ba432e16567839706 100644 (file)
@@ -1,39 +1,38 @@
-/*
-    PSPPIRE --- A Graphical User Interface for PSPP
-    Copyright (C) 2006  Free Software Foundation
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2006  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
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
+   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
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301, USA. */
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 #include <string.h>
 #include <stdlib.h>
 
-#include "psppire-object.h"
 #include "psppire-case-file.h"
 
 #include <gtksheet/gtkextra-marshal.h>
 
+#include <data/format.h>
 #include <data/case.h>
-#include <ui/flexifile.h>
-#include "flexifile-factory.h"
-#include <data/casefile.h>
 #include <data/data-in.h>
+#include <data/datasheet.h>
+#include <data/casereader.h>
 #include <math/sort.h>
 #include <libpspp/misc.h>
 
+#include "xalloc.h"
+#include "xmalloca.h"
+
 /* --- prototypes --- */
 static void psppire_case_file_class_init       (PsppireCaseFileClass   *class);
 static void psppire_case_file_init     (PsppireCaseFile        *case_file);
@@ -48,7 +47,7 @@ enum  {CASE_CHANGED,
        CASES_DELETED,
        n_SIGNALS};
 
-static guint signal[n_SIGNALS];
+static guint signal[n_SIGNALS];
 
 
 /* --- functions --- */
@@ -75,26 +74,101 @@ psppire_case_file_get_type (void)
        (GInstanceInitFunc) psppire_case_file_init,
       };
 
-      object_type = g_type_register_static (G_TYPE_PSPPIRE_OBJECT, "PsppireCaseFile",
+      object_type = g_type_register_static (G_TYPE_OBJECT, "PsppireCaseFile",
                                            &object_info, 0);
     }
 
   return object_type;
 }
 
+/* Properties */
+enum
+{
+  PROP_0,
+  PROP_DATASHEET,
+  PROP_READER
+};
+
+
+
+
+static void
+psppire_case_file_set_property (GObject         *object,
+                               guint            prop_id,
+                               const GValue    *value,
+                               GParamSpec      *pspec)
+
+{
+  PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object);
+
+  switch (prop_id)
+    {
+    case PROP_READER:
+      cf->datasheet = datasheet_create (g_value_get_pointer (value));
+      cf->accessible = TRUE;
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    };
+}
+
+static void
+psppire_case_file_get_property (GObject         *object,
+                               guint            prop_id,
+                               GValue          *value,
+                               GParamSpec      *pspec)
+{
+  PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object);
+
+  switch (prop_id)
+    {
+    case PROP_DATASHEET:
+      g_value_set_pointer (value, cf->datasheet);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    };
+}
+
 
 static void
 psppire_case_file_class_init (PsppireCaseFileClass *class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GParamSpec *datasheet_spec ;
+  GParamSpec *reader_spec ;
 
   parent_class = g_type_class_peek_parent (class);
 
   object_class->finalize = psppire_case_file_finalize;
 
-  signal[CASE_CHANGED] =
-    g_signal_new ("case_changed",
-                 G_TYPE_FROM_CLASS(class),
+  datasheet_spec =
+    g_param_spec_pointer ("datasheet",
+                         "Datasheet",
+                         "A pointer to the datasheet belonging to this object",
+                         G_PARAM_READABLE );
+  reader_spec =
+    g_param_spec_pointer ("casereader",
+                         "CaseReader",
+                         "A pointer to the case reader from which this object is constructed",
+                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE );
+
+  object_class->set_property = psppire_case_file_set_property;
+  object_class->get_property = psppire_case_file_get_property;
+
+  g_object_class_install_property (object_class,
+                                   PROP_DATASHEET,
+                                   datasheet_spec);
+
+  g_object_class_install_property (object_class,
+                                   PROP_READER,
+                                   reader_spec);
+
+  signals [CASE_CHANGED] =
+    g_signal_new ("case-changed",
+                 G_TYPE_FROM_CLASS (class),
                  G_SIGNAL_RUN_FIRST,
                  0,
                  NULL, NULL,
@@ -104,9 +178,9 @@ psppire_case_file_class_init (PsppireCaseFileClass *class)
                  G_TYPE_INT);
 
 
-  signal[CASE_INSERTED] =
-    g_signal_new ("case_inserted",
-                 G_TYPE_FROM_CLASS(class),
+  signal[CASE_INSERTED] =
+    g_signal_new ("case-inserted",
+                 G_TYPE_FROM_CLASS (class),
                  G_SIGNAL_RUN_FIRST,
                  0,
                  NULL, NULL,
@@ -116,9 +190,9 @@ psppire_case_file_class_init (PsppireCaseFileClass *class)
                  G_TYPE_INT);
 
 
-  signal[CASES_DELETED] =
-    g_signal_new ("cases_deleted",
-                 G_TYPE_FROM_CLASS(class),
+  signal[CASES_DELETED] =
+    g_signal_new ("cases-deleted",
+                 G_TYPE_FROM_CLASS (class),
                  G_SIGNAL_RUN_FIRST,
                  0,
                  NULL, NULL,
@@ -133,8 +207,8 @@ psppire_case_file_finalize (GObject *object)
 {
   PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object);
 
-  if ( cf->flexifile)
-    casefile_destroy (cf->flexifile);
+  if ( cf->accessible)
+    datasheet_destroy (cf->datasheet);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -142,7 +216,8 @@ psppire_case_file_finalize (GObject *object)
 static void
 psppire_case_file_init (PsppireCaseFile *cf)
 {
-  cf->flexifile = 0;
+  cf->datasheet = NULL;
+  cf->accessible = FALSE;
 }
 
 
@@ -153,241 +228,221 @@ psppire_case_file_init (PsppireCaseFile *cf)
  * Creates a new #PsppireCaseFile.
  */
 PsppireCaseFile*
-psppire_case_file_new (gint val_cnt)
+psppire_case_file_new (struct casereader *reader)
 {
-  PsppireCaseFile *cf = g_object_new (G_TYPE_PSPPIRE_CASE_FILE, NULL);
-
-  cf->flexifile = flexifile_create (val_cnt);
-
-  return cf;
-}
-
-
-/**
- * psppire_case_file_new_from_flexifile:
- * @returns: a new #PsppireCaseFile object
- *
- * Creates a new #PsppireCaseFile from an existing flexifile
- */
-PsppireCaseFile*
-psppire_case_file_new_from_flexifile (struct flexifile *ff)
-{
-  PsppireCaseFile *cf = g_object_new (G_TYPE_PSPPIRE_CASE_FILE, NULL);
-
-  cf->flexifile = (struct casefile *) ff;
-
-  return cf;
+  return g_object_new (G_TYPE_PSPPIRE_CASE_FILE,
+                      "casereader", reader,
+                      NULL);
 }
 
 
 gboolean
-psppire_case_file_delete_cases (PsppireCaseFile *cf, gint n_cases, gint first)
+psppire_case_file_delete_cases (PsppireCaseFile *cf, casenumber n_cases, casenumber first)
 {
-  int result;
-
   g_return_val_if_fail (cf, FALSE);
-  g_return_val_if_fail (cf->flexifile, FALSE);
+  g_return_val_if_fail (cf->datasheet, FALSE);
+  g_return_val_if_fail (cf->accessible, FALSE);
 
-  result =  flexifile_delete_cases (FLEXIFILE(cf->flexifile), n_cases,  first);
+  g_return_val_if_fail (first + n_cases <=
+                       psppire_case_file_get_case_count (cf), FALSE);
 
-  g_signal_emit (cf, signal[CASES_DELETED], 0, n_cases, first);
+  datasheet_delete_rows (cf->datasheet, first, n_cases);
 
-  return result;
+  g_signal_emit (cf, signals [CASES_DELETED], 0, first, n_cases);
+
+  return TRUE;
 }
 
 /* Insert case CC into the case file before POSN */
 gboolean
 psppire_case_file_insert_case (PsppireCaseFile *cf,
-                             struct ccase *cc,
-                             gint posn)
+                              struct ccase *cc,
+                              casenumber posn)
 {
+  struct ccase tmp;
   bool result ;
 
   g_return_val_if_fail (cf, FALSE);
-  g_return_val_if_fail (cf->flexifile, FALSE);
+  g_return_val_if_fail (cf->datasheet, FALSE);
+  g_return_val_if_fail (cf->accessible, FALSE);
 
-  result = flexifile_insert_case (FLEXIFILE(cf->flexifile), cc, posn);
+  case_clone (&tmp, cc);
+  result = datasheet_insert_rows (cf->datasheet, posn, &tmp, 1);
 
   if ( result )
-    g_signal_emit (cf, signal[CASE_INSERTED], 0, posn);
+    g_signal_emit (cf, signal[CASE_INSERTED], 0, posn);
   else
-    g_warning ("Cannot insert case at position %d\n", posn);
+    g_warning ("Cannot insert case at position %ld\n", posn);
 
   return result;
 }
 
 
-/* Append a case to the case file */
-gboolean
-psppire_case_file_append_case (PsppireCaseFile *cf,
-                             struct ccase *c)
-{
-  bool result ;
-  gint posn ;
-
-  g_return_val_if_fail (cf, FALSE);
-  g_return_val_if_fail (cf->flexifile, FALSE);
-
-  posn = casefile_get_case_cnt (cf->flexifile);
-
-  result = casefile_append (cf->flexifile, c);
-
-  g_signal_emit (cf, signal[CASE_INSERTED], 0, posn);
-
-  return result;
-}
-
-
-inline gint
+casenumber
 psppire_case_file_get_case_count (const PsppireCaseFile *cf)
 {
   g_return_val_if_fail (cf, FALSE);
+  g_return_val_if_fail (cf->accessible, FALSE);
 
-  if ( ! cf->flexifile)
+  if ( ! cf->datasheet)
     return 0;
 
-  return casefile_get_case_cnt (cf->flexifile);
+  return datasheet_get_row_cnt (cf->datasheet);
 }
 
-/* Return the IDXth value from case CASENUM.
-   The return value must not be freed or written to
- */
-const union value *
-psppire_case_file_get_value (const PsppireCaseFile *cf, gint casenum, gint idx)
+/* Copies the IDXth value from case CASENUM into VALUE.
+   If VALUE is null, then memory is allocated is allocated with
+   malloc.  Returns the value if successful, NULL on failure. */
+union value *
+psppire_case_file_get_value (const PsppireCaseFile *cf,
+                             casenumber casenum, size_t idx,
+                             union value *value, int width)
 {
-  const union value *v;
-  struct ccase c;
-
-  g_return_val_if_fail (cf, NULL);
-  g_return_val_if_fail (cf->flexifile, NULL);
-
-  g_return_val_if_fail (idx < casefile_get_value_cnt (cf->flexifile), NULL);
+  bool allocated;
 
-  flexifile_get_case (FLEXIFILE(cf->flexifile), casenum, &c);
+  g_return_val_if_fail (cf, false);
+  g_return_val_if_fail (cf->datasheet, false);
 
-  v = case_data_idx (&c, idx);
-  case_destroy (&c);
+  g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), false);
 
-  return v;
+  if (value == NULL)
+    {
+      value = xnmalloc (value_cnt_from_width (width), sizeof *value);
+      allocated = true;
+    }
+  else
+    allocated = false;
+  if (!datasheet_get_value (cf->datasheet, casenum, idx, value, width))
+    {
+      if (allocated)
+        free (value);
+      value = NULL;
+    }
+  return value;
 }
 
 void
 psppire_case_file_clear (PsppireCaseFile *cf)
 {
-  casefile_destroy (cf->flexifile);
-  cf->flexifile = 0;
-  g_signal_emit (cf, signal[CASES_DELETED], 0, 0, -1);
+  datasheet_destroy (cf->datasheet);
+  cf->datasheet = NULL;
+  g_signal_emit (cf, signal[CASES_DELETED], 0, 0, -1);
 }
 
-/* Set the IDXth value of case C to SYSMIS/EMPTY */
+/* Set the IDXth value of case C to V.
+   Returns true if successful, false on I/O error. */
 gboolean
-psppire_case_file_set_value (PsppireCaseFile *cf, gint casenum, gint idx,
+psppire_case_file_set_value (PsppireCaseFile *cf, casenumber casenum, gint idx,
                            union value *v, gint width)
 {
-  struct ccase cc ;
-  int bytes;
+  bool ok;
 
   g_return_val_if_fail (cf, FALSE);
-  g_return_val_if_fail (cf->flexifile, FALSE);
-
-  g_return_val_if_fail (idx < casefile_get_value_cnt (cf->flexifile), FALSE);
-
-  if ( ! flexifile_get_case (FLEXIFILE(cf->flexifile), casenum, &cc) )
-    return FALSE;
-
-  if ( width == 0 )
-    bytes = MAX_SHORT_STRING;
-  else
-    bytes = DIV_RND_UP(width, MAX_SHORT_STRING) * MAX_SHORT_STRING ;
+  g_return_val_if_fail (cf->datasheet, FALSE);
 
-  /* Cast away const in flagrant abuse of the casefile */
-  memcpy ((union value *)case_data_idx (&cc, idx), v, bytes);
+  g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), FALSE);
 
-  g_signal_emit (cf, signal[CASE_CHANGED], 0, casenum);
-
-  return TRUE;
+  ok = datasheet_put_value (cf->datasheet, casenum, idx, v, width);
+  if (ok)
+    g_signal_emit (cf, signals [CASE_CHANGED], 0, casenum);
+  return ok;
 }
 
 
 
 /* Set the IDXth value of case C using D_IN */
 gboolean
-psppire_case_file_data_in (PsppireCaseFile *cf, gint casenum, gint idx,
+psppire_case_file_data_in (PsppireCaseFile *cf, casenumber casenum, gint idx,
                           struct substring input, const struct fmt_spec *fmt)
 {
-  struct ccase cc ;
+  union value *value = NULL;
+  int width;
+  bool ok;
 
   g_return_val_if_fail (cf, FALSE);
-  g_return_val_if_fail (cf->flexifile, FALSE);
+  g_return_val_if_fail (cf->datasheet, FALSE);
 
-  g_return_val_if_fail (idx < casefile_get_value_cnt (cf->flexifile), FALSE);
+  g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), FALSE);
 
-  if ( ! flexifile_get_case (FLEXIFILE(cf->flexifile), casenum, &cc) )
-    return FALSE;
+  width = fmt_var_width (fmt);
+  value = xmalloca (value_cnt_from_width (width) * sizeof *value);
+  ok = (datasheet_get_value (cf->datasheet, casenum, idx, value, width)
+        && data_in (input, LEGACY_NATIVE, fmt->type, 0, 0, 0, value, width)
+        && datasheet_put_value (cf->datasheet, casenum, idx, value, width));
 
-  /* Cast away const in flagrant abuse of the casefile */
-  if (!data_in (input, fmt->type, 0, 0,
-                (union value *) case_data_idx (&cc, idx), fmt_var_width (fmt)))
-    g_warning ("Cant set value\n");
+  if (ok)
+    g_signal_emit (cf, signals [CASE_CHANGED], 0, casenum);
 
-  g_signal_emit (cf, signal[CASE_CHANGED], 0, casenum);
+  freea (value);
 
   return TRUE;
 }
 
 
 void
-psppire_case_file_sort (PsppireCaseFile *cf, const struct sort_criteria *sc)
+psppire_case_file_sort (PsppireCaseFile *cf, struct case_ordering *ordering)
 {
+  struct casereader *sorted_data;
   gint c;
 
-  struct casereader *reader = casefile_get_reader (cf->flexifile, NULL);
-  struct casefile *cfile;
-
-  struct casefile_factory *factory  = flexifile_factory_create ();
-
-  cfile = sort_execute (reader, sc, factory);
-
-  casefile_destroy (cf->flexifile);
-
-  cf->flexifile = cfile;
+  sorted_data = sort_execute (datasheet_make_reader (cf->datasheet), ordering);
+  cf->datasheet = datasheet_create (sorted_data);
 
   /* FIXME: Need to have a signal to change a range of cases, instead of
      calling a signal many times */
-  for ( c = 0 ; c < casefile_get_case_cnt (cf->flexifile) ; ++c )
-    g_signal_emit (cf, signal[CASE_CHANGED], 0, c);
-
-  flexifile_factory_destroy (factory);
+  for ( c = 0 ; c < datasheet_get_row_cnt (cf->datasheet) ; ++c )
+    g_signal_emit (cf, signals [CASE_CHANGED], 0, c);
 }
 
 
 /* Resize the cases in the casefile, by inserting N_VALUES into every
-   one of them. */
+   one of them at the position immediately preceeding WHERE.
+*/
 gboolean
 psppire_case_file_insert_values (PsppireCaseFile *cf,
-                                gint n_values, gint before)
+                                gint n_values, gint where)
 {
   g_return_val_if_fail (cf, FALSE);
+  g_return_val_if_fail (cf->accessible, FALSE);
 
-  if ( ! cf->flexifile )
-    {
-      cf->flexifile = flexifile_create (n_values);
+  if ( n_values == 0 )
+    return FALSE;
 
-      return TRUE;
-    }
+  g_assert (n_values > 0);
 
-  return flexifile_resize (FLEXIFILE(cf->flexifile), n_values, before);
+  if ( ! cf->datasheet )
+    cf->datasheet = datasheet_create (NULL);
+
+  {
+    union value *values = xcalloc (n_values, sizeof *values);
+    datasheet_insert_columns (cf->datasheet, values, n_values, where);
+    free (values);
+  }
+
+  return TRUE;
 }
 
+
 /* Fills C with the CASENUMth case.
    Returns true on success, false otherwise.
  */
 gboolean
-psppire_case_file_get_case (const PsppireCaseFile *cf, gint casenum,
+psppire_case_file_get_case (const PsppireCaseFile *cf, casenumber casenum,
                           struct ccase *c)
 {
   g_return_val_if_fail (cf, FALSE);
-  g_return_val_if_fail (cf->flexifile, FALSE);
+  g_return_val_if_fail (cf->datasheet, FALSE);
+
+  return datasheet_get_row (cf->datasheet, casenum, c);
+}
+
+
 
-  return flexifile_get_case (FLEXIFILE(cf->flexifile), casenum, c);
+struct casereader *
+psppire_case_file_make_reader (PsppireCaseFile *cf)
+{
+  struct casereader *r = datasheet_make_reader (cf->datasheet);
+  cf->accessible = FALSE;
+  return r;
 }
+