1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010 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
27 /* Maximum length of a "short" string, that is represented in
28 "union value" without a separate pointer.
30 This is an implementation detail of the "union value" code.
31 There is little reason for client code to use it. */
32 #define MAX_SHORT_STRING 8
34 /* A numeric or string value.
36 The client is responsible for keeping track of the value's
39 This structure is semi-opaque:
41 - If the value is a number, clients may access the 'f'
44 - Clients should not access other members directly.
49 uint8_t short_string[MAX_SHORT_STRING];
53 static inline void value_init (union value *, int width);
54 static inline void value_clone (union value *, const union value *, int width);
55 static inline bool value_needs_init (int width);
56 static inline bool value_try_init (union value *, int width);
57 static inline void value_destroy (union value *, int width);
59 static inline double value_num (const union value *);
60 static inline const uint8_t *value_str (const union value *, int width);
61 static inline uint8_t *value_str_rw (union value *, int width);
63 static inline void value_copy (union value *, const union value *, int width);
64 void value_copy_rpad (union value *, int dst_width,
65 const union value *, int src_width,
67 void value_copy_str_rpad (union value *, int dst_width, const uint8_t *,
69 void value_copy_buf_rpad (union value *dst, int dst_width,
70 const uint8_t *src, size_t src_len, char pad);
71 void value_set_missing (union value *, int width);
72 int value_compare_3way (const union value *, const union value *, int width);
73 bool value_equal (const union value *, const union value *, int width);
74 unsigned int value_hash (const union value *, int width, unsigned int basis);
76 bool value_is_resizable (const union value *, int old_width, int new_width);
77 bool value_needs_resize (int old_width, int new_width);
78 void value_resize (union value *, int old_width, int new_width);
80 static inline void value_swap (union value *, union value *);
83 void value_init_pool (struct pool *, union value *, int width);
84 void value_clone_pool (struct pool *, union value *, const union value *,
86 void value_resize_pool (struct pool *, union value *,
87 int old_width, int new_width);
89 /* Initializes V as a value of the given WIDTH, where 0
90 represents a numeric value and a positive integer represents a
91 string value WIDTH bytes long.
93 A WIDTH of -1 is ignored.
95 The contents of value V are indeterminate after
98 value_init (union value *v, int width)
100 if (width > MAX_SHORT_STRING)
101 v->long_string = xmalloc (width);
104 /* Initializes V as a value of the given WIDTH, as with value_init(), and
105 copies SRC's value into V as its initial value. */
107 value_clone (union value *v, const union value *src, int width)
109 if (width <= MAX_SHORT_STRING)
112 v->long_string = xmemdup (src->long_string, width);
115 /* Returns true if a value of the given WIDTH actually needs to
116 have the value_init and value_destroy functions called, false
117 if those functions are no-ops for values of the given WIDTH.
119 Using this function is only a valuable optimization if a large
120 number of values of the given WIDTH are to be initialized*/
122 value_needs_init (int width)
124 return width > MAX_SHORT_STRING;
127 /* Same as value_init, except that failure to allocate memory
128 causes it to return false instead of terminating the
129 program. On success, returns true. */
131 value_try_init (union value *v, int width)
133 if (width > MAX_SHORT_STRING)
135 v->long_string = malloc (width);
136 return v->long_string != NULL;
142 /* Frees any memory allocated by value_init for V, which must
143 have the given WIDTH. */
145 value_destroy (union value *v, int width)
147 if (width > MAX_SHORT_STRING)
148 free (v->long_string);
151 /* Returns the numeric value in V, which must have width 0. */
153 value_num (const union value *v)
158 /* Returns the string value in V, which must have width WIDTH.
160 The returned value is not null-terminated.
162 It is important that WIDTH be the actual value that was passed
163 to value_init. Passing, e.g., a smaller value because only
164 that number of bytes will be accessed will not always work. */
165 static inline const uint8_t *
166 value_str (const union value *v, int width)
169 return (width > MAX_SHORT_STRING ? v->long_string : v->short_string);
172 /* Returns the string value in V, which must have width WIDTH.
174 The returned value is not null-terminated.
176 It is important that WIDTH be the actual value that was passed
177 to value_init. Passing, e.g., a smaller value because only
178 that number of bytes will be accessed will not always work. */
179 static inline uint8_t *
180 value_str_rw (union value *v, int width)
183 return (width > MAX_SHORT_STRING ? v->long_string : v->short_string);
186 /* Copies SRC to DST, given that they both contain data of the
189 value_copy (union value *dst, const union value *src, int width)
191 if (width <= MAX_SHORT_STRING)
194 memcpy (dst->long_string, src->long_string, width);
197 /* Exchanges the contents of A and B. */
199 value_swap (union value *a, union value *b)
201 union value tmp = *a;
206 #endif /* data/value.h */