From 196db9f58754f9f42d8679384b610aa41c820cfb Mon Sep 17 00:00:00 2001 From: John Darrington Date: Thu, 29 Sep 2011 13:22:00 +0200 Subject: [PATCH] Implemented the SET=TNUMBERS subcommand --- doc/utilities.texi | 12 ++++++++++ src/data/settings.c | 28 +++++++++++++++++++++- src/data/settings.h | 24 +++++++++++++++++++ src/data/variable.c | 46 ++++++++++++++++++++++++++++++------ src/language/utilities/set.q | 30 +++++++++++++++++++++++ 5 files changed, 132 insertions(+), 8 deletions(-) diff --git a/doc/utilities.texi b/doc/utilities.texi index fdad9a59ac..93701fd781 100644 --- a/doc/utilities.texi +++ b/doc/utilities.texi @@ -415,6 +415,7 @@ SET /LENGTH=@{NONE,length_in_lines@} /MORE=@{ON,OFF@} /WIDTH=@{NARROW,WIDTH,n_characters@} + /TNUMBERS=@{VALUES,LABELS,BOTH@} (logging) /JOURNAL=@{ON,OFF@} ['file-name'] @@ -702,6 +703,16 @@ subcommands are @itemx MORE @itemx PAGER @itemx WIDTH +@itemx TNUMBERS +The TNUMBERS option sets the way in which values are displayed in output tables. +The valid settings are VALUES, LABELS and BOTH. +If TNUMBERS is set to VALUES, then all values are displayed with their literal value +(which for a numeric value is a number and for a string value an alphanumeric string). +If TNUMBERS is set to LABELS, then values are displayed using their assigned labels if any. +(@xref{VALUE LABELS}.) +If the a value has no label, then it will be displayed using its literal value. +If TNUMBERS is set to BOTH, then values will be displayed with both their label +(if any) and their literal value in parenthesis. @end table @cindex headers @@ -709,6 +720,7 @@ subcommands are @cindex more @cindex pager @cindex width +@cindex tnumbers Logging subcommands affect logging of commands executed to external diff --git a/src/data/settings.c b/src/data/settings.c index 71d4261de8..0868a33189 100644 --- a/src/data/settings.c +++ b/src/data/settings.c @@ -75,6 +75,9 @@ struct settings struct fmt_settings *styles; enum settings_output_devices output_routing[SETTINGS_N_OUTPUT_TYPES]; + + enum settings_var_style var_output_style; + enum settings_value_style value_output_style; }; static struct settings the_settings = { @@ -115,7 +118,10 @@ static struct settings the_settings = { {SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL, SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL, 0, - SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL} + SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL}, + + SETTINGS_VAR_STYLE_LABELS, + SETTINGS_VAL_STYLE_LABELS }; /* Initializes the settings module. */ @@ -687,3 +693,23 @@ settings_get_output_routing (enum settings_output_type type) assert (type < SETTINGS_N_OUTPUT_TYPES); return the_settings.output_routing[type] | SETTINGS_DEVICE_UNFILTERED; } + +enum settings_value_style +settings_get_value_style (void) +{ + return the_settings.value_output_style; +} + +void +settings_set_value_style (enum settings_value_style s) +{ + the_settings.value_output_style = s; +} + + + +enum settings_var_style +settings_get_var_style (void) +{ + return the_settings.var_output_style; +} diff --git a/src/data/settings.h b/src/data/settings.h index 963458d826..559f64ebb6 100644 --- a/src/data/settings.h +++ b/src/data/settings.h @@ -102,6 +102,28 @@ void settings_set_format ( const struct fmt_spec *); bool settings_get_testing_mode (void); void settings_set_testing_mode (bool); +enum settings_var_style + { + SETTINGS_VAR_STYLE_NAMES, + SETTINGS_VAR_STYLE_LABELS, + SETTINGS_VAR_STYLE_BOTH + }; + +enum settings_value_style + { + SETTINGS_VAL_STYLE_VALUES, + SETTINGS_VAL_STYLE_LABELS, + SETTINGS_VAL_STYLE_BOTH + }; + + +enum settings_value_style settings_get_value_style (void); +enum settings_var_style settings_get_var_style (void); + +void settings_set_value_style (enum settings_value_style s); +void settings_set_var_style (enum settings_value_style s); + + enum behavior_mode { ENHANCED, /* Use improved PSPP behavior. */ COMPATIBLE /* Be as compatible as possible. */ @@ -143,6 +165,8 @@ enum settings_output_type SETTINGS_N_OUTPUT_TYPES }; + + void settings_set_output_routing (enum settings_output_type, enum settings_output_devices); enum settings_output_devices settings_get_output_routing ( diff --git a/src/data/variable.c b/src/data/variable.c index 1f9b62e192..537b247cd3 100644 --- a/src/data/variable.c +++ b/src/data/variable.c @@ -26,6 +26,7 @@ #include "data/format.h" #include "data/identifier.h" #include "data/missing-values.h" +#include "data/settings.h" #include "data/value-labels.h" #include "data/vardict.h" #include "libpspp/assertion.h" @@ -467,6 +468,19 @@ var_lookup_value_label (const struct variable *v, const union value *value) return val_labs_find (v->val_labs, value); } +/* + Append to STR the string representation of VALUE for variable V. + STR must be a pointer to an initialised struct string. +*/ +static void +append_value (const struct variable *v, const union value *value, + struct string *str) +{ + char *s = data_out (value, var_get_encoding (v), &v->print); + ds_put_cstr (str, s); + free (s); +} + /* Append STR with a string representing VALUE for variable V. That is, if VALUE has a label, append that label, otherwise format VALUE and append the formatted string. @@ -476,15 +490,33 @@ void var_append_value_name (const struct variable *v, const union value *value, struct string *str) { + enum settings_value_style style = settings_get_value_style (); const char *name = var_lookup_value_label (v, value); - if (name == NULL) + + switch (style) { - char *s = data_out (value, var_get_encoding (v), &v->print); - ds_put_cstr (str, s); - free (s); - } - else - ds_put_cstr (str, name); + case SETTINGS_VAL_STYLE_VALUES: + append_value (v, value, str); + break; + + case SETTINGS_VAL_STYLE_LABELS: + if (name == NULL) + append_value (v, value, str); + else + ds_put_cstr (str, name); + break; + + case SETTINGS_VAL_STYLE_BOTH: + default: + append_value (v, value, str); + if (name != NULL) + { + ds_put_cstr (str, " ("); + ds_put_cstr (str, name); + ds_put_cstr (str, ")"); + } + break; + }; } /* Print and write formats. */ diff --git a/src/language/utilities/set.q b/src/language/utilities/set.q index ce358ae8a4..bb52fe3414 100644 --- a/src/language/utilities/set.q +++ b/src/language/utilities/set.q @@ -101,6 +101,7 @@ int tgetnum (const char *); scompression=scompress:on/off; scripttab=string "x==1" "one character long"; seed=custom; + tnumbers=custom; tb1=string "x==3 || x==11" "3 or 11 characters long"; tbfonts=string; undefined=undef:warn/nowarn; @@ -298,6 +299,35 @@ stc_custom_blanks (struct lexer *lexer, return 1; } +static int +stc_custom_tnumbers (struct lexer *lexer, + struct dataset *ds UNUSED, + struct cmd_set *cmd UNUSED, void *aux UNUSED) +{ + lex_match (lexer, T_EQUALS); + + if (lex_match_id (lexer, "VALUES")) + { + settings_set_value_style (SETTINGS_VAL_STYLE_VALUES); + } + else if (lex_match_id (lexer, "LABELS")) + { + settings_set_value_style (SETTINGS_VAL_STYLE_LABELS); + } + else if (lex_match_id (lexer, "BOTH")) + { + settings_set_value_style (SETTINGS_VAL_STYLE_BOTH); + } + else + { + lex_error (lexer, _("expecting VALUES, LABELS or BOTH")); + return 0; + } + + return 1; +} + + /* Parses the EPOCH subcommand, which controls the epoch used for parsing 2-digit years. */ static int -- 2.30.2