Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

expandlib-vartype-date.h

Go to the documentation of this file.
00001 
00002 /* This file is part of Expandlib.
00003 
00004   Copyright (C) 2002 Jasper van de Gronde
00005 
00006   This software is provided 'as-is', without any express or implied
00007   warranty.  In no event will the authors be held liable for any damages
00008   arising from the use of this software.
00009 
00010   Permission is granted to anyone to use this software for any purpose,
00011   including commercial applications, and to alter it and redistribute it
00012   freely, subject to the following restrictions:
00013 
00014   1. The origin of this software must not be misrepresented; you must not
00015      claim that you wrote the original software. If you use this software
00016      in a product, an acknowledgment in the product documentation would be
00017      appreciated but is not required.
00018   2. Altered source versions must be plainly marked as such, and must not be
00019      misrepresented as being the original software.
00020   3. This notice may not be removed or altered from any source distribution.
00021 
00022   Jasper van de Gronde th.v.d.gronde@hccnet.nl
00023 
00024 */
00025 
00030 #ifndef EXPANDLIB_VARTYPE_DATE_H_INCLUDED
00031 #define EXPANDLIB_VARTYPE_DATE_H_INCLUDED
00032 
00033 #include <boost/lexical_cast.hpp>
00034 
00035 #include "expandlib-vartype.h"
00036 
00037 namespace expander { // *** Start of namespace
00038 
00040 
00045     template<
00046         // Used for the expandable string (and the expanded string)
00047         class StringType = ::std::string
00048         >
00049     class expvartype_date : public expandvartype<StringType> {
00050     protected:
00051 
00053         class DateType {
00054         public:
00055             DateType(int y=0, int m=0, int d=0) { year=y; month=m; day=d; }
00056 
00057             int year;
00058             int month;
00059             int day;
00060         } Date;
00061 
00063         inline StringType PadWithZeroes(const StringType &s, const unsigned int num) const {
00064             const StringType::value_type padchar(_T('0'));
00065 
00066             if ( s.size() < num ) {
00067                 StringType d(num-s.size(), padchar);
00068                 //for(unsigned int u=0; u<(num-s.size()); ++u) d+=padchar;
00069                 d+=s;
00070                 return d;
00071             } else {
00072                 return s;
00073             }
00074         }
00075 
00077         inline void PadWithZeroesTo(StringType &d, const StringType &s, const unsigned int num) const {
00078             const StringType::value_type padchar(_T('0'));
00079 
00080             if ( s.size() < num ) {
00081                 d.append(num-s.size(), padchar);
00082                 d+=s;
00083             } else {
00084                 d+=s;
00085             }
00086         }
00087 
00088     public:
00089 
00090 //      /// Default contructor, inits to 0:0:0.0
00091 //      expvartype_date() {}
00092 
00094         expvartype_date(int y=0, int m=0, int d=0) : Time(y,m,d) {}
00095 
00097         expvartype_date(const _TCHAR * cs) {
00098             const StringType s(cs);
00099             SetValue(s);
00100         }
00101 
00103         expvartype_date(const StringType s) {
00104             SetValue(s);
00105         }
00106 
00107         virtual void AppendValue(StringType &dest) const {
00108             try {
00109                 dest+=boost::lexical_cast<StringType>(Date.year);
00110             } catch(...) {
00111                 dest+=_T("0000");
00112             }
00113             dest+=_T('-');
00114             try {
00115                 PadWithZeroesTo(dest, boost::lexical_cast<StringType>(Date.month),2);
00116             } catch(...) {
00117                 dest+=_T("00");
00118             }
00119             dest+=_T('-');
00120             try {
00121                 PadWithZeroesTo(dest, boost::lexical_cast<StringType>(Date.day),2);
00122             } catch(...) {
00123                 dest+=_T("00");
00124             }
00125         }
00126 
00127         virtual void AppendValue(StringType &dest, const FParamVectorType &params, const FVarMapType &vars, ExpandResultsType &results) const {
00128             AppendValue(dest);
00129         }
00130 
00131         virtual void SetValue(const StringType &s) {
00132             std::vector<int> vals;
00133             {
00134                 StringType temp;
00135                 for(StringType::const_iterator it=s.begin(); it!=s.end(); ++it) {
00136                     if ( *it >= _T('0') && *it <= _T('9') ) {
00137                         temp+=*it;
00138                     } else if ( *it == _T('-') ) {
00139                         try {
00140                             vals.push_back(boost::lexical_cast<int>(temp));
00141                         } catch (boost::bad_lexical_cast) {
00142                             vals.push_back(0);
00143                         }
00144                         temp=_T("");
00145                     } else {
00146                         break;
00147                     }
00148                 }
00149                 try {
00150                     vals.push_back(boost::lexical_cast<int>(temp));
00151                 } catch (boost::bad_lexical_cast) {
00152                     vals.push_back(0);
00153                 }
00154             }
00155 
00156             if ( vals.size() == 3 ) {
00157                 Date.year=vals[0];
00158                 Date.month=vals[1];
00159                 Date.day=vals[2];
00160                 if ( Date.month > 12 ) Date.month=12;
00161                 if ( Date.month < 1 ) Date.month=1;
00162                 if ( Date.day > 31 ) Date.day=31;
00163                 if ( Date.day < 1 ) Date.day=1;
00164             }
00165         }
00166 
00168         inline StringType get_string() {
00169             StringType s;
00170             AppendValue(s);
00171             return s;
00172         }
00173     };
00174 
00175 } // *** End of namespace
00176 
00177 #endif //EXPANDLIB_VARTYPE_DATE_H_INCLUDED

Generated on Tue Feb 4 17:24:13 2003 for ExpandLib by doxygen1.3-rc2