Removed trailing whitespaces from .h and .c files using the
[metze/wireshark/wip.git] / epan / ftypes / ftype-string.c
1 /*
2  * $Id: ftype-string.c,v 1.7 2002/08/28 20:41:00 jmayer Exp $
3  *
4  * Ethereal - Network traffic analyzer
5  * By Gerald Combs <gerald@ethereal.com>
6  * Copyright 2001 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <ftypes-int.h>
28 #include <string.h>
29
30 static void
31 string_fvalue_new(fvalue_t *fv)
32 {
33         fv->value.string = NULL;
34 }
35
36 static void
37 string_fvalue_free(fvalue_t *fv)
38 {
39         if (fv->value.string) {
40                 g_free(fv->value.string);
41         }
42 }
43
44 static void
45 string_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
46 {
47         g_assert(value != NULL);
48         if (already_copied) {
49                 fv->value.string = value;
50         }
51         else {
52                 fv->value.string = g_strdup(value);
53         }
54 }
55
56 static gpointer
57 value_get(fvalue_t *fv)
58 {
59         return fv->value.string;
60 }
61
62 static gboolean
63 val_from_string(fvalue_t *fv, char *s, LogFunc logfunc _U_)
64 {
65         fv->value.string = g_strdup(s);
66         return TRUE;
67 }
68
69 static guint
70 len(fvalue_t *fv)
71 {
72         return strlen(fv->value.string);
73 }
74
75 static void
76 slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
77 {
78         guint8* data;
79
80         data = fv->value.string + offset;
81
82         g_byte_array_append(bytes, data, length);
83 }
84
85
86 static gboolean
87 cmp_eq(fvalue_t *a, fvalue_t *b)
88 {
89         return (strcmp(a->value.string, b->value.string) == 0);
90 }
91
92 static gboolean
93 cmp_ne(fvalue_t *a, fvalue_t *b)
94 {
95         return (strcmp(a->value.string, b->value.string) != 0);
96 }
97
98 static gboolean
99 cmp_gt(fvalue_t *a, fvalue_t *b)
100 {
101         return (strcmp(a->value.string, b->value.string) > 0);
102 }
103
104 static gboolean
105 cmp_ge(fvalue_t *a, fvalue_t *b)
106 {
107         return (strcmp(a->value.string, b->value.string) >= 0);
108 }
109
110 static gboolean
111 cmp_lt(fvalue_t *a, fvalue_t *b)
112 {
113         return (strcmp(a->value.string, b->value.string) < 0);
114 }
115
116 static gboolean
117 cmp_le(fvalue_t *a, fvalue_t *b)
118 {
119         return (strcmp(a->value.string, b->value.string) <= 0);
120 }
121
122 void
123 ftype_register_string(void)
124 {
125
126         static ftype_t string_type = {
127                 "FT_STRING",
128                 "character string",
129                 0,
130                 string_fvalue_new,
131                 string_fvalue_free,
132                 val_from_string,
133
134                 string_fvalue_set,
135                 NULL,
136                 NULL,
137
138                 value_get,
139                 NULL,
140                 NULL,
141
142                 cmp_eq,
143                 cmp_ne,
144                 cmp_gt,
145                 cmp_ge,
146                 cmp_lt,
147                 cmp_le,
148
149                 len,
150                 slice,
151         };
152         static ftype_t stringz_type = {
153                 "FT_STRINGZ",
154                 "character string",
155                 0,
156                 string_fvalue_new,
157                 string_fvalue_free,
158                 val_from_string,
159
160                 string_fvalue_set,
161                 NULL,
162                 NULL,
163
164                 value_get,
165                 NULL,
166                 NULL,
167
168                 cmp_eq,
169                 cmp_ne,
170                 cmp_gt,
171                 cmp_ge,
172                 cmp_lt,
173                 cmp_le,
174
175                 len,
176                 slice,
177         };
178         static ftype_t uint_string_type = {
179                 "FT_UINT_STRING",
180                 "character string",
181                 0,
182                 string_fvalue_new,
183                 string_fvalue_free,
184                 val_from_string,
185
186                 string_fvalue_set,
187                 NULL,
188                 NULL,
189
190                 value_get,
191                 NULL,
192                 NULL,
193
194                 cmp_eq,
195                 cmp_ne,
196                 cmp_gt,
197                 cmp_ge,
198                 cmp_lt,
199                 cmp_le,
200
201                 len,
202                 slice,
203         };
204
205         ftype_register(FT_STRING, &string_type);
206         ftype_register(FT_STRINGZ, &stringz_type);
207         ftype_register(FT_UINT_STRING, &uint_string_type);
208 }