File indexing completed on 2024-12-01 06:51:48
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_CLASS_WITH_JSON(cStringsPlugin, "stringsplugin.json") 0031 0032 0033 // all function implementations start here: 0034 0035 class cFunctionLower: public cFunction { 0036 public: 0037 cFunctionLower () : cFunction ("lower") {} 0038 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0039 if (params.size() < 1) return cValue::empty(); 0040 std::list<cValue>::iterator it = params.begin(); 0041 QString ret = (*it).asString().toLower(); 0042 return cValue (ret); 0043 } 0044 }; 0045 0046 class cFunctionUpper: public cFunction { 0047 public: 0048 cFunctionUpper () : cFunction ("upper") {} 0049 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0050 if (params.size() < 1) return cValue::empty(); 0051 std::list<cValue>::iterator it = params.begin(); 0052 QString ret = (*it).asString().toUpper(); 0053 return cValue (ret); 0054 } 0055 }; 0056 0057 class cFunctionCaps: public cFunction { 0058 public: 0059 cFunctionCaps () : cFunction ("caps") {} 0060 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0061 if (params.size() < 1) return cValue::empty(); 0062 std::list<cValue>::iterator it = params.begin(); 0063 QString ret = (*it).asString().toLower(); 0064 bool wasws = true; 0065 for (int i = 0; i < ret.length(); ++i) 0066 if (!ret[i].isLetterOrNumber()) 0067 wasws = true; 0068 else 0069 if (wasws) { 0070 ret[i] = ret[i].toUpper(); 0071 wasws = false; 0072 } 0073 return cValue (ret); 0074 } 0075 }; 0076 0077 class cFunctionLeft: public cFunction { 0078 public: 0079 cFunctionLeft () : cFunction ("left") {} 0080 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0081 if (params.size() < 1) return cValue::empty(); 0082 std::list<cValue>::iterator it = params.begin(); 0083 QString ret = (*it).asString(); 0084 if (params.size() < 2) return ret; 0085 ++it; 0086 int chars = (*it).asInteger(); 0087 ret = ret.left (chars); 0088 return cValue (ret); 0089 } 0090 }; 0091 0092 class cFunctionRight: public cFunction { 0093 public: 0094 cFunctionRight () : cFunction ("right") {} 0095 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0096 if (params.size() < 1) return cValue::empty(); 0097 std::list<cValue>::iterator it = params.begin(); 0098 QString ret = (*it).asString(); 0099 if (params.size() < 2) return ret; 0100 ++it; 0101 int chars = (*it).asInteger(); 0102 ret = ret.right (chars); 0103 return cValue (ret); 0104 } 0105 }; 0106 0107 class cFunctionMid: public cFunction { 0108 public: 0109 cFunctionMid () : cFunction ("mid") {} 0110 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0111 if (params.size() < 1) return cValue::empty(); 0112 std::list<cValue>::iterator it = params.begin(); 0113 QString ret = (*it).asString(); 0114 if (params.size() < 2) return ret; 0115 ++it; 0116 int chars = (*it).asInteger(); 0117 if (params.size() >= 3) { 0118 ++it; 0119 int len = (*it).asInteger(); 0120 ret = ret.mid (chars, len); 0121 } else 0122 ret = ret.mid (chars); 0123 return cValue (ret); 0124 } 0125 }; 0126 0127 class cFunctionStrlen: public cFunction { 0128 public: 0129 cFunctionStrlen () : cFunction ("strlen") {} 0130 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0131 if (params.size() < 1) return cValue::empty(); 0132 std::list<cValue>::iterator it = params.begin(); 0133 int ret = (*it).asString().length(); 0134 return cValue (ret); 0135 } 0136 }; 0137 0138 class cFunctionTrim: public cFunction { 0139 public: 0140 cFunctionTrim () : cFunction ("trim") {} 0141 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0142 if (params.size() < 1) return cValue::empty(); 0143 std::list<cValue>::iterator it = params.begin(); 0144 QString ret = (*it).asString().trimmed(); 0145 return cValue (ret); 0146 } 0147 }; 0148 0149 class cFunctionStrpos: public cFunction { 0150 public: 0151 cFunctionStrpos () : cFunction ("strpos") {} 0152 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0153 if (params.size() < 2) return cValue::empty(); 0154 std::list<cValue>::iterator it = params.begin(); 0155 QString haystack = (*it).asString(); 0156 ++it; 0157 QString needle = (*it).asString(); 0158 return cValue (haystack.indexOf (needle)); 0159 } 0160 }; 0161 0162 class cFunctionStrfill: public cFunction { 0163 public: 0164 cFunctionStrfill () : cFunction ("strfill") {} 0165 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0166 if (params.size() < 1) return cValue::empty(); 0167 std::list<cValue>::iterator it = params.begin(); 0168 QString ret = (*it).asString(); 0169 if (params.size() < 3) return ret; 0170 ++it; 0171 QChar character = (*it).asString()[0]; 0172 ++it; 0173 int len = (*it).asInteger(); 0174 0175 if (len <= ret.length()) 0176 return cValue (ret); 0177 0178 QString filler; 0179 filler.fill (character, len - ret.length()); 0180 return cValue (ret + filler); 0181 } 0182 }; 0183 0184 class cFunctionStrleftfill: public cFunction { 0185 public: 0186 cFunctionStrleftfill () : cFunction ("strleftfill") {} 0187 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0188 // same as strfill, except for the last line 0189 if (params.size() < 1) return cValue::empty(); 0190 std::list<cValue>::iterator it = params.begin(); 0191 QString ret = (*it).asString(); 0192 if (params.size() < 3) return ret; 0193 ++it; 0194 QChar character = (*it).asString()[0]; 0195 ++it; 0196 int len = (*it).asInteger(); 0197 0198 if (len <= ret.length()) 0199 return cValue (ret); 0200 0201 QString filler; 0202 filler.fill (character, len - ret.length()); 0203 return cValue (filler + ret); 0204 } 0205 }; 0206 0207 class cFunctionStrcat: public cFunction { 0208 public: 0209 cFunctionStrcat () : cFunction ("strcat") {} 0210 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0211 if (params.size() < 1) return cValue::empty(); 0212 std::list<cValue>::iterator it; 0213 QString ret; 0214 for (it = params.begin(); it != params.end(); ++it) 0215 ret += (*it).asString(); 0216 return cValue (ret); 0217 } 0218 }; 0219 0220 class cFunctionJoin: public cFunction { 0221 public: 0222 cFunctionJoin () : cFunction ("join") {} 0223 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0224 if (params.size() < 1) return cValue::empty(); 0225 std::list<cValue>::iterator it = params.begin(); 0226 cValue list = *it; 0227 QString sep = "|"; 0228 if (params.size() >= 2) 0229 { 0230 ++it; 0231 sep = (*it).asString(); 0232 } 0233 return list.listJoin (sep); 0234 } 0235 }; 0236 0237 class cFunctionExplode: public cFunction { 0238 public: 0239 cFunctionExplode () : cFunction ("explode") {} 0240 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0241 if (params.size() < 1) return cValue::empty(); 0242 std::list<cValue>::iterator it = params.begin(); 0243 cValue str = *it; 0244 QString sep = "|"; 0245 if (params.size() >= 2) 0246 { 0247 ++it; 0248 sep = (*it).asString(); 0249 } 0250 return str.toList (sep); 0251 } 0252 }; 0253 0254 class cFunctionReplace: public cFunction { 0255 public: 0256 cFunctionReplace () : cFunction ("replace") {} 0257 cValue eval (std::list<cValue> ¶ms, int, cCmdQueue *) override { 0258 if (params.size() < 3) return cValue::empty(); 0259 std::list<cValue>::iterator it = params.begin(); 0260 QString str = (*it).asString(); 0261 ++it; 0262 QString from = (*it).asString(); 0263 ++it; 0264 QString to = (*it).asString(); 0265 str.replace (from, to); 0266 return cValue (str); 0267 } 0268 }; 0269 0270 0271 // the main plug-in code which registers the functions: 0272 0273 struct cStringsPluginPrivate { 0274 cFunctionLower *flower; 0275 cFunctionUpper *fupper; 0276 cFunctionCaps *fcaps; 0277 cFunctionLeft *fleft; 0278 cFunctionRight *fright; 0279 cFunctionMid *fmid; 0280 cFunctionStrlen *fstrlen; 0281 cFunctionTrim *ftrim; 0282 cFunctionStrpos *fstrpos; 0283 cFunctionStrfill *fstrfill; 0284 cFunctionStrleftfill *fstrleftfill; 0285 cFunctionStrcat *fstrcat; 0286 cFunctionJoin *fjoin; 0287 cFunctionExplode *fexplode; 0288 cFunctionReplace *freplace; 0289 }; 0290 0291 cStringsPlugin::cStringsPlugin (QObject *, const QVariantList &) 0292 { 0293 d = new cStringsPluginPrivate; 0294 registerFunctions (); 0295 } 0296 0297 cStringsPlugin::~cStringsPlugin() 0298 { 0299 unregisterFunctions (); 0300 delete d; 0301 } 0302 0303 void cStringsPlugin::registerFunctions () 0304 { 0305 d->flower = new cFunctionLower; 0306 d->fupper = new cFunctionUpper; 0307 d->fcaps = new cFunctionCaps; 0308 d->fleft = new cFunctionLeft; 0309 d->fright = new cFunctionRight; 0310 d->fmid = new cFunctionMid; 0311 d->fstrlen = new cFunctionStrlen; 0312 d->ftrim = new cFunctionTrim; 0313 d->fstrpos = new cFunctionStrpos; 0314 d->fstrfill = new cFunctionStrfill; 0315 d->fstrleftfill = new cFunctionStrleftfill; 0316 d->fstrcat = new cFunctionStrcat; 0317 d->fjoin = new cFunctionJoin; 0318 d->fexplode = new cFunctionExplode; 0319 d->freplace = new cFunctionReplace; 0320 } 0321 0322 void cStringsPlugin::unregisterFunctions () 0323 { 0324 delete d->flower; 0325 delete d->fupper; 0326 delete d->fcaps; 0327 delete d->fleft; 0328 delete d->fright; 0329 delete d->fmid; 0330 delete d->fstrlen; 0331 delete d->ftrim; 0332 delete d->fstrpos; 0333 delete d->fstrfill; 0334 delete d->fstrleftfill; 0335 delete d->fstrcat; 0336 delete d->fjoin; 0337 delete d->fexplode; 0338 delete d->freplace; 0339 } 0340 0341 0342 #include "stringsplugin.moc"