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

expandlib-vartype-time.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_TIME_H_INCLUDED
00031 #define EXPANDLIB_VARTYPE_TIME_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         long NotimeMSValue = -1
00049         >
00050     class expvartype_time : public expandvartype<StringType> {
00051     protected:
00052 
00054         class TimeType {
00055         public:
00056             TimeType(int H=0, int M=0, int S=0, int MS=0) { h=H; m=M; s=S; ms=MS; }
00057 
00058             int h; 
00059             int m; 
00060             int s; 
00061             int ms; 
00062         } Time;
00063 
00065         inline StringType PadWithZeroes(const StringType &s, const unsigned int num) const {
00066             const StringType::value_type padchar('0');
00067             StringType d;
00068 
00069             if ( s.size() < num ) {
00070                 for(unsigned int u=0; u<(num-s.size()); ++u) d+=padchar;
00071                 d+=s;
00072             } else {
00073                 d=s;
00074             }
00075 
00076             return d;
00077         }
00078 
00080         inline void PadWithZeroesTo(StringType &d, const StringType &s, const unsigned int num) const {
00081             const StringType::value_type padchar(_T('0'));
00082 
00083             if ( s.size() < num ) {
00084                 d.append(num-s.size(), padchar);
00085                 d+=s;
00086             } else {
00087                 d+=s;
00088             }
00089         }
00090 
00092         bool notime;
00093 
00094     public:
00095 
00096 //      /// Default contructor, inits to 0:0:0.0
00097 //      expvartype_time() {}
00098 
00100         expvartype_time(int H=0, int M=0, int S=0, int MS=0, bool nt=false) : Time(H,M,S,MS) { notime=nt; }
00101 
00103         expvartype_time(const _TCHAR * cs) {
00104             const StringType s(cs);
00105             SetValue(s);
00106         }
00107 
00109         expvartype_time(const StringType s) {
00110             SetValue(s);
00111         }
00112 
00113         virtual void AppendValue(StringType &dest) const {
00114             if ( notime ) {
00115                 dest+=_T('-');
00116                 return;
00117             }
00118             try {
00119                 PadWithZeroesTo(dest, boost::lexical_cast<StringType>(Time.h),2);
00120             } catch(...) {
00121                 dest+=_T("00");
00122             }
00123             dest+=_T(':');
00124             try {
00125                 PadWithZeroesTo(dest, boost::lexical_cast<StringType>(Time.m),2);
00126             } catch(...) {
00127                 dest+=_T("00");
00128             }
00129             dest+=_T(':');
00130             try {
00131                 PadWithZeroesTo(dest, boost::lexical_cast<StringType>(Time.s),2);
00132             } catch(...) {
00133                 dest+=_T("00");
00134             }
00135             if ( Time.ms >= 100 ) {
00136                 dest+=_T('.');
00137                 try {
00138                     if ( Time.ms % 100 == 0 ) {
00139                         dest+=boost::lexical_cast<StringType>(Time.ms/100);
00140                     } else if ( Time.ms % 10 == 0 ) {
00141                         dest+=boost::lexical_cast<StringType>(Time.ms/10);
00142                     } else {
00143                         dest+=boost::lexical_cast<StringType>(Time.ms);
00144                     }
00145                 } catch(...) {
00146                     dest+=_T('0');
00147                 }
00148             } else if ( Time.ms >= 10 ) {
00149                 dest+=_T('.');
00150                 try {
00151                     if ( Time.ms % 10 == 0 ) {
00152                         dest+=PadWithZeroes(boost::lexical_cast<StringType>(Time.ms/10),2);
00153                     } else {
00154                         dest+=PadWithZeroes(boost::lexical_cast<StringType>(Time.ms),3);
00155                     }
00156                 } catch(...) {
00157                     dest+=_T('0');
00158                 }
00159             } else if ( Time.ms >= 1 ) {
00160                 dest+=_T('.');
00161                 try {
00162                     PadWithZeroesTo(dest, boost::lexical_cast<StringType>(Time.ms),3);
00163                 } catch(...) {
00164                     dest+=_T('0');
00165                 }
00166             }
00167         }
00168 
00169         virtual void AppendValue(StringType &dest, const FParamVectorType &params, const FVarMapType &vars, ExpandResultsType &results) const {
00170             AppendValue(dest);
00171         }
00172 
00173         virtual void SetValue(const StringType &s) {
00174             if ( s == _T("-") ) {
00175                 Time.h=0;
00176                 Time.m=0;
00177                 Time.s=0;
00178                 Time.ms=0;
00179                 notime=true;
00180                 return;
00181             } else {
00182                 notime=false;
00183             }
00184             std::vector<int> vals;
00185             bool dot=false;
00186             {
00187                 StringType temp;
00188                 for(StringType::const_iterator it(s.begin()); it!=s.end(); ++it) {
00189                     if ( dot ) {
00190                         if ( *it >= _T('0') && *it <= _T('9') ) {
00191                             temp+=*it;
00192                         } else {
00193                             break;
00194                         }
00195                     } else {
00196                         if ( *it >= _T('0') && *it <= _T('9') ) {
00197                             temp+=*it;
00198                         } else if ( *it == _T(':') || *it == _T('.') ) {
00199                             try {
00200                                 vals.push_back(boost::lexical_cast<int>(temp));
00201                             } catch(boost::bad_lexical_cast) {
00202                                 vals.push_back(0);
00203                             }
00204                             temp=_T("");
00205                             if ( *it == _T('.') ) dot=true;
00206                         } else {
00207                             break;
00208                         }
00209                     }
00210                 }
00211                 if ( dot ) {
00212                     if ( temp.size() > 3 ) {
00213                         temp.resize(3);
00214                     } else {
00215                         temp.append(3-temp.size(), _T('0'));
00216                     }
00217                     try {
00218                         vals.push_back(boost::lexical_cast<int>(temp));
00219                     } catch (boost::bad_lexical_cast) {
00220                         vals.push_back(0);
00221                     }
00222                 } else {
00223                     try {
00224                         vals.push_back(boost::lexical_cast<int>(temp));
00225                     } catch (boost::bad_lexical_cast) {
00226                         vals.push_back(0);
00227                     }
00228                 }
00229             }
00230 
00231             if ( vals.size() == (unsigned int)3 + (dot?1:0) ) {
00232                     Time.h=vals[0];
00233                     Time.m=vals[1];
00234                     Time.s=vals[2];
00235                     if ( dot ) Time.ms=vals[3]; else Time.ms=0;
00236             } else if ( vals.size() == (unsigned int)2 + (dot?1:0) ) {
00237                     Time.h=0;
00238                     Time.m=vals[0];
00239                     Time.s=vals[1];
00240                     if ( dot ) Time.ms=vals[2]; else Time.ms=0;
00241             } else if ( vals.size() == (unsigned int)1 + (dot?1:0) ) {
00242                     Time.h=0;
00243                     Time.m=0;
00244                     Time.s=vals[0];
00245                     if ( dot ) Time.ms=vals[1]; else Time.ms=0;
00246             } else {
00247                     Time.h=0;
00248                     Time.m=0;
00249                     Time.s=0;
00250                     Time.ms=0;
00251             }
00252             if ( Time.h == 24 ) Time.h=0;
00253             if ( Time.h > 23 ) Time.h=23;
00254             if ( Time.h < 0 ) Time.h=0;
00255             if ( Time.m > 59 ) Time.m=59;
00256             if ( Time.m < 0 ) Time.m=0;
00257             if ( Time.s > 59 ) Time.s=59;
00258             if ( Time.s < 0 ) Time.s=0;
00259             if ( Time.ms > 999 ) Time.ms=999;
00260             if ( Time.ms < 0 ) Time.ms=0;
00261         }
00262 
00264         inline long get_milliseconds() { return (notime?NotimeMSValue:(Time.h*3600000)+(Time.m*60000)+(Time.s*1000)+Time.ms); }
00265 
00267         inline StringType get_string() {
00268             StringType s;
00269             AppendValue(s);
00270             return s;
00271         }
00272     };
00273 
00274 } // *** End of namespace
00275 
00276 #endif //EXPANDLIB_VARTYPE_TIME_H_INCLUDED

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