case: Introduce new functions for numbers and substrings in cases.
[pspp] / src / language / stats / flip.c
index 6378ffeb90ef6d4ee3a01709a5d58d631153ad02..993f8975a629d6beae47ba95b7fed7f0c0cf2747 100644 (file)
@@ -97,8 +97,8 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
   bool ok;
 
   if (proc_make_temporary_transformations_permanent (ds))
-    msg (SW, _("FLIP ignores TEMPORARY.  "
-               "Temporary transformations will be made permanent."));
+    msg (SW, _("%s ignores %s.  "
+               "Temporary transformations will be made permanent."), "FLIP", "TEMPORARY");
 
   flip = pool_create_container (struct flip_pgm, pool);
   flip->n_vars = 0;
@@ -144,11 +144,13 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
            break;
          }
     }
+  if (flip->n_vars <= 0)
+    goto error;
 
   flip->file = pool_create_temp_file (flip->pool);
   if (flip->file == NULL)
     {
-      msg (SE, _("Could not create temporary file for FLIP."));
+      msg (SE, _("Could not create temporary file for %s."), "FLIP");
       goto error;
     }
 
@@ -166,7 +168,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
   flip->encoding = dict_get_encoding (new_dict);
   dict_clear (new_dict);
 
-  input = proc_open (ds);
+  input = proc_open_filtering (ds, false);
   while ((c = casereader_read (input)) != NULL)
     {
       flip->n_cases++;
@@ -192,7 +194,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
             {
               name = data_out_pool (value, dict_get_encoding (old_dict),
                                     var_get_write_format (flip->new_names_var),
-                                    flip->pool);
+                                    settings_get_fmt_settings (), flip->pool);
             }
           var_names_add (flip->pool, &flip->new_names, name);
         }
@@ -229,7 +231,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
   return CMD_SUCCESS;
 
  error:
-  dict_destroy (new_dict);
+  dict_unref (new_dict);
   destroy_flip_pgm (flip);
   return CMD_CASCADING_FAILURE;
 }
@@ -269,9 +271,15 @@ make_new_var (struct dictionary *dict, const char *name_)
       }
   *cp = '\0';
 
+  if (strlen (name) == 0)
+    {
+      free (name);
+      name = xstrdup ("v");
+    }
+
   /* Use the mangled name, if it is available, or add numeric
      extensions until we find one that is. */
-  if (!dict_create_var (dict, name, 0))
+  if (!id_is_plausible (name, false) || !dict_create_var (dict, name, 0))
     {
       int len = strlen (name);
       int i;
@@ -282,7 +290,7 @@ make_new_var (struct dictionary *dict, const char *name_)
           strncpy (n, name, ofs);
           sprintf (&n[ofs], "%d", i);
 
-          if (dict_create_var (dict, n, 0))
+          if (id_is_plausible (n, false) && dict_create_var (dict, n, 0))
             break;
         }
     }
@@ -330,18 +338,18 @@ flip_file (struct flip_pgm *flip)
   input_file = flip->file;
   if (fseeko (input_file, 0, SEEK_SET) != 0)
     {
-      msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno));
+      msg (SE, _("Error rewinding %s file: %s."), "FLIP", strerror (errno));
       return false;
     }
 
   output_file = pool_create_temp_file (flip->pool);
   if (output_file == NULL)
     {
-      msg (SE, _("Error creating FLIP source file."));
+      msg (SE, _("Error creating %s source file."), "FLIP");
       return false;
     }
 
-  for (case_idx = 0; case_idx < flip->n_cases; )
+  for (case_idx = 0; case_idx < flip->n_cases;)
     {
       unsigned long read_cases = MIN (flip->n_cases - case_idx,
                                       case_capacity);
@@ -350,9 +358,9 @@ flip_file (struct flip_pgm *flip)
       if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
         {
           if (ferror (input_file))
-            msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
+            msg (SE, _("Error reading %s file: %s."), "FLIP", strerror (errno));
           else
-            msg (SE, _("Unexpected end of file reading FLIP file."));
+            msg (SE, _("Unexpected end of file reading %s file."), "FLIP");
           return false;
         }
 
@@ -368,7 +376,7 @@ flip_file (struct flip_pgm *flip)
                                            + (off_t) i * flip->n_cases),
                       SEEK_SET) != 0)
             {
-              msg (SE, _("Error seeking FLIP source file: %s."),
+              msg (SE, _("Error seeking %s source file: %s."), "FLIP",
                    strerror (errno));
               return false;
             }
@@ -376,7 +384,7 @@ flip_file (struct flip_pgm *flip)
          if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
              != read_cases)
             {
-              msg (SE, _("Error writing FLIP source file: %s."),
+              msg (SE, _("Error writing %s source file: %s."), "FLIP",
                    strerror (errno));
               return false;
             }
@@ -391,7 +399,7 @@ flip_file (struct flip_pgm *flip)
 
   if (fseeko (output_file, 0, SEEK_SET) != 0)
     {
-      msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno));
+      msg (SE, _("Error rewinding %s source file: %s."), "FLIP", strerror (errno));
       return false;
     }
   flip->file = output_file;
@@ -413,7 +421,8 @@ flip_casereader_read (struct casereader *reader, void *flip_)
 
   c = case_create (casereader_get_proto (reader));
   data_in (ss_cstr (flip->old_names.names[flip->cases_read]), flip->encoding,
-           FMT_A, case_data_rw_idx (c, 0), 8, flip->encoding);
+           FMT_A, settings_get_fmt_settings (), case_data_rw_idx (c, 0),
+           8, flip->encoding);
 
   for (i = 0; i < flip->n_cases; i++)
     {
@@ -422,16 +431,16 @@ flip_casereader_read (struct casereader *reader, void *flip_)
         {
           case_unref (c);
           if (ferror (flip->file))
-            msg (SE, _("Error reading FLIP temporary file: %s."),
+            msg (SE, _("Error reading %s temporary file: %s."), "FLIP",
                  strerror (errno));
           else if (feof (flip->file))
-            msg (SE, _("Unexpected end of file reading FLIP temporary file."));
+            msg (SE, _("Unexpected end of file reading %s temporary file."), "FLIP");
           else
             NOT_REACHED ();
           flip->error = true;
           return NULL;
         }
-      case_data_rw_idx (c, i + 1)->f = in;
+      *case_num_rw_idx (c, i + 1) = in;
     }
 
   flip->cases_read++;
@@ -443,7 +452,7 @@ flip_casereader_read (struct casereader *reader, void *flip_)
    Returns true if successful read, false if an I/O occurred
    during destruction or previously. */
 static void
-flip_casereader_destroy (struct casereader *reader UNUSED, void *flip_)
+flip_casereader_destroy (struct casereader *reader, void *flip_)
 {
   struct flip_pgm *flip = flip_;
   if (flip->error)