Finish converting struct variable to an opaque type. In this
[pspp] / src / ui / gui / psppire-data-store.c
index 16f709ff302996c0ecab4f80fc8f309dd8892894..65a4391d224402e353648de7795607c57e03366b 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <data/casefile.h>
 #include <data/case.h>
+#include <data/data-out.h>
 
 #include <gtksheet/gtksheet.h>
 #include <gtksheet/gsheetmodel.h>
@@ -37,6 +38,7 @@
 
 #include "psppire-variable.h"
 #include "psppire-data-store.h"
+#include "psppire-case-file.h"
 #include "helper.h"
 
 #include <data/dictionary.h>
@@ -326,9 +328,6 @@ dict_size_change_callback(GObject *obj,
 
   store  = PSPPIRE_DATA_STORE(data);
 
-  /* 
-  if ( adjustment > 0 )
-  */
   psppire_case_file_insert_values (store->case_file, adjustment, posn);
 }
 
@@ -422,8 +421,43 @@ psppire_data_store_finalize (GObject *object)
 }
 
 
+
+/* Insert a blank case before POSN */
+gboolean
+psppire_data_store_insert_new_case(PsppireDataStore *ds, gint posn)
+{
+  gboolean result;
+  gint val_cnt, v; 
+  struct ccase cc;
+  g_return_val_if_fail (ds, FALSE);
+
+
+  /* Opportunity for optimisation exists here when creating a blank case */
+  val_cnt = casefile_get_value_cnt(ds->case_file->flexifile) ;
+  
+  case_create (&cc, val_cnt);
+
+  memset ( case_data_rw (&cc, 0), 0, val_cnt * MAX_SHORT_STRING);
+
+  for (v = 0 ; v < psppire_dict_get_var_cnt (ds->dict) ; ++v) 
+    {
+      const struct PsppireVariable *pv = psppire_dict_get_variable(ds->dict, v);
+      if (VAR_STRING ==  psppire_variable_get_type(pv) ) 
+       continue;
+
+      case_data_rw_idx (&cc, psppire_variable_get_fv (pv))->f = SYSMIS;
+    }
+
+  result = psppire_case_file_insert_case (ds->case_file, &cc, posn);
+
+  case_destroy (&cc);
+
+  return result;
+}
+
+
 static gchar *
-psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
+psppire_data_store_get_string (const GSheetModel *model, gint row, gint column)
 {
   gint idx;
   char *text;
@@ -433,26 +467,26 @@ psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
   GString *s;
   PsppireDataStore *store = PSPPIRE_DATA_STORE(model);
 
-  g_return_val_if_fail(store->dict, NULL);
-  g_return_val_if_fail(store->case_file, NULL);
+  g_return_val_if_fail (store->dict, NULL);
+  g_return_val_if_fail (store->case_file, NULL);
 
-  if (column >= psppire_dict_get_var_cnt(store->dict))
+  if (column >= psppire_dict_get_var_cnt (store->dict))
     return NULL;
 
-  if ( row >= psppire_case_file_get_case_count(store->case_file))
+  if ( row >= psppire_case_file_get_case_count (store->case_file))
     return NULL;
 
-  pv = psppire_dict_get_variable(store->dict, column);
+  pv = psppire_dict_get_variable (store->dict, column);
 
-  idx = psppire_variable_get_fv(pv);
+  idx = psppire_variable_get_fv (pv);
 
-  v = psppire_case_file_get_value(store->case_file, row, idx);
+  v = psppire_case_file_get_value (store->case_file, row, idx);
 
   g_return_val_if_fail(v, NULL);
 
   if ( store->show_labels) 
     {
-      const struct val_labs * vl = psppire_variable_get_value_labels(pv);
+      const struct val_labs * vl = psppire_variable_get_value_labels (pv);
 
       const gchar *label;
       if ( (label = val_labs_find(vl, *v)) )
@@ -461,29 +495,31 @@ psppire_data_store_get_string(const GSheetModel *model, gint row, gint column)
        }
     }
 
-  fp = psppire_variable_get_write_spec(pv);
+  fp = psppire_variable_get_write_spec (pv);
 
   s = g_string_sized_new (fp->w + 1);
-  g_string_set_size(s, fp->w);
+  g_string_set_size (s, fp->w);
   
-  memset(s->str, 0, fp->w);
+  memset (s->str, 0, fp->w);
 
-  g_assert(fp->w == s->len);
+  g_assert (fp->w == s->len);
     
   /* Converts binary value V into printable form in the exactly
      FP->W character in buffer S according to format specification
      FP.  No null terminator is appended to the buffer.  */
-  data_out (s->str, fp, v);
+  data_out (v, fp, s->str);
+
+  text = pspp_locale_to_utf8 (s->str, fp->w, 0);
+  g_string_free (s, TRUE);
 
-  text = pspp_locale_to_utf8(s->str, fp->w, 0);
-  g_string_free(s, TRUE);
+  g_strchomp (text);
 
   return text;
 }
 
 
 static gboolean 
-psppire_data_store_clear_datum(GSheetModel *model, 
+psppire_data_store_clear_datum (GSheetModel *model, 
                                          gint row, gint col)
 
 {
@@ -494,7 +530,7 @@ psppire_data_store_clear_datum(GSheetModel *model,
 
   const gint index = psppire_variable_get_fv(pv) ;
 
-  if ( psppire_variable_get_type(pv) == NUMERIC) 
+  if ( psppire_variable_get_type(pv) == VAR_NUMERIC) 
     v.f = SYSMIS;
   else
     memcpy(v.s, "", MAX_SHORT_STRING);
@@ -533,20 +569,10 @@ psppire_data_store_set_string(GSheetModel *model,
     }
 #endif
 
-  {
-    const gint index = psppire_variable_get_fv(pv);
-
-    struct data_in d_in;
-    d_in.s = text;
-    d_in.e = text + strlen(text);
-    d_in.v = 0;
-    d_in.f1 = d_in.f2 = 0;
-    d_in.format = * psppire_variable_get_write_spec(pv);
-    d_in.flags = 0;
-
-    psppire_case_file_data_in(store->case_file, row, index, &d_in) ;
-  }
-
+  psppire_case_file_data_in (store->case_file, row,
+                             psppire_variable_get_fv (pv), ss_cstr (text),
+                             psppire_variable_get_write_spec (pv));
+  
   return TRUE;
 }
 
@@ -584,10 +610,13 @@ psppire_data_store_show_labels(PsppireDataStore *store, gboolean show_labels)
 
 
 
+/* FIXME: There's no reason to actually have this function.
+   It should be done by a procedure */
 void
 psppire_data_store_create_system_file(PsppireDataStore *store,
                              struct file_handle *handle)
 {
+  gint i, var_cnt;
   const struct sfm_write_options wo = {
     true, /* writeable */
     false, /* dont compress */
@@ -603,9 +632,19 @@ psppire_data_store_create_system_file(PsppireDataStore *store,
   if ( ! writer) 
     return;
 
-#if 0
-  psppire_case_array_iterate_case(store->cases, write_case, writer);
-#endif
+
+  var_cnt = psppire_data_store_get_var_count (G_SHEET_MODEL(store));
+
+  for (i = 0 ; i < psppire_case_file_get_case_count(store->case_file); ++i ) 
+    {
+      struct ccase c;
+      
+      case_create (&c, var_cnt);
+      psppire_case_file_get_case (store->case_file, i, &c);
+      sfm_write_case (writer, &c);
+
+      case_destroy (&c);
+    }
 
   sfm_close_writer(writer);
 }
@@ -707,7 +746,6 @@ geometry_get_sensitivity(const GSheetColumn *geom, gint unit)
 {
   PsppireDataStore *ds = PSPPIRE_DATA_STORE(geom);
 
-
   return (unit < psppire_dict_get_var_cnt(ds->dict));
 }
 
@@ -721,7 +759,6 @@ psppire_data_store_sheet_column_init (GSheetColumnIface *iface)
   iface->get_visibility = always_true;
   iface->get_sensitivity = geometry_get_sensitivity;
   iface->get_justification = geometry_get_justification;
-
   iface->get_button_label = geometry_get_column_button_label;
 }