vbase.h

Go to the documentation of this file.
00001 ///
00002 /// \file       vbase.h
00003 ///             Base class for vformat support
00004 ///
00005 
00006 /*
00007     Copyright (C) 2006-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __BARRY_SYNC_VBASE_H__
00023 #define __BARRY_SYNC_VBASE_H__
00024 
00025 #include "dll.h"
00026 #include "vsmartptr.h"
00027 #include "vformat.h"
00028 #include "error.h"
00029 #include <vector>
00030 
00031 namespace Barry {
00032         class CategoryList;
00033 }
00034 
00035 namespace Barry { namespace Sync {
00036 
00037 //
00038 // vTimeConverter
00039 //
00040 /// A virtual base class that the plugins may override, to do
00041 /// time related conversions.  Default implementations for these
00042 /// functions are provided, but may be overrided depending on need.
00043 ///
00044 /// We do this in a "callback" style, so that it doesn't matter what
00045 /// version of the opensync library we link against, in case the
00046 /// user wishes to use the opensync time functions.
00047 ///
00048 class BXEXPORT vTimeConverter
00049 {
00050 public:
00051         virtual ~vTimeConverter() {}
00052 
00053         /// Convert a time_t into an ISO timestamp string
00054         /// Throws Barry::ConvertError on error, but these errors
00055         /// must be rare.
00056         virtual std::string unix2vtime(const time_t *timestamp);
00057 
00058         /// Convert an ISO timestamp string into a time_t, using
00059         /// the current system timezone if vtime is not in UTC.
00060         /// Returns (time_t)-1 on error.
00061         virtual time_t vtime2unix(const char *vtime);
00062 
00063         /// Convert a VEVENT alarm duration string in the format
00064         /// of "[+-]P.W.DT.H.M.S" where the periods represent numbers
00065         /// and each letter besides P and T represent Week, Day,
00066         /// Hour, Minute, and Second respectively.
00067         virtual int alarmduration2sec(const char *alarm);
00068 };
00069 
00070 
00071 typedef Barry::vSmartPtr<b_VFormatAttribute, b_VFormatAttribute, &b_vformat_attribute_free> vAttrPtr;
00072 typedef Barry::vSmartPtr<b_VFormatParam, b_VFormatParam, &b_vformat_attribute_param_free> vParamPtr;
00073 typedef Barry::vSmartPtr<char, void, &g_free> gStringPtr;
00074 
00075 
00076 //
00077 // vAttr
00078 //
00079 /// Class for reading a b_VFormatAttribute.  Reading does not require
00080 /// memory management, so none is done.
00081 ///
00082 class BXEXPORT vAttr
00083 {
00084         b_VFormatAttribute *m_attr;
00085 
00086 public:
00087         vAttr()
00088                 : m_attr(0)
00089         {
00090         }
00091 
00092         vAttr(b_VFormatAttribute *attr)
00093                 : m_attr(attr)
00094         {
00095         }
00096 
00097         vAttr& operator=(b_VFormatAttribute *attr)
00098         {
00099                 m_attr = attr;
00100                 return *this;
00101         }
00102 
00103         b_VFormatAttribute* Get() { return m_attr; }
00104 
00105         // These functions do not throw an error if the value
00106         // is NULL or does not exist (for example, if you ask for
00107         // value #5 and there are only 4).
00108         std::string GetName();
00109         std::string GetValue(int nth = 0);
00110         std::string GetDecodedValue();
00111         std::string GetParam(const char *name, int nth = 0);
00112         std::string GetAllParams(const char *name);
00113 };
00114 
00115 
00116 //
00117 // vBase
00118 //
00119 /// Base class containing vformat helper API.
00120 ///
00121 class BXEXPORT vBase
00122 {
00123         // internal data for managing the vformat
00124         b_VFormat *m_format;
00125 
00126 public:
00127 protected:
00128         vBase();
00129         explicit vBase(b_VFormat *format);
00130         virtual ~vBase();
00131 
00132         b_VFormat* Format() { return m_format; }
00133         const b_VFormat* Format() const { return m_format; }
00134         void SetFormat(b_VFormat *format);
00135 
00136         void Clear();
00137 
00138         vAttrPtr NewAttr(const char *name);
00139         vAttrPtr NewAttr(const char *name, const char *value);
00140         void AddAttr(vAttrPtr attr);
00141         void AddValue(vAttrPtr &attr, const char *value);
00142         void AddEncodedValue(vAttrPtr &attr, b_VFormatEncoding encoding, const char *value, int len);
00143         void AddParam(vAttrPtr &attr, const char *name, const char *value);
00144 
00145         void AddCategories(const Barry::CategoryList &categories);
00146 
00147         std::string GetAttr(const char *attrname, const char *block = 0);
00148         std::vector<std::string> GetValueVector(const char *attrname, const char *block = 0);
00149         vAttr GetAttrObj(const char *attrname, int nth = 0, const char *block = 0);
00150 
00151         std::vector<std::string> Tokenize(const std::string &str, const char delim = ',');
00152         std::string ToStringList(const std::vector<std::string> &list, const char delim = ',');
00153 };
00154 
00155 }} // namespace Barry::Sync
00156 
00157 #endif
00158