3 // vformat support routines in base class
7 Copyright (C) 2006-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
24 #include "vformat.h" // comes from opensync, but not a public header yet
29 namespace Barry
{ namespace Sync
{
31 //////////////////////////////////////////////////////////////////////////////
34 std::string
vAttr::GetName()
41 const char *name
= b_vformat_attribute_get_name(m_attr
);
47 std::string
vAttr::GetValue(int nth
)
50 const char *value
= 0;
53 if( b_vformat_attribute_is_single_valued(m_attr
) ) {
55 value
= b_vformat_attribute_get_value(m_attr
);
58 value
= b_vformat_attribute_get_nth_value(m_attr
, nth
);
68 std::string
vAttr::GetDecodedValue()
71 GString
*value
= NULL
;
74 if( b_vformat_attribute_is_single_valued(m_attr
) ) {
75 value
= b_vformat_attribute_get_value_decoded(m_attr
);
80 ret
.assign(value
->str
, value
->len
);
85 std::string
vAttr::GetParam(const char *name
, int nth
)
92 b_VFormatParam
*param
= b_vformat_attribute_find_param(m_attr
, name
, 0);
96 const char *value
= b_vformat_attribute_param_get_nth_value(param
, nth
);
103 /// Does an exhaustive search through the attribute, searching for all
104 /// param values that exist for the given name, and returns all values
105 /// in a comma delimited string.
106 std::string
vAttr::GetAllParams(const char *name
)
113 b_VFormatParam
*param
= 0;
115 (param
= b_vformat_attribute_find_param(m_attr
, name
, level
));
118 const char *value
= 0;
120 (value
= b_vformat_attribute_param_get_nth_value(param
, nth
));
133 //////////////////////////////////////////////////////////////////////////////
144 b_vformat_free(m_format
);
148 void vBase::SetFormat(b_VFormat
*format
)
151 b_vformat_free(m_format
);
160 b_vformat_free(m_format
);
165 vAttrPtr
vBase::NewAttr(const char *name
)
167 // Trace trace("vBase::NewAttr");
169 // trace.logf("creating valueless attr: %s", name);
171 vAttrPtr
attr(b_vformat_attribute_new(NULL
, name
));
173 throw Barry::ConvertError("resource error allocating vformat attribute");
177 vAttrPtr
vBase::NewAttr(const char *name
, const char *value
)
179 // Trace trace("vBase::NewAttr");
182 some vCard values are positional (like name), so blank should be allowed...
184 if( strlen(value) == 0 ) {
185 trace.logf("attribute '%s' contains no data, skipping", name);
190 // trace.logf("creating attr: %s, %s", name, value);
192 vAttrPtr
attr(b_vformat_attribute_new(NULL
, name
));
194 throw ConvertError("resource error allocating vformat attribute");
196 b_vformat_attribute_add_value(attr
.Get(), value
);
200 void vBase::AddAttr(vAttrPtr attr
)
202 // Trace trace("vBase::AddAttr");
205 // trace.log("attribute contains no data, skipping");
209 b_vformat_add_attribute(m_format
, attr
.Extract());
212 void vBase::AddValue(vAttrPtr
&attr
, const char *value
)
214 // Trace trace("vBase::AddValue");
216 // trace.log("attribute pointer contains no data, skipping");
220 if( strlen(value) == 0 ) {
221 trace.log("attribute value is empty, skipping");
225 b_vformat_attribute_add_value(attr
.Get(), value
);
228 void vBase::AddEncodedValue(vAttrPtr
&attr
, b_VFormatEncoding encoding
, const char *value
, int len
)
230 // Trace trace("vBase::AddValue");
232 // trace.log("attribute pointer contains no data, skipping");
236 attr
.Get()->encoding
= encoding
;
237 attr
.Get()->encoding_set
= TRUE
;
239 b_vformat_attribute_add_value_decoded(attr
.Get(), value
, len
);
242 void vBase::AddParam(vAttrPtr
&attr
, const char *name
, const char *value
)
244 // Trace trace("vBase::AddParam");
247 // trace.log("attribute pointer contains no data, skipping");
251 if( strlen(value) == 0 ) {
252 trace.log("parameter value is empty, skipping");
257 b_VFormatParam
*pParam
= b_vformat_attribute_param_new(name
);
258 b_vformat_attribute_param_add_value(pParam
, value
);
259 b_vformat_attribute_add_param(attr
.Get(), pParam
);
262 std::string
vBase::GetAttr(const char *attrname
, const char *block
)
264 // Trace trace("vBase::GetAttr");
265 // trace.logf("getting attr: %s", attrname);
268 const char *value
= 0;
270 bool needs_freeing
= false;
272 b_VFormatAttribute
*attr
= b_vformat_find_attribute(m_format
, attrname
, 0, block
);
274 if( b_vformat_attribute_is_single_valued(attr
) ) {
275 value
= b_vformat_attribute_get_value(attr
);
276 needs_freeing
= true;
279 // FIXME, this is hardcoded
280 value
= b_vformat_attribute_get_nth_value(attr
, 0);
288 g_free((char *)value
);
290 // trace.logf("attr value: %s", ret.c_str());
294 vAttr
vBase::GetAttrObj(const char *attrname
, int nth
, const char *block
)
296 // Trace trace("vBase::GetAttrObj");
297 // trace.logf("getting attr: %s", attrname);
299 return vAttr(b_vformat_find_attribute(m_format
, attrname
, nth
, block
));
302 }} // namespace Barry::Sync