]> granicus.if.org Git - icinga2/blob - lib/base/string-script.cpp
Build fix for Win64
[icinga2] / lib / base / string-script.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
28
29 using namespace icinga;
30
31 static int StringLen(void)
32 {
33         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
34         String self = vframe->Self;
35         return self.GetLength();
36 }
37
38 static String StringToString(void)
39 {
40         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
41         return vframe->Self;
42 }
43
44 static String StringSubstr(const std::vector<Value>& args)
45 {
46         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
47         String self = vframe->Self;
48
49         if (args.empty())
50                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
51
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"));
54
55         if (args.size() > 1)
56                 return self.SubStr(args[0], args[1]);
57         else
58                 return self.SubStr(args[0]);
59 }
60
61 static String StringUpper(void)
62 {
63         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
64         String self = vframe->Self;
65         return boost::to_upper_copy(self);
66 }
67
68 static String StringLower(void)
69 {
70         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
71         String self = vframe->Self;
72         return boost::to_lower_copy(self);
73 }
74
75 static Array::Ptr StringSplit(const String& delims)
76 {
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));
81
82         Array::Ptr result = new Array();
83         BOOST_FOREACH(const String& token, tokens) {
84                 result->Add(token);
85         }
86         return result;
87 }
88
89 static int StringFind(const std::vector<Value>& args)
90 {
91         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
92         String self = vframe->Self;
93
94         if (args.empty())
95                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
96
97         String::SizeType result;
98
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"));
102
103                 result = self.Find(args[0], args[1]);
104         } else
105                 result = self.Find(args[0]);
106
107         if (result == String::NPos)
108                 return -1;
109         else
110                 return result;
111 }
112
113 static bool StringContains(const String& str)
114 {
115         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
116         String self = vframe->Self;
117         return self.Contains(str);
118 }
119
120 static Value StringReplace(const String& search, const String& replacement)
121 {
122         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
123         String self = vframe->Self;
124
125         boost::algorithm::replace_all(self, search, replacement);
126         return self;
127 }
128
129 static String StringReverse(void)
130 {
131         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
132         String self = vframe->Self;
133         return self.Reverse();
134 }
135
136 static String StringTrim(void)
137 {
138         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
139         String self = vframe->Self;
140         return self.Trim();
141 }
142
143 Object::Ptr String::GetPrototype(void)
144 {
145         static Dictionary::Ptr prototype;
146
147         if (!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));
160         }
161
162         return prototype;
163 }
164