libanjuta: Fix anjuta_token functions
[anjuta.git] / libanjuta / anjuta-serializer.c
blob36fe6e5b3569fee2c8b844acb9d157d210925779
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 <stdlib.h>
32 #include <anjuta-enum-types.h>
33 #include "anjuta-serializer.h"
35 static void anjuta_serializer_class_init(AnjutaSerializerClass *klass);
36 static void anjuta_serializer_init(AnjutaSerializer *sp);
37 static void anjuta_serializer_finalize(GObject *object);
39 struct _AnjutaSerializerPrivate {
40 AnjutaSerializerMode mode;
41 gchar *filepath;
42 FILE *stream;
45 static GObjectClass *parent_class = NULL;
47 enum
49 PROP_0,
50 PROP_FILEPATH,
51 PROP_MODE
54 GType
55 anjuta_serializer_get_type (void)
57 static GType type = 0;
59 if(type == 0) {
60 static const GTypeInfo our_info = {
61 sizeof (AnjutaSerializerClass),
62 NULL,
63 NULL,
64 (GClassInitFunc)anjuta_serializer_class_init,
65 NULL,
66 NULL,
67 sizeof (AnjutaSerializer),
69 (GInstanceInitFunc)anjuta_serializer_init,
72 type = g_type_register_static(G_TYPE_OBJECT,
73 "AnjutaSerializer", &our_info, 0);
76 return type;
79 static void
80 anjuta_serializer_set_property (GObject * object,
81 guint property_id,
82 const GValue * value, GParamSpec * pspec)
84 AnjutaSerializer *self = ANJUTA_SERIALIZER (object);
85 g_return_if_fail (value != NULL);
86 g_return_if_fail (pspec != NULL);
88 switch (property_id)
90 case PROP_MODE:
91 self->priv->mode = g_value_get_enum (value);
92 break;
93 case PROP_FILEPATH:
94 g_free (self->priv->filepath);
95 self->priv->filepath = g_value_dup_string (value);
96 if (self->priv->stream)
97 fclose (self->priv->stream);
98 if (self->priv->mode == ANJUTA_SERIALIZER_READ)
99 self->priv->stream = fopen (self->priv->filepath, "r");
100 else
101 self->priv->stream = fopen (self->priv->filepath, "w");
102 if (self->priv->stream == NULL)
105 g_warning ("Could not open %s for serialization: %s",
106 self->priv->filepath, g_strerror (errno));
109 break;
110 default:
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
115 static void
116 anjuta_serializer_get_property (GObject * object,
117 guint property_id,
118 GValue * value, GParamSpec * pspec)
120 AnjutaSerializer *self = ANJUTA_SERIALIZER (object);
122 g_return_if_fail(value != NULL);
123 g_return_if_fail(pspec != NULL);
125 switch (property_id)
127 case PROP_MODE:
128 g_value_set_enum (value, self->priv->mode);
129 break;
130 case PROP_FILEPATH:
131 g_value_set_string (value, self->priv->filepath);
132 break;
133 default:
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
138 static void
139 anjuta_serializer_class_init (AnjutaSerializerClass *klass)
141 GObjectClass *object_class = G_OBJECT_CLASS(klass);
143 parent_class = g_type_class_peek_parent(klass);
144 object_class->finalize = anjuta_serializer_finalize;
145 object_class->set_property = anjuta_serializer_set_property;
146 object_class->get_property = anjuta_serializer_get_property;
148 g_object_class_install_property (object_class,
149 PROP_FILEPATH,
150 g_param_spec_string ("filepath",
151 "File path",
152 "Used to store and retrieve the stream"
153 "translateable",
154 NULL,
155 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
156 g_object_class_install_property (object_class,
157 PROP_MODE,
158 g_param_spec_enum ("mode",
159 "Serialization mode",
160 "Used to decide read or write operation",
161 ANJUTA_TYPE_SERIALIZER_MODE,
162 ANJUTA_SERIALIZER_READ,
163 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
166 static void
167 anjuta_serializer_init (AnjutaSerializer *obj)
169 obj->priv = g_new0 (AnjutaSerializerPrivate, 1);
172 static void
173 anjuta_serializer_finalize (GObject *object)
175 AnjutaSerializer *cobj;
176 cobj = ANJUTA_SERIALIZER(object);
178 g_free (cobj->priv->filepath);
179 if (cobj->priv->stream)
180 fclose (cobj->priv->stream);
181 g_free (cobj->priv);
182 G_OBJECT_CLASS (parent_class)->finalize (object);
185 AnjutaSerializer *
186 anjuta_serializer_new (const gchar *filepath, AnjutaSerializerMode mode)
188 AnjutaSerializer *obj;
190 obj = ANJUTA_SERIALIZER (g_object_new (ANJUTA_TYPE_SERIALIZER,
191 "mode", mode,
192 "filepath", filepath, NULL));
193 if (obj->priv->stream == NULL)
195 g_object_unref (obj);
196 return NULL;
198 return obj;
201 static gboolean
202 anjuta_serializer_write_buffer (AnjutaSerializer *serializer,
203 const gchar *name, const gchar *value)
205 gint length;
206 gchar *buffer;
208 g_return_val_if_fail (ANJUTA_IS_SERIALIZER (serializer), FALSE);
209 g_return_val_if_fail (serializer->priv->stream != NULL, FALSE);
210 g_return_val_if_fail (serializer->priv->mode == ANJUTA_SERIALIZER_WRITE,
211 FALSE);
212 g_return_val_if_fail (name != NULL, FALSE);
213 g_return_val_if_fail (value != NULL, FALSE);
215 buffer = g_strconcat (name, ": ", value, NULL);
216 length = strlen (buffer);
217 if (fprintf (serializer->priv->stream, "%d\n", length) < 1)
219 g_free (buffer);
220 return FALSE;
222 if (fwrite (buffer, length, 1, serializer->priv->stream) < 1)
224 g_free (buffer);
225 return FALSE;
227 if (fprintf (serializer->priv->stream, "\n") < 0)
229 g_free (buffer);
230 return FALSE;
232 g_free (buffer);
233 return TRUE;
236 static gboolean
237 anjuta_serializer_read_buffer (AnjutaSerializer *serializer,
238 const gchar *name, gchar **value)
240 gint length;
241 gchar *buffer;
243 g_return_val_if_fail (ANJUTA_IS_SERIALIZER (serializer), FALSE);
244 g_return_val_if_fail (serializer->priv->stream != NULL, FALSE);
245 g_return_val_if_fail (serializer->priv->mode == ANJUTA_SERIALIZER_READ,
246 FALSE);
247 g_return_val_if_fail (name != NULL, FALSE);
248 g_return_val_if_fail (value != NULL, FALSE);
250 if (fscanf (serializer->priv->stream, "%d\n", &length) < 1)
251 return FALSE;
253 buffer = g_new0 (gchar, length + 1);
254 if (fread (buffer, length, 1, serializer->priv->stream) < 1)
256 g_free (buffer);
257 return FALSE;
259 if (fscanf (serializer->priv->stream, "\n") < 0)
261 g_free (buffer);
262 return FALSE;
264 if (strncmp (buffer, name, strlen (name)) != 0)
266 g_free (buffer);
267 return FALSE;
269 if (strncmp (buffer + strlen (name), ": ", 2) != 0)
271 g_free (buffer);
272 return FALSE;
274 /* Is it dangerous to use the same buffer to shift the text up? */
275 strcpy (buffer, buffer + strlen (name) + 2);
276 *value = buffer;
278 return TRUE;
281 gboolean
282 anjuta_serializer_write_int (AnjutaSerializer *serializer,
283 const gchar *name, gint value)
285 gchar buffer[64];
286 snprintf (buffer, 64, "%d", value);
287 return anjuta_serializer_write_buffer (serializer, name, buffer);
290 gboolean
291 anjuta_serializer_write_float (AnjutaSerializer *serializer,
292 const gchar *name, gfloat value)
294 gchar buffer[64];
295 snprintf (buffer, 64, "%f", value);
296 return anjuta_serializer_write_buffer (serializer, name, buffer);
299 gboolean
300 anjuta_serializer_write_string (AnjutaSerializer *serializer,
301 const gchar *name, const gchar *value)
303 if (value)
304 return anjuta_serializer_write_buffer (serializer, name, value);
305 else
306 return anjuta_serializer_write_buffer (serializer, name, "(null)");
309 gboolean
310 anjuta_serializer_read_int (AnjutaSerializer *serializer,
311 const gchar *name, gint *value)
313 gchar *buffer;
315 g_return_val_if_fail (value != NULL, FALSE);
317 if (!anjuta_serializer_read_buffer (serializer, name, &buffer))
318 return FALSE;
319 *value = atoi (buffer);
320 g_free (buffer);
321 return TRUE;
324 gboolean
325 anjuta_serializer_read_float (AnjutaSerializer *serializer,
326 const gchar *name, gfloat *value)
328 gchar *buffer;
330 g_return_val_if_fail (value != NULL, FALSE);
332 if (!anjuta_serializer_read_buffer (serializer, name, &buffer))
333 return FALSE;
334 *value = atof (buffer);
335 g_free (buffer);
336 return TRUE;
339 gboolean
340 anjuta_serializer_read_string (AnjutaSerializer *serializer,
341 const gchar *name, gchar **value,
342 gboolean replace)
344 gchar *buffer;
346 g_return_val_if_fail (value != NULL, FALSE);
348 if (!anjuta_serializer_read_buffer (serializer, name, &buffer))
349 return FALSE;
350 if (replace)
351 g_free (*value);
352 if (strcmp (buffer, "(null)") == 0)
354 g_free (buffer);
355 *value = NULL;
357 else
359 *value = buffer;
361 return TRUE;