X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fformat.c;h=a37f71e814082bc6fadc02df4ce778f724baf162;hb=1f8dd363d6c20d07fcca14cb948018465fa5ed8b;hp=744cd6feccaf3644fb118cc9cdfb0c51c4f6a3f3;hpb=92bfefccd465052e492f669ce561aa25b0110283;p=pspp-builds.git diff --git a/src/format.c b/src/format.c index 744cd6fe..a37f71e8 100644 --- a/src/format.c +++ b/src/format.c @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. */ + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ #include #include "format.h" @@ -26,6 +26,10 @@ #include "lexer.h" #include "misc.h" #include "str.h" +#include "var.h" + +#include "gettext.h" +#define _(msgid) gettext (msgid) #define DEFFMT(LABEL, NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, \ OUTPUT, SPSS_FMT) \ @@ -36,6 +40,9 @@ struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] = {"", -1, -1, -1, -1, -1, 0000, -1, -1}, }; +/* Common formats. */ +const struct fmt_spec f8_2 = {FMT_F, 8, 2}; + /* 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, @@ -44,7 +51,7 @@ struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] = in the current token on success or to a null pointer on failure. */ int -parse_format_specifier_name (const char **cp, int allow_xt) +parse_format_specifier_name (const char **cp, enum fmt_parse_flags flags) { char *sp, *ep; int idx; @@ -58,23 +65,25 @@ parse_format_specifier_name (const char **cp, int allow_xt) /* 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)) + && !buf_compare_case (formats[idx].name, sp, ep - sp)) break; /* Check format. */ if (idx < FMT_NUMBER_OF_FORMATS) { - if (!allow_xt && (idx == FMT_T || idx == FMT_X)) + if (!(flags & FMTP_ALLOW_XT) && (idx == FMT_T || idx == FMT_X)) { - msg (SE, _("X and T format specifiers not allowed here.")); + if (!(flags & FMTP_SUPPRESS_ERRORS)) + 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_c_str (&tokstr)); + if (!(flags & FMTP_SUPPRESS_ERRORS)) + msg (SE, _("%.*s is not a valid data format."), + (int) (ep - sp), ds_c_str (&tokstr)); idx = -1; } } @@ -109,110 +118,149 @@ fmt_to_string (const struct fmt_spec *f) return buf; } +/* Does checks in common betwen check_input_specifier() and + check_output_specifier() and returns true if so. Otherwise, + emits an error message (if EMIT_ERROR is nonzero) and returns + false. */ +static bool +check_common_specifier (const struct fmt_spec *spec, bool emit_error) +{ + struct fmt_desc *f = &formats[spec->type]; + char *str = fmt_to_string (spec); + + if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2) + { + if (emit_error) + msg (SE, _("Format %s specifies an odd width %d, but " + "an even width is required."), + str, spec->w); + return false; + } + if (f->n_args > 1 && (spec->d < 0 || spec->d > 16)) + { + if (emit_error) + msg (SE, _("Format %s specifies a bad number of " + "implied decimal places %d. Input format %s allows " + "up to 16 implied decimal places."), str, spec->d, f->name); + return false; + } + return true; +} + /* Checks whether SPEC is valid as an input format and returns - nonzero if so. Otherwise, emits an error message and returns - zero. */ + nonzero if so. Otherwise, emits an error message (if + EMIT_ERROR is nonzero) and returns zero. */ int -check_input_specifier (const struct fmt_spec *spec) +check_input_specifier (const struct fmt_spec *spec, int emit_error) { - struct fmt_desc *f; - char *str; + struct fmt_desc *f = &formats[spec->type]; + char *str = fmt_to_string (spec); - f = &formats[spec->type]; - str = fmt_to_string (spec); + if (!check_common_specifier (spec, emit_error)) + return false; if (spec->type == FMT_X) return 1; if (f->cat & FCAT_OUTPUT_ONLY) { - msg (SE, _("Format %s may not be used as an input format."), f->name); + if (emit_error) + msg (SE, _("Format %s may not be used for input."), f->name); return 0; } if (spec->w < f->Imin_w || spec->w > f->Imax_w) { - msg (SE, _("Input format %s specifies a bad width %d. " - "Format %s requires a width between %d and %d."), - str, spec->w, f->name, f->Imin_w, f->Imax_w); - return 0; - } - if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2) - { - msg (SE, _("Input format %s specifies an odd width %d, but " - "format %s requires an even width between %d and " - "%d."), str, spec->w, f->name, f->Imin_w, f->Imax_w); + if (emit_error) + msg (SE, _("Input format %s specifies a bad width %d. " + "Format %s requires a width between %d and %d."), + str, spec->w, f->name, f->Imin_w, f->Imax_w); return 0; } - if (f->n_args > 1 && (spec->d < 0 || spec->d > 16)) + if ((spec->type == FMT_F || spec->type == FMT_COMMA + || spec->type == FMT_DOLLAR) + && spec->d > spec->w) { - msg (SE, _("Input format %s specifies a bad number of " - "implied decimal places %d. Input format %s allows " - "up to 16 implied decimal places."), str, spec->d, f->name); + if (emit_error) + msg (SE, _("Input format %s is invalid because it specifies more " + "decimal places than the field width."), str); return 0; } return 1; } /* Checks whether SPEC is valid as an output format and returns - nonzero if so. Otherwise, emits an error message and returns - zero. */ + nonzero if so. Otherwise, emits an error message (if + EMIT_ERROR is nonzero) and returns zero. */ int -check_output_specifier (const struct fmt_spec *spec) +check_output_specifier (const struct fmt_spec *spec, int emit_error) { - struct fmt_desc *f; - char *str; + struct fmt_desc *f = &formats[spec->type]; + char *str = fmt_to_string (spec); - f = &formats[spec->type]; - str = fmt_to_string (spec); + if (!check_common_specifier (spec, emit_error)) + return false; if (spec->type == FMT_X) return 1; if (spec->w < f->Omin_w || spec->w > f->Omax_w) { - msg (SE, _("Output format %s specifies a bad width %d. " - "Format %s requires a width between %d and %d."), - str, spec->w, f->name, f->Omin_w, f->Omax_w); + if (emit_error) + msg (SE, _("Output format %s specifies a bad width %d. " + "Format %s requires a width between %d and %d."), + str, spec->w, f->name, f->Omin_w, f->Omax_w); return 0; } - if (spec->d > 1 - && (spec->type == FMT_F || spec->type == FMT_COMMA + if ((spec->type == FMT_F || spec->type == FMT_COMMA || spec->type == FMT_DOLLAR) - && spec->w < f->Omin_w + 1 + spec->d) - { - msg (SE, _("Output format %s requires minimum width %d to allow " - "%d decimal places. Try %s%d.%d instead of %s."), - f->name, f->Omin_w + 1 + spec->d, spec->d, f->name, - f->Omin_w + 1 + spec->d, spec->d, str); - return 0; - } - if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2) - { - msg (SE, _("Output format %s specifies an odd width %d, but " - "output format %s requires an even width between %d and " - "%d."), str, spec->w, f->name, f->Omin_w, f->Omax_w); - return 0; - } - if (f->n_args > 1 && (spec->d < 0 || spec->d > 16)) + && spec->d >= spec->w) { - msg (SE, _("Output format %s specifies a bad number of " - "implied decimal places %d. Output format %s allows " - "a number of implied decimal places between 1 " - "and 16."), str, spec->d, f->name); + if (emit_error) + msg (SE, _("Output format %s is invalid because it specifies as " + "many decimal places as the field width, which " + "fails to allow space for a decimal point. " + "Try %s%d.%d instead."), + str, f->name, spec->d + 1, spec->d); return 0; } return 1; } -/* If a string variable has width W, you can't display it with a - format specifier with a required width MIN_LEN>W. */ -int -check_string_specifier (const struct fmt_spec *f, int min_len) +/* Checks that FORMAT is appropriate for a variable of the given + TYPE and returns true if so. Otherwise returns false and (if + EMIT_ERROR is true) emits an error message. */ +bool +check_specifier_type (const struct fmt_spec *format, + int type, bool emit_error) { - if ((f->type == FMT_A && min_len > f->w) - || (f->type == FMT_AHEX && min_len * 2 > f->w)) + const struct fmt_desc *f = &formats[format->type]; + assert (type == NUMERIC || type == ALPHA); + if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0)) { - msg (SE, _("Can't display a string variable of width %d with " - "format specifier %s."), min_len, fmt_to_string (f)); - return 0; + if (emit_error) + msg (SE, _("%s variables are not compatible with %s format %s."), + type == ALPHA ? _("String") : _("Numeric"), + type == ALPHA ? _("numeric") : _("string"), + fmt_to_string (format)); + return false; } - return 1; + return true; +} + +/* Checks that FORMAT is appropriate for a variable of the given + WIDTH and returns true if so. Otherwise returns false and (if + EMIT_ERROR is true) emits an error message. */ +bool +check_specifier_width (const struct fmt_spec *format, + int width, bool emit_error) +{ + if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error)) + return false; + if (get_format_var_width (format) != width) + { + if (emit_error) + msg (SE, _("String variable with width %d not compatible with " + "format %s."), + width, fmt_to_string (format)); + return false; + } + return true; } /* Converts input format specifier INPUT into output format @@ -220,6 +268,8 @@ check_string_specifier (const struct fmt_spec *f, int min_len) void convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output) { + assert (check_input_specifier (input, 0)); + output->type = formats[input->type].output; output->w = input->w; if (output->w > formats[output->type].Omax_w) @@ -230,8 +280,8 @@ convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output) { case FMT_F: case FMT_N: - if (output->d > 1 && output->w < 2 + output->d) - output->w = 2 + output->d; + if (output->d > 0) + output->w++; break; case FMT_E: output->w = max (max (input->w, input->d+7), 10); @@ -307,6 +357,8 @@ convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output) default: assert (0); } + + assert (check_output_specifier (output, 0)); } /* Parses a format specifier from the token stream and returns @@ -316,7 +368,7 @@ convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output) check_output_specifier() on the parsed format as necessary. */ int -parse_format_specifier (struct fmt_spec *input, int allow_xt) +parse_format_specifier (struct fmt_spec *input, enum fmt_parse_flags flags) { struct fmt_spec spec; struct fmt_desc *f; @@ -326,10 +378,11 @@ parse_format_specifier (struct fmt_spec *input, int allow_xt) if (token != T_ID) { - msg (SE, _("Format specifier expected.")); + if (!(flags & FMTP_SUPPRESS_ERRORS)) + msg (SE, _("Format specifier expected.")); return 0; } - type = parse_format_specifier_name (&cp, allow_xt); + type = parse_format_specifier_name (&cp, flags); if (type == -1) return 0; f = &formats[type]; @@ -337,8 +390,9 @@ parse_format_specifier (struct fmt_spec *input, int allow_xt) w = strtol (cp, &cp2, 10); if (cp2 == cp && type != FMT_X) { - msg (SE, _("Data format %s does not specify a width."), - ds_c_str (&tokstr)); + if (!(flags & FMTP_SUPPRESS_ERRORS)) + msg (SE, _("Data format %s does not specify a width."), + ds_c_str (&tokstr)); return 0; } @@ -354,7 +408,8 @@ parse_format_specifier (struct fmt_spec *input, int allow_xt) if (*cp) { - msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr)); + if (!(flags & FMTP_SUPPRESS_ERRORS)) + msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr)); return 0; } lex_get (); @@ -374,7 +429,7 @@ int get_format_var_width (const struct fmt_spec *spec) { if (spec->type == FMT_AHEX) - return spec->w * 2; + return spec->w / 2; else if (spec->type == FMT_A) return spec->w; else @@ -393,3 +448,29 @@ translate_fmt (int spss) return type; return -1; } + +/* Returns an input format specification with type TYPE, width W, + and D decimals. */ +struct fmt_spec +make_input_format (int type, int w, int d) +{ + struct fmt_spec f; + f.type = type; + f.w = w; + f.d = d; + assert (check_input_specifier (&f, 0)); + return f; +} + +/* Returns an output format specification with type TYPE, width + W, and D decimals. */ +struct fmt_spec +make_output_format (int type, int w, int d) +{ + struct fmt_spec f; + f.type = type; + f.w = w; + f.d = d; + assert (check_output_specifier (&f, 0)); + return f; +}