X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fformat.c;h=5e06ab2291e43ff4f40efcaeb2fbae4bcfe2a569;hb=79e615c130eb0eaa23c7c220a51c3cf00e17b3a6;hp=2adee2055d3706afeb6e6f1ed8118a44d65facea;hpb=25fae0555073f526e5d5825133a2f62454a7b4c6;p=pspp diff --git a/src/format.c b/src/format.c index 2adee2055d..5e06ab2291 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" @@ -37,6 +37,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, @@ -59,7 +62,7 @@ parse_format_specifier_name (const char **cp, enum fmt_parse_flags flags) /* 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. */ @@ -157,7 +160,7 @@ check_input_specifier (const struct fmt_spec *spec, int emit_error) 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 +171,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; } @@ -192,16 +204,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, f->name, spec->d + 1, spec->d); return 0; } return 1; @@ -253,6 +265,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 +277,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,6 +354,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 @@ -429,3 +445,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; +}