# 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-2011, 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 { namespace Sync {
    00032
    00033 //
    00034 // vTimeZone
    00035 //
    00036 /// A virtual base class that the plugins may override, to do
    00037 /// time related conversions.  Default implementations for these
    00038 /// functions are provided, but may be overrided depending on need.
    00039 ///
    00040 /// We do this in a "callback" style, so that it doesn't matter what
    00041 /// version of the opensync library we link against, in case the
    00042 /// user wishes to use the opensync time functions.
    00043 ///
    00044 class BXEXPORT vTimeConverter
    00045 {
    00046 public:
    00047         virtual ~vTimeConverter() {}
    00048
    00049         /// Convert a time_t into an ISO timestamp string
    00050         /// Throws Barry::ConvertError on error, but these errors
    00051         /// must be rare.
    00052         virtual std::string unix2vtime(const time_t *timestamp);
    00053
    00054         /// Convert an ISO timestamp string into a time_t, using
    00055         /// the current system timezone if vtime is not in UTC.
    00056         /// Returns (time_t)-1 on error.
    00057         virtual time_t vtime2unix(const char *vtime);
    00058
    00059         /// Convert a VEVENT alarm duration string in the format
    00060         /// of "[+-]P.W.DT.H.M.S" where the periods represent numbers
    00061         /// and each letter besides P and T represent Week, Day,
    00062         /// Hour, Minute, and Second respectively.
    00063         virtual int alarmduration2sec(const char *alarm);
    00064 };
    00065
    00066
    00067 typedef Barry::vSmartPtr<b_VFormatAttribute, b_VFormatAttribute, &b_vformat_attribute_free> vAttrPtr;
    00068 typedef Barry::vSmartPtr<b_VFormatParam, b_VFormatParam, &b_vformat_attribute_param_free> vParamPtr;
    00069 typedef Barry::vSmartPtr<char, void, &g_free> gStringPtr;
    00070
    00071
    00072 //
    00073 // vAttr
    00074 //
    00075 /// Class for reading a b_VFormatAttribute.  Reading does not require
    00076 /// memory management, so none is done.
    00077 ///
    00078 class BXEXPORT vAttr
    00079 {
    00080         b_VFormatAttribute *m_attr;
    00081
    00082 public:
    00083         vAttr()
    00084                 : m_attr(0)
    00085         {
    00086         }
    00087
    00088         vAttr(b_VFormatAttribute *attr)
    00089                 : m_attr(attr)
    00090         {
    00091         }
    00092
    00093         vAttr& operator=(b_VFormatAttribute *attr)
    00094         {
    00095                 m_attr = attr;
    00096                 return *this;
    00097         }
    00098
    00099         b_VFormatAttribute* Get() { return m_attr; }
    00100
    00101         // These functions do not throw an error if the value
    00102         // is NULL or does not exist (for example, if you ask for
    00103         // value #5 and there are only 4).
    00104         std::string GetName();
    00105         std::string GetValue(int nth = 0);
    00106         std::string GetDecodedValue();
    00107         std::string GetParam(const char *name, int nth = 0);
    00108         std::string GetAllParams(const char *name);
    00109 };
    00110
    00111
    00112 //
    00113 // vBase
    00114 //
    00115 /// Base class containing vformat helper API.
    00116 ///
    00117 class BXEXPORT vBase
    00118 {
    00119         // internal data for managing the vformat
    00120         b_VFormat *m_format;
    00121
    00122 public:
    00123 protected:
    00124         vBase();
    00125         explicit vBase(b_VFormat *format);
    00126         virtual ~vBase();
    00127
    00128         b_VFormat* Format() { return m_format; }
    00129         const b_VFormat* Format() const { return m_format; }
    00130         void SetFormat(b_VFormat *format);
    00131
    00132         void Clear();
    00133
    00134         vAttrPtr NewAttr(const char *name);
    00135         vAttrPtr NewAttr(const char *name, const char *value);
    00136         void AddAttr(vAttrPtr attr);
    00137         void AddValue(vAttrPtr &attr, const char *value);
    00138         void AddEncodedValue(vAttrPtr &attr, b_VFormatEncoding encoding, const char *value, int len);
    00139         void AddParam(vAttrPtr &attr, const char *name, const char *value);
    00140
    00141         std::string GetAttr(const char *attrname, const char *block = 0);
    00142         std::vector<std::string> GetValueVector(const char *attrname, const char *block = 0);
    00143         vAttr GetAttrObj(const char *attrname, int nth = 0, const char *block = 0);
    00144
    00145         std::vector<std::string> Tokenize(const std::string &str, const char delim = ',');
    00146         std::string ToStringList(const std::vector<std::string> &list, const char delim = ',');
    00147 };
    00148
    00149 }} // namespace Barry::Sync
    00150
    00151 #endif
    00152