1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/* Copyright (C) 2013 BMW Group
* Author: Manfred Bathelt (manfred.bathelt@bmw.de)
* Author: Juergen Gehring (juergen.gehring@bmw.de)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef COMMONAPI_UTILS_H_
#define COMMONAPI_UTILS_H_
#ifndef WIN32
#include <dirent.h>
#include <dlfcn.h>
#endif
#include <sys/stat.h>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iostream>
#include <locale>
#include <functional>
#ifdef WIN32
#include <xfunctional>
#define WIN32_LEAN_AND_MEAN // this prevents windows.h from including winsock.h, which causes duplicate definitions with winsock2.h
#include <windows.h>
#else
#include <unistd.h>
#endif
namespace CommonAPI {
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
# define COMMONAPI_DEPRECATED __attribute__ ((__deprecated__))
#elif defined(_MSC_VER) && (_MSC_VER >= 1300)
# define COMMONAPI_DEPRECATED __declspec(deprecated)
#else
# define COMMONAPI_DEPRECATED
#endif
/**
* \brief Returns the fully qualified name of the binary.
*
* @return The name of the currently executing binary.
*/
std::string getCurrentBinaryFileFQN();
/**
* \brief Splits a std::string according to the given delim-char.
*
* The string will be splitted at each position the delim char is encountered. The delim itself
* will be removed from the result.
*
* @param s: The string that is to be splitted
* @param delim: The character that separates the resulting string tokens in the original string
* @param elems: Reference to the vector that shall be filled with the splitted string elements.
*
* @return A reference to the vector you passed in (elems)
*/
std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems);
/**
* \brief Splits a std::string according to the given delim-char.
*
* The string will be splitted at each position the delim char is encountered. The delim itself
* will be removed from the result.
*
* @param s: The string that is to be splitted
* @param delim: The character that separates the resulting string tokens in the original string
*
* @return A vector containing the splitted string elements.
*/
std::vector<std::string> split(const std::string& s, char delim);
inline bool isspace(char c) {
return std::isspace(c, std::locale());
}
/**
* \brief Trims whitespaces from beginning and end of a std::string.
*
* @param toTrim: The string that is to be trimmed.
*/
void trim(std::string& toTrim);
bool notIsdigit(char c);
/**
* \brief Checks whether the given string contains nothing but digits.
*
* @param toCheck: The string that is to be checked on the presence of anything but digits.
*
* @return true if toCheck contains nothing but digits, false otherwise.
*/
bool containsOnlyDigits(const std::string& toCheck);
bool notIsalnum(char c);
/**
* \brief Checks whether the given string contains nothing but alphanumeric characters.
*
* @param toCheck: The string that is to be checked on the presence of anything but alphanumeric characters.
*
* @return true if toCheck contains nothing but alphanumeric characters, false otherwise.
*/
bool containsOnlyAlphanumericCharacters(const std::string& toCheck);
/**
* \brief Checks whether the given std::string is a valid CommonAPI domain name.
*
* @param domainName: The std::string that is to be checked.
*
* @return true if domainName is a valid CommonAPI domainName, false otherwise.
*/
bool isValidDomainName(const std::string& domainName);
/**
* \brief Checks whether the given std::string is a valid CommonAPI service name.
*
* @param serviceName: The std::string that is to be checked.
*
* @return true if serviceName is a valid CommonAPI serviceName, false otherwise.
*/
bool isValidServiceName(const std::string& serviceName);
/**
* \brief Checks whether the given std::string is a valid CommonAPI instance ID.
*
* @param instanceId: The std::string that is to be checked.
*
* @return true if instanceId is a valid CommonAPI instance ID, false otherwise.
*/
bool isValidInstanceId(const std::string& instanceId);
/**
* \brief Checks whether the given std::string is a valid CommonAPI address.
*
* @param commonApiAddressName: The std::string that is to be checked.
*
* @return true if commonApiAddress is a valid CommonAPI address, false otherwise.
*/
bool isValidCommonApiAddress(const std::string& commonApiAddress);
#ifndef WIN32
/**
* \brief Loads a specific generic library at runtime.
*
* The library will be loaded using dlopen(3) with the flags (RTLD_NOW | RTLD_GLOBAL), if all pre-checks are
* successful. Pre-checks include the verification that the given parameters actually point to a library and
* optionally if the library matches the standard name pattern for CommonAPI generic libraries. The standard
* name pattern is "lib<wellKnownMiddlewareName>Gen-<arbitraryName>.so[.major[.minor.revision]]".
*
* @param wellKnownMiddlewareName: The name of the middleware that requests the loading of the library.
* @param libraryName: The name of the library that shall be loaded.
* @param path: The path at which the library is to be found. path + library name together make up the fully
* qualified name of the library.
* @param checkStandardNamePattern: If set to true, it will be ensured the library matches the CommonAPI
* standard name pattern for generic libraries. This is meant as a safety measure
* to prevent the loading of unnecessary or the wrong libraries. Set to false if
* you are sure about what you are doing.
* @return true if the library could be loaded successfully, false otherwise.
*
* @note The well known middleware name is included as a parameter because the additional libraries normally are needed
* only by specific middlewares. This name however will only be taken into consideration during name checking
* if the checkStandardNamePattern flag is set to true.
*/
bool loadGenericLibrary(const std::string& wellKnownMiddlewareName, const std::string& libraryName, const std::string& path, bool checkStandardNamePattern = true);
/**
* \brief Loads a specific generic library at runtime.
*
* The library will be loaded using dlopen(3) with the flags (RTLD_NOW | RTLD_GLOBAL), if all pre-checks are
* successful. Pre-checks include the verification that the given parameters actually point to a library and
* optionally if the library matches the standard name pattern for CommonAPI generic libraries. The standard
* name pattern is "lib<wellKnownMiddlewareName>Gen-<arbitraryName>.so[.major[.minor.revision]]".
*
* @param wellKnownMiddlewareName: The name of the middleware that requests the loading of the library.
* @param fqnOfLibrary: The fully qualified name of the library.
* @param checkStandardNamePattern: If set to true, it will be ensured the library matches the CommonAPI
* standard name pattern for generic libraries. This is meant as a safety measure
* to prevent the loading of unnecessary or the wrong libraries. Set to false if
* you are sure about what you are doing.
* @return true if the library could be loaded successfully, false otherwise.
*
* @note The well known middleware name is included as a parameter because the additional libraries normally are needed
* only by specific middlewares. This name however will only be taken into consideration during name checking
* if the checkStandardNamePattern flag is set to true.
*/
bool loadGenericLibrary(const std::string& wellKnownMiddlewareName,
const std::string& fqnOfLibrary,
bool checkStandardNamePattern = true);
/**
* \brief Searches the given path for additional generic CommonAPI libraries and loads them.
*
* All libraries for which the pre-checks are successful will be loaded using dlopen(3) with the flags
* (RTLD_NOW | RTLD_GLOBAL). Pre-checks include the verification that the given parameters actually point
* to a library and if the library matches the standard name pattern for CommonAPI generic libraries.
* The standard name pattern is "lib<wellKnownMiddlewareName>Gen-<arbitraryName>.so[.major[.minor.revision]]".
*
* @param wellKnownMiddlewareName: The name of the middleware that requests the loading of the library. Will
* be used to perform the name check.
* @param singleSearchPath: The directory that is to be searched for libraries.
*/
void findAndLoadGenericLibraries(const std::string& requestedMiddlewareName, const std::string& singleSearchPath);
/**
* \brief Searches the given paths for additional generic CommonAPI libraries and loads them.
*
* All libraries for which the pre-checks are successful will be loaded using dlopen(3) with the flags
* (RTLD_NOW | RTLD_GLOBAL). Pre-checks include the verification that the given parameters actually point
* to a library and if the library matches the standard name pattern for CommonAPI generic libraries.
* The standard name pattern is "lib<wellKnownMiddlewareName>Gen-<arbitraryName>.so[.major[.minor.revision]]".
*
* @param wellKnownMiddlewareName: The name of the middleware that requests the loading of the library. Will
* be used to perform the name check.
* @param searchPaths: The directories that are to be searched for libraries.
*/
void findAndLoadGenericLibraries(const std::string& requestedMiddlewareName, const std::vector<std::string>& searchPaths);
#endif
} //namespace CommonAPI
#endif /* COMMONAPI_UTILS_H_ */
|