Eliminated global variable current_dataset.
[pspp-builds.git] / src / language / dictionary / apply-dictionary.c
index 00025ead43b1793d118417c4e603ac7ecb48674d..e04341610aca35e559514ce8238029d6c796c6a0 100644 (file)
    02110-1301, USA. */
 
 #include <config.h>
+
 #include <stdlib.h>
+
 #include <data/any-reader.h>
-#include <language/command.h>
 #include <data/dictionary.h>
-#include <libpspp/message.h>
 #include <data/file-handle-def.h>
-#include <libpspp/hash.h>
-#include <language/lexer/lexer.h>
-#include <libpspp/str.h>
+#include <data/procedure.h>
 #include <data/value-labels.h>
 #include <data/variable.h>
+#include <language/command.h>
+#include <language/data-io/file-handle.h>
+#include <language/lexer/lexer.h>
+#include <libpspp/hash.h>
+#include <libpspp/message.h>
+#include <libpspp/str.h>
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
-#include <libpspp/debug-print.h>
-
 /* Parses and executes APPLY DICTIONARY. */
 int
-cmd_apply_dictionary (void)
+cmd_apply_dictionary (struct dataset *ds)
 {
   struct file_handle *handle;
   struct any_reader *reader;
@@ -61,7 +63,7 @@ cmd_apply_dictionary (void)
   for (i = 0; i < dict_get_var_cnt (dict); i++)
     {
       struct variable *s = dict_get_var (dict, i);
-      struct variable *t = dict_lookup_var (default_dict, s->name);
+      struct variable *t = dict_lookup_var (dataset_dict (ds), s->name);
       if (t == NULL)
        continue;
 
@@ -89,38 +91,7 @@ cmd_apply_dictionary (void)
             s->name);
       else if (val_labs_count (s->val_labs))
        {
-          /* Whether to apply the value labels. */
-          int apply = 1;
-          
-         if (t->width < s->width)
-           {
-             struct val_labs_iterator *i;
-             struct val_lab *lab;
-
-              for (lab = val_labs_first (s->val_labs, &i); lab != NULL;
-                   lab = val_labs_next (s->val_labs, &i))
-               {
-                 int j;
-
-                 /* We will apply the value labels only if all
-                     the truncated characters are blanks. */
-                 for (j = t->width; j < s->width; j++)
-                   if (lab->value.s[j] != ' ') 
-                      {
-                        val_labs_done (&i);
-                        apply = 0;
-                        break; 
-                      }
-               }
-           }
-         else
-           {
-             /* Fortunately, we follow the convention that all value
-                label values are right-padded with spaces, so it is
-                unnecessary to bother padding values here. */
-           }
-
-         if (apply) 
+          if (val_labs_can_set_width (s->val_labs, t->width))
             {
               val_labs_destroy (t->val_labs);
               t->val_labs = s->val_labs;
@@ -157,10 +128,10 @@ cmd_apply_dictionary (void)
   if (dict_get_weight (dict) != NULL) 
     {
       struct variable *new_weight
-        = dict_lookup_var (default_dict, dict_get_weight (dict)->name);
+        = dict_lookup_var (dataset_dict (ds), dict_get_weight (dict)->name);
 
       if (new_weight != NULL)
-        dict_set_weight (default_dict, new_weight);
+        dict_set_weight (dataset_dict (ds), new_weight);
     }
   
   any_reader_close (reader);