Rework check_input so that it can work with any widget that gives a string.
[anjuta-git-plugin.git] / libanjuta / anjuta-serializer.c
blob187cb0cbc934e65218dbf81bbd3d943552685d8b
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta-serializer.c
4 * Copyright (C) 2000 Naba Kumar <naba@gnome.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:anjuta-serializer
23 * @short_description: Data serializer
24 * @see_also:
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-serializer.h
30 #include <stdio.h>
31 #include <anjuta-enum-types.h>
32 #include "anjuta-serializer.h"
34 static void anjuta_serializer_class_init(AnjutaSerializerClass *klass);
35 static void anjuta_serializer_init(AnjutaSerializer *sp);
36 static void anjuta_serializer_finalize(GObject *object);
38 struct _AnjutaSerializerPrivate {
39 AnjutaSerializerMode mode;
40 gchar *filepath;
41 FILE *stream;
44 static GObjectClass *parent_class = NULL;
46 enum
48 PROP_0,
49 PROP_FILEPATH,
50 PROP_MODE
53 GType
54 anjuta_serializer_get_type (void)
56 static GType type = 0;
58 if(type == 0) {
59 static const GTypeInfo our_info = {
60 sizeof (AnjutaSerializerClass),
61 NULL,
62 NULL,
63 (GClassInitFunc)anjuta_serializer_class_init,
64 NULL,
65 NULL,
66 sizeof (AnjutaSerializer),
68 (GInstanceInitFunc)anjuta_serializer_init,
71 type = g_type_register_static(G_TYPE_OBJECT,
72 "AnjutaSerializer", &our_info, 0);
75 return type;
78 static void
79 anjuta_serializer_set_property (GObject * object,
80 guint property_id,
81 const GValue * value, GParamSpec * pspec)
83 AnjutaSerializer *self = ANJUTA_SERIALIZER (object);
84 g_return_if_fail (value != NULL);
85 g_return_if_fail (pspec != NULL);
87 switch (property_id)
89 case PROP_MODE:
90 self->priv->mode = g_value_get_enum (value);
91 break;
92 case PROP_FILEPATH:
93 g_free (self->priv->filepath);
94 self->priv->filepath = g_value_dup_string (value);
95 if (self->priv->stream)
96 fclose (self->priv->stream);
97 if (self->priv->mode == ANJUTA_SERIALIZER_READ)
98 self->priv->stream = fopen (self->priv->filepath, "r");
99 else
100 self->priv->stream = fopen (self->priv->filepath, "w");
101 if (self->priv->stream == NULL)
104 g_warning ("Could not open %s for serialization: %s",
105 self->priv->filepath, g_strerror (errno));
108 break;
109 default:
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
114 static void
115 anjuta_serializer_get_property (GObject * object,
116 guint property_id,
117 GValue * value, GParamSpec * pspec)
119 AnjutaSerializer *self = ANJUTA_SERIALIZER (object);
121 g_return_if_fail(value != NULL);
122 g_return_if_fail(pspec != NULL);
124 switch (property_id)
126 case PROP_MODE:
127 g_value_set_enum (value, self->priv->mode);
128 break;
129 case PROP_FILEPATH:
130 g_value_set_string (value, self->priv->filepath);
131 break;
132 default:
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
137 static void
138 anjuta_serializer_class_init (AnjutaSerializerClass *klass)
140 GObjectClass *object_class = G_OBJECT_CLASS(klass);
142 parent_class = g_type_class_peek_parent(klass);
143 object_class->finalize = anjuta_serializer_finalize;
144 object_class->set_property = anjuta_serializer_set_property;
145 object_class->get_property = anjuta_serializer_get_property;
147 g_object_class_install_property (object_class,
148 PROP_FILEPATH,
149 g_param_spec_string ("filepath",
150 "File path",
151 "Used to store and retrieve the stream"
152 "translateable",
153 NULL,
154 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
155 g_object_class_install_property (object_class,
156 PROP_MODE,
157 g_param_spec_enum ("mode",
158 "Serialization mode",
159 "Used to decide read or write operation",
160 ANJUTA_TYPE_SERIALIZER_MODE,
161 ANJUTA_SERIALIZER_READ,
162 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
165 static void
166 anjuta_serializer_init (AnjutaSerializer *obj)
168 obj->priv = g_new0 (AnjutaSerializerPrivate, 1);
171 static void
172 anjuta_serializer_finalize (GObject *object)
174 AnjutaSerializer *cobj;
175 cobj = ANJUTA_SERIALIZER(object);
177 g_free (cobj->priv->filepath);
178 if (cobj->priv->stream)
179 fclose (cobj->priv->stream);
180 g_free (cobj->priv);
181 G_OBJECT_CLASS (parent_class)->finalize (object);
184 AnjutaSerializer *
185 anjuta_serializer_new (const gchar *filepath, AnjutaSerializerMode mode)
187 AnjutaSerializer *obj;
189 obj = ANJUTA_SERIALIZER (g_object_new (ANJUTA_TYPE_SERIALIZER,
190 "mode", mode,
191 "filepath", filepath, NULL));
192 if (obj->priv->stream == NULL)
194 g_object_unref (obj);
195 return NULL;
197 return obj;
200 static gboolean
201 anjuta_serializer_write_buffer (AnjutaSerializer *serializer,
202 const gchar *name, const gchar *value)
204 gint length;
205 gchar *buffer;
207 g_return_val_if_fail (ANJUTA_IS_SERIALIZER (serializer), FALSE);
208 g_return_val_if_fail (serializer->priv->stream != NULL, FALSE);
209 g_return_val_if_fail (serializer->priv->mode == ANJUTA_SERIALIZER_WRITE,
210 FALSE);
211 g_return_val_if_fail (name != NULL, FALSE);
212 g_return_val_if_fail (value != NULL, FALSE);
214 buffer = g_strconcat (name, ": ", value, NULL);
215 length = strlen (buffer);
216 if (fprintf (serializer->priv->stream, "%d\n", length) < 1)
218 g_free (buffer);
219 return FALSE;
221 if (fwrite (buffer, length, 1, serializer->priv->stream) < 1)
223 g_free (buffer);
224 return FALSE;
226 if (fprintf (serializer->priv->stream, "\n") < 0)
228 g_free (buffer);
229 return FALSE;
231 g_free (buffer);
232 return TRUE;
235 static gboolean
236 anjuta_serializer_read_buffer (AnjutaSerializer *serializer,
237 const gchar *name, gchar **value)
239 gint length;
240 gchar *buffer;
242 g_return_val_if_fail (ANJUTA_IS_SERIALIZER (serializer), FALSE);
243 g_return_val_if_fail (serializer->priv->stream != NULL, FALSE);
244 g_return_val_if_fail (serializer->priv->mode == ANJUTA_SERIALIZER_READ,
245 FALSE);
246 g_return_val_if_fail (name != NULL, FALSE);
247 g_return_val_if_fail (value != NULL, FALSE);
249 if (fscanf (serializer->priv->stream, "%d\n", &length) < 1)
250 return FALSE;
252 buffer = g_new0 (gchar, length + 1);
253 if (fread (buffer, length, 1, serializer->priv->stream) < 1)
255 g_free (buffer);
256 return FALSE;
258 if (fscanf (serializer->priv->stream, "\n") < 0)
260 g_free (buffer);
261 return FALSE;
263 if (strncmp (buffer, name, strlen (name)) != 0)
265 g_free (buffer);
266 return FALSE;
268 if (strncmp (buffer + strlen (name), ": ", 2) != 0)
270 g_free (buffer);
271 return FALSE;
273 /* Is it dangerous to use the same buffer to shift the text up? */
274 strcpy (buffer, buffer + strlen (name) + 2);
275 *value = buffer;
277 return TRUE;
280 gboolean
281 anjuta_serializer_write_int (AnjutaSerializer *serializer,
282 const gchar *name, gint value)
284 gchar buffer[64];
285 snprintf (buffer, 64, "%d", value);
286 return anjuta_serializer_write_buffer (serializer, name, buffer);
289 gboolean
290 anjuta_serializer_write_float (AnjutaSerializer *serializer,
291 const gchar *name, gfloat value)
293 gchar buffer[64];
294 snprintf (buffer, 64, "%f", value);
295 return anjuta_serializer_write_buffer (serializer, name, buffer);
298 gboolean
299 anjuta_serializer_write_string (AnjutaSerializer *serializer,
300 const gchar *name, const gchar *value)
302 if (value)
303 return anjuta_serializer_write_buffer (serializer, name, value);
304 else
305 return anjuta_serializer_write_buffer (serializer, name, "(null)");
308 gboolean
309 anjuta_serializer_read_int (AnjutaSerializer *serializer,
310 const gchar *name, gint *value)
312 gchar *buffer;
314 g_return_val_if_fail (value != NULL, FALSE);
316 if (!anjuta_serializer_read_buffer (serializer, name, &buffer))
317 return FALSE;
318 *value = atoi (buffer);
319 g_free (buffer);
320 return TRUE;
323 gboolean
324 anjuta_serializer_read_float (AnjutaSerializer *serializer,
325 const gchar *name, gfloat *value)
327 gchar *buffer;
329 g_return_val_if_fail (value != NULL, FALSE);
331 if (!anjuta_serializer_read_buffer (serializer, name, &buffer))
332 return FALSE;
333 *value = atof (buffer);
334 g_free (buffer);
335 return TRUE;
338 gboolean
339 anjuta_serializer_read_string (AnjutaSerializer *serializer,
340 const gchar *name, gchar **value,
341 gboolean replace)
343 gchar *buffer;
345 g_return_val_if_fail (value != NULL, FALSE);
347 if (!anjuta_serializer_read_buffer (serializer, name, &buffer))
348 return FALSE;
349 if (replace)
350 g_free (*value);
351 if (strcmp (buffer, "(null)") == 0)
353 g_free (buffer);
354 *value = NULL;
356 else
358 *value = buffer;
360 return TRUE;