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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <anjuta-enum-types.h>
22 #include "anjuta-serializer.h"
24 static void anjuta_serializer_class_init(AnjutaSerializerClass
*klass
);
25 static void anjuta_serializer_init(AnjutaSerializer
*sp
);
26 static void anjuta_serializer_finalize(GObject
*object
);
28 struct _AnjutaSerializerPrivate
{
29 AnjutaSerializerMode mode
;
34 static GObjectClass
*parent_class
= NULL
;
44 anjuta_serializer_get_type (void)
46 static GType type
= 0;
49 static const GTypeInfo our_info
= {
50 sizeof (AnjutaSerializerClass
),
53 (GClassInitFunc
)anjuta_serializer_class_init
,
56 sizeof (AnjutaSerializer
),
58 (GInstanceInitFunc
)anjuta_serializer_init
,
61 type
= g_type_register_static(G_TYPE_OBJECT
,
62 "AnjutaSerializer", &our_info
, 0);
69 anjuta_serializer_set_property (GObject
* object
,
71 const GValue
* value
, GParamSpec
* pspec
)
73 AnjutaSerializer
*self
= ANJUTA_SERIALIZER (object
);
74 g_return_if_fail (value
!= NULL
);
75 g_return_if_fail (pspec
!= NULL
);
80 self
->priv
->mode
= g_value_get_enum (value
);
83 g_free (self
->priv
->filepath
);
84 self
->priv
->filepath
= g_value_dup_string (value
);
85 if (self
->priv
->stream
)
86 fclose (self
->priv
->stream
);
87 if (self
->priv
->mode
== ANJUTA_SERIALIZER_READ
)
88 self
->priv
->stream
= fopen (self
->priv
->filepath
, "r");
90 self
->priv
->stream
= fopen (self
->priv
->filepath
, "w");
91 if (self
->priv
->stream
== NULL
)
94 g_warning ("Could not open %s for serialization: %s",
95 self->priv->filepath, g_strerror (errno));
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
105 anjuta_serializer_get_property (GObject
* object
,
107 GValue
* value
, GParamSpec
* pspec
)
109 AnjutaSerializer
*self
= ANJUTA_SERIALIZER (object
);
111 g_return_if_fail(value
!= NULL
);
112 g_return_if_fail(pspec
!= NULL
);
117 g_value_set_enum (value
, self
->priv
->mode
);
120 g_value_set_string (value
, self
->priv
->filepath
);
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
128 anjuta_serializer_class_init (AnjutaSerializerClass
*klass
)
130 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
132 parent_class
= g_type_class_peek_parent(klass
);
133 object_class
->finalize
= anjuta_serializer_finalize
;
134 object_class
->set_property
= anjuta_serializer_set_property
;
135 object_class
->get_property
= anjuta_serializer_get_property
;
137 g_object_class_install_property (object_class
,
139 g_param_spec_string ("filepath",
141 "Used to store and retrieve the stream"
144 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
145 g_object_class_install_property (object_class
,
147 g_param_spec_enum ("mode",
148 "Serialization mode",
149 "Used to decide read or write operation",
150 ANJUTA_TYPE_SERIALIZER_MODE
,
151 ANJUTA_SERIALIZER_READ
,
152 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
156 anjuta_serializer_init (AnjutaSerializer
*obj
)
158 obj
->priv
= g_new0 (AnjutaSerializerPrivate
, 1);
162 anjuta_serializer_finalize (GObject
*object
)
164 AnjutaSerializer
*cobj
;
165 cobj
= ANJUTA_SERIALIZER(object
);
167 g_free (cobj
->priv
->filepath
);
168 if (cobj
->priv
->stream
)
169 fclose (cobj
->priv
->stream
);
171 G_OBJECT_CLASS (parent_class
)->finalize (object
);
175 anjuta_serializer_new (const gchar
*filepath
, AnjutaSerializerMode mode
)
177 AnjutaSerializer
*obj
;
179 obj
= ANJUTA_SERIALIZER (g_object_new (ANJUTA_TYPE_SERIALIZER
,
181 "filepath", filepath
, NULL
));
182 if (obj
->priv
->stream
== NULL
)
184 g_object_unref (obj
);
191 anjuta_serializer_write_buffer (AnjutaSerializer
*serializer
,
192 const gchar
*name
, const gchar
*value
)
197 g_return_val_if_fail (ANJUTA_IS_SERIALIZER (serializer
), FALSE
);
198 g_return_val_if_fail (serializer
->priv
->stream
!= NULL
, FALSE
);
199 g_return_val_if_fail (serializer
->priv
->mode
== ANJUTA_SERIALIZER_WRITE
,
201 g_return_val_if_fail (name
!= NULL
, FALSE
);
202 g_return_val_if_fail (value
!= NULL
, FALSE
);
204 buffer
= g_strconcat (name
, ": ", value
, NULL
);
205 length
= strlen (buffer
);
206 if (fprintf (serializer
->priv
->stream
, "%d\n", length
) < 1)
211 if (fwrite (buffer
, length
, 1, serializer
->priv
->stream
) < 1)
216 if (fprintf (serializer
->priv
->stream
, "\n") < 0)
226 anjuta_serializer_read_buffer (AnjutaSerializer
*serializer
,
227 const gchar
*name
, gchar
**value
)
232 g_return_val_if_fail (ANJUTA_IS_SERIALIZER (serializer
), FALSE
);
233 g_return_val_if_fail (serializer
->priv
->stream
!= NULL
, FALSE
);
234 g_return_val_if_fail (serializer
->priv
->mode
== ANJUTA_SERIALIZER_READ
,
236 g_return_val_if_fail (name
!= NULL
, FALSE
);
237 g_return_val_if_fail (value
!= NULL
, FALSE
);
239 if (fscanf (serializer
->priv
->stream
, "%d\n", &length
) < 1)
242 buffer
= g_new0 (gchar
, length
+ 1);
243 if (fread (buffer
, length
, 1, serializer
->priv
->stream
) < 1)
248 if (fscanf (serializer
->priv
->stream
, "\n") < 0)
253 if (strncmp (buffer
, name
, strlen (name
)) != 0)
258 if (strncmp (buffer
+ strlen (name
), ": ", 2) != 0)
263 strcpy (buffer
, buffer
+ strlen (name
) + 2);
270 anjuta_serializer_write_int (AnjutaSerializer
*serializer
,
271 const gchar
*name
, gint value
)
274 snprintf (buffer
, 64, "%d", value
);
275 return anjuta_serializer_write_buffer (serializer
, name
, buffer
);
279 anjuta_serializer_write_float (AnjutaSerializer
*serializer
,
280 const gchar
*name
, gfloat value
)
283 snprintf (buffer
, 64, "%f", value
);
284 return anjuta_serializer_write_buffer (serializer
, name
, buffer
);
288 anjuta_serializer_write_string (AnjutaSerializer
*serializer
,
289 const gchar
*name
, const gchar
*value
)
292 return anjuta_serializer_write_buffer (serializer
, name
, value
);
294 return anjuta_serializer_write_buffer (serializer
, name
, "(null)");
298 anjuta_serializer_read_int (AnjutaSerializer
*serializer
,
299 const gchar
*name
, gint
*value
)
303 g_return_val_if_fail (value
!= NULL
, FALSE
);
305 if (!anjuta_serializer_read_buffer (serializer
, name
, &buffer
))
307 *value
= atoi (buffer
);
313 anjuta_serializer_read_float (AnjutaSerializer
*serializer
,
314 const gchar
*name
, gfloat
*value
)
318 g_return_val_if_fail (value
!= NULL
, FALSE
);
320 if (!anjuta_serializer_read_buffer (serializer
, name
, &buffer
))
322 *value
= atof (buffer
);
328 anjuta_serializer_read_string (AnjutaSerializer
*serializer
,
329 const gchar
*name
, gchar
**value
,
334 g_return_val_if_fail (value
!= NULL
, FALSE
);
336 if (!anjuta_serializer_read_buffer (serializer
, name
, &buffer
))
340 if (strcmp (buffer
, "(null)") == 0)