libsync: moved vformat-related code from os22 to library
[barry.git] / src / vbase.cc
blob1f3c0318c89221ef9e3474cd78975550027a11d4
1 //
2 // \file vbase.cc
3 // vformat support routines in base class
4 //
6 /*
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.
22 #include "vbase.h"
23 //#include "trace.h"
24 #include "vformat.h" // comes from opensync, but not a public header yet
25 #include <stdint.h>
26 #include <glib.h>
27 #include <sstream>
29 namespace Barry { namespace Sync {
31 //////////////////////////////////////////////////////////////////////////////
32 // vAttr
34 std::string vAttr::GetName()
36 std::string ret;
38 if( !m_attr )
39 return ret;
41 const char *name = b_vformat_attribute_get_name(m_attr);
42 if( name )
43 ret = name;
44 return ret;
47 std::string vAttr::GetValue(int nth)
49 std::string ret;
50 const char *value = 0;
52 if( m_attr ) {
53 if( b_vformat_attribute_is_single_valued(m_attr) ) {
54 if( nth == 0 )
55 value = b_vformat_attribute_get_value(m_attr);
57 else {
58 value = b_vformat_attribute_get_nth_value(m_attr, nth);
62 if( value )
63 ret = value;
65 return ret;
68 std::string vAttr::GetDecodedValue()
70 std::string ret;
71 GString *value = NULL;
73 if( m_attr ) {
74 if( b_vformat_attribute_is_single_valued(m_attr) ) {
75 value = b_vformat_attribute_get_value_decoded(m_attr);
79 if( value )
80 ret.assign(value->str, value->len);
82 return ret;
85 std::string vAttr::GetParam(const char *name, int nth)
87 std::string ret;
89 if( !m_attr )
90 return ret;
92 b_VFormatParam *param = b_vformat_attribute_find_param(m_attr, name, 0);
93 if( !param )
94 return ret;
96 const char *value = b_vformat_attribute_param_get_nth_value(param, nth);
97 if( value )
98 ret = value;
100 return ret;
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)
108 std::string ret;
110 if( !m_attr )
111 return ret;
113 b_VFormatParam *param = 0;
114 for( int level = 0;
115 (param = b_vformat_attribute_find_param(m_attr, name, level));
116 level++ )
118 const char *value = 0;
119 for( int nth = 0;
120 (value = b_vformat_attribute_param_get_nth_value(param, nth));
121 nth++ )
123 if( ret.size() )
124 ret += ",";
125 ret += value;
129 return ret;
133 //////////////////////////////////////////////////////////////////////////////
134 // vCalendar
136 vBase::vBase()
137 : m_format(0)
141 vBase::~vBase()
143 if( m_format ) {
144 b_vformat_free(m_format);
148 void vBase::SetFormat(b_VFormat *format)
150 if( m_format ) {
151 b_vformat_free(m_format);
152 m_format = 0;
154 m_format = format;
157 void vBase::Clear()
159 if( m_format ) {
160 b_vformat_free(m_format);
161 m_format = 0;
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));
172 if( !attr.Get() )
173 throw Barry::ConvertError("resource error allocating vformat attribute");
174 return attr;
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);
186 return vAttrPtr();
190 // trace.logf("creating attr: %s, %s", name, value);
192 vAttrPtr attr(b_vformat_attribute_new(NULL, name));
193 if( !attr.Get() )
194 throw ConvertError("resource error allocating vformat attribute");
196 b_vformat_attribute_add_value(attr.Get(), value);
197 return attr;
200 void vBase::AddAttr(vAttrPtr attr)
202 // Trace trace("vBase::AddAttr");
204 if( !attr.Get() ) {
205 // trace.log("attribute contains no data, skipping");
206 return;
209 b_vformat_add_attribute(m_format, attr.Extract());
212 void vBase::AddValue(vAttrPtr &attr, const char *value)
214 // Trace trace("vBase::AddValue");
215 if( !attr.Get() ) {
216 // trace.log("attribute pointer contains no data, skipping");
217 return;
220 if( strlen(value) == 0 ) {
221 trace.log("attribute value is empty, skipping");
222 return;
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");
231 if( !attr.Get() ) {
232 // trace.log("attribute pointer contains no data, skipping");
233 return;
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");
246 if( !attr.Get() ) {
247 // trace.log("attribute pointer contains no data, skipping");
248 return;
251 if( strlen(value) == 0 ) {
252 trace.log("parameter value is empty, skipping");
253 return;
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);
267 std::string ret;
268 const char *value = 0;
270 bool needs_freeing = false;
272 b_VFormatAttribute *attr = b_vformat_find_attribute(m_format, attrname, 0, block);
273 if( attr ) {
274 if( b_vformat_attribute_is_single_valued(attr) ) {
275 value = b_vformat_attribute_get_value(attr);
276 needs_freeing = true;
278 else {
279 // FIXME, this is hardcoded
280 value = b_vformat_attribute_get_nth_value(attr, 0);
284 if( value )
285 ret = value;
287 if( needs_freeing )
288 g_free((char *)value);
290 // trace.logf("attr value: %s", ret.c_str());
291 return ret;
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