var_create (const char *name, int width)
{
struct variable *v;
+ enum val_type type;
assert (width >= 0 && width <= MAX_STRING);
v->width = width;
mv_init (&v->miss, width);
v->leave = var_must_leave (v);
- if (var_is_numeric (v))
- {
- v->print = fmt_for_output (FMT_F, 8, 2);
- v->alignment = ALIGN_RIGHT;
- v->measure = MEASURE_SCALE;
- }
- else
- {
- v->print = fmt_for_output (FMT_A, var_get_width (v), 0);
- v->alignment = ALIGN_LEFT;
- v->measure = MEASURE_NOMINAL;
- }
+ type = val_type_from_width (width);
+ v->alignment = var_default_alignment (type);
+ v->measure = var_default_measure (type);
v->display_width = var_default_display_width (width);
- v->write = v->print;
+ v->print = v->write = var_default_formats (width);
v->val_labs = NULL;
v->label = NULL;
v->short_names = NULL;
}
/* Sets V's print format specification to PRINT, which must be a
- valid format specification for outputting a variable of V's
- width. */
+ valid format specification for a variable of V's width
+ (ordinarily an output format, but input formats are not
+ rejected). */
void
var_set_print_format (struct variable *v, const struct fmt_spec *print)
{
}
/* Sets V's write format specification to WRITE, which must be a
- valid format specification for outputting a variable of V's
- width. */
+ valid format specification for a variable of V's width
+ (ordinarily an output format, but input formats are not
+ rejected). */
void
var_set_write_format (struct variable *v, const struct fmt_spec *write)
{
}
/* Sets V's print and write format specifications to FORMAT,
- which must be a valid format specification for outputting a
- variable of V's width. */
+ which must be a valid format specification for a variable of
+ V's width (ordinarily an output format, but input formats are
+ not rejected). */
void
var_set_both_formats (struct variable *v, const struct fmt_spec *format)
{
var_set_print_format (v, format);
var_set_write_format (v, format);
}
+
+/* Returns the default print and write format for a variable of
+ the given TYPE, as set by var_create. The return value can be
+ used to reset a variable's print and write formats to the
+ default. */
+struct fmt_spec
+var_default_formats (int width)
+{
+ return (width == 0
+ ? fmt_for_output (FMT_F, 8, 2)
+ : fmt_for_output (FMT_A, width, 0));
+}
\f
/* Return a string representing this variable, in the form most
appropriate from a human factors perspective, that is, its
v->measure = measure;
dict_var_changed (v);
}
+
+/* Returns the default measurement level for a variable of the
+ given TYPE, as set by var_create. The return value can be
+ used to reset a variable's measurement level to the
+ default. */
+enum measure
+var_default_measure (enum val_type type)
+{
+ return type == VAL_NUMERIC ? MEASURE_SCALE : MEASURE_NOMINAL;
+}
\f
/* Returns V's display width, which applies only to GUIs. */
int
v->alignment = alignment;
dict_var_changed (v);
}
+
+/* Returns the default display alignment for a variable of the
+ given TYPE, as set by var_create. The return value can be
+ used to reset a variable's display alignment to the default. */
+enum alignment
+var_default_alignment (enum val_type type)
+{
+ return type == VAL_NUMERIC ? ALIGN_RIGHT : ALIGN_LEFT;
+}
\f
/* Whether variables' values should be preserved from case to
case. */
void var_set_write_format (struct variable *, const struct fmt_spec *);
void var_set_both_formats (struct variable *, const struct fmt_spec *);
+struct fmt_spec var_default_formats (int width);
+
/* Variable labels. */
const char *var_to_string (const struct variable *);
const char *var_get_label (const struct variable *);
enum measure var_get_measure (const struct variable *);
void var_set_measure (struct variable *, enum measure);
+enum measure var_default_measure (enum val_type);
+
/* GUI display width. */
int var_get_display_width (const struct variable *);
void var_set_display_width (struct variable *, int display_width);
enum alignment var_get_alignment (const struct variable *);
void var_set_alignment (struct variable *, enum alignment);
+enum alignment var_default_alignment (enum val_type);
+
/* Whether variables' values should be preserved from case to
case. */
bool var_get_leave (const struct variable *);