3 // vformat support routines in base class
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.
24 #include "vformat.h" // comes from opensync, but not a public header yet
30 //////////////////////////////////////////////////////////////////////////////
33 std::string
vAttr::GetName()
40 const char *name
= vformat_attribute_get_name(m_attr
);
46 std::string
vAttr::GetValue(int nth
)
49 const char *value
= 0;
52 if( vformat_attribute_is_single_valued(m_attr
) ) {
53 value
= vformat_attribute_get_value(m_attr
);
56 value
= vformat_attribute_get_nth_value(m_attr
, nth
);
66 std::string
vAttr::GetParam(const char *name
, int nth
)
73 VFormatParam
*param
= vformat_attribute_find_param(m_attr
, name
);
77 const char *value
= vformat_attribute_param_get_nth_value(param
, nth
);
85 //////////////////////////////////////////////////////////////////////////////
96 vformat_free(m_format
);
100 void vBase::SetFormat(VFormat
*format
)
103 vformat_free(m_format
);
112 vformat_free(m_format
);
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
));
125 throw ConvertError("resource error allocating vformat attribute");
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);
142 trace
.logf("creating attr: %s, %s", name
, value
);
144 vAttrPtr
attr(vformat_attribute_new(NULL
, name
));
146 throw ConvertError("resource error allocating vformat attribute");
148 vformat_attribute_add_value(attr
.Get(), value
);
152 void vBase::AddAttr(vAttrPtr attr
)
154 Trace
trace("vBase::AddAttr");
157 trace
.log("attribute contains no data, skipping");
161 vformat_add_attribute(m_format
, attr
.Extract());
164 void vBase::AddValue(vAttrPtr
&attr
, const char *value
)
166 Trace
trace("vBase::AddValue");
168 trace
.log("attribute pointer contains no data, skipping");
172 if( strlen(value) == 0 ) {
173 trace.log("attribute value is empty, skipping");
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");
185 trace
.log("attribute pointer contains no data, skipping");
189 if( strlen(value) == 0 ) {
190 trace.log("parameter value is empty, skipping");
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
);
206 const char *value
= 0;
208 bool needs_freeing
= false;
210 VFormatAttribute
*attr
= vformat_find_attribute(m_format
, attrname
, 0, block
);
212 if( vformat_attribute_is_single_valued(attr
) ) {
213 value
= vformat_attribute_get_value(attr
);
214 needs_freeing
= true;
217 // FIXME, this is hardcoded
218 value
= vformat_attribute_get_nth_value(attr
, 0);
226 g_free((char *)value
);
228 trace
.logf("attr value: %s", ret
.c_str());
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
));