1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/object.hpp"
21 #include "base/dictionary.hpp"
22 #include "base/function.hpp"
23 #include "base/functionwrapper.hpp"
24 #include "base/scriptframe.hpp"
25 #include "base/exception.hpp"
26 #include <boost/algorithm/string.hpp>
27 #include <boost/foreach.hpp>
29 using namespace icinga;
31 static int StringLen(void)
33 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
34 String self = vframe->Self;
35 return self.GetLength();
38 static String StringToString(void)
40 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
44 static String StringSubstr(const std::vector<Value>& args)
46 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
47 String self = vframe->Self;
50 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
52 if (static_cast<double>(args[0]) < 0 || static_cast<double>(args[0]) >= self.GetLength())
53 BOOST_THROW_EXCEPTION(std::invalid_argument("String index is out of range"));
56 return self.SubStr(args[0], args[1]);
58 return self.SubStr(args[0]);
61 static String StringUpper(void)
63 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
64 String self = vframe->Self;
65 return boost::to_upper_copy(self);
68 static String StringLower(void)
70 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
71 String self = vframe->Self;
72 return boost::to_lower_copy(self);
75 static Array::Ptr StringSplit(const String& delims)
77 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
78 String self = vframe->Self;
79 std::vector<String> tokens;
80 boost::algorithm::split(tokens, self, boost::is_any_of(delims));
82 Array::Ptr result = new Array();
83 BOOST_FOREACH(const String& token, tokens) {
89 static int StringFind(const std::vector<Value>& args)
91 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
92 String self = vframe->Self;
95 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
97 String::SizeType result;
99 if (args.size() > 1) {
100 if (static_cast<double>(args[1]) < 0)
101 BOOST_THROW_EXCEPTION(std::invalid_argument("String index is out of range"));
103 result = self.Find(args[0], args[1]);
105 result = self.Find(args[0]);
107 if (result == String::NPos)
113 static bool StringContains(const String& str)
115 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
116 String self = vframe->Self;
117 return self.Contains(str);
120 static Value StringReplace(const String& search, const String& replacement)
122 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
123 String self = vframe->Self;
125 boost::algorithm::replace_all(self, search, replacement);
129 static String StringReverse(void)
131 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
132 String self = vframe->Self;
133 return self.Reverse();
136 static String StringTrim(void)
138 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
139 String self = vframe->Self;
143 Object::Ptr String::GetPrototype(void)
145 static Dictionary::Ptr prototype;
148 prototype = new Dictionary();
149 prototype->Set("len", new Function(WrapFunction(StringLen), true));
150 prototype->Set("to_string", new Function(WrapFunction(StringToString), true));
151 prototype->Set("substr", new Function(WrapFunction(StringSubstr), true));
152 prototype->Set("upper", new Function(WrapFunction(StringUpper), true));
153 prototype->Set("lower", new Function(WrapFunction(StringLower), true));
154 prototype->Set("split", new Function(WrapFunction(StringSplit), true));
155 prototype->Set("find", new Function(WrapFunction(StringFind), true));
156 prototype->Set("contains", new Function(WrapFunction(StringContains), true));
157 prototype->Set("replace", new Function(WrapFunction(StringReplace), true));
158 prototype->Set("reverse", new Function(WrapFunction(StringReverse), true));
159 prototype->Set("trim", new Function(WrapFunction(StringTrim), true));