Eliminate temp_case, and a few other cleanups.
[pspp-builds.git] / src / format.c
index 33a81ff30142366fc5c044ee1deadd86b8e10e1c..37e691a4a7ae20f0483de8a262f1851012ed1a0b 100644 (file)
@@ -36,50 +36,58 @@ struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] =
   {"",         -1, -1,  -1, -1,   -1, 0000, -1, -1},
 };
 
-const int translate_fmt[40] =
-  {
-    -1, FMT_A, FMT_AHEX, FMT_COMMA, FMT_DOLLAR, FMT_F, FMT_IB,
-    FMT_PIBHEX, FMT_P, FMT_PIB, FMT_PK, FMT_RB, FMT_RBHEX, -1,
-    -1, FMT_Z, FMT_N, FMT_E, -1, -1, FMT_DATE, FMT_TIME,
-    FMT_DATETIME, FMT_ADATE, FMT_JDATE, FMT_DTIME, FMT_WKDAY,
-    FMT_MONTH, FMT_MOYR, FMT_QYR, FMT_WKYR, FMT_PCT, FMT_DOT,
-    FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE, FMT_EDATE,
-    FMT_SDATE,
-  };
-
+/* Parses the alphabetic prefix of the current token as a format
+   specifier name.  Returns the corresponding format specifier
+   type if successful, or -1 on failure.  If ALLOW_XT is zero,
+   then X and T format specifiers are not allowed.  If CP is
+   nonzero, then *CP is set to the first non-alphabetic character
+   in the current token on success or to a null pointer on
+   failure. */
 int
 parse_format_specifier_name (const char **cp, int allow_xt)
 {
-  struct fmt_desc *f;
-  char *ep;
-  int x;
+  char *sp, *ep;
+  int idx;
 
-  ep = ds_value (&tokstr);
+  sp = ep = ds_value (&tokstr);
   while (isalpha ((unsigned char) *ep))
     ep++;
-  x = *ep;
-  *ep = 0;
-
-  for (f = formats; f->name[0]; f++)
-    if (!strcmp (f->name, ds_value (&tokstr)))
-      {
-       int indx = f - formats;
 
-       *ep = x;
-       if (cp)
-         *cp = ep;
+  if (sp != ep) 
+    {
+      /* Find format. */
+      for (idx = 0; idx < FMT_NUMBER_OF_FORMATS; idx++)
+        if (strlen (formats[idx].name) == ep - sp
+            && memcmp (formats[idx].name, sp, ep - sp))
+          break;
 
-       if (!allow_xt && (indx == FMT_T || indx == FMT_X))
-         {
-           msg (SE, _("X and T format specifiers not allowed here."));
-           return -1;
-         }
-       return indx;
-      }
+      /* Check format. */
+      if (idx < FMT_NUMBER_OF_FORMATS)
+        {
+          if (!allow_xt && (idx == FMT_T || idx == FMT_X)) 
+            {
+              msg (SE, _("X and T format specifiers not allowed here."));
+              idx = -1; 
+            }
+        }
+      else 
+        {
+          /* No match. */
+          msg (SE, _("%.*s is not a valid data format."),
+               (int) (ep - sp), ds_value (&tokstr));
+          idx = -1; 
+        }
+    }
+  else 
+    {
+      lex_error ("expecting data format");
+      idx = -1;
+    }
+      
+  if (cp != NULL)
+    *cp = ep;
 
-  msg (SE, _("%s is not a valid data format."), ds_value (&tokstr));
-  *ep = x;
-  return -1;
+  return idx;
 }
 
 /* Converts F to its string representation (for instance, "F8.2") and
@@ -96,6 +104,9 @@ fmt_to_string (const struct fmt_spec *f)
   return buf;
 }
 
+/* Checks whether SPEC is valid as an input format and returns
+   nonzero if so.  Otherwise, emits an error message and returns
+   zero. */
 int
 check_input_specifier (const struct fmt_spec *spec)
 {
@@ -135,6 +146,9 @@ check_input_specifier (const struct fmt_spec *spec)
   return 1;
 }
 
+/* Checks whether SPEC is valid as an output format and returns
+   nonzero if so.  Otherwise, emits an error message and returns
+   zero. */
 int
 check_output_specifier (const struct fmt_spec *spec)
 {
@@ -196,6 +210,8 @@ check_string_specifier (const struct fmt_spec *f, int min_len)
   return 1;
 }
 
+/* Converts input format specifier INPUT into output format
+   specifier OUTPUT. */
 void
 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
 {
@@ -288,6 +304,12 @@ convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
     }
 }
 
+/* Parses a format specifier from the token stream and returns
+   nonzero only if successful.  Emits an error message on
+   failure.  Allows X and T format specifiers only if ALLOW_XT is
+   nonzero.  The caller should call check_input_specifier() or
+   check_output_specifier() on the parsed format as
+   necessary.  */
 int
 parse_format_specifier (struct fmt_spec *input, int allow_xt)
 {
@@ -340,6 +362,9 @@ parse_format_specifier (struct fmt_spec *input, int allow_xt)
   return 1;
 }
 
+/* Returns the width corresponding to the format specifier.  The
+   return value is the value of the `width' member of a `struct
+   variable' for such an input format. */
 int
 get_format_var_width (const struct fmt_spec *spec) 
 {
@@ -350,3 +375,16 @@ get_format_var_width (const struct fmt_spec *spec)
   else
     return 0;
 }
+
+/* Returns the PSPP format corresponding to the given SPSS
+   format. */
+int
+translate_fmt (int spss) 
+{
+  int type;
+  
+  for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
+    if (formats[type].spss == spss)
+      return type;
+  return -1;
+}