lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / language / stats / flip.c
index 3ca2413fcf35f2d56bba7b2200cde2934bb92985..23544c8b1ae2e7b822c193fe01fe928e1838219c 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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
 #include <limits.h>
 #include <stdlib.h>
 
-#include <data/case.h>
-#include <data/casereader.h>
-#include <data/casereader-provider.h>
-#include <data/dictionary.h>
-#include <data/procedure.h>
-#include <data/settings.h>
-#include <data/short-names.h>
-#include <data/value.h>
-#include <data/variable.h>
-#include <language/command.h>
-#include <language/lexer/lexer.h>
-#include <language/lexer/variable-parser.h>
-#include <libpspp/array.h>
-#include <libpspp/assertion.h>
-#include <libpspp/message.h>
-#include <libpspp/misc.h>
-#include <libpspp/pool.h>
-#include <libpspp/str.h>
-
-#include "intprops.h"
-#include "minmax.h"
-#include "xalloc.h"
+#include "data/case.h"
+#include "data/casereader.h"
+#include "data/casereader-provider.h"
+#include "data/dictionary.h"
+#include "data/procedure.h"
+#include "data/settings.h"
+#include "data/short-names.h"
+#include "data/value.h"
+#include "data/variable.h"
+#include "language/command.h"
+#include "language/lexer/lexer.h"
+#include "language/lexer/variable-parser.h"
+#include "libpspp/array.h"
+#include "libpspp/assertion.h"
+#include "libpspp/message.h"
+#include "libpspp/misc.h"
+#include "libpspp/pool.h"
+#include "libpspp/str.h"
+#include "data/data-in.h"
+#include "data/data-out.h"
+
+#include "gl/intprops.h"
+#include "gl/minmax.h"
+#include "gl/xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -66,6 +68,7 @@ struct flip_pgm
     int n_cases;                /* Pre-flip number of cases. */
 
     struct variable *new_names_var; /* Variable with new variable names. */
+    struct dictionary *dict;     /* Dictionary of the names */
     struct var_names old_names; /* Variable names before FLIP. */
     struct var_names new_names; /* Variable names after FLIP. */
 
@@ -105,24 +108,25 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
   flip->file = NULL;
   flip->cases_read = 0;
   flip->error = false;
+  flip->dict = dict;
 
-  lex_match (lexer, '/');
+  lex_match (lexer, T_SLASH);
   if (lex_match_id (lexer, "VARIABLES"))
     {
-      lex_match (lexer, '=');
+      lex_match (lexer, T_EQUALS);
       if (!parse_variables_const (lexer, dict, &vars, &flip->n_vars,
                                   PV_NO_DUPLICATE))
        goto error;
-      lex_match (lexer, '/');
+      lex_match (lexer, T_SLASH);
     }
   else
     dict_get_vars (dict, &vars, &flip->n_vars, DC_SYSTEM);
   pool_register (flip->pool, free, vars);
 
-  lex_match (lexer, '/');
+  lex_match (lexer, T_SLASH);
   if (lex_match_id (lexer, "NEWNAMES"))
     {
-      lex_match (lexer, '=');
+      lex_match (lexer, T_EQUALS);
       flip->new_names_var = parse_variable (lexer, dict);
       if (!flip->new_names_var)
         goto error;
@@ -141,7 +145,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
          }
     }
 
-  flip->file = pool_tmpfile (flip->pool);
+  flip->file = pool_create_temp_file (flip->pool);
   if (flip->file == NULL)
     {
       msg (SE, _("Could not create temporary file for FLIP."));
@@ -181,9 +185,8 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
             }
           else
             {
-              int width = var_get_width (flip->new_names_var);
-              name = pool_strdup0 (flip->pool,
-                                   value_str (value, width), width);
+              name = data_out_pool (value, dict_get_encoding (flip->dict), var_get_write_format (flip->new_names_var),
+                flip->pool);
             }
           var_names_add (flip->pool, &flip->new_names, name);
         }
@@ -207,8 +210,8 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
       make_new_var (dict, flip->new_names.names[i]);
     else
       {
-        char s[VAR_NAME_LEN + 1];
-        sprintf (s, "VAR%03d", i);
+        char s[3 + INT_STRLEN_BOUND (i) + 1];
+        sprintf (s, "VAR%03zu", i);
         dict_create_var_assert (dict, s, 0);
       }
 
@@ -217,7 +220,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
                                          flip->n_vars,
                                          &flip_casereader_class, flip);
   proc_set_active_file_data (ds, reader);
-  return lex_end_of_command (lexer);
+  return CMD_SUCCESS;
 
  error:
   destroy_flip_pgm (flip);
@@ -246,7 +249,7 @@ make_new_var (struct dictionary *dict, const char *name_)
     *--cp = '\0';
 
   /* Fix invalid characters. */
-  for (cp = name; *cp && cp < name + VAR_NAME_LEN; cp++)
+  for (cp = name; *cp && cp < name + ID_MAX_LEN; cp++)
     if (cp == name)
       {
         if (!lex_is_id1 (*cp) || *cp == '$')
@@ -258,7 +261,6 @@ make_new_var (struct dictionary *dict, const char *name_)
           *cp = '_';
       }
   *cp = '\0';
-  str_uppercase (name);
 
   /* Use the mangled name, if it is available, or add numeric
      extensions until we find one that is. */
@@ -268,9 +270,9 @@ make_new_var (struct dictionary *dict, const char *name_)
       int i;
       for (i = 1; ; i++)
         {
-          char n[VAR_NAME_LEN + 1];
-          int ofs = MIN (VAR_NAME_LEN - 1 - intlog10 (i), len);
-          memcpy (n, name, ofs);
+          char n[ID_MAX_LEN + 1];
+          int ofs = MIN (ID_MAX_LEN - 1 - intlog10 (i), len);
+          strncpy (n, name, ofs);
           sprintf (&n[ofs], "%d", i);
 
           if (dict_create_var (dict, n, 0))
@@ -319,13 +321,13 @@ flip_file (struct flip_pgm *flip)
   output_buf = input_buf + flip->n_vars * case_capacity;
 
   input_file = flip->file;
-  if (fseek (input_file, 0, SEEK_SET) != 0)
+  if (fseeko (input_file, 0, SEEK_SET) != 0)
     {
       msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno));
       return false;
     }
 
-  output_file = pool_tmpfile (flip->pool);
+  output_file = pool_create_temp_file (flip->pool);
   if (output_file == NULL)
     {
       msg (SE, _("Error creating FLIP source file."));
@@ -376,15 +378,11 @@ flip_file (struct flip_pgm *flip)
       case_idx += read_cases;
     }
 
-  if (pool_fclose (flip->pool, input_file) == EOF)
-    {
-      msg (SE, _("Error closing FLIP source file: %s."), strerror (errno));
-      return false;
-    }
+  pool_fclose_temp_file (flip->pool, input_file);
   pool_unregister (flip->pool, input_buf);
   free (input_buf);
 
-  if (fseek (output_file, 0, SEEK_SET) != 0)
+  if (fseeko (output_file, 0, SEEK_SET) != 0)
     {
       msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno));
       return false;
@@ -400,6 +398,7 @@ static struct ccase *
 flip_casereader_read (struct casereader *reader, void *flip_)
 {
   struct flip_pgm *flip = flip_;
+  const char *encoding;
   struct ccase *c;
   size_t i;
 
@@ -407,8 +406,10 @@ flip_casereader_read (struct casereader *reader, void *flip_)
     return false;
 
   c = case_create (casereader_get_proto (reader));
-  value_copy_str_rpad (case_data_rw_idx (c, 0), 8,
-                       flip->old_names.names[flip->cases_read], ' ');
+  encoding = dict_get_encoding (flip->dict);
+  data_in (ss_cstr (flip->old_names.names[flip->cases_read]), encoding,
+           FMT_A, case_data_rw_idx (c, 0), 8, encoding);
+
   for (i = 0; i < flip->n_cases; i++)
     {
       double in;