File indexing completed on 2023-09-24 04:17:34

0001 //
0002 // C++ Implementation: stringsplugin
0003 //
0004 // Description: Strings plugin.
0005 //
0006 /*
0007 Copyright 2007-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "stringsplugin.h"
0024 
0025 #include "cmacromanager.h"
0026 
0027 #include <kpluginfactory.h>
0028 #include <kpluginloader.h>
0029 
0030 K_PLUGIN_FACTORY (cStringsPluginFactory, registerPlugin<cStringsPlugin>();)
0031 K_EXPORT_PLUGIN (cStringsPluginFactory("kmuddy"))
0032 
0033     
0034 // all function implementations start here:
0035 
0036 class cFunctionLower: public cFunction {
0037   public:
0038     cFunctionLower () : cFunction ("lower") {}
0039     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0040       if (params.size() < 1) return cValue::empty();
0041       std::list<cValue>::iterator it = params.begin();
0042       QString ret = (*it).asString().toLower();
0043       return cValue (ret);
0044     }
0045 };
0046 
0047 class cFunctionUpper: public cFunction {
0048   public:
0049     cFunctionUpper () : cFunction ("upper") {}
0050     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0051       if (params.size() < 1) return cValue::empty();
0052       std::list<cValue>::iterator it = params.begin();
0053       QString ret = (*it).asString().toUpper();
0054       return cValue (ret);
0055     }
0056 };
0057 
0058 class cFunctionCaps: public cFunction {
0059   public:
0060     cFunctionCaps () : cFunction ("caps") {}
0061     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0062       if (params.size() < 1) return cValue::empty();
0063       std::list<cValue>::iterator it = params.begin();
0064       QString ret = (*it).asString().toLower();
0065       bool wasws = true;
0066       for (int i = 0; i < ret.length(); ++i)
0067         if (!ret[i].isLetterOrNumber())
0068           wasws = true;
0069         else
0070           if (wasws) {
0071             ret[i] = ret[i].toUpper();
0072             wasws = false;
0073           }
0074       return cValue (ret);
0075     }
0076 };
0077 
0078 class cFunctionLeft: public cFunction {
0079   public:
0080     cFunctionLeft () : cFunction ("left") {}
0081     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0082       if (params.size() < 1) return cValue::empty();
0083       std::list<cValue>::iterator it = params.begin();
0084       QString ret = (*it).asString();
0085       if (params.size() < 2) return ret;
0086       ++it;
0087       int chars = (*it).asInteger();
0088       ret = ret.left (chars);
0089       return cValue (ret);
0090     }
0091 };
0092 
0093 class cFunctionRight: public cFunction {
0094   public:
0095     cFunctionRight () : cFunction ("right") {}
0096     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0097       if (params.size() < 1) return cValue::empty();
0098       std::list<cValue>::iterator it = params.begin();
0099       QString ret = (*it).asString();
0100       if (params.size() < 2) return ret;
0101       ++it;
0102       int chars = (*it).asInteger();
0103       ret = ret.right (chars);
0104       return cValue (ret);
0105     }
0106 };
0107 
0108 class cFunctionMid: public cFunction {
0109   public:
0110     cFunctionMid () : cFunction ("mid") {}
0111     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0112       if (params.size() < 1) return cValue::empty();
0113       std::list<cValue>::iterator it = params.begin();
0114       QString ret = (*it).asString();
0115       if (params.size() < 2) return ret;
0116       ++it;
0117       int chars = (*it).asInteger();
0118       if (params.size() >= 3) {
0119         ++it;
0120         int len = (*it).asInteger();
0121         ret = ret.mid (chars, len);
0122       } else
0123         ret = ret.mid (chars);
0124       return cValue (ret);
0125     }
0126 };
0127 
0128 class cFunctionStrlen: public cFunction {
0129   public:
0130     cFunctionStrlen () : cFunction ("strlen") {}
0131     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0132       if (params.size() < 1) return cValue::empty();
0133       std::list<cValue>::iterator it = params.begin();
0134       int ret = (*it).asString().length();
0135       return cValue (ret);
0136     }
0137 };
0138 
0139 class cFunctionTrim: public cFunction {
0140   public:
0141     cFunctionTrim () : cFunction ("trim") {}
0142     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0143       if (params.size() < 1) return cValue::empty();
0144       std::list<cValue>::iterator it = params.begin();
0145       QString ret = (*it).asString().trimmed();
0146       return cValue (ret);
0147     }
0148 };
0149 
0150 class cFunctionStrpos: public cFunction {
0151   public:
0152     cFunctionStrpos () : cFunction ("strpos") {}
0153     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0154       if (params.size() < 2) return cValue::empty();
0155       std::list<cValue>::iterator it = params.begin();
0156       QString haystack = (*it).asString();
0157       ++it;
0158       QString needle = (*it).asString();
0159       return cValue (haystack.indexOf (needle));
0160     }
0161 };
0162 
0163 class cFunctionStrfill: public cFunction {
0164   public:
0165     cFunctionStrfill () : cFunction ("strfill") {}
0166     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0167       if (params.size() < 1) return cValue::empty();
0168       std::list<cValue>::iterator it = params.begin();
0169       QString ret = (*it).asString();
0170       if (params.size() < 3) return ret;
0171       ++it;
0172       QChar character = (*it).asString()[0];
0173       ++it;
0174       int len = (*it).asInteger();
0175       
0176       if (len <= ret.length())
0177         return cValue (ret);
0178       
0179       QString filler;
0180       filler.fill (character, len - ret.length());
0181       return cValue (ret + filler);
0182     }
0183 };
0184 
0185 class cFunctionStrleftfill: public cFunction {
0186   public:
0187     cFunctionStrleftfill () : cFunction ("strleftfill") {}
0188     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0189       // same as strfill, except for the last line
0190       if (params.size() < 1) return cValue::empty();
0191       std::list<cValue>::iterator it = params.begin();
0192       QString ret = (*it).asString();
0193       if (params.size() < 3) return ret;
0194       ++it;
0195       QChar character = (*it).asString()[0];
0196       ++it;
0197       int len = (*it).asInteger();
0198       
0199       if (len <= ret.length())
0200         return cValue (ret);
0201       
0202       QString filler;
0203       filler.fill (character, len - ret.length());
0204       return cValue (filler + ret);
0205     }
0206 };
0207 
0208 class cFunctionStrcat: public cFunction {
0209   public:
0210     cFunctionStrcat () : cFunction ("strcat") {}
0211     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0212       if (params.size() < 1) return cValue::empty();
0213       std::list<cValue>::iterator it;
0214       QString ret;
0215       for (it = params.begin(); it != params.end(); ++it)
0216         ret += (*it).asString();
0217       return cValue (ret);
0218     }
0219 };
0220 
0221 class cFunctionJoin: public cFunction {
0222   public:
0223     cFunctionJoin () : cFunction ("join") {}
0224     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0225       if (params.size() < 1) return cValue::empty();
0226       std::list<cValue>::iterator it = params.begin();
0227       cValue list = *it;
0228       QString sep = "|";
0229       if (params.size() >= 2)
0230       {
0231         ++it;
0232         sep = (*it).asString();
0233       }
0234       return list.listJoin (sep);
0235     }
0236 };
0237 
0238 class cFunctionExplode: public cFunction {
0239   public:
0240     cFunctionExplode () : cFunction ("explode") {}
0241     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0242       if (params.size() < 1) return cValue::empty();
0243       std::list<cValue>::iterator it = params.begin();
0244       cValue str = *it;
0245       QString sep = "|";
0246       if (params.size() >= 2)
0247       {
0248         ++it;
0249         sep = (*it).asString();
0250       }
0251       return str.toList (sep);
0252     }
0253 };
0254 
0255 class cFunctionReplace: public cFunction {
0256   public:
0257     cFunctionReplace () : cFunction ("replace") {}
0258     cValue eval (std::list<cValue> &params, int, cCmdQueue *) override {
0259       if (params.size() < 3) return cValue::empty();
0260       std::list<cValue>::iterator it = params.begin();
0261       QString str = (*it).asString();
0262       ++it;
0263       QString from = (*it).asString();
0264       ++it;
0265       QString to = (*it).asString();
0266       str.replace (from, to);
0267       return cValue (str);
0268     }
0269 };
0270 
0271 
0272 // the main plug-in code which registers the functions:
0273 
0274 struct cStringsPluginPrivate {
0275   cFunctionLower *flower;
0276   cFunctionUpper *fupper;
0277   cFunctionCaps *fcaps;
0278   cFunctionLeft *fleft;
0279   cFunctionRight *fright;
0280   cFunctionMid *fmid;
0281   cFunctionStrlen *fstrlen;
0282   cFunctionTrim *ftrim;
0283   cFunctionStrpos *fstrpos;
0284   cFunctionStrfill *fstrfill;
0285   cFunctionStrleftfill *fstrleftfill;
0286   cFunctionStrcat *fstrcat;
0287   cFunctionJoin *fjoin;
0288   cFunctionExplode *fexplode;
0289   cFunctionReplace *freplace;
0290 };
0291 
0292 cStringsPlugin::cStringsPlugin (QObject *, const QVariantList &)
0293 {
0294   d = new cStringsPluginPrivate;
0295   registerFunctions ();
0296 }
0297 
0298 cStringsPlugin::~cStringsPlugin()
0299 {
0300   unregisterFunctions ();
0301   delete d;
0302 }
0303 
0304 void cStringsPlugin::registerFunctions ()
0305 {
0306   d->flower = new cFunctionLower;
0307   d->fupper = new cFunctionUpper;
0308   d->fcaps = new cFunctionCaps;
0309   d->fleft = new cFunctionLeft;
0310   d->fright = new cFunctionRight;
0311   d->fmid = new cFunctionMid;
0312   d->fstrlen = new cFunctionStrlen;
0313   d->ftrim = new cFunctionTrim;
0314   d->fstrpos = new cFunctionStrpos;
0315   d->fstrfill = new cFunctionStrfill;
0316   d->fstrleftfill = new cFunctionStrleftfill;
0317   d->fstrcat = new cFunctionStrcat;
0318   d->fjoin = new cFunctionJoin;
0319   d->fexplode = new cFunctionExplode;
0320   d->freplace = new cFunctionReplace;
0321 }
0322 
0323 void cStringsPlugin::unregisterFunctions ()
0324 {
0325   delete d->flower;
0326   delete d->fupper;
0327   delete d->fcaps;
0328   delete d->fleft;
0329   delete d->fright;
0330   delete d->fmid;
0331   delete d->fstrlen;
0332   delete d->ftrim;
0333   delete d->fstrpos;
0334   delete d->fstrfill;
0335   delete d->fstrleftfill;
0336   delete d->fstrcat;
0337   delete d->fjoin;
0338   delete d->fexplode;
0339   delete d->freplace;
0340 }
0341 
0342 
0343 #include "stringsplugin.moc"