X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fval-labs.c;h=1af46a2c5d81496e5bc5f5156805a395d3002883;hb=f62199cac6213ed1c03ffe48450a583da0fadc68;hp=69709820ed5f20782cfc70647a10e75b3a299e8b;hpb=3a7fba81ceae5b049d0f7d671e9e3c3c43bbf703;p=pspp-builds.git diff --git a/src/val-labs.c b/src/val-labs.c index 69709820..1af46a2c 100644 --- a/src/val-labs.c +++ b/src/val-labs.c @@ -31,58 +31,23 @@ /* Declarations. */ -#include "debug-print.h" - -/* Variable list. */ -static struct variable **v; - -/* Number of variables. */ -static int nv; - static int do_value_labels (int); -static int verify_val_labs (int erase); -static int get_label (void); - -#if DEBUGGING -static void debug_print (void); -#endif +static int verify_val_labs (struct variable **vars, int var_cnt); +static void erase_labels (struct variable **vars, int var_cnt); +static int get_label (struct variable **vars, int var_cnt); /* Stubs. */ -static void -init (void) -{ - v = NULL; -} - -static void -done (void) -{ - free (v); -} - int cmd_value_labels (void) { - int code; - init (); - lex_match_id ("VALUE"); - lex_match_id ("LABELS"); - code = do_value_labels (1); - done (); - return code; + return do_value_labels (1); } int cmd_add_value_labels (void) { - int code; - lex_match_id ("ADD"); - lex_match_id ("VALUE"); - lex_match_id ("LABELS"); - code = do_value_labels (0); - done (); - return code; + return do_value_labels (0); } /* Do it. */ @@ -90,24 +55,36 @@ cmd_add_value_labels (void) static int do_value_labels (int erase) { + struct variable **vars; /* Variable list. */ + int var_cnt; /* Number of variables. */ + int parse_err=0; /* true if error parsing variables */ + lex_match ('/'); while (token != '.') { - parse_variables (default_dict, &v, &nv, PV_SAME_TYPE); - if (!verify_val_labs (erase)) - return CMD_PART_SUCCESS_MAYBE; + parse_err = !parse_variables (default_dict, &vars, &var_cnt, + PV_SAME_TYPE) ; + if (var_cnt < 1) + { + free(vars); + return CMD_FAILURE; + } + if (!verify_val_labs (vars, var_cnt)) + goto lossage; + if (erase) + erase_labels (vars, var_cnt); while (token != '/' && token != '.') - if (!get_label ()) - return CMD_PART_SUCCESS_MAYBE; + if (!get_label (vars, var_cnt)) + goto lossage; if (token != '/') break; lex_get (); - free (v); - v = NULL; + free (vars); } + free (vars); if (token != '.') { @@ -115,23 +92,23 @@ do_value_labels (int erase) return CMD_TRAILING_GARBAGE; } -#if 0 && DEBUGGING - debug_print (); -#endif - return CMD_SUCCESS; + return parse_err ? CMD_PART_SUCCESS_MAYBE : CMD_SUCCESS; + + lossage: + free (vars); + return CMD_PART_SUCCESS_MAYBE; } +/* Verifies that none of the VAR_CNT variables in VARS are long + string variables. */ static int -verify_val_labs (int erase) +verify_val_labs (struct variable **vars, int var_cnt) { int i; - if (!nv) - return 1; - - for (i = 0; i < nv; i++) + for (i = 0; i < var_cnt; i++) { - struct variable *vp = v[i]; + struct variable *vp = vars[i]; if (vp->type == ALPHA && vp->width > 8) { @@ -139,59 +116,57 @@ verify_val_labs (int erase) "string variables such as %s."), vp->name); return 0; } - - if (erase) - val_labs_clear (vp->val_labs); } return 1; } -/* Parse all the labels for a particular set of variables and add the - specified labels to those variables. */ -static int -get_label (void) +/* Erases all the labels for the VAR_CNT variables in VARS. */ +static void +erase_labels (struct variable **vars, int var_cnt) { int i; - /* Make sure there's some variables. */ - if (!nv) - { - if (token != T_STRING && token != T_NUM) - return 0; - lex_get (); - return 1; - } + /* Erase old value labels if desired. */ + for (i = 0; i < var_cnt; i++) + val_labs_clear (vars[i]->val_labs); +} +/* Parse all the labels for the VAR_CNT variables in VARS and add + the specified labels to those variables. */ +static int +get_label (struct variable **vars, int var_cnt) +{ /* Parse all the labels and add them to the variables. */ do { union value value; char *label; + int i; /* Set value. */ - if (v[0]->type == ALPHA) + if (vars[0]->type == ALPHA) { if (token != T_STRING) { - msg (SE, _("String expected for value.")); + lex_error (_("expecting string")); return 0; } - st_bare_pad_copy (value.s, ds_value (&tokstr), MAX_SHORT_STRING); + st_bare_pad_copy (value.s, ds_c_str (&tokstr), MAX_SHORT_STRING); } else { if (token != T_NUM) { - msg (SE, _("Number expected for value.")); + lex_error (_("expecting integer")); return 0; } if (!lex_integer_p ()) msg (SW, _("Value label `%g' is not integer."), tokval); value.f = tokval; } + lex_get (); /* Set label. */ - lex_get (); if (!lex_force_string ()) return 0; if (ds_length (&tokstr) > 60) @@ -199,10 +174,10 @@ get_label (void) msg (SW, _("Truncating value label to 60 characters.")); ds_truncate (&tokstr, 60); } - label = ds_value (&tokstr); + label = ds_c_str (&tokstr); - for (i = 0; i < nv; i++) - val_labs_replace (v[i]->val_labs, value, label); + for (i = 0; i < var_cnt; i++) + val_labs_replace (vars[i]->val_labs, value, label); lex_get (); } @@ -210,31 +185,3 @@ get_label (void) return 1; } - -#if 0 && DEBUGGING -static void -debug_print () -{ - int i; - - puts (_("Value labels:")); - for (i = 0; i < nvar; i++) - { - struct hsh_iterator i; - struct value_label *val; - - printf (" %s\n", var[i]->name); - if (var[i]->val_lab) - { - for (val = hsh_first (var[i]->val_lab, &i); val != NULL; - val = hsh_next (var[i]->val_lab, &i)) - if (var[i]->type == NUMERIC) - printf (" %g: `%s'\n", val->v.f, val->s); - else - printf (" `%.8s': `%s'\n", val->v.s, val->s); - } - else - printf (_(" (no value labels)\n")); - } -} -#endif /* DEBUGGING */