"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / gsf / gsf-docprop-vector.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gsf-docprop-vector.c: A type implementing OLE Document Property vectors using a GValueArray
4  *
5  * Copyright (C) 2004-2006 Frank Chiulli (fc-linux@cox.net)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2.1 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21
22 #include <gsf-config.h>
23 #include <gsf/gsf-docprop-vector.h>
24 #include <gsf/gsf-impl-utils.h>
25 #include <stdio.h>
26
27 struct _GsfDocPropVector {
28         GObject      parent;
29
30         GValueArray *gva;
31 };
32 typedef GObjectClass  GsfDocPropVectorClass;
33
34 #define GSF_DOCPROP_VECTOR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), GSF_DOCPROP_VECTOR_TYPE, GsfDocPropVectorClass))
35 #define GSF_DOCPROP_VECTOR_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), GSF_DOCPROP_VECTOR_TYPE, GsfDocPropVectorClass))
36 #define IS_GSF_DOCPROP_VECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GSF_DOCPROP_VECTOR_TYPE))
37
38 static GObjectClass *parent_class;
39
40 GValueArray *
41 gsf_value_get_docprop_varray (GValue const *value)
42 {
43         GsfDocPropVector *v = gsf_value_get_docprop_vector (value);
44         return v ? v->gva : NULL;
45 }
46
47 /**
48  * gsf_docprop_value_get_vector
49  * @value: A GValue of type #GsfDocPropVector.
50  *
51  * This function returns a pointer to the GsfDocPropVector structure in @value.
52  * No additional references are created.
53  *
54  * Returns: A pointer to the #GsfDocPropVector structure in @value
55  **/
56 GsfDocPropVector *
57 gsf_value_get_docprop_vector (GValue const *value)
58 {
59         g_return_val_if_fail (VAL_IS_GSF_DOCPROP_VECTOR (value), NULL);
60         
61         return (GsfDocPropVector *) g_value_get_object (value);
62 }
63
64 /**
65  * gsf_docprop_vector_append
66  * @vector: The vector to which the GValue will be added
67  * @value:  The GValue to add to @vector
68  *
69  * Insert a copy of @value as the last element of @vector.
70  **/
71 void
72 gsf_docprop_vector_append (GsfDocPropVector *vector, GValue *value)
73 {
74         g_return_if_fail (vector != NULL);
75         g_return_if_fail (value != NULL);
76
77         if (G_IS_VALUE (value))
78                 vector->gva = g_value_array_append (vector->gva, value);
79 }
80
81 /**
82  * gsf_docprop_vector_as_string
83  * @vector: The #GsfDocPropVector from which GValues will be extracted.
84  *
85  * This function returns a string which represents all the GValues in @vector.
86  * The caller is responsible for freeing the result.
87  *
88  * Returns: a string of comma-separated values
89  **/
90 gchar*
91 gsf_docprop_vector_as_string (GsfDocPropVector const *vector)
92 {
93         gchar           *rstring;
94
95         guint            i;
96         guint            num_values;
97
98         g_return_val_if_fail (vector != NULL, NULL);
99         g_return_val_if_fail (vector->gva != NULL, NULL);
100
101         rstring    = g_new0 (gchar, 1);
102         num_values = vector->gva->n_values;
103         
104         for (i = 0; i < num_values; i++) {
105                 char    *str;
106                 GValue  *v;
107                 
108                 v = g_value_array_get_nth (vector->gva, i);
109                 str = g_strdup_value_contents (v);
110                 rstring = g_strconcat (rstring, str, ",", NULL);
111                 g_free (str);
112         }
113
114         return rstring;
115 }
116
117 static void
118 gsf_docprop_vector_finalize (GObject *obj)
119 {
120         GsfDocPropVector *vector = (GsfDocPropVector *) obj;
121         if (vector->gva != NULL) {
122                 g_value_array_free (vector->gva);
123                 vector->gva = NULL;
124         }
125         parent_class->finalize (obj);
126 }
127
128 static void
129 gsf_docprop_vector_class_init (GObjectClass *gobject_class)
130 {
131         parent_class = g_type_class_peek (G_TYPE_OBJECT);
132         gobject_class->finalize = gsf_docprop_vector_finalize;
133 }
134
135 static void
136 gsf_docprop_vector_init (GsfDocPropVector *vector)
137 {
138         vector->gva = g_value_array_new (0);
139 }
140
141 GSF_CLASS (GsfDocPropVector, gsf_docprop_vector,
142            gsf_docprop_vector_class_init, gsf_docprop_vector_init,
143            G_TYPE_OBJECT)
144
145 /**
146  * gsf_docprop_vector_new
147  *
148  * This function creates a new gsf_docprop_vector object.
149  *
150  * Returns: GsfDocPropVector*
151  **/
152 GsfDocPropVector*
153 gsf_docprop_vector_new (void)
154 {
155         return g_object_new (GSF_DOCPROP_VECTOR_TYPE, NULL);
156 }
157