1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
18 #define DATA_VALUE_H 1
26 #define MAX_SHORT_STRING 8
28 /* A numeric or string value.
30 The client is responsible for keeping track of the value's
33 This structure is semi-opaque:
35 - If the value is a number, clients may access the 'f'
38 - Clients should not access other members directly.
43 char short_string[MAX_SHORT_STRING];
47 static inline void value_init (union value *, int width);
48 static inline bool value_needs_init (int width);
49 static inline bool value_try_init (union value *, int width);
50 static inline void value_destroy (union value *, int width);
52 static inline double value_num (const union value *);
53 static inline const char *value_str (const union value *, int width);
54 static inline char *value_str_rw (union value *, int width);
56 int compare_values (const void *, const void *, const void *var);
57 unsigned hash_value (const void *, const void *var);
59 static inline void value_copy (union value *, const union value *, int width);
60 void value_copy_rpad (union value *, int dst_width,
61 const union value *, int src_width,
63 void value_copy_str_rpad (union value *, int dst_width, const char *,
65 void value_copy_buf_rpad (union value *dst, int dst_width,
66 const char *src, size_t src_len, char pad);
67 void value_set_missing (union value *, int width);
68 int value_compare_3way (const union value *, const union value *, int width);
69 bool value_equal (const union value *, const union value *, int width);
70 size_t value_hash (const union value *, int width, unsigned int basis);
72 bool value_is_resizable (const union value *, int old_width, int new_width);
73 bool value_needs_resize (int old_width, int new_width);
74 void value_resize (union value *, int old_width, int new_width);
77 void value_init_pool (struct pool *, union value *, int width);
78 void value_resize_pool (struct pool *, union value *,
79 int old_width, int new_width);
81 /* Initializes V as a value of the given WIDTH, where 0
82 represents a numeric value and a positive integer represents a
83 string value WIDTH bytes long.
85 A WIDTH of -1 is ignored.
87 The contents of value V are indeterminate after
90 value_init (union value *v, int width)
92 if (width > MAX_SHORT_STRING)
93 v->long_string = xmalloc (width);
96 /* Returns true if a value of the given WIDTH actually needs to
97 have the value_init and value_destroy functions called, false
98 if those functions are no-ops for values of the given WIDTH.
100 Using this function is only a valuable optimization if a large
101 number of values of the given WIDTH are to be initialized*/
103 value_needs_init (int width)
105 return width > MAX_SHORT_STRING;
108 /* Same as value_init, except that failure to allocate memory
109 causes it to return false instead of terminating the
110 program. On success, returns true. */
112 value_try_init (union value *v, int width)
114 if (width > MAX_SHORT_STRING)
116 v->long_string = malloc (width);
117 return v->long_string != NULL;
123 /* Frees any memory allocated by value_init for V, which must
124 have the given WIDTH. */
126 value_destroy (union value *v, int width)
128 if (width > MAX_SHORT_STRING)
129 free (v->long_string);
132 /* Returns the numeric value in V, which must have width 0. */
134 value_num (const union value *v)
139 /* Returns the string value in V, which must have width WIDTH.
141 The returned value is not null-terminated.
143 It is important that WIDTH be the actual value that was passed
144 to value_init. Passing, e.g., a smaller value because only
145 that number of bytes will be accessed will not always work. */
146 static inline const char *
147 value_str (const union value *v, int width)
150 return (width > MAX_SHORT_STRING ? v->long_string : v->short_string);
153 /* Returns the string value in V, which must have width WIDTH.
155 The returned value is not null-terminated.
157 It is important that WIDTH be the actual value that was passed
158 to value_init. Passing, e.g., a smaller value because only
159 that number of bytes will be accessed will not always work. */
161 value_str_rw (union value *v, int width)
164 return (width > MAX_SHORT_STRING ? v->long_string : v->short_string);
167 /* Copies SRC to DST, given that they both contain data of the
170 value_copy (union value *dst, const union value *src, int width)
172 if (width <= MAX_SHORT_STRING)
175 memcpy (dst->long_string, src->long_string, width);
178 #endif /* data/value.h */