From: Ben Pfaff Date: Tue, 19 Feb 2008 06:41:25 +0000 (+0000) Subject: (var_create): Use the new functions for default variable attributes below. X-Git-Tag: v0.6.0~109 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=154fc1235e5f2be16af4cb9f61bdd160d3880452;p=pspp-builds.git (var_create): Use the new functions for default variable attributes below. (var_default_formats): New function. (var_default_measure): New function. (var_default_alignment): New function. --- diff --git a/src/data/ChangeLog b/src/data/ChangeLog index 4f67dceb..9c68bb3b 100644 --- a/src/data/ChangeLog +++ b/src/data/ChangeLog @@ -8,6 +8,11 @@ references. (max_decimals): Renamed fmt_max_decimals and made public. Updated all references. + (var_create): Use the new functions for default variable + attributes below. + (var_default_formats): New function. + (var_default_measure): New function. + (var_default_alignment): New function. * format.h (macro FMT_MAX_NUMERIC_WIDTH): New macro. diff --git a/src/data/variable.c b/src/data/variable.c index 0b8079ca..a455e40a 100644 --- a/src/data/variable.c +++ b/src/data/variable.c @@ -86,6 +86,7 @@ struct variable * var_create (const char *name, int width) { struct variable *v; + enum val_type type; assert (width >= 0 && width <= MAX_STRING); @@ -95,20 +96,11 @@ var_create (const char *name, int width) 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; @@ -597,8 +589,9 @@ var_get_print_format (const struct variable *v) } /* 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) { @@ -615,8 +608,9 @@ var_get_write_format (const struct variable *v) } /* 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) { @@ -626,14 +620,27 @@ 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)); +} /* Return a string representing this variable, in the form most appropriate from a human factors perspective, that is, its @@ -711,6 +718,16 @@ var_set_measure (struct variable *v, enum measure measure) 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; +} /* Returns V's display width, which applies only to GUIs. */ int @@ -759,6 +776,15 @@ var_set_alignment (struct variable *v, enum alignment alignment) 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; +} /* Whether variables' values should be preserved from case to case. */ diff --git a/src/data/variable.h b/src/data/variable.h index ccfdd7f8..c7f86aaf 100644 --- a/src/data/variable.h +++ b/src/data/variable.h @@ -99,6 +99,8 @@ const struct fmt_spec *var_get_write_format (const struct variable *); 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 *); @@ -119,6 +121,8 @@ bool measure_is_valid (enum measure); 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); @@ -138,6 +142,8 @@ bool alignment_is_valid (enum alignment); 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 *);