Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / ui / gui / psppire-case-file.c
index 8a4f196df7fd17b58d2cc41edda66d31c6518df2..2401be4a27c501f9b2fa8ec44d3308491fde8a37 100644 (file)
@@ -1,21 +1,18 @@
-/*
-    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 <data/case.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 "xallocsa.h"
+#include "xmalloca.h"
 
 /* --- prototypes --- */
 static void psppire_case_file_class_init       (PsppireCaseFileClass   *class);
@@ -93,7 +91,7 @@ psppire_case_file_class_init (PsppireCaseFileClass *class)
   object_class->finalize = psppire_case_file_finalize;
 
   signals [CASE_CHANGED] =
-    g_signal_new ("case_changed",
+    g_signal_new ("case-changed",
                  G_TYPE_FROM_CLASS (class),
                  G_SIGNAL_RUN_FIRST,
                  0,
@@ -105,7 +103,7 @@ psppire_case_file_class_init (PsppireCaseFileClass *class)
 
 
   signals [CASE_INSERTED] =
-    g_signal_new ("case_inserted",
+    g_signal_new ("case-inserted",
                  G_TYPE_FROM_CLASS (class),
                  G_SIGNAL_RUN_FIRST,
                  0,
@@ -117,7 +115,7 @@ psppire_case_file_class_init (PsppireCaseFileClass *class)
 
 
   signals [CASES_DELETED] =
-    g_signal_new ("cases_deleted",
+    g_signal_new ("cases-deleted",
                  G_TYPE_FROM_CLASS (class),
                  G_SIGNAL_RUN_FIRST,
                  0,
@@ -133,7 +131,8 @@ psppire_case_file_finalize (GObject *object)
 {
   PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object);
 
-  datasheet_destroy (cf->datasheet);
+  if ( cf->accessible)
+    datasheet_destroy (cf->datasheet);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -142,6 +141,7 @@ static void
 psppire_case_file_init (PsppireCaseFile *cf)
 {
   cf->datasheet = NULL;
+  cf->accessible = FALSE;
 }
 
 
@@ -152,24 +152,17 @@ psppire_case_file_init (PsppireCaseFile *cf)
  * Creates a new #PsppireCaseFile.
  */
 PsppireCaseFile*
-psppire_case_file_new (void)
+psppire_case_file_new (const struct casereader *reader)
 {
   PsppireCaseFile *cf = g_object_new (G_TYPE_PSPPIRE_CASE_FILE, NULL);
 
-  cf->datasheet = datasheet_create (NULL);
+  cf->datasheet = datasheet_create (casereader_clone (reader));
+  cf->accessible = TRUE;
 
   return cf;
 }
 
 
-void
-psppire_case_file_replace_datasheet (PsppireCaseFile *cf, struct datasheet *ds)
-{
-  cf->datasheet = ds;
-}
-
-
-
 gboolean
 psppire_case_file_delete_cases (PsppireCaseFile *cf, gint n_cases, gint first)
 {
@@ -234,6 +227,7 @@ inline gint
 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->datasheet)
     return 0;
@@ -250,13 +244,13 @@ psppire_case_file_get_value (const PsppireCaseFile *cf,
                              union value *value, int width)
 {
   bool allocated;
-  
+
   g_return_val_if_fail (cf, false);
   g_return_val_if_fail (cf->datasheet, false);
 
   g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), false);
 
-  if (value == NULL) 
+  if (value == NULL)
     {
       value = xnmalloc (value_cnt_from_width (width), sizeof *value);
       allocated = true;
@@ -265,7 +259,7 @@ psppire_case_file_get_value (const PsppireCaseFile *cf,
     allocated = false;
   if (!datasheet_get_value (cf->datasheet, casenum, idx, value, width))
     {
-      if (allocated) 
+      if (allocated)
         free (value);
       value = NULL;
     }
@@ -316,7 +310,7 @@ psppire_case_file_data_in (PsppireCaseFile *cf, gint casenum, gint idx,
   g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), FALSE);
 
   width = fmt_var_width (fmt);
-  value = xallocsa (value_cnt_from_width (width) * sizeof *value);
+  value = xmalloca (value_cnt_from_width (width) * sizeof *value);
   ok = (datasheet_get_value (cf->datasheet, casenum, idx, value, width)
         && data_in (input, fmt->type, 0, 0, value, width)
         && datasheet_put_value (cf->datasheet, casenum, idx, value, width));
@@ -324,7 +318,7 @@ psppire_case_file_data_in (PsppireCaseFile *cf, gint casenum, gint idx,
   if (ok)
     g_signal_emit (cf, signals [CASE_CHANGED], 0, casenum);
 
-  freesa (value);
+  freea (value);
 
   return TRUE;
 }
@@ -354,6 +348,7 @@ psppire_case_file_insert_values (PsppireCaseFile *cf,
 {
   union value *values;
   g_return_val_if_fail (cf, FALSE);
+  g_return_val_if_fail (cf->accessible, FALSE);
 
   if ( ! cf->datasheet )
     cf->datasheet = datasheet_create (NULL);
@@ -377,3 +372,14 @@ psppire_case_file_get_case (const PsppireCaseFile *cf, gint casenum,
 
   return datasheet_get_row (cf->datasheet, casenum, c);
 }
+
+
+
+struct casereader *
+psppire_case_file_make_reader (PsppireCaseFile *cf)
+{
+  struct casereader *r = datasheet_make_reader (cf->datasheet);
+  cf->accessible = FALSE;
+  return r;
+}
+