180f1b6d1e064d8195fda7f033bf72ecb3cccfd6
[pspp-builds.git] / src / data / value.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <data/value.h>
19
20 #include <data/val-type.h>
21 #include <libpspp/hash.h>
22 #include <libpspp/str.h>
23 #include "variable.h"
24
25 #include "xalloc.h"
26
27 /* Duplicate a value.
28    The caller is responsible for freeing the returned value. */
29 union value *
30 value_dup (const union value *val, int width)
31 {
32   return xmemdup (val, MAX (width, sizeof *val));
33 }
34
35
36 /* Create a value of specified width.
37    The caller is responsible for freeing the returned value. */
38 union value *
39 value_create (int width)
40 {
41   return xnmalloc (value_cnt_from_width (width), sizeof (union value));
42 }
43
44
45 /* Compares A and B, which both have the given WIDTH, and returns
46    a strcmp()-type result.
47    Only the short string portion of longer strings are
48    compared. */
49 int
50 compare_values_short (const void *a_, const void *b_, const void *var_) 
51 {
52   const union value *a = a_;
53   const union value *b = b_;
54   const struct variable *var = var_;
55   int width = var_get_width (var);
56   return value_compare_3way (a, b, MIN (width, MAX_SHORT_STRING));
57 }
58
59
60 /* Create a hash of V, which has the given WIDTH.
61    Only the short string portion of a longer string is hashed. */
62 unsigned
63 hash_value_short (const void *v_, const void *var_)
64 {
65   const union value *v = v_;
66   const struct variable *var = var_;
67   int width = var_get_width (var);
68   return width == 0 ? hash_double (v->f, 0) : hash_bytes (v->s, width, 0);
69 }
70
71
72 /* Copies SRC to DST, given that they both contain data of the
73    given WIDTH. */
74 void
75 value_copy (union value *dst, const union value *src, int width)
76 {
77   if (width == 0)
78     dst->f = src->f;
79   else
80     memcpy (dst->s, src->s, width);
81 }
82
83 /* Sets V to the system-missing value for data of the given
84    WIDTH. */
85 void
86 value_set_missing (union value *v, int width)
87 {
88   if (width == 0)
89     v->f = SYSMIS;
90   else
91     memset (v->s, ' ', width);
92 }
93
94 /* Tests whether VALUE may be resized from OLD_WIDTH to
95    NEW_WIDTH, using the following rules that match those for
96    resizing missing values and value labels.  First, OLD_WIDTH
97    and NEW_WIDTH must be both numeric or both string.  Second, if
98    NEW_WIDTH is less than OLD_WIDTH, then the bytes that would be
99    trimmed off the right end of VALUE must be all spaces. */
100 bool
101 value_is_resizable (const union value *value, int old_width, int new_width)
102 {
103   int i;
104
105   if (val_type_from_width (old_width) != val_type_from_width (new_width))
106     return false;
107   for (i = new_width; i < old_width; i++)
108     if (value->s[i] != ' ')
109       return false;
110   return true;
111 }
112
113 /* Resizes VALUE from OLD_WIDTH to NEW_WIDTH.  The arguments must
114    satisfy the rules specified above for value_is_resizable. */
115 void
116 value_resize (union value *value, int old_width, int new_width)
117 {
118   assert (value_is_resizable (value, old_width, new_width));
119   if (new_width > old_width)
120     memset (&value->s[old_width], ' ', new_width - old_width);
121 }
122
123 /* Compares A and B, which both have the given WIDTH, and returns
124    a strcmp()-type result. */
125 int
126 value_compare_3way (const union value *a, const union value *b, int width)
127 {
128   return (width == 0
129           ? (a->f < b->f ? -1 : a->f > b->f)
130           : memcmp (a->s, b->s, width));
131 }