X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fprint.c;h=0376fa1cbb6497b33e4bead8f92ebaa2b7a4dbd6;hb=b9799cdd10b30ea96d9178b7a0d48504d052228c;hp=d8f4d77040834a375d9fa31bdb16734b99137abc;hpb=d807ad29cc0d3caa4f0e04ee4b75c70a225cfeaf;p=pspp-builds.git diff --git a/src/print.c b/src/print.c index d8f4d770..0376fa1c 100644 --- a/src/print.c +++ b/src/print.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. */ /* FIXME: seems like a lot of code duplication with data-list.c. */ @@ -35,6 +35,9 @@ #include "tab.h" #include "var.h" +#include "gettext.h" +#define _(msgid) gettext (msgid) + /* Describes what to do when an output field is encountered. */ enum { @@ -77,7 +80,6 @@ enum /* PRINT, PRINT EJECT, WRITE private data structure. */ struct print_trns { - struct trns_header h; struct dfm_writer *writer; /* Output file, NULL=listing file. */ int options; /* PRT_* bitmapped field. */ struct prt_out_spec *spec; /* Output specifications. */ @@ -138,8 +140,6 @@ internal_cmd_print (int f) struct file_handle *fh = NULL; /* Fill in prt to facilitate error-handling. */ - prt.h.proc = print_trns_proc; - prt.h.free = print_trns_free; prt.writer = NULL; prt.options = f; prt.spec = NULL; @@ -206,12 +206,12 @@ internal_cmd_print (int f) /* Put the transformation in the queue. */ trns = xmalloc (sizeof *trns); memcpy (trns, &prt, sizeof *trns); - add_transformation ((struct trns_header *) trns); + add_transformation (print_trns_proc, print_trns_free, trns); return CMD_SUCCESS; error: - print_trns_free ((struct trns_header *) & prt); + print_trns_free (&prt); return CMD_FAILURE; } @@ -246,8 +246,8 @@ struct fmt_list static struct { struct variable **v; /* variable list */ - int nv; /* number of variables in list */ - int cv; /* number of variables from list used up so far + size_t nv; /* number of variables in list */ + size_t cv; /* number of variables from list used up so far by the FORTRAN-like format specifiers */ int recno; /* current 1-based record number */ @@ -287,7 +287,7 @@ parse_specs (void) int prev_recno = fx.recno; fx.recno++; - if (token == T_NUM) + if (lex_is_number ()) { if (!lex_force_int ()) return 0; @@ -349,7 +349,7 @@ parse_string_argument (void) lex_get (); /* Parse the included column range. */ - if (token == T_NUM) + if (lex_is_number ()) { /* Width of column range in characters. */ int c_len; @@ -360,7 +360,7 @@ parse_string_argument (void) /* 1-based index of last column in range. */ int lc; - if (!lex_integer_p () || lex_integer () <= 0) + if (!lex_is_integer () || lex_integer () <= 0) { msg (SE, _("%g is not a valid column location."), tokval); goto fail; @@ -371,7 +371,7 @@ parse_string_argument (void) lex_negative_to_dash (); if (lex_match ('-')) { - if (!lex_integer_p ()) + if (!lex_is_integer ()) { msg (SE, _("Column location expected following `%d-'."), fx.spec.fc + 1); @@ -434,7 +434,7 @@ parse_variable_argument (void) if (!parse_variables (default_dict, &fx.v, &fx.nv, PV_DUPLICATE)) return 0; - if (token == T_NUM) + if (lex_is_number ()) { if (!fixed_parse_compatible ()) goto fail; @@ -449,7 +449,7 @@ parse_variable_argument (void) else { /* User wants dictionary format specifiers. */ - int i; + size_t i; lex_match ('*'); for (i = 0; i < fx.nv; i++) @@ -478,13 +478,28 @@ fail: return 0; } +/* Verifies that FORMAT doesn't need a variable wider than WIDTH. + Returns true iff that is the case. */ +static bool +check_string_width (const struct fmt_spec *format, const struct variable *v) +{ + if (get_format_var_width (format) > v->width) + { + msg (SE, _("Variable %s has width %d so it cannot be output " + "as format %s."), + v->name, v->width, fmt_to_string (format)); + return false; + } + return true; +} + /* Parses a column specification for parse_specs(). */ static int fixed_parse_compatible (void) { - int dividend; + int individual_var_width; int type; - int i; + size_t i; type = fx.v[0]->type; for (i = 1; i < fx.nv; i++) @@ -553,7 +568,7 @@ fixed_parse_compatible (void) else fx.spec.u.v.f.type = FMT_F; - if (token == T_NUM) + if (lex_is_number ()) { if (!lex_force_int ()) return 0; @@ -592,40 +607,28 @@ fixed_parse_compatible (void) if ((fx.lc - fx.fc + 1) % fx.nv) { - msg (SE, _("The %d columns %d-%d can't be evenly divided into %d " - "fields."), fx.lc - fx.fc + 1, fx.fc + 1, fx.lc + 1, fx.nv); + msg (SE, _("The %d columns %d-%d can't be evenly divided into %u " + "fields."), + fx.lc - fx.fc + 1, fx.fc + 1, fx.lc + 1, (unsigned) fx.nv); return 0; } - dividend = (fx.lc - fx.fc + 1) / fx.nv; - fx.spec.u.v.f.w = dividend; - if (!check_output_specifier (&fx.spec.u.v.f, 1)) + individual_var_width = (fx.lc - fx.fc + 1) / fx.nv; + fx.spec.u.v.f.w = individual_var_width; + if (!check_output_specifier (&fx.spec.u.v.f, true) + || !check_specifier_type (&fx.spec.u.v.f, type, true)) return 0; - if ((type == ALPHA) ^ (formats[fx.spec.u.v.f.type].cat & FCAT_STRING)) - { - msg (SE, _("%s variables cannot be displayed with format %s."), - type == ALPHA ? _("String") : _("Numeric"), - fmt_to_string (&fx.spec.u.v.f)); - return 0; - } - - /* Check that, for string variables, the user didn't specify a width - longer than an actual string width. */ if (type == ALPHA) { - /* Minimum width of all the string variables specified. */ - int min_len = fx.v[0]->width; - - for (i = 1; i < fx.nv; i++) - min_len = min (min_len, fx.v[i]->width); - if (!check_string_specifier (&fx.spec.u.v.f, min_len)) - return 0; + for (i = 0; i < fx.nv; i++) + if (!check_string_width (&fx.spec.u.v.f, fx.v[i])) + return false; } fx.spec.type = PRT_VAR; for (i = 0; i < fx.nv; i++) { - fx.spec.fc = fx.fc + dividend * i; + fx.spec.fc = fx.fc + individual_var_width * i; fx.spec.u.v.v = fx.v[i]; append_var_spec (&fx.spec); } @@ -634,7 +637,7 @@ fixed_parse_compatible (void) /* Destroy a format list and, optionally, all its sublists. */ static void -destroy_fmt_list (struct fmt_list * f, int recurse) +destroy_fmt_list (struct fmt_list *f, int recurse) { struct fmt_list *next; @@ -651,7 +654,7 @@ destroy_fmt_list (struct fmt_list * f, int recurse) FORTRAN-like format specifications, like 4(F10,2X)) into the structure prt. */ static int -dump_fmt_list (struct fmt_list * f) +dump_fmt_list (struct fmt_list *f) { int i; @@ -688,15 +691,10 @@ dump_fmt_list (struct fmt_list * f) } v = fx.v[fx.cv++]; - if ((v->type == ALPHA) ^ (formats[f->f.type].cat & FCAT_STRING)) - { - msg (SE, _("Display format %s may not be used with a " - "%s variable."), fmt_to_string (&f->f), - v->type == ALPHA ? _("string") : _("numeric")); - return 0; - } - if (!check_string_specifier (&f->f, v->width)) - return 0; + if (!check_output_specifier (&f->f, true) + || !check_specifier_type (&f->f, v->type, true) + || !check_string_width (&f->f, v)) + return false; fx.spec.type = PRT_VAR; fx.spec.u.v.v = v; @@ -727,9 +725,9 @@ fixed_parse_fortran (void) else head = fl = xmalloc (sizeof *fl); - if (token == T_NUM) + if (lex_is_number ()) { - if (!lex_integer_p ()) + if (!lex_is_integer ()) goto fail; fl->count = lex_integer (); lex_get (); @@ -898,11 +896,10 @@ alloc_line (void) /* Performs the transformation inside print_trns T on case C. */ static int -print_trns_proc (struct trns_header * trns, struct ccase * c, - int case_num UNUSED) +print_trns_proc (void *trns_, struct ccase *c, int case_num UNUSED) { /* Transformation. */ - struct print_trns *t = (struct print_trns *) trns; + struct print_trns *t = trns_; /* Iterator. */ struct prt_out_spec *i; @@ -975,9 +972,9 @@ print_trns_proc (struct trns_header * trns, struct ccase * c, /* Frees all the data inside print_trns T. Does not free T. */ static void -print_trns_free (struct trns_header * t) +print_trns_free (void *prt_) { - struct print_trns *prt = (struct print_trns *) t; + struct print_trns *prt = prt_; struct prt_out_spec *i, *n; for (i = prt->spec; i; i = n) @@ -1002,6 +999,7 @@ print_trns_free (struct trns_header * t) if (prt->writer != NULL) dfm_close_writer (prt->writer); free (prt->line); + free (prt); } /* PRINT SPACE. */ @@ -1009,8 +1007,6 @@ print_trns_free (struct trns_header * t) /* PRINT SPACE transformation. */ struct print_space_trns { - struct trns_header h; - struct dfm_writer *writer; /* Output data file. */ struct expression *e; /* Number of lines; NULL=1. */ } @@ -1065,23 +1061,18 @@ cmd_print_space (void) writer = NULL; t = xmalloc (sizeof *t); - t->h.proc = print_space_trns_proc; - if (e) - t->h.free = print_space_trns_free; - else - t->h.free = NULL; t->writer = writer; t->e = e; - add_transformation ((struct trns_header *) t); + add_transformation (print_space_trns_proc, print_space_trns_free, t); return CMD_SUCCESS; } static int -print_space_trns_proc (struct trns_header * trns, struct ccase * c, +print_space_trns_proc (void *t_, struct ccase *c, int case_num UNUSED) { - struct print_space_trns *t = (struct print_space_trns *) trns; + struct print_space_trns *t = t_; double n = 1.; if (t->e) @@ -1117,7 +1108,9 @@ print_space_trns_proc (struct trns_header * trns, struct ccase * c, } static void -print_space_trns_free (struct trns_header * trns) +print_space_trns_free (void *trns_) { - expr_free (((struct print_space_trns *) trns)->e); + struct print_space_trns *trns = trns_; + expr_free (trns->e); + free (trns); }