4d5177460858926006ac7b5dd1d47ebb4bb0e676
[pspp] / src / data / value.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2012 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 #ifndef DATA_VALUE_H
18 #define DATA_VALUE_H 1
19
20 #include "libpspp/compiler.h"
21 #include <assert.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include "xalloc.h"
27 \f
28 /* A numeric or string value.  The client is responsible for keeping track of
29    the value's width. */
30 union value
31   {
32     double f;
33     uint8_t *s;
34   };
35
36 static inline void value_init (union value *, int width);
37 static inline void value_clone (union value *, const union value *, int width);
38 static inline bool value_needs_init (int width);
39 static inline bool value_try_init (union value *, int width);
40 static inline void value_destroy (union value *, int width);
41
42 static inline double value_num (const union value *);
43 static inline const uint8_t *value_str (const union value *, int width);
44 static inline uint8_t *value_str_rw (union value *, int width);
45
46 static inline void value_copy (union value *, const union value *, int width);
47 void value_copy_rpad (union value *, int dst_width,
48                       const union value *, int src_width,
49                       char pad);
50 void value_copy_str_rpad (union value *, int dst_width, const uint8_t *,
51                           char pad);
52 void value_copy_buf_rpad (union value *dst, int dst_width,
53                           const uint8_t *src, size_t src_len, char pad);
54 void value_set_missing (union value *, int width);
55 int value_compare_3way (const union value *, const union value *, int width);
56 bool value_equal (const union value *, const union value *, int width);
57 unsigned int value_hash (const union value *, int width, unsigned int basis) WARN_UNUSED_RESULT;
58
59 bool value_is_resizable (const union value *, int old_width, int new_width);
60 bool value_needs_resize (int old_width, int new_width);
61 void value_resize (union value *, int old_width, int new_width);
62
63 bool value_is_spaces (const union value *, int width);
64
65 static inline void value_swap (union value *, union value *);
66
67 struct pool;
68 void value_init_pool (struct pool *, union value *, int width);
69 void value_clone_pool (struct pool *, union value *, const union value *,
70                        int width);
71 void value_resize_pool (struct pool *, union value *,
72                         int old_width, int new_width);
73 \f
74 /* Initializes V as a value of the given WIDTH, where 0
75    represents a numeric value and a positive integer represents a
76    string value WIDTH bytes long.
77
78    A WIDTH of -1 is ignored.
79
80    The contents of value V are indeterminate after
81    initialization. */
82 static inline void
83 value_init (union value *v, int width)
84 {
85   if (width > 0)
86     v->s = xmalloc (width);
87 }
88
89 /* Initializes V as a value of the given WIDTH, as with value_init(), and
90    copies SRC's value into V as its initial value. */
91 static inline void
92 value_clone (union value *v, const union value *src, int width)
93 {
94   if (width <= 0)
95     v->f = src->f;
96   else
97     v->s = xmemdup (src->s, width);
98 }
99
100 /* Returns true if a value of the given WIDTH actually needs to
101    have the value_init and value_destroy functions called, false
102    if those functions are no-ops for values of the given WIDTH.
103
104    Using this function is only a valuable optimization if a large
105    number of values of the given WIDTH are to be initialized*/
106 static inline bool
107 value_needs_init (int width)
108 {
109   return width > 0;
110 }
111
112 /* Same as value_init, except that failure to allocate memory
113    causes it to return false instead of terminating the
114    program.  On success, returns true. */
115 static inline bool
116 value_try_init (union value *v, int width)
117 {
118   if (width > 0)
119     {
120       v->s = malloc (width);
121       return v->s != NULL;
122     }
123   else
124     return true;
125 }
126
127 /* Frees any memory allocated by value_init for V, which must
128    have the given WIDTH. */
129 static inline void
130 value_destroy (union value *v, int width)
131 {
132   if (width > 0)
133     free (v->s);
134 }
135
136 /* Returns the numeric value in V, which must have width 0. */
137 static inline double
138 value_num (const union value *v)
139 {
140   return v->f;
141 }
142
143 /* Returns the string value in V, which must have width WIDTH.
144
145    The returned value is not null-terminated.
146
147    It is important that WIDTH be the actual value that was passed
148    to value_init.  Passing, e.g., a smaller value because only
149    that number of bytes will be accessed will not always work. */
150 static inline const uint8_t *
151 value_str (const union value *v, int width)
152 {
153   assert (width > 0);
154   return v->s;
155 }
156
157 /* Returns the string value in V, which must have width WIDTH.
158
159    The returned value is not null-terminated.
160
161    It is important that WIDTH be the actual value that was passed
162    to value_init.  Passing, e.g., a smaller value because only
163    that number of bytes will be accessed will not always work. */
164 static inline uint8_t *
165 value_str_rw (union value *v, int width)
166 {
167   assert (width > 0);
168   return v->s;
169 }
170
171 /* Copies SRC to DST, given that they both contain data of the
172    given WIDTH. */
173 static inline void
174 value_copy (union value *dst, const union value *src, int width)
175 {
176   if (width <= 0)
177     dst->f = src->f;
178   else
179     memcpy (dst->s, src->s, width);
180 }
181
182 /* Exchanges the contents of A and B. */
183 static inline void
184 value_swap (union value *a, union value *b)
185 {
186   union value tmp = *a;
187   *a = *b;
188   *b = tmp;
189 }
190
191 #endif /* data/value.h */