2 * Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "quvi/quvi.h"
24 #include "quvi/llst.h"
28 extern const char empty
[]; /* quvi_api.c */
30 /* Title: quvi Network API */
32 static QUVIcode
_net_getprop(_quvi_net_t n
, QUVInetProperty p
, ...)
49 type
= QUVIPROPERTY_TYPEMASK
& (int)p
;
53 case QUVIPROPERTY_DOUBLE
:
54 _init_from_arg(dp
, double*);
55 case QUVIPROPERTY_STRING
:
56 _init_from_arg(sp
, char**);
57 case QUVIPROPERTY_LONG
:
58 _init_from_arg(lp
, long*);
59 case QUVIPROPERTY_VOID
:
60 _init_from_arg(vp
, void **);
71 case QUVI_NET_PROPERTY_URL
:
73 case QUVI_NET_PROPERTY_REDIRECTURL
:
74 _set_s(n
->redirect
.url
);
75 case QUVI_NET_PROPERTY_CONTENT
:
76 _set_s(n
->fetch
.content
);
77 case QUVI_NET_PROPERTY_CONTENTTYPE
:
78 _set_s(n
->verify
.content_type
);
79 case QUVI_NET_PROPERTY_CONTENTLENGTH
:
80 _set_from_value_n(dp
, n
->verify
.content_length
);
81 case QUVI_NET_PROPERTY_RESPONSECODE
:
82 _set_from_value_n(lp
, n
->resp_code
);
83 case QUVI_NET_PROPERTY_FEATURES
:
92 * Function: quvi_net_getprop
94 * Returns a network property.
97 * net - Network handle
98 * property - Property ID
101 * Non-zero value if an error occurred.
103 QUVIcode
quvi_net_getprop(quvi_net_propfeat_t net
,
104 QUVInetProperty property
,
112 va_start(arg
, property
);
113 p
= va_arg(arg
, void*);
116 return (_net_getprop(net
, property
, p
));
119 static QUVIcode
_net_setprop(_quvi_net_t n
, QUVInetProperty p
, va_list arg
)
123 case QUVI_NET_PROPERTY_URL
:
124 _set_alloc_s(n
->url
);
125 case QUVI_NET_PROPERTY_FEATURES
:
126 break; /* Ignored: read-only */
127 case QUVI_NET_PROPERTY_REDIRECTURL
:
128 _set_alloc_s(n
->redirect
.url
);
129 case QUVI_NET_PROPERTY_CONTENT
:
130 _set_alloc_s(n
->fetch
.content
);
131 case QUVI_NET_PROPERTY_CONTENTTYPE
:
132 _set_alloc_s(n
->verify
.content_type
);
133 case QUVI_NET_PROPERTY_CONTENTLENGTH
:
134 _set_from_arg_n(n
->verify
.content_length
);
135 case QUVI_NET_PROPERTY_RESPONSECODE
:
136 _set_from_arg_n(n
->resp_code
);
138 return (QUVI_INVARG
);
144 * Function: quvi_net_setprop
146 * Sets a network property.
149 * net - Network handle
150 * property - Property ID
154 * Non-zero value if an error occurred.
156 QUVIcode
quvi_net_setprop(quvi_net_t net
, QUVInetProperty property
, ...)
163 va_start(arg
, property
);
164 rc
= _net_setprop(net
, property
, arg
);
170 static QUVIcode
_net_getprop_feat(_quvi_net_propfeat_t n
,
171 QUVInetPropertyFeature feature
,
192 va_start(arg
, feature
);
193 type
= QUVIPROPERTY_TYPEMASK
& (int)feature
;
197 case QUVIPROPERTY_DOUBLE
:
198 _init_from_arg(dp
, double*);
199 case QUVIPROPERTY_STRING
:
200 _init_from_arg(sp
, char**);
211 case QUVI_NET_PROPERTY_FEATURE_NAME
:
213 case QUVI_NET_PROPERTY_FEATURE_VALUE
:
222 * Function: quvi_net_getprop_feat
224 * Returns a network property feature.
227 * handle - Property feature handle
228 * feature - Property feature ID
232 * Non-zero value if an error occurred.
234 QUVIcode
quvi_net_getprop_feat(quvi_net_propfeat_t handle
,
235 QUVInetPropertyFeature feature
,
241 _is_badhandle(handle
);
243 va_start(arg
, feature
);
244 p
= va_arg(arg
, void*);
247 return (_net_getprop_feat(handle
, feature
, p
));
251 * Function: quvi_net_seterr
253 * Sets a network error message.
256 * net - Network handle
257 * fmt - Format string
261 * Non-zero value if an error occurred.
263 QUVIcode
quvi_net_seterr(quvi_net_t net
, const char *fmt
, ...)
269 n
= (_quvi_net_t
) net
;
272 vafreprintf(&n
->errmsg
, fmt
, args
);
277 /* Title: Convenience functions */
279 extern const char *net_prop_feats
[];
281 static const char *_feat_to_str(QUVInetPropertyFeatureName id
)
283 const char *s
= NULL
;
285 if (id
> QUVI_NET_PROPERTY_FEATURE_NAME_NONE
286 && id
< _QUVI_NET_PROPERTY_FEATURE_NAME_LAST
)
288 s
= net_prop_feats
[id
];
294 * Function: quvi_net_get_one_prop_feat
296 * Returns the first matching property feature from the list. A
297 * convenience function that allows finding the feature by an ID rather
301 * net - Network handle
302 * name - Property feature ID
305 * A null-terminated string, otherwise NULL.
308 * <quvi_net_getprop_feat>
310 char *quvi_net_get_one_prop_feat(quvi_net_t net
,
311 QUVInetPropertyFeatureName name
)
313 quvi_llst_node_t opt
;
315 quvi_net_getprop(net
, QUVI_NET_PROPERTY_FEATURES
, &opt
);
319 const char *feat_name
, *feat_value
, *s
;
320 quvi_net_propfeat_t popt
;
322 popt
= (quvi_net_propfeat_t
) quvi_llst_data(opt
);
324 quvi_net_getprop_feat(popt
,
325 QUVI_NET_PROPERTY_FEATURE_NAME
, &feat_name
);
327 quvi_net_getprop_feat(popt
,
328 QUVI_NET_PROPERTY_FEATURE_VALUE
, &feat_value
);
330 s
= _feat_to_str(name
);
332 if (s
&& !strcmp(feat_name
,s
))
333 return ((char*)feat_value
);
335 opt
= quvi_llst_next(opt
);
341 /* vim: set ts=2 sw=2 tw=72 expandtab: */