- added suppot to the Probe class to limit its search for
[barry.git] / opensync-plugin / src / vbase.cc
blob647ace44960d9ffbd6632cbd64e48577cd60ba12
1 //
2 // \file vbase.cc
3 // vformat support routines in base class
4 //
6 /*
7 Copyright (C) 2006-2007, 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>
30 //////////////////////////////////////////////////////////////////////////////
31 // vAttr
33 std::string vAttr::GetName()
35 std::string ret;
37 if( !m_attr )
38 return ret;
40 const char *name = vformat_attribute_get_name(m_attr);
41 if( name )
42 ret = name;
43 return ret;
46 std::string vAttr::GetValue(int nth)
48 std::string ret;
49 const char *value = 0;
51 if( m_attr ) {
52 if( vformat_attribute_is_single_valued(m_attr) ) {
53 value = vformat_attribute_get_value(m_attr);
55 else {
56 value = vformat_attribute_get_nth_value(m_attr, nth);
60 if( value )
61 ret = value;
63 return ret;
66 std::string vAttr::GetParam(const char *name, int nth)
68 std::string ret;
70 if( !m_attr )
71 return ret;
73 VFormatParam *param = vformat_attribute_find_param(m_attr, name);
74 if( !param )
75 return ret;
77 const char *value = vformat_attribute_param_get_nth_value(param, nth);
78 if( value )
79 ret = value;
81 return ret;
85 //////////////////////////////////////////////////////////////////////////////
86 // vCalendar
88 vBase::vBase()
89 : m_format(0)
93 vBase::~vBase()
95 if( m_format ) {
96 vformat_free(m_format);
100 void vBase::SetFormat(VFormat *format)
102 if( m_format ) {
103 vformat_free(m_format);
104 m_format = 0;
106 m_format = format;
109 void vBase::Clear()
111 if( m_format ) {
112 vformat_free(m_format);
113 m_format = 0;
117 vAttrPtr vBase::NewAttr(const char *name)
119 Trace trace("vBase::NewAttr");
121 trace.logf("creating valueless attr: %s", name);
123 vAttrPtr attr(vformat_attribute_new(NULL, name));
124 if( !attr.Get() )
125 throw ConvertError("resource error allocating vformat attribute");
126 return attr;
129 vAttrPtr vBase::NewAttr(const char *name, const char *value)
131 Trace trace("vBase::NewAttr");
134 some vCard values are positional (like name), so blank should be allowed...
136 if( strlen(value) == 0 ) {
137 trace.logf("attribute '%s' contains no data, skipping", name);
138 return vAttrPtr();
142 trace.logf("creating attr: %s, %s", name, value);
144 vAttrPtr attr(vformat_attribute_new(NULL, name));
145 if( !attr.Get() )
146 throw ConvertError("resource error allocating vformat attribute");
148 vformat_attribute_add_value(attr.Get(), value);
149 return attr;
152 void vBase::AddAttr(vAttrPtr attr)
154 Trace trace("vBase::AddAttr");
156 if( !attr.Get() ) {
157 trace.log("attribute contains no data, skipping");
158 return;
161 vformat_add_attribute(m_format, attr.Extract());
164 void vBase::AddValue(vAttrPtr &attr, const char *value)
166 Trace trace("vBase::AddValue");
167 if( !attr.Get() ) {
168 trace.log("attribute pointer contains no data, skipping");
169 return;
172 if( strlen(value) == 0 ) {
173 trace.log("attribute value is empty, skipping");
174 return;
177 vformat_attribute_add_value(attr.Get(), value);
180 void vBase::AddParam(vAttrPtr &attr, const char *name, const char *value)
182 Trace trace("vBase::AddParam");
184 if( !attr.Get() ) {
185 trace.log("attribute pointer contains no data, skipping");
186 return;
189 if( strlen(value) == 0 ) {
190 trace.log("parameter value is empty, skipping");
191 return;
195 VFormatParam *pParam = vformat_attribute_param_new(name);
196 vformat_attribute_param_add_value(pParam, value);
197 vformat_attribute_add_param(attr.Get(), pParam);
200 std::string vBase::GetAttr(const char *attrname, const char *block)
202 Trace trace("vBase::GetAttr");
203 trace.logf("getting attr: %s", attrname);
205 std::string ret;
206 const char *value = 0;
208 bool needs_freeing = false;
210 VFormatAttribute *attr = vformat_find_attribute(m_format, attrname, 0, block);
211 if( attr ) {
212 if( vformat_attribute_is_single_valued(attr) ) {
213 value = vformat_attribute_get_value(attr);
214 needs_freeing = true;
216 else {
217 // FIXME, this is hardcoded
218 value = vformat_attribute_get_nth_value(attr, 0);
222 if( value )
223 ret = value;
225 if( needs_freeing )
226 g_free((char *)value);
228 trace.logf("attr value: %s", ret.c_str());
229 return ret;
232 vAttr vBase::GetAttrObj(const char *attrname, int nth, const char *block)
234 Trace trace("vBase::GetAttrObj");
235 trace.logf("getting attr: %s", attrname);
237 return vAttr(vformat_find_attribute(m_format, attrname, nth, block));