~ubuntu-branches/ubuntu/vivid/librest/vivid-proposed

« back to all changes in this revision

Viewing changes to rest/rest-params.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2010-10-20 06:48:27 UTC
  • mfrom: (3.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20101020064827-xke57n8ff9ywlm11
Tags: 0.7.2+git20100820.ad370df7-1ubuntu1
* Sync package from Debian (LP: #646960).
* fix-missing-libs-binutils-gold.patch: fix FTBFS with binutils-gold by
  patching Makefile.am to add explicitly 2 libs (libgobject-2.0 and
  libglib-2.0).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * librest - RESTful web services access
 
3
 * Copyright (c) 2008, 2009, Intel Corporation.
 
4
 *
 
5
 * Authors: Rob Bradford <rob@linux.intel.com>
 
6
 *          Ross Burton <ross@linux.intel.com>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify it
 
9
 * under the terms and conditions of the GNU Lesser General Public License,
 
10
 * version 2.1, as published by the Free Software Foundation.
 
11
 *
 
12
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 
13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
14
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 
15
 * more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public License
 
18
 * along with this program; if not, write to the Free Software Foundation,
 
19
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 *
 
21
 */
 
22
 
 
23
#include <config.h>
 
24
#include <glib-object.h>
 
25
#include "rest-params.h"
 
26
 
 
27
/**
 
28
 * SECTION:rest-params
 
29
 * @short_description: Container for call parameters
 
30
 * @see_also: #RestParam, #RestProxyCall.
 
31
 */
 
32
 
 
33
/*
 
34
 * RestParams is an alias for GHashTable achieved by opaque types in the public
 
35
 * headers and casting internally. This has several limitations, mainly
 
36
 * supporting multiple parameters with the same name and preserving the ordering
 
37
 * of parameters.
 
38
 *
 
39
 * These are not requirements for the bulk of the web services, but this
 
40
 * limitation does mean librest can't be used for a few web services.
 
41
 *
 
42
 * TODO: this should be a list to support multiple parameters with the same
 
43
 * name.
 
44
 */
 
45
 
 
46
/**
 
47
 * rest_params_new:
 
48
 *
 
49
 * Create a new #RestParams.
 
50
 *
 
51
 * Returns: A empty #RestParams.
 
52
 **/
 
53
RestParams *
 
54
rest_params_new (void)
 
55
{
 
56
  /* The key is a string that is owned by the RestParam, so we don't need to
 
57
     explicitly free it on removal. */
 
58
  return (RestParams *)
 
59
    g_hash_table_new_full (g_str_hash, g_str_equal,
 
60
                           NULL, (GDestroyNotify)rest_param_unref);
 
61
}
 
62
 
 
63
/**
 
64
 * rest_params_free:
 
65
 * @params: a valid #RestParams
 
66
 *
 
67
 * Destroy the #RestParams and the #RestParam objects that it contains.
 
68
 **/
 
69
void
 
70
rest_params_free (RestParams *params)
 
71
{
 
72
  GHashTable *hash = (GHashTable *)params;
 
73
 
 
74
  g_return_if_fail (params);
 
75
 
 
76
  g_hash_table_destroy (hash);
 
77
}
 
78
 
 
79
/**
 
80
 * rest_params_add:
 
81
 * @params: a valid #RestParams
 
82
 * @param: a valid #RestParam
 
83
 *
 
84
 * Add @param to @params.
 
85
 **/
 
86
void
 
87
rest_params_add (RestParams *params, RestParam *param)
 
88
{
 
89
  GHashTable *hash = (GHashTable *)params;
 
90
 
 
91
  g_return_if_fail (params);
 
92
  g_return_if_fail (param);
 
93
 
 
94
  g_hash_table_insert (hash, (gpointer)rest_param_get_name (param), param);
 
95
}
 
96
 
 
97
/**
 
98
 * rest_params_get:
 
99
 * @params: a valid #RestParams
 
100
 * @name: a parameter name
 
101
 *
 
102
 * Return the #RestParam called @name, or %NULL if it doesn't exist.
 
103
 *
 
104
 * Returns: a #RestParam or %NULL if the name doesn't exist
 
105
 **/
 
106
RestParam *
 
107
rest_params_get (RestParams *params, const char *name)
 
108
{
 
109
  GHashTable *hash = (GHashTable *)params;
 
110
 
 
111
  g_return_val_if_fail (params, NULL);
 
112
  g_return_val_if_fail (name, NULL);
 
113
 
 
114
  return g_hash_table_lookup (hash, name);
 
115
}
 
116
 
 
117
/**
 
118
 * rest_params_remove:
 
119
 * @params: a valid #RestParams
 
120
 * @name: a parameter name
 
121
 *
 
122
 * Remove the #RestParam called @name.
 
123
 **/
 
124
void
 
125
rest_params_remove (RestParams *params, const char *name)
 
126
{
 
127
  GHashTable *hash = (GHashTable *)params;
 
128
 
 
129
  g_return_if_fail (params);
 
130
  g_return_if_fail (name);
 
131
 
 
132
  g_hash_table_remove (hash, name);
 
133
}
 
134
 
 
135
/**
 
136
 * rest_params_are_strings:
 
137
 * @params: a valid #RestParams
 
138
 *
 
139
 * Checks if the parameters are all simple strings (have a content type of
 
140
 * "text/plain").
 
141
 *
 
142
 * Returns: %TRUE if all of the parameters are simple strings, %FALSE otherwise.
 
143
 **/
 
144
gboolean
 
145
rest_params_are_strings (RestParams *params)
 
146
{
 
147
  GHashTable *hash = (GHashTable *)params;
 
148
  GHashTableIter iter;
 
149
  RestParam *param;
 
150
 
 
151
  g_return_val_if_fail (params, FALSE);
 
152
 
 
153
  g_hash_table_iter_init (&iter, hash);
 
154
  while (g_hash_table_iter_next (&iter, NULL, (gpointer)&param)) {
 
155
    if (!rest_param_is_string (param))
 
156
      return FALSE;
 
157
  }
 
158
 
 
159
  return TRUE;
 
160
 
 
161
}
 
162
 
 
163
/**
 
164
 * rest_params_as_string_hash_table:
 
165
 * @params: a valid #RestParams
 
166
 *
 
167
 * Create a new #GHashTable which contains the name and value of all string
 
168
 * (content type of text/plain) parameters.
 
169
 *
 
170
 * The values are owned by the #RestParams, so don't destroy the #RestParams
 
171
 * before the hash table.
 
172
 *
 
173
 * Returns: a new #GHashTable.
 
174
 **/
 
175
GHashTable *
 
176
rest_params_as_string_hash_table (RestParams *params)
 
177
{
 
178
  GHashTable *hash, *strings;
 
179
  GHashTableIter iter;
 
180
  const char *name = NULL;
 
181
  RestParam *param = NULL;
 
182
 
 
183
  g_return_val_if_fail (params, NULL);
 
184
 
 
185
  hash = (GHashTable *)params;
 
186
  strings = g_hash_table_new (g_str_hash, g_str_equal);
 
187
 
 
188
  g_hash_table_iter_init (&iter, hash);
 
189
  while (g_hash_table_iter_next (&iter, (gpointer)&name, (gpointer)&param)) {
 
190
    if (rest_param_is_string (param))
 
191
      g_hash_table_insert (strings, (gpointer)name, (gpointer)rest_param_get_content (param));
 
192
  }
 
193
 
 
194
  return strings;
 
195
}
 
196
 
 
197
/**
 
198
 * rest_params_iter_init:
 
199
 * @iter: an uninitialized #RestParamsIter
 
200
 * @params: a valid #RestParams
 
201
 *
 
202
 * Initialize a parameter iterator over @params. Modifying @params after calling
 
203
 * this function invalidates the returned iterator.
 
204
 * |[
 
205
 * RestParamsIter iter;
 
206
 * const char *name;
 
207
 * RestParam *param;
 
208
 *
 
209
 * rest_params_iter_init (&iter, params);
 
210
 * while (rest_params_iter_next (&iter, &name, &param)) {
 
211
 *   /&ast; do something with name and param &ast;/
 
212
 * }
 
213
 * ]|
 
214
 **/
 
215
void
 
216
rest_params_iter_init (RestParamsIter *iter, RestParams *params)
 
217
{
 
218
  g_return_if_fail (iter);
 
219
  g_return_if_fail (params);
 
220
 
 
221
  g_hash_table_iter_init ((GHashTableIter *)iter, (GHashTable *)params);
 
222
}
 
223
 
 
224
/**
 
225
 * rest_params_iter_next:
 
226
 * @iter: an initialized #RestParamsIter
 
227
 * @name: a location to store the name, or %NULL
 
228
 * @param: a location to store the #RestParam, or %NULL
 
229
 *
 
230
 * Advances @iter and retrieves the name and/or parameter that are now pointed
 
231
 * at as a result of this advancement.  If FALSE is returned, @name and @param
 
232
 * are not set and the iterator becomes invalid.
 
233
 *
 
234
 * Returns: %FALSE if the end of the #RestParams has been reached, %TRUE otherwise.
 
235
 **/
 
236
gboolean
 
237
rest_params_iter_next (RestParamsIter *iter, const char **name, RestParam **param)
 
238
{
 
239
  g_return_val_if_fail (iter, FALSE);
 
240
 
 
241
  return g_hash_table_iter_next ((GHashTableIter *)iter, (gpointer)name, (gpointer)param);
 
242
}