00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #ifndef EXPANDLIB_CMD_BTRUNC_H_INCLUDED
00031 #define EXPANDLIB_CMD_BTRUNC_H_INCLUDED
00032
00033 #include <boost/lexical_cast.hpp>
00034
00035 #include "expandlib-cmd.h"
00036
00037 namespace expander {
00038
00040
00044 template<
00045
00046 class StringType = ::std::string
00047 >
00048 class expcmd_btrunc : public expandcmd<StringType> {
00049 protected:
00050 public:
00052
00055 expcmd_btrunc(expandable<StringType> * const eobj) : expandcmd<StringType>(eobj, _T("btrunc")) {};
00056
00058 virtual void operator()(StringType &dest, const FParamVectorType ¶ms, const FVarMapType &vars, ExpandResultsType &results) const
00059 {
00060 if ( params.size() == 2 || params.size() == 3 ) {
00061 StringType d;
00062 expobj->expand(d, params[0], vars, results);
00063
00064 try {
00065 const StringType::size_type num=boost::lexical_cast<StringType::size_type>(params[1]);
00066
00067 if ( d.size() > num ) {
00068 if ( params.size() == 3 ) {
00069 StringType e;
00070 expobj->expand(e, params[2], vars, results);
00071 if ( e.size() >= num ) e=_T("");
00072 dest+=e;
00073 dest.append(d.end()-(num-e.size()), d.end());
00074 } else {
00075 dest.append(d.end()-num, d.end());
00076 }
00077 } else {
00078 dest+=d;
00079 }
00080 } catch(boost::bad_lexical_cast) {
00081 throw expand_error(_T("expcmd_btrunc"),_T("Illegal size"),params[1].c_str());
00082 }
00083 } else {
00084 throw expand_error(_T("expcmd_btrunc"),_T("Parameter error"));
00085 }
00086 }
00087 };
00088
00089 }
00090
00091 #endif //EXPANDLIB_CMD_BTRUNC_H_INCLUDED