sack works
[pspp] / src / data / format-guesser.c
index 189320d293d16ba8787a06b4b5896137e8ebacb5..880fe7c646aa5da556b5064792447487bb7e3112 100644 (file)
@@ -70,7 +70,7 @@ struct date_syntax
   {
     enum fmt_type format;       /* Format type. */
 #define MAX_TOKENS 11
-    size_t token_cnt;           /* Number of tokens. */
+    size_t n_tokens;           /* Number of tokens. */
     enum date_token tokens[MAX_TOKENS]; /* Tokens. */
   };
 
@@ -251,33 +251,31 @@ fmt_guesser_add (struct fmt_guesser *g, struct substring s)
     add_date_time (g, s);
 }
 
-/* Guesses the format of the input previously added to G using
-   fmt_guesser_add, storing the guess into *F.  The guessed
-   format may not actually a valid input or output format, in
-   that its width and number of decimal places may be outside the
-   valid range for the guessed format type.  The caller must
-   therefore adjust the format to make it valid, e.g. by calling
-   fmt_fix. */
-void
-fmt_guesser_guess (struct fmt_guesser *g, struct fmt_spec *f)
+/* Returns a guess about the format of the input previously added to G using
+   fmt_guesser_add().  The guessed format may not actually a valid input or
+   output format, in that its width and number of decimal places may be outside
+   the valid range for the guessed format type.  The caller must therefore
+   adjust the format to make it valid, e.g. by calling fmt_fix(). */
+struct fmt_spec
+fmt_guesser_guess (struct fmt_guesser *g)
 {
   if (g->count > 0)
     {
       /* Set defaults.  The guesser functions typically override
          the width and type. */
-      f->type = FMT_A;
-      f->w = g->width;
-      f->d = 0;
+      struct fmt_spec f = { .type = FMT_A, .w = g->width };
 
       if (g->any_numeric > g->count / 2)
-        guess_numeric (g, f);
+        guess_numeric (g, &f);
       else if (g->any_date > g->count / 2)
-        guess_date_time (g, f);
+        guess_date_time (g, &f);
+
+      return f;
     }
   else
     {
       /* No data at all.  Use fallback default. */
-      *f = fmt_default_for_width (0);
+      return fmt_default_for_width (0);
     }
 }
 \f
@@ -404,7 +402,7 @@ add_numeric (struct fmt_guesser *g, struct substring s)
          can't tell whether the ',' or '.' is a grouping or
          decimal character.  Assume that the decimal character
          from the settings is in use. */
-      if (prev_delim == settings_get_decimal_char (FMT_F))
+      if (prev_delim == settings_get_fmt_settings ()->decimal)
         {
           decimal = prev_delim;
           precision = delim_digits;
@@ -448,7 +446,7 @@ add_numeric (struct fmt_guesser *g, struct substring s)
 static void
 guess_numeric (struct fmt_guesser *g, struct fmt_spec *f)
 {
-  int decimal_char = settings_get_decimal_char (FMT_COMMA);
+  int decimal_char = settings_get_fmt_settings ()->decimal;
 
   f->d = g->decimals / g->count;
   if (g->pct)
@@ -491,27 +489,27 @@ add_date_time (struct fmt_guesser *g, struct substring s)
   enum date_token token;
   enum date_token tokens[MAX_TOKENS];
   enum date_token tokens_seen;
-  size_t token_cnt;
+  size_t n_tokens;
   int decimals;
   bool is_date;
   int i;
 
   /* Break S into tokens. */
-  token_cnt = 0;
+  n_tokens = 0;
   tokens_seen = 0;
   decimals = 0;
   while (!ss_is_empty (s))
     {
-      if (token_cnt >= MAX_TOKENS)
+      if (n_tokens >= MAX_TOKENS)
         return;
 
       token = parse_date_token (&s, tokens_seen, &decimals);
       if (token == 0)
         return;
-      tokens[token_cnt++] = token;
+      tokens[n_tokens++] = token;
       tokens_seen |= token;
     }
-  if (token_cnt == 0)
+  if (n_tokens == 0)
     return;
 
   /* Find matching date formats, if any, and increment the
@@ -520,7 +518,7 @@ add_date_time (struct fmt_guesser *g, struct substring s)
   for (i = 0; i < DATE_SYNTAX_CNT; i++)
     {
       struct date_syntax *s = &syntax[i];
-      if (match_date_syntax (tokens, token_cnt, s->tokens, s->token_cnt))
+      if (match_date_syntax (tokens, n_tokens, s->tokens, s->n_tokens))
         {
           is_date = true;
           g->date[i]++;
@@ -589,7 +587,7 @@ guess_date_time (struct fmt_guesser *g, struct fmt_spec *f)
     {
       for (i = 0; i < DATE_SYNTAX_CNT; i++)
         if (g->date[i]
-            && syntax[i].tokens[syntax[i].token_cnt - 1] == DT_SECOND)
+            && syntax[i].tokens[syntax[i].n_tokens - 1] == DT_SECOND)
           {
             f->d = g->decimals / g->count;
             f->w = MAX (f->w, fmt_min_input_width (f->type) + 3);
@@ -676,10 +674,10 @@ parse_date_number (struct substring *s, enum date_token tokens_seen,
                    int *decimals)
 {
   long int value;
-  size_t digit_cnt = ss_get_long (s, &value);
+  size_t n_digits = ss_get_long (s, &value);
   enum date_token token = 0;
 
-  if (ss_match_byte (s, settings_get_decimal_char (FMT_F))
+  if (ss_match_byte (s, settings_get_fmt_settings ()->decimal)
       && tokens_seen & DT_COLON
       && value <= 59)
     {
@@ -703,13 +701,13 @@ parse_date_number (struct substring *s, enum date_token tokens_seen,
       else
         token = DT_DAY_COUNT;
 
-      if (digit_cnt == 2)
+      if (n_digits == 2)
         {
           token |= DT_YEAR;
           if (value <= 59)
             token |= DT_MINUTE | DT_SECOND;
         }
-      else if (digit_cnt == 4)
+      else if (n_digits == 4)
         token |= DT_YEAR;
     }