X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fformat.c;h=cce8c46ed2def8225719b514f30a777cfbb029b3;hb=d5fd364b203a2a84e5034b6ff5ac5d6c4412edb7;hp=b3070e2149f1e9c30250ccd8759896f279b55b95;hpb=1339492699ce7e12c9bf9fa17f9d60a66024cbd1;p=pspp-builds.git diff --git a/src/format.c b/src/format.c index b3070e21..cce8c46e 100644 --- a/src/format.c +++ b/src/format.c @@ -23,11 +23,14 @@ #include "error.h" #include #include "error.h" -#include "lexer.h" +#include "lex-def.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) \ {NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, OUTPUT, SPSS_FMT}, @@ -37,66 +40,8 @@ struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] = {"", -1, -1, -1, -1, -1, 0000, -1, -1}, }; -/* 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, enum fmt_parse_flags flags) -{ - char *sp, *ep; - int idx; - - sp = ep = ds_c_str (&tokstr); - while (isalpha ((unsigned char) *ep)) - ep++; - - if (sp != ep) - { - /* Find format. */ - for (idx = 0; idx < FMT_NUMBER_OF_FORMATS; idx++) - if (strlen (formats[idx].name) == ep - sp - && !mm_case_compare (formats[idx].name, sp, ep - sp)) - break; - - /* Check format. */ - if (idx < FMT_NUMBER_OF_FORMATS) - { - if (!(flags & FMTP_ALLOW_XT) && (idx == FMT_T || idx == FMT_X)) - { - if (!(flags & FMTP_SUPPRESS_ERRORS)) - msg (SE, _("X and T format specifiers not allowed here.")); - idx = -1; - } - } - else - { - /* No match. */ - if (!(flags & FMTP_SUPPRESS_ERRORS)) - msg (SE, _("%.*s is not a valid data format."), - (int) (ep - sp), ds_c_str (&tokstr)); - idx = -1; - } - } - else - { - lex_error ("expecting data format"); - idx = -1; - } - - if (cp != NULL) - { - if (idx != -1) - *cp = ep; - else - *cp = NULL; - } - - return idx; -} +/* Common formats. */ +const struct fmt_spec f8_2 = {FMT_F, 8, 2}; /* Converts F to its string representation (for instance, "F8.2") and returns a pointer to a static buffer containing that string. */ @@ -119,15 +64,26 @@ fmt_to_string (const struct fmt_spec *f) 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); + struct fmt_desc *f ; + char *str; + + if ( spec->type > FMT_NUMBER_OF_FORMATS ) + { + if (emit_error) + msg (SE, _("Format specifies a bad type (%d)"), spec->type); + + return false; + } + + f = &formats[spec->type]; + 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 " - "format %s requires an even width."), - str, spec->w, f->name, f->Imin_w, f->Imax_w); + "an even width is required."), + str, spec->w); return false; } if (f->n_args > 1 && (spec->d < 0 || spec->d > 16)) @@ -147,17 +103,22 @@ check_common_specifier (const struct fmt_spec *spec, bool emit_error) int check_input_specifier (const struct fmt_spec *spec, int emit_error) { - struct fmt_desc *f = &formats[spec->type]; - char *str = fmt_to_string (spec); + struct fmt_desc *f ; + char *str ; if (!check_common_specifier (spec, emit_error)) return false; + + f = &formats[spec->type]; + str = fmt_to_string (spec); + + if (spec->type == FMT_X) return 1; if (f->cat & FCAT_OUTPUT_ONLY) { if (emit_error) - msg (SE, _("Format %s may not be used as an input format."), f->name); + 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) @@ -168,6 +129,15 @@ check_input_specifier (const struct fmt_spec *spec, int emit_error) str, spec->w, f->name, f->Imin_w, f->Imax_w); return 0; } + if ((spec->type == FMT_F || spec->type == FMT_COMMA + || spec->type == FMT_DOLLAR) + && spec->d > spec->w) + { + 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; } @@ -177,11 +147,15 @@ check_input_specifier (const struct fmt_spec *spec, int emit_error) int check_output_specifier (const struct fmt_spec *spec, int emit_error) { - struct fmt_desc *f = &formats[spec->type]; - char *str = fmt_to_string (spec); + struct fmt_desc *f; + char *str ; if (!check_common_specifier (spec, emit_error)) return false; + + f = &formats[spec->type]; + str = fmt_to_string (spec); + if (spec->type == FMT_X) return 1; if (spec->w < f->Omin_w || spec->w > f->Omax_w) @@ -192,16 +166,16 @@ check_output_specifier (const struct fmt_spec *spec, int emit_error) 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) + && spec->d >= spec->w) { if (emit_error) - 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); + 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; @@ -253,6 +227,8 @@ check_specifier_width (const struct fmt_spec *format, 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) @@ -263,8 +239,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); @@ -340,67 +316,8 @@ convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output) default: assert (0); } -} - -/* 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, enum fmt_parse_flags flags) -{ - struct fmt_spec spec; - struct fmt_desc *f; - const char *cp; - char *cp2; - int type, w, d; - - if (token != T_ID) - { - if (!(flags & FMTP_SUPPRESS_ERRORS)) - msg (SE, _("Format specifier expected.")); - return 0; - } - type = parse_format_specifier_name (&cp, flags); - if (type == -1) - return 0; - f = &formats[type]; - - w = strtol (cp, &cp2, 10); - if (cp2 == cp && type != FMT_X) - { - if (!(flags & FMTP_SUPPRESS_ERRORS)) - msg (SE, _("Data format %s does not specify a width."), - ds_c_str (&tokstr)); - return 0; - } - cp = cp2; - if (f->n_args > 1 && *cp == '.') - { - cp++; - d = strtol (cp, &cp2, 10); - cp = cp2; - } - else - d = 0; - - if (*cp) - { - if (!(flags & FMTP_SUPPRESS_ERRORS)) - msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr)); - return 0; - } - lex_get (); - - spec.type = type; - spec.w = w; - spec.d = d; - *input = spec; - - return 1; + assert (check_output_specifier (output, 0)); } /* Returns the width corresponding to the format specifier. The @@ -429,3 +346,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; +}