+2007-07-22 Ben Pfaff <blp@gnu.org>
+
+ * variable.c (var_set_width): Use new var_set_width function.
+
+ * missing-values.c (mv_n_values): Drop assertion, which was not
+ needed.
+
+ * format.c (fmt_default_for_width): New function.
+ (fmt_resize): New function.
+
2007-07-18 John Darrington <john@darrington.wattle.id.au>
* datasheet.c (datasheet_delete_columns): Added assertion to check
return output;
}
+/* Returns the default format for the given WIDTH: F8.2 format
+ for a numeric value, A format for a string value. */
+struct fmt_spec
+fmt_default_for_width (int width)
+{
+ return (width == 0
+ ? fmt_for_output (FMT_F, 8, 2)
+ : fmt_for_output (FMT_A, width, 0));
+}
+
/* Checks whether SPEC is valid as an input format (if FOR_INPUT)
or an output format (otherwise) and returns nonzero if so.
Otherwise, emits an error message and returns zero. */
{
return a->type == b->type && a->w == b->w && a->d == b->d;
}
+
+/* Adjusts FMT to be valid for a value of the given WIDTH. */
+void
+fmt_resize (struct fmt_spec *fmt, int width)
+{
+ if ((width > 0) != fmt_is_string (fmt->type))
+ {
+ /* Changed from numeric to string or vice versa. Set to
+ default format for new width. */
+ *fmt = fmt_default_for_width (width);
+ }
+ else if (width > 0)
+ {
+ /* Changed width of string. Preserve format type, adjust
+ width. */
+ fmt->w = fmt->type == FMT_AHEX ? width * 2 : width;
+ }
+ else
+ {
+ /* Still numeric. */
+ }
+}
\f
/* Describes a display format. */
struct fmt_desc
int fmt_var_width (const struct fmt_spec *);
char *fmt_to_string (const struct fmt_spec *, char s[FMT_STRING_LEN_MAX + 1]);
bool fmt_equal (const struct fmt_spec *, const struct fmt_spec *);
+void fmt_resize (struct fmt_spec *, int new_width);
/* Format types. */
const char *fmt_name (enum fmt_type) PURE_FUNCTION;
}
-
+/* Returns the number of individual (not part of a range) missing
+ values in MV. */
int
mv_n_values (const struct missing_values *mv)
{
- assert(mv_has_value(mv));
return mv->type & 3;
}
var_set_width (struct variable *v, int new_width)
{
const int old_width = v->width;
- enum var_type new_type = var_type_from_width (new_width);
if (mv_is_resizable (&v->miss, new_width))
mv_resize (&v->miss, new_width);
}
}
- if (var_get_type (v) != new_type)
- {
- v->print = (new_type == VAR_NUMERIC
- ? fmt_for_output (FMT_F, 8, 2)
- : fmt_for_output (FMT_A, new_width, 0));
- v->write = v->print;
- }
- else if (new_type == VAR_STRING)
- {
- v->print.w = v->print.type == FMT_AHEX ? new_width * 2 : new_width;
- v->write.w = v->write.type == FMT_AHEX ? new_width * 2 : new_width;
- }
+ fmt_resize (&v->print, new_width);
+ fmt_resize (&v->write, new_width);
v->width = new_width;