Populate the data sheet in the import assistant
[pspp] / src / ui / gui / psppire-delimited-text.c
index a4874eefe0861909f21edbb8c6f93148fda47015..98f76b4301953f330f881d37b193111eb8893f52 100644 (file)
@@ -71,6 +71,18 @@ count_delims (PsppireDelimitedText *tf)
   //  g_print ("Max Number of delimiters per row: %d\n", tf->max_delimiters);
 }
 
+static void
+cache_invalidate (PsppireDelimitedText *tf)
+{
+  memset (tf->cache_starts, 0, 512);
+  if (tf->const_cache.string)
+    {
+      ss_dealloc (&tf->const_cache);
+      tf->const_cache.string = NULL;
+      tf->cache_row = -1;
+    }
+}
+
 static void
 psppire_delimited_text_set_property (GObject         *object,
                                guint            prop_id,
@@ -83,11 +95,6 @@ psppire_delimited_text_set_property (GObject         *object,
     {
     case PROP_FIRST_LINE:
       tf->first_line = g_value_get_int (value);
-      if (tf->const_cache.string)
-       {
-         ss_dealloc (&tf->const_cache);
-         tf->cache_row = -1;
-       }
       break;
     case PROP_CHILD:
       tf->child = g_value_get_object (value);
@@ -101,8 +108,8 @@ psppire_delimited_text_set_property (GObject         *object,
       break;
     };
 
-  if (tf->child)
-    count_delims (tf);
+  cache_invalidate (tf);
+  count_delims (tf);
 }
 
 static void
@@ -253,9 +260,13 @@ static gint
 __tree_model_iter_n_children (GtkTreeModel *tree_model,
                              GtkTreeIter *iter)
 {
-  g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
+  PsppireDelimitedText *file  = PSPPIRE_DELIMITED_TEXT (tree_model);
+  //  g_print ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
   g_assert (iter == NULL);
-  return 0;
+
+  gint children = gtk_tree_model_iter_n_children (file->child, NULL);
+
+  return children - file->first_line;
 }
 
 static GtkTreeModelFlags
@@ -312,41 +323,46 @@ __iter_nth_child (GtkTreeModel *tree_model,
 static void
 split_row_into_fields (PsppireDelimitedText *file, gint n)
 {
-  if (n != file->cache_row)
+  if (n == file->cache_row)  /* Cache hit */
     {
-      if (file->const_cache.string)
-       {
-         ss_dealloc (&file->const_cache);
-       }
-      ss_alloc_substring (&file->const_cache, PSPPIRE_TEXT_FILE (file->child)->lines[n]);
-      file->cache = file->const_cache;
-      int field = 0;
-      file->cache_starts[0] = file->cache.string;
-      for (;
-          UINT32_MAX != ss_first_mb (file->cache);
-          ss_get_mb (&file->cache))
+      return;
+    }
+
+  memset (file->cache_starts, 0, 512);
+  /* Cache miss */
+  if (file->const_cache.string)
+    {
+      ss_dealloc (&file->const_cache);
+    }
+  ss_alloc_substring_pool (&file->const_cache,
+                          PSPPIRE_TEXT_FILE (file->child)->lines[n], NULL);
+  struct substring cs = file->const_cache;
+  int field = 0;
+  file->cache_starts[0] = cs.string;
+  for (;
+       UINT32_MAX != ss_first_mb (cs);
+       ss_get_mb (&cs))
+    {
+      ucs4_t xx = ss_first_mb (cs);
+      GSList *del;
+      for (del = file->delimiters; del; del = g_slist_next (del))
        {
-         ucs4_t xx = ss_first_mb (file->cache);
-         GSList *del;
-         for (del = file->delimiters; del; del = g_slist_next (del))
+         if (xx == GPOINTER_TO_INT (del->data))
            {
-             if (xx == GPOINTER_TO_INT (del->data))
+             field++;
+             int char_len = ss_first_mblen (cs);
+             file->cache_starts[field] = cs.string + char_len;
+             while (char_len > 0)
                {
-                 field++;
-                 int char_len = ss_first_mblen (file->cache);
-                 file->cache_starts[field] = file->cache.string + char_len;
-                 while (char_len > 0)
-                   {
-                     file->cache.string[char_len - 1] = '\0';
-                     char_len--;
-                   }
-                 break;
+                 cs.string[char_len - 1] = '\0';
+                 char_len--;
                }
+             break;
            }
        }
-
-      file->cache_row = n;
     }
+
+  file->cache_row = n;
 }
 
 const gchar *
@@ -505,6 +521,7 @@ psppire_delimited_text_init (PsppireDelimitedText *text_file)
   text_file->const_cache.string = NULL;
   text_file->const_cache.length = 0;
   text_file->cache_row = -1;
+  memset (text_file->cache_starts, 0, 512);
 
   text_file->max_delimiters = 0;
 
@@ -513,7 +530,7 @@ psppire_delimited_text_init (PsppireDelimitedText *text_file)
 }
 
 
-PsppireDelimitedText *
+GtkTreeModel *
 psppire_delimited_text_new (GtkTreeModel *child)
 {
   PsppireDelimitedText *retval =