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
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
;
44 static GObjectClass
*parent_class
= NULL
;
54 anjuta_serializer_get_type (void)
56 static GType type
= 0;
59 static const GTypeInfo our_info
= {
60 sizeof (AnjutaSerializerClass
),
63 (GClassInitFunc
)anjuta_serializer_class_init
,
66 sizeof (AnjutaSerializer
),
68 (GInstanceInitFunc
)anjuta_serializer_init
,
71 type
= g_type_register_static(G_TYPE_OBJECT
,
72 "AnjutaSerializer", &our_info
, 0);
79 anjuta_serializer_set_property (GObject
* object
,
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
);
90 self
->priv
->mode
= g_value_get_enum (value
);
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");
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));
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
115 anjuta_serializer_get_property (GObject
* object
,
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
);
127 g_value_set_enum (value
, self
->priv
->mode
);
130 g_value_set_string (value
, self
->priv
->filepath
);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
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
,
149 g_param_spec_string ("filepath",
151 "Used to store and retrieve the stream"
154 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
155 g_object_class_install_property (object_class
,
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
));
166 anjuta_serializer_init (AnjutaSerializer
*obj
)
168 obj
->priv
= g_new0 (AnjutaSerializerPrivate
, 1);
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
);
181 G_OBJECT_CLASS (parent_class
)->finalize (object
);
185 anjuta_serializer_new (const gchar
*filepath
, AnjutaSerializerMode mode
)
187 AnjutaSerializer
*obj
;
189 obj
= ANJUTA_SERIALIZER (g_object_new (ANJUTA_TYPE_SERIALIZER
,
191 "filepath", filepath
, NULL
));
192 if (obj
->priv
->stream
== NULL
)
194 g_object_unref (obj
);
201 anjuta_serializer_write_buffer (AnjutaSerializer
*serializer
,
202 const gchar
*name
, const gchar
*value
)
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
,
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)
221 if (fwrite (buffer
, length
, 1, serializer
->priv
->stream
) < 1)
226 if (fprintf (serializer
->priv
->stream
, "\n") < 0)
236 anjuta_serializer_read_buffer (AnjutaSerializer
*serializer
,
237 const gchar
*name
, gchar
**value
)
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
,
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)
252 buffer
= g_new0 (gchar
, length
+ 1);
253 if (fread (buffer
, length
, 1, serializer
->priv
->stream
) < 1)
258 if (fscanf (serializer
->priv
->stream
, "\n") < 0)
263 if (strncmp (buffer
, name
, strlen (name
)) != 0)
268 if (strncmp (buffer
+ strlen (name
), ": ", 2) != 0)
273 /* Is it dangerous to use the same buffer to shift the text up? */
274 strcpy (buffer
, buffer
+ strlen (name
) + 2);
281 anjuta_serializer_write_int (AnjutaSerializer
*serializer
,
282 const gchar
*name
, gint value
)
285 snprintf (buffer
, 64, "%d", value
);
286 return anjuta_serializer_write_buffer (serializer
, name
, buffer
);
290 anjuta_serializer_write_float (AnjutaSerializer
*serializer
,
291 const gchar
*name
, gfloat value
)
294 snprintf (buffer
, 64, "%f", value
);
295 return anjuta_serializer_write_buffer (serializer
, name
, buffer
);
299 anjuta_serializer_write_string (AnjutaSerializer
*serializer
,
300 const gchar
*name
, const gchar
*value
)
303 return anjuta_serializer_write_buffer (serializer
, name
, value
);
305 return anjuta_serializer_write_buffer (serializer
, name
, "(null)");
309 anjuta_serializer_read_int (AnjutaSerializer
*serializer
,
310 const gchar
*name
, gint
*value
)
314 g_return_val_if_fail (value
!= NULL
, FALSE
);
316 if (!anjuta_serializer_read_buffer (serializer
, name
, &buffer
))
318 *value
= atoi (buffer
);
324 anjuta_serializer_read_float (AnjutaSerializer
*serializer
,
325 const gchar
*name
, gfloat
*value
)
329 g_return_val_if_fail (value
!= NULL
, FALSE
);
331 if (!anjuta_serializer_read_buffer (serializer
, name
, &buffer
))
333 *value
= atof (buffer
);
339 anjuta_serializer_read_string (AnjutaSerializer
*serializer
,
340 const gchar
*name
, gchar
**value
,
345 g_return_val_if_fail (value
!= NULL
, FALSE
);
347 if (!anjuta_serializer_read_buffer (serializer
, name
, &buffer
))
351 if (strcmp (buffer
, "(null)") == 0)