SAVE TRANSLATE: Improve error message.
[pspp] / src / language / data-io / save-translate.c
index cbed4b1008e656a4aab819280cc5db2de0e4ff8a..26941329f35ad378426ad441654031e9ab586791 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 2010, 2011, 2013, 2016 Free Software Foundation, Inc.
 
    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
@@ -45,6 +45,7 @@ cmd_save_translate (struct lexer *lexer, struct dataset *ds)
   enum { CSV_FILE = 1, TAB_FILE } type;
 
   struct dictionary *dict;
+  struct case_map_stage *stage;
   struct case_map *map;
   struct casewriter *writer;
   struct file_handle *handle;
@@ -67,6 +68,7 @@ cmd_save_translate (struct lexer *lexer, struct dataset *ds)
   type = 0;
 
   dict = dict_clone (dataset_dict (ds));
+  stage = NULL;
   map = NULL;
 
   handle = NULL;
@@ -81,11 +83,14 @@ cmd_save_translate (struct lexer *lexer, struct dataset *ds)
   delimiter = 0;
   qualifier = '"';
 
-  case_map_prepare_dict (dict);
+  stage = case_map_stage_create (dict);
   dict_delete_scratch_vars (dict);
 
-  while (lex_match (lexer, T_SLASH))
+  while (lex_token (lexer) != T_ENDCMD)
     {
+      if (!lex_force_match (lexer, T_SLASH))
+        goto error;
+
       if (lex_match_id (lexer, "OUTFILE"))
        {
           if (handle != NULL)
@@ -232,23 +237,21 @@ cmd_save_translate (struct lexer *lexer, struct dataset *ds)
       else if (!parse_dict_trim (lexer, dict))
         goto error;
     }
-  if (lex_end_of_command (lexer) != CMD_SUCCESS)
-    goto error;
 
   if (type == 0)
     {
-      lex_sbc_missing (lexer, "TYPE");
+      lex_sbc_missing ("TYPE");
       goto error;
     }
   else if (handle == NULL)
     {
-      lex_sbc_missing (lexer, "OUTFILE");
+      lex_sbc_missing ("OUTFILE");
       goto error;
     }
-  else if (!replace && fn_exists (fh_get_file_name (handle)))
+  else if (!replace && fn_exists (handle))
     {
-      msg (SE, _("Output file `%s' exists but REPLACE was not specified."),
-           fh_get_file_name (handle));
+      msg (SE, _("Output file `%s' exists but %s was not specified."),
+           fh_get_file_name (handle), "REPLACE");
       goto error;
     }
 
@@ -271,7 +274,8 @@ cmd_save_translate (struct lexer *lexer, struct dataset *ds)
     goto error;
   fh_unref (handle);
 
-  map = case_map_from_dict (dict);
+  map = case_map_stage_get_case_map (stage);
+  case_map_stage_destroy (stage);
   if (map != NULL)
     writer = case_map_create_output_translator (map, writer);
   dict_destroy (dict);
@@ -283,6 +287,7 @@ cmd_save_translate (struct lexer *lexer, struct dataset *ds)
   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
 
 error:
+  case_map_stage_destroy (stage);
   fh_unref (handle);
   dict_destroy (dict);
   case_map_destroy (map);