]> granicus.if.org Git - icinga2/blob - lib/base/string-script.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / string-script.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/object.hpp"
4 #include "base/dictionary.hpp"
5 #include "base/function.hpp"
6 #include "base/functionwrapper.hpp"
7 #include "base/scriptframe.hpp"
8 #include "base/exception.hpp"
9 #include <boost/algorithm/string.hpp>
10
11 using namespace icinga;
12
13 static int StringLen()
14 {
15         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
16         String self = vframe->Self;
17         return self.GetLength();
18 }
19
20 static String StringToString()
21 {
22         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
23         return vframe->Self;
24 }
25
26 static String StringSubstr(const std::vector<Value>& args)
27 {
28         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
29         String self = vframe->Self;
30
31         if (args.empty())
32                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
33
34         if (static_cast<double>(args[0]) < 0 || static_cast<double>(args[0]) >= self.GetLength())
35                 BOOST_THROW_EXCEPTION(std::invalid_argument("String index is out of range"));
36
37         if (args.size() > 1)
38                 return self.SubStr(args[0], args[1]);
39         else
40                 return self.SubStr(args[0]);
41 }
42
43 static String StringUpper()
44 {
45         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46         String self = vframe->Self;
47         return boost::to_upper_copy(self);
48 }
49
50 static String StringLower()
51 {
52         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
53         String self = vframe->Self;
54         return boost::to_lower_copy(self);
55 }
56
57 static Array::Ptr StringSplit(const String& delims)
58 {
59         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
60         String self = vframe->Self;
61         std::vector<String> tokens = self.Split(delims.CStr());
62
63         return Array::FromVector(tokens);
64 }
65
66 static int StringFind(const std::vector<Value>& args)
67 {
68         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
69         String self = vframe->Self;
70
71         if (args.empty())
72                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
73
74         String::SizeType result;
75
76         if (args.size() > 1) {
77                 if (static_cast<double>(args[1]) < 0)
78                         BOOST_THROW_EXCEPTION(std::invalid_argument("String index is out of range"));
79
80                 result = self.Find(args[0], args[1]);
81         } else
82                 result = self.Find(args[0]);
83
84         if (result == String::NPos)
85                 return -1;
86         else
87                 return result;
88 }
89
90 static bool StringContains(const String& str)
91 {
92         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
93         String self = vframe->Self;
94         return self.Contains(str);
95 }
96
97 static Value StringReplace(const String& search, const String& replacement)
98 {
99         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
100         String self = vframe->Self;
101
102         boost::algorithm::replace_all(self, search, replacement);
103         return self;
104 }
105
106 static String StringReverse()
107 {
108         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
109         String self = vframe->Self;
110         return self.Reverse();
111 }
112
113 static String StringTrim()
114 {
115         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
116         String self = vframe->Self;
117         return self.Trim();
118 }
119
120 Object::Ptr String::GetPrototype()
121 {
122         static Dictionary::Ptr prototype = new Dictionary({
123                 { "len", new Function("String#len", StringLen, {}, true) },
124                 { "to_string", new Function("String#to_string", StringToString, {}, true) },
125                 { "substr", new Function("String#substr", StringSubstr, { "start", "len" }, true) },
126                 { "upper", new Function("String#upper", StringUpper, {}, true) },
127                 { "lower", new Function("String#lower", StringLower, {}, true) },
128                 { "split", new Function("String#split", StringSplit, { "delims" }, true) },
129                 { "find", new Function("String#find", StringFind, { "str", "start" }, true) },
130                 { "contains", new Function("String#contains", StringContains, { "str" }, true) },
131                 { "replace", new Function("String#replace", StringReplace, { "search", "replacement" }, true) },
132                 { "reverse", new Function("String#reverse", StringReverse, {}, true) },
133                 { "trim", new Function("String#trim", StringTrim, {}, true) }
134         });
135
136         return prototype;
137 }
138