1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) 2000 Naba Kumar <naba@gnome.org>
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.
22 * SECTION:anjuta-serializer
23 * @short_description: Data serializer
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-serializer.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
;
45 static GObjectClass
*parent_class
= NULL
;
55 anjuta_serializer_get_type (void)
57 static GType type
= 0;
60 static const GTypeInfo our_info
= {
61 sizeof (AnjutaSerializerClass
),
64 (GClassInitFunc
)anjuta_serializer_class_init
,
67 sizeof (AnjutaSerializer
),
69 (GInstanceInitFunc
)anjuta_serializer_init
,
72 type
= g_type_register_static(G_TYPE_OBJECT
,
73 "AnjutaSerializer", &our_info
, 0);
80 anjuta_serializer_set_property (GObject
* object
,
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
);
91 self
->priv
->mode
= g_value_get_enum (value
);
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");
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));
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
116 anjuta_serializer_get_property (GObject
* object
,
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
);
128 g_value_set_enum (value
, self
->priv
->mode
);
131 g_value_set_string (value
, self
->priv
->filepath
);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
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
,
150 g_param_spec_string ("filepath",
152 "Used to store and retrieve the stream"
155 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
156 g_object_class_install_property (object_class
,
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
));
167 anjuta_serializer_init (AnjutaSerializer
*obj
)
169 obj
->priv
= g_new0 (AnjutaSerializerPrivate
, 1);
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
);
182 G_OBJECT_CLASS (parent_class
)->finalize (object
);
186 anjuta_serializer_new (const gchar
*filepath
, AnjutaSerializerMode mode
)
188 AnjutaSerializer
*obj
;
190 obj
= ANJUTA_SERIALIZER (g_object_new (ANJUTA_TYPE_SERIALIZER
,
192 "filepath", filepath
, NULL
));
193 if (obj
->priv
->stream
== NULL
)
195 g_object_unref (obj
);
202 anjuta_serializer_write_buffer (AnjutaSerializer
*serializer
,
203 const gchar
*name
, const gchar
*value
)
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
,
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)
222 if (fwrite (buffer
, length
, 1, serializer
->priv
->stream
) < 1)
227 if (fprintf (serializer
->priv
->stream
, "\n") < 0)
237 anjuta_serializer_read_buffer (AnjutaSerializer
*serializer
,
238 const gchar
*name
, gchar
**value
)
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
,
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)
253 buffer
= g_new0 (gchar
, length
+ 1);
254 if (fread (buffer
, length
, 1, serializer
->priv
->stream
) < 1)
259 if (fscanf (serializer
->priv
->stream
, "\n") < 0)
264 if (strncmp (buffer
, name
, strlen (name
)) != 0)
269 if (strncmp (buffer
+ strlen (name
), ": ", 2) != 0)
274 /* Is it dangerous to use the same buffer to shift the text up? */
275 strcpy (buffer
, buffer
+ strlen (name
) + 2);
282 anjuta_serializer_write_int (AnjutaSerializer
*serializer
,
283 const gchar
*name
, gint value
)
286 snprintf (buffer
, 64, "%d", value
);
287 return anjuta_serializer_write_buffer (serializer
, name
, buffer
);
291 anjuta_serializer_write_float (AnjutaSerializer
*serializer
,
292 const gchar
*name
, gfloat value
)
295 snprintf (buffer
, 64, "%f", value
);
296 return anjuta_serializer_write_buffer (serializer
, name
, buffer
);
300 anjuta_serializer_write_string (AnjutaSerializer
*serializer
,
301 const gchar
*name
, const gchar
*value
)
304 return anjuta_serializer_write_buffer (serializer
, name
, value
);
306 return anjuta_serializer_write_buffer (serializer
, name
, "(null)");
310 anjuta_serializer_read_int (AnjutaSerializer
*serializer
,
311 const gchar
*name
, gint
*value
)
315 g_return_val_if_fail (value
!= NULL
, FALSE
);
317 if (!anjuta_serializer_read_buffer (serializer
, name
, &buffer
))
319 *value
= atoi (buffer
);
325 anjuta_serializer_read_float (AnjutaSerializer
*serializer
,
326 const gchar
*name
, gfloat
*value
)
330 g_return_val_if_fail (value
!= NULL
, FALSE
);
332 if (!anjuta_serializer_read_buffer (serializer
, name
, &buffer
))
334 *value
= atof (buffer
);
340 anjuta_serializer_read_string (AnjutaSerializer
*serializer
,
341 const gchar
*name
, gchar
**value
,
346 g_return_val_if_fail (value
!= NULL
, FALSE
);
348 if (!anjuta_serializer_read_buffer (serializer
, name
, &buffer
))
352 if (strcmp (buffer
, "(null)") == 0)