2 * $Id: ftype-string.c,v 1.15 2003/11/25 13:20:36 sahlberg Exp $
4 * Ethereal - Network traffic analyzer
5 * By Gerald Combs <gerald@ethereal.com>
6 * Copyright 2001 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <ftypes-int.h>
31 string_fvalue_new(fvalue_t *fv)
33 fv->value.string = NULL;
37 string_fvalue_free(fvalue_t *fv)
39 if (fv->value.string) {
40 g_free(fv->value.string);
45 string_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
47 g_assert(value != NULL);
49 /* Free up the old value, if we have one */
50 string_fvalue_free(fv);
53 fv->value.string = value;
56 fv->value.string = g_strdup(value);
61 string_repr_len(fvalue_t *fv, ftrepr_t rtype)
68 return strlen(fv->value.string);
71 for (p = fv->value.string; (c = *p) != '\0'; p++) {
72 if (c == '\\' || c == '"') {
73 /* Backslashes and double-quotes
79 return repr_len + 2; /* string plus leading and trailing quotes */
81 g_assert_not_reached();
86 string_to_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
91 if (rtype == FTREPR_DFILTER) {
94 for (p = fv->value.string; (c = *p) != '\0'; p++) {
95 if (c == '\\' || c == '"') {
96 /* Backslashes and double-quotes
106 strcpy(buf, fv->value.string);
112 value_get(fvalue_t *fv)
114 return fv->value.string;
118 val_from_string(fvalue_t *fv, char *s, LogFunc logfunc _U_)
120 /* Free up the old value, if we have one */
121 string_fvalue_free(fv);
123 fv->value.string = g_strdup(s);
128 val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
132 /* Free up the old value, if we have one */
133 string_fvalue_free(fv);
135 /* Does this look like a byte-string? */
136 fv_bytes = fvalue_from_unparsed(FT_BYTES, s, TRUE, NULL);
138 /* Copy the bytes over to a string and terminate it
139 * with a NUL. XXX - what if the user embeds a NUL
140 * in the middle of the byte string? */
141 int num_bytes = fv_bytes->value.bytes->len;
143 fv->value.string = g_malloc(num_bytes + 1);
144 memcpy(fv->value.string, fv->value.bytes->data, num_bytes);
145 fv->value.string[num_bytes] = '\0';
147 FVALUE_FREE(fv_bytes);
151 /* Just turn it into a string */
152 return val_from_string(fv, s, logfunc);
154 g_assert_not_reached();
160 return strlen(fv->value.string);
164 slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
168 data = fv->value.string + offset;
170 g_byte_array_append(bytes, data, length);
175 cmp_eq(fvalue_t *a, fvalue_t *b)
177 return (strcmp(a->value.string, b->value.string) == 0);
181 cmp_ne(fvalue_t *a, fvalue_t *b)
183 return (strcmp(a->value.string, b->value.string) != 0);
187 cmp_gt(fvalue_t *a, fvalue_t *b)
189 return (strcmp(a->value.string, b->value.string) > 0);
193 cmp_ge(fvalue_t *a, fvalue_t *b)
195 return (strcmp(a->value.string, b->value.string) >= 0);
199 cmp_lt(fvalue_t *a, fvalue_t *b)
201 return (strcmp(a->value.string, b->value.string) < 0);
205 cmp_le(fvalue_t *a, fvalue_t *b)
207 return (strcmp(a->value.string, b->value.string) <= 0);
211 cmp_contains(fvalue_t *fv_a, fvalue_t *fv_b)
214 * http://www.introl.com/introl-demo/Libraries/C/ANSI_C/string/strstr.html
215 * strstr() returns a non-NULL value if needle is an empty
216 * string. We don't that behavior for cmp_contains. */
217 if (strlen(fv_b->value.string) == 0) {
221 if (strstr(fv_a->value.string, fv_b->value.string)) {
230 ftype_register_string(void)
233 static ftype_t string_type = {
234 "FT_STRING", /* name */
235 "character string", /* pretty_name */
237 string_fvalue_new, /* new_value */
238 string_fvalue_free, /* free_value */
239 val_from_unparsed, /* val_from_unparsed */
240 val_from_string, /* val_from_string */
241 string_to_repr, /* val_to_string_repr */
242 string_repr_len, /* len_string_repr */
244 string_fvalue_set, /* set_value */
245 NULL, /* set_value_integer */
246 NULL, /* set_value_floating */
248 value_get, /* get_value */
249 NULL, /* get_value_integer */
250 NULL, /* get_value_floating */
258 cmp_contains, /* cmp_contains */
263 static ftype_t stringz_type = {
269 val_from_unparsed, /* val_from_unparsed */
270 val_from_string, /* val_from_string */
271 NULL, /* val_to_string_repr */
272 NULL, /* len_string_repr */
288 cmp_contains, /* cmp_contains */
293 static ftype_t uint_string_type = {
299 val_from_unparsed, /* val_from_unparsed */
300 val_from_string, /* val_from_string */
301 NULL, /* val_to_string_repr */
302 NULL, /* len_string_repr */
318 cmp_contains, /* cmp_contains */
324 ftype_register(FT_STRING, &string_type);
325 ftype_register(FT_STRINGZ, &stringz_type);
326 ftype_register(FT_UINT_STRING, &uint_string_type);