]> granicus.if.org Git - icinga2/blob - lib/base/utility.hpp
lib->compat->statusdatawriter: fix notifications_enabled
[icinga2] / lib / base / utility.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
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 #ifndef UTILITY_H
21 #define UTILITY_H
22
23 #include "base/i2-base.hpp"
24 #include "base/string.hpp"
25 #include "base/array.hpp"
26 #include "base/threadpool.hpp"
27 #include <boost/thread/tss.hpp>
28 #include <typeinfo>
29 #include <vector>
30
31 namespace icinga
32 {
33
34 #ifdef _WIN32
35 #define MS_VC_EXCEPTION 0x406D1388
36
37 #       pragma pack(push, 8)
38 struct THREADNAME_INFO
39 {
40         DWORD dwType;
41         LPCSTR szName;
42         DWORD dwThreadID;
43         DWORD dwFlags;
44 };
45 #       pragma pack(pop)
46 #endif
47
48 enum GlobType
49 {
50         GlobFile = 1,
51         GlobDirectory = 2
52 };
53
54 /**
55  * Helper functions.
56  *
57  * @ingroup base
58  */
59 class Utility
60 {
61 public:
62         static String DemangleSymbolName(const String& sym);
63         static String GetTypeName(const std::type_info& ti);
64         static String GetSymbolName(const void *addr);
65
66         static bool Match(const String& pattern, const String& text);
67         static bool CidrMatch(const String& pattern, const String& ip);
68
69         static String DirName(const String& path);
70         static String BaseName(const String& path);
71
72         static void NullDeleter(void *);
73
74         static double GetTime();
75
76         static pid_t GetPid();
77
78         static void Sleep(double timeout);
79
80         static String NewUniqueID();
81
82         static bool Glob(const String& pathSpec, const std::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
83         static bool GlobRecursive(const String& path, const String& pattern, const std::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
84         static void MkDir(const String& path, int mode);
85         static void MkDirP(const String& path, int mode);
86         static bool SetFileOwnership(const String& file, const String& user, const String& group);
87
88         static void QueueAsyncCallback(const std::function<void ()>& callback, SchedulerPolicy policy = DefaultScheduler);
89
90         static String NaturalJoin(const std::vector<String>& tokens);
91         static String Join(const Array::Ptr& tokens, char separator, bool escapeSeparator = true);
92
93         static String FormatDuration(double duration);
94         static String FormatDateTime(const char *format, double ts);
95         static String FormatErrorNumber(int code);
96
97 #ifndef _WIN32
98         static void SetNonBlocking(int fd, bool nb = true);
99         static void SetCloExec(int fd, bool cloexec = true);
100 #endif /* _WIN32 */
101
102         static void SetNonBlockingSocket(SOCKET s, bool nb = true);
103
104         static String EscapeShellCmd(const String& s);
105         static String EscapeShellArg(const String& s);
106 #ifdef _WIN32
107         static String EscapeCreateProcessArg(const String& arg);
108 #endif /* _WIN32 */
109
110         static String EscapeString(const String& s, const String& chars, const bool illegal);
111         static String UnescapeString(const String& s);
112
113         static void SetThreadName(const String& name, bool os = true);
114         static String GetThreadName();
115
116         static unsigned long SDBM(const String& str, size_t len = String::NPos);
117
118         static int CompareVersion(const String& v1, const String& v2);
119
120         static int Random();
121
122         static String GetHostName();
123         static String GetFQDN();
124
125         static tm LocalTime(time_t ts);
126
127         static bool PathExists(const String& path);
128
129         static void RemoveDirRecursive(const String& path);
130         static void CopyFile(const String& source, const String& target);
131
132         static Value LoadJsonFile(const String& path);
133         static void SaveJsonFile(const String& path, int mode, const Value& value);
134
135         static String GetPlatformKernel();
136         static String GetPlatformKernelVersion();
137         static String GetPlatformName();
138         static String GetPlatformVersion();
139         static String GetPlatformArchitecture();
140
141         static String ValidateUTF8(const String& input);
142
143         static String CreateTempFile(const String& path, int mode, std::fstream& fp);
144
145 #ifdef _WIN32
146         static String GetIcingaInstallPath();
147         static String GetIcingaDataPath();
148 #endif /* _WIN32 */
149
150         static String GetFromEnvironment(const String& env);
151
152 #ifdef I2_DEBUG
153         static void SetTime(double);
154         static void IncrementTime(double);
155 #endif /* I2_DEBUG */
156
157 private:
158         Utility();
159         static void CollectPaths(const String& path, std::vector<String>& paths);
160
161 #ifdef _WIN32
162         static int MksTemp (char *tmpl);
163 #endif /* _WIN32 */
164
165 #ifdef I2_DEBUG
166         static double m_DebugTime;
167 #endif /* I2_DEBUG */
168
169         static boost::thread_specific_ptr<String> m_ThreadName;
170         static boost::thread_specific_ptr<unsigned int> m_RandSeed;
171 };
172
173 }
174
175 #endif /* UTILITY_H */