"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / gsf / gsf-infile.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gsf-infile.c :
4  *
5  * Copyright (C) 2002-2006 Jody Goldberg (jody@gnome.org)
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-infile-impl.h>
24 #include <gsf/gsf-impl-utils.h>
25
26 #include <stdarg.h>
27
28 #define GET_CLASS(instance) G_TYPE_INSTANCE_GET_CLASS (instance, GSF_INFILE_TYPE, GsfInfileClass)
29
30 /**
31  * gsf_infile_num_children :
32  * @infile : the structured storage
33  *
34  * Returns: the number of children the storage has, or -1 if the storage can not
35  *      have children.
36  **/
37 int
38 gsf_infile_num_children (GsfInfile *infile)
39 {
40         g_return_val_if_fail (infile != NULL, -1);
41
42         return GET_CLASS (infile)->num_children (infile);
43 }
44
45 /**
46  * gsf_infile_name_by_index :
47  * @infile :
48  * @i      :
49  *
50  * Returns: the utf8 encoded name of the @i-th child
51  *      <emphasis>DO NOT FREE THE STRING.</emphasis>
52  **/
53 char const *
54 gsf_infile_name_by_index (GsfInfile *infile, int i)
55 {
56         g_return_val_if_fail (infile != NULL, NULL);
57
58         return GET_CLASS (infile)->name_by_index (infile, i);
59 }
60
61 /**
62  * gsf_infile_child_by_index :
63  * @infile : #GsfInfile
64  * @i : target index
65  *
66  * TODO : For 2.0 api will change to include a GError.
67  * Returns: a newly created child which must be unrefed.
68  **/
69 GsfInput *
70 gsf_infile_child_by_index (GsfInfile *infile, int i)
71 {
72         GError *err = NULL;
73         GsfInput *res;
74
75         g_return_val_if_fail (GSF_INFILE (infile) != NULL, NULL);
76
77         res = GET_CLASS (infile)->child_by_index (infile, i, &err);
78
79         if (err != NULL) {
80                 char const *iname = gsf_input_name (GSF_INPUT (infile));
81                 g_warning ("Unable to get child[%d] for infile '%s' because : %s",
82                            i, iname ? iname : "?", err->message);
83                 g_error_free (err);
84                 g_return_val_if_fail (res == NULL, NULL); /* be anal */
85         }
86
87         return res;
88 }
89
90 /**
91  * gsf_infile_child_by_name :
92  * @infile : #GsfInfile
93  * @name : target name
94  *
95  * TODO : For 2.0 api will change to include a GError.
96  * Returns: a newly created child which must be unrefed.
97  **/
98 GsfInput *
99 gsf_infile_child_by_name (GsfInfile *infile, char const *name)
100 {
101         GError *err = NULL;
102         GsfInput *res;
103
104         g_return_val_if_fail (GSF_INFILE (infile) != NULL, NULL);
105         g_return_val_if_fail (name != NULL, NULL);
106
107         res = GET_CLASS (infile)->child_by_name (infile, name, &err);
108
109         if (err != NULL) {
110                 char const *iname = gsf_input_name (GSF_INPUT (infile));
111                 g_warning ("Unable to get child['%s'] for infile '%s' because : %s",
112                            name, iname ? iname : "?", err->message);
113                 g_error_free (err);
114                 g_return_val_if_fail (res == NULL, NULL); /* be anal */
115         }
116
117         return res;
118 }
119
120 /**
121  * gsf_infile_child_by_vname :
122  * @infile :
123  * @Varargs : A %NULL terminated list of names
124  *
125  * Returns: a newly created child which must be unrefed.
126  **/
127 GsfInput *
128 gsf_infile_child_by_vname (GsfInfile *infile,  ...)
129 {
130         GsfInput *res;
131         va_list   names;
132
133         va_start (names, infile);
134         res = gsf_infile_child_by_vaname (infile, names);
135         va_end (names);
136
137         return res;
138 }
139
140 /**
141  * gsf_infile_child_by_vaname :
142  * @infile : #GsfInfile
143  * @names : A %NULL terminated array of names (e.g. from g_strsplit)
144  *
145  * New in 1.14.9.
146  *
147  * Returns: a newly created child which must be unrefed.
148  **/
149 GsfInput *
150 gsf_infile_child_by_vaname (GsfInfile *infile, va_list names)
151 {
152         GsfInput  *child = GSF_INPUT (infile);
153         GsfInfile *tmp = NULL;
154         char const *name;
155
156         g_return_val_if_fail (GSF_IS_INFILE (infile), NULL);
157
158         while (NULL != (name = va_arg (names, char const *))) {
159                 child = gsf_infile_child_by_name (infile, name);
160                 if (child == NULL)
161                         break;
162                 if (tmp != NULL)
163                         g_object_unref (G_OBJECT (tmp));
164
165                 g_return_val_if_fail (GSF_IS_INFILE (child), NULL);
166
167                 infile = tmp = GSF_INFILE (child);
168         }
169
170         return child;
171 }
172
173 /**
174  * gsf_infile_child_by_aname :
175  * @infile : #GsfInfile
176  * @names : A %NULL terminated array of names (e.g. from g_strsplit)
177  *
178  * New in 1.14.9.
179  *
180  * Returns: a newly created child which must be unrefed.
181  **/
182 GsfInput *
183 gsf_infile_child_by_aname (GsfInfile *infile, char const *names[])
184 {
185         GsfInput  *child = GSF_INPUT (infile);
186         GsfInfile *tmp = NULL;
187
188         g_return_val_if_fail (GSF_IS_INFILE (infile), NULL);
189         g_return_val_if_fail (names != NULL, NULL);
190
191         for (;*names ; names++) {
192                 child = gsf_infile_child_by_name (infile, *names);
193                 if (tmp != NULL)
194                         g_object_unref (G_OBJECT (tmp));
195                 if (child == NULL)
196                         break;
197
198                 g_return_val_if_fail (GSF_IS_INFILE (child), NULL);
199
200                 infile = tmp = GSF_INFILE (child);
201         }
202
203         return child;
204 }
205
206 GSF_CLASS_ABSTRACT (GsfInfile, gsf_infile, NULL, NULL, GSF_INPUT_TYPE)