From: Ben Pfaff Date: Tue, 16 Mar 2010 05:05:24 +0000 (-0700) Subject: value: New function value_clone(). X-Git-Tag: sav-api~345 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pspp;a=commitdiff_plain;h=4fcdb5b5c34de891adca5256e9409ac7234ab19d value: New function value_clone(). --- diff --git a/src/data/value-labels.c b/src/data/value-labels.c index 6d6b57a618..ff78b9d2b3 100644 --- a/src/data/value-labels.c +++ b/src/data/value-labels.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2009, 2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -131,8 +131,7 @@ do_add_val_lab (struct val_labs *vls, const union value *value, const char *label) { struct val_lab *lab = xmalloc (sizeof *lab); - value_init (&lab->value, vls->width); - value_copy (&lab->value, value, vls->width); + value_clone (&lab->value, value, vls->width); lab->label = intern_new (label); hmap_insert (&vls->labels, &lab->node, value_hash (value, vls->width, 0)); } diff --git a/src/data/value.h b/src/data/value.h index f9782e2d86..61df087754 100644 --- a/src/data/value.h +++ b/src/data/value.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2007, 2009 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2007, 2009, 2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -51,6 +51,7 @@ union value }; static inline void value_init (union value *, int width); +static inline void value_clone (union value *, const union value *, int width); static inline bool value_needs_init (int width); static inline bool value_try_init (union value *, int width); static inline void value_destroy (union value *, int width); @@ -98,6 +99,17 @@ value_init (union value *v, int width) v->long_string = xmalloc (width); } +/* Initializes V as a value of the given WIDTH, as with value_init(), and + copies SRC's value into V as its initial value. */ +static inline void +value_clone (union value *v, const union value *src, int width) +{ + if (width <= MAX_SHORT_STRING) + *v = *src; + else + v->long_string = xmemdup (src->long_string, width); +} + /* Returns true if a value of the given WIDTH actually needs to have the value_init and value_destroy functions called, false if those functions are no-ops for values of the given WIDTH. diff --git a/src/language/stats/crosstabs.q b/src/language/stats/crosstabs.q index 5dc7069af5..b137e28496 100644 --- a/src/language/stats/crosstabs.q +++ b/src/language/stats/crosstabs.q @@ -654,9 +654,7 @@ tabulate_general_case (struct pivot_table *pt, const struct ccase *c, for (j = 0; j < pt->n_vars; j++) { const struct variable *var = pt->vars[j]; - int width = var_get_width (var); - value_init (&te->values[j], width); - value_copy (&te->values[j], case_data (c, var), width); + value_clone (&te->values[j], case_data (c, var), var_get_width (var)); } hmap_insert (&pt->data, &te->node, hash); }