added method AbstractObject.getDashboards
[public/netxms.git] / include / nms_util.h
CommitLineData
cc022855 1/*
7ac71e60 2** NetXMS - Network Management System
6f95ef89 3** Copyright (C) 2003-2015 Victor Kirhenshtein
e9246d26
VK
4**
5** This program is free software; you can redistribute it and/or modify
68f384ea
VK
6** it under the terms of the GNU Lesser General Public License as published
7** by the Free Software Foundation; either version 3 of the License, or
e9246d26
VK
8** (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**
68f384ea 15** You should have received a copy of the GNU Lesser General Public License
e9246d26
VK
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
fb7ce4b9 19** File: nms_util.h
e9246d26
VK
20**
21**/
22
23#ifndef _nms_util_h_
24#define _nms_util_h_
25
64584976
VK
26#ifdef _WIN32
27#ifdef LIBNETXMS_EXPORTS
28#define LIBNETXMS_EXPORTABLE __declspec(dllexport)
29#else
30#define LIBNETXMS_EXPORTABLE __declspec(dllimport)
31#endif
32#else /* _WIN32 */
33#define LIBNETXMS_EXPORTABLE
34#endif
35
36
e9246d26
VK
37#include <nms_common.h>
38#include <nms_cscp.h>
953dfe76 39#include <nms_threads.h>
64584976 40#include <time.h>
e9246d26 41
6d8aee95
VK
42#if HAVE_BYTESWAP_H
43#include <byteswap.h>
44#endif
45
92502d5f
VK
46#include <base64.h>
47
4b47d7d7
VK
48/*** Byte swapping ***/
49#if WORDS_BIGENDIAN
50#define htonq(x) (x)
51#define ntohq(x) (x)
52#define htond(x) (x)
53#define ntohd(x) (x)
54#define SwapWideString(x)
55#else
56#ifdef HAVE_HTONLL
57#define htonq(x) htonll(x)
58#else
59#define htonq(x) __bswap_64(x)
60#endif
61#ifdef HAVE_NTOHLL
62#define ntohq(x) ntohll(x)
63#else
64#define ntohq(x) __bswap_64(x)
65#endif
66#define htond(x) __bswap_double(x)
67#define ntohd(x) __bswap_double(x)
68#define SwapWideString(x) __bswap_wstr(x)
69#endif
6d8aee95 70
4b47d7d7
VK
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
76UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
77#endif
78#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
79UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
80#endif
81double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
82void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
6d8aee95 83
4b47d7d7
VK
84#ifdef __cplusplus
85}
86#endif
87
88
89/*** Serial communications ***/
6d8aee95
VK
90#ifdef _WIN32
91
92#define FLOW_NONE 0
93#define FLOW_SOFTWARE 1
94#define FLOW_HARDWARE 2
95
96#else /* _WIN32 */
97
70fed300
AK
98#ifdef HAVE_TERMIOS_H
99# include <termios.h>
100#else
101# error termios.h not found
102#endif
103
6d8aee95
VK
104#endif /* _WIN32 */
105
e2e66145
VK
106/**
107 * Return codes for IcmpPing()
108 */
66c04cc7
VK
109#define ICMP_SUCCESS 0
110#define ICMP_UNREACHEABLE 1
111#define ICMP_TIMEOUT 2
112#define ICMP_RAW_SOCK_FAILED 3
5418d4bb 113#define ICMP_API_ERROR 4
66c04cc7 114
e2e66145
VK
115/**
116 * Token types for configuration loader
117 */
4826c8a5
VK
118#define CT_LONG 0
119#define CT_STRING 1
120#define CT_STRING_LIST 2
121#define CT_END_OF_LIST 3
122#define CT_BOOLEAN 4
123#define CT_WORD 5
fc935a60 124#define CT_IGNORE 6
dda7c270 125#define CT_MB_STRING 7
e95680e5 126#define CT_BOOLEAN64 8
4826c8a5 127
605fc935 128/**
e2e66145
VK
129 * Uninitialized value for override indicator
130 */
131#define NXCONFIG_UNINITIALIZED_VALUE (-1)
132
133/**
605fc935
VK
134 * Return codes for NxLoadConfig()
135 */
4826c8a5
VK
136#define NXCFG_ERR_OK 0
137#define NXCFG_ERR_NOFILE 1
138#define NXCFG_ERR_SYNTAX 2
139
605fc935
VK
140/**
141 * nxlog_open() flags
142 */
4e0e77e6
VK
143#define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
144#define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
145#define NXLOG_BACKGROUND_WRITER ((UINT32)0x00000004)
146#define NXLOG_IS_OPEN ((UINT32)0x80000000)
9f2902ac 147
605fc935
VK
148/**
149 * nxlog rotation policy
150 */
5945d50a
VK
151#define NXLOG_ROTATION_DISABLED 0
152#define NXLOG_ROTATION_DAILY 1
153#define NXLOG_ROTATION_BY_SIZE 2
154
605fc935
VK
155/**
156 * _tcsdup() replacement
157 */
8ff13508
VK
158#if defined(_WIN32) && defined(USE_WIN32_HEAP)
159#ifdef __cplusplus
160extern "C" {
161#endif
fd72502a 162char LIBNETXMS_EXPORTABLE *nx_strdup(const char *src);
8ff13508
VK
163WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
164#ifdef __cplusplus
165}
166#endif
167#endif
168
605fc935 169/**
f4d2c9ba
VK
170 * Custom wcsdup
171 */
172#if !HAVE_WCSDUP && !defined(_WIN32)
fd72502a 173
f4d2c9ba
VK
174#ifdef __cplusplus
175extern "C" {
176#endif
177 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
178#ifdef __cplusplus
179}
180#endif
fd72502a
VK
181
182#elif defined(_AIX)
183
184// Some AIX versions have broken wcsdup() so we use internal implementation
185#ifdef __cplusplus
186extern "C" {
187#endif
188 WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
189#ifdef __cplusplus
190}
191#endif
192
193#define wcsdup nx_wcsdup
194
f4d2c9ba
VK
195#endif
196
c012b7b7
VK
197#ifdef __cplusplus
198
f4d2c9ba 199/**
c83f6449
VK
200 * Extended tcsdup: returns NULL if NULL pointer passed
201 */
202inline TCHAR *_tcsdup_ex(const TCHAR *s)
203{
204 return (s != NULL) ? _tcsdup(s) : NULL;
205}
206
c012b7b7
VK
207#endif
208
c83f6449 209/**
d65c16a8
VK
210 * String conversion helpers
211 */
212#ifdef __cplusplus
213extern "C" {
214#endif
215WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
216WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
217char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
218char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
219#ifdef __cplusplus
220}
221#endif
222
223/**
605fc935
VK
224 * Class for serial communications
225 */
b160350d
VK
226#ifdef __cplusplus
227
228#ifndef _WIN32
229enum
230{
231 NOPARITY,
232 ODDPARITY,
233 EVENPARITY,
234 ONESTOPBIT,
235 TWOSTOPBITS
236};
237
70fed300
AK
238enum
239{
240 FLOW_NONE,
241 FLOW_HARDWARE,
242 FLOW_SOFTWARE
243};
244
b160350d
VK
245#ifndef INVALID_HANDLE_VALUE
246#define INVALID_HANDLE_VALUE (-1)
247#endif
248#endif /* _WIN32 */
249
250class LIBNETXMS_EXPORTABLE Serial
251{
b160350d
VK
252private:
253 TCHAR *m_pszPort;
254 int m_nTimeout;
255 int m_nSpeed;
256 int m_nDataBits;
257 int m_nStopBits;
258 int m_nParity;
70fed300 259 int m_nFlowControl;
b160350d
VK
260
261#ifndef _WIN32
262 int m_hPort;
70fed300 263 struct termios m_originalSettings;
b160350d
VK
264#else
265 HANDLE m_hPort;
266#endif
b160350d 267
826fcc1f
VK
268public:
269 Serial();
270 ~Serial();
271
272 bool open(const TCHAR *pszPort);
273 void close();
274 void setTimeout(int nTimeout);
275 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
276 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
c3f71625 277 int readToMark(char *buff, int size, const char **marks, char **occurence);
826fcc1f
VK
278 bool write(const char *pBuff, int nSize);
279 void flush();
280 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
281 bool restart();
282};
ff214673 283
3fe40613
VK
284/**
285 * Dynamic string class
286 */
8bad8476
VK
287class LIBNETXMS_EXPORTABLE String
288{
289protected:
4e0e77e6
VK
290 TCHAR *m_buffer;
291 size_t m_length;
292 size_t m_allocated;
dcc7a2b0 293 size_t m_allocationStep;
8bad8476
VK
294
295public:
c577e360
VK
296 static const int npos;
297
8bad8476 298 String();
f3cdd1c5 299 String(const TCHAR *init);
643c9dcb 300 String(const String &src);
8bad8476
VK
301 ~String();
302
4e0e77e6
VK
303 TCHAR *getBuffer() { return m_buffer; }
304 void setBuffer(TCHAR *buffer);
8bad8476 305
dcc7a2b0
VK
306 size_t getAllocationStep() { return m_allocationStep; }
307 void setAllocationStep(size_t step) { m_allocationStep = step; }
4e0e77e6
VK
308
309 const String& operator =(const TCHAR *str);
49bf9d8d 310 const String& operator =(const String &src);
1d34c533
VK
311 const String& operator +=(const TCHAR *str);
312 const String& operator +=(const String &str);
4e0e77e6 313 operator const TCHAR*() { return CHECK_NULL_EX(m_buffer); }
8bad8476 314
08b214c6
VK
315 char *getUTF8String();
316
4e0e77e6
VK
317 void append(const TCHAR *str) { if (str != NULL) append(str, _tcslen(str)); }
318 void append(const TCHAR *str, size_t len);
f98a8402 319 void append(const TCHAR c) { append(&c, 1); }
5f648670
VK
320 void append(INT32 n);
321 void append(UINT32 n);
322 void append(INT64 n);
323 void append(UINT64 n);
4e0e77e6
VK
324
325 void appendPreallocated(TCHAR *str) { if (str != NULL) { append(str); free(str); } }
326
327 void appendMBString(const char *str, size_t len, int nCodePage);
328 void appendWideString(const WCHAR *str, size_t len);
c577e360 329
4e0e77e6
VK
330 void appendFormattedString(const TCHAR *format, ...);
331 void appendFormattedStringV(const TCHAR *format, va_list args);
ff175e91 332
4e0e77e6 333 void clear();
c577e360 334
4e0e77e6
VK
335 size_t length() const { return m_length; }
336 bool isEmpty() const { return m_length == 0; }
c577e360 337
4e0e77e6
VK
338 TCHAR *substring(int nStart, int nLen, TCHAR *pszBuffer = NULL);
339 int find(const TCHAR *str, int nStart = 0);
1e1450ef 340
6bcafa2d
VK
341 void escapeCharacter(int ch, int esc);
342 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
0bbab9d3
VK
343 void trim();
344 void shrink(int chars = 1);
c577e360
VK
345};
346
3fe40613 347/**
f9861ff9
VK
348 * Dynamic array class
349 */
350class LIBNETXMS_EXPORTABLE Array
351{
352private:
353 int m_size;
354 int m_allocated;
355 int m_grow;
356 size_t m_elementSize;
357 void **m_data;
358 bool m_objectOwner;
359
360 void internalRemove(int index, bool allowDestruction);
361 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
362
363protected:
364 bool m_storePointers;
365 void (*m_objectDestructor)(void *);
366
367 Array(void *data, int initial, int grow, size_t elementSize);
dcc7a2b0 368 void *__getBuffer() const { return m_data; }
f9861ff9
VK
369
370public:
371 Array(int initial = 0, int grow = 16, bool owner = false);
372 virtual ~Array();
373
374 int add(void *element);
dcc7a2b0
VK
375 void *get(int index) const { return ((index >= 0) && (index < m_size)) ? (m_storePointers ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
376 int indexOf(void *element) const;
f9861ff9
VK
377 void set(int index, void *element);
378 void replace(int index, void *element);
379 void remove(int index) { internalRemove(index, true); }
380 void remove(void *element) { internalRemove(indexOf(element), true); }
381 void unlink(int index) { internalRemove(index, false); }
382 void unlink(void *element) { internalRemove(indexOf(element), false); }
383 void clear();
384 void sort(int (*cb)(const void *, const void *));
dcc7a2b0 385 void *find(const void *key, int (*cb)(const void *, const void *)) const;
f9861ff9 386
dcc7a2b0 387 int size() const { return m_size; }
f9861ff9
VK
388
389 void setOwner(bool owner) { m_objectOwner = owner; }
dcc7a2b0 390 bool isOwner() const { return m_objectOwner; }
f9861ff9
VK
391};
392
393/**
394 * Template class for dynamic array which holds objects
395 */
396template <class T> class ObjectArray : public Array
397{
398private:
399 static void destructor(void *object) { delete (T*)object; }
400
401public:
402 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
403 virtual ~ObjectArray() { }
404
405 int add(T *object) { return Array::add((void *)object); }
dcc7a2b0
VK
406 T *get(int index) const { return (T*)Array::get(index); }
407 int indexOf(T *object) const { return Array::indexOf((void *)object); }
dfed892b 408 bool contains(T *object) const { return indexOf(object) >= 0; }
f9861ff9
VK
409 void set(int index, T *object) { Array::set(index, (void *)object); }
410 void replace(int index, T *object) { Array::replace(index, (void *)object); }
411 void remove(int index) { Array::remove(index); }
412 void remove(T *object) { Array::remove((void *)object); }
413 void unlink(int index) { Array::unlink(index); }
414 void unlink(T *object) { Array::unlink((void *)object); }
415};
416
417/**
418 * Template class for dynamic array which holds scalar values
419 */
420template <class T> class IntegerArray : public Array
421{
422private:
423 static void destructor(void *element) { }
424
425public:
426 IntegerArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; m_storePointers = (sizeof(T) == sizeof(void *)); }
427 virtual ~IntegerArray() { }
428
429 int add(T value) { return Array::add(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
e5390fb5 430 T get(int index) const { if (m_storePointers) return CAST_FROM_POINTER(Array::get(index), T); T *p = (T*)Array::get(index); return (p != NULL) ? *p : 0; }
dcc7a2b0 431 int indexOf(T value) const { return Array::indexOf(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
dfed892b 432 bool contains(T value) const { return indexOf(value) >= 0; }
f9861ff9
VK
433 void set(int index, T value) { Array::set(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
434 void replace(int index, T value) { Array::replace(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
435
dcc7a2b0 436 T *getBuffer() const { return (T*)__getBuffer(); }
f9861ff9
VK
437};
438
439/**
440 * Auxilliary class to hold dynamically allocated array of structures
441 */
442template <class T> class StructArray : public Array
443{
444private:
445 static void destructor(void *element) { }
446
447public:
448 StructArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; }
449 StructArray(T *data, int size) : Array(data, size, 16, sizeof(T)) { m_objectDestructor = destructor; }
450 virtual ~StructArray() { }
451
452 int add(T *element) { return Array::add((void *)element); }
dcc7a2b0
VK
453 T *get(int index) const { return (T*)Array::get(index); }
454 int indexOf(T *element) const { return Array::indexOf((void *)element); }
dfed892b 455 bool contains(T *element) const { return indexOf(element) >= 0; }
f9861ff9
VK
456 void set(int index, T *element) { Array::set(index, (void *)element); }
457 void replace(int index, T *element) { Array::replace(index, (void *)element); }
458 void remove(int index) { Array::remove(index); }
459 void remove(T *element) { Array::remove((void *)element); }
460 void unlink(int index) { Array::unlink(index); }
461 void unlink(T *element) { Array::unlink((void *)element); }
462
dcc7a2b0 463 T *getBuffer() const { return (T*)__getBuffer(); }
f9861ff9
VK
464};
465
466/**
467 * Entry of string map (internal)
400e55c4
VK
468 */
469struct StringMapEntry;
470
471/**
f9861ff9
VK
472 * Key/value pair
473 */
474struct KeyValuePair
475{
476 const TCHAR *key;
477 const void *value;
478};
479
480/**
3fe40613
VK
481 * String maps base class
482 */
483class LIBNETXMS_EXPORTABLE StringMapBase
c577e360
VK
484{
485protected:
400e55c4 486 StringMapEntry *m_data;
3fe40613 487 bool m_objectOwner;
55bdca5a 488 bool m_ignoreCase;
3fe40613 489 void (*m_objectDestructor)(void *);
c577e360 490
400e55c4 491 StringMapEntry *find(const TCHAR *key);
3fe40613
VK
492 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
493 void *getObject(const TCHAR *key);
494 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
c577e360
VK
495
496public:
3fe40613
VK
497 StringMapBase(bool objectOwner);
498 virtual ~StringMapBase();
499
55bdca5a 500 void setOwner(bool owner) { m_objectOwner = owner; }
91a581c5 501 void setIgnoreCase(bool ignore);
55bdca5a 502
3fe40613
VK
503 void remove(const TCHAR *key);
504 void clear();
505
c1a30973 506 int size();
400e55c4
VK
507 bool contains(const TCHAR *key) { return find(key) != NULL; }
508
53c3d1e7 509 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const TCHAR *, const void *, void *), void *userData);
beab0421 510 const void *findElement(bool (*comparator)(const TCHAR *, const void *, void *), void *userData);
f9861ff9
VK
511
512 StructArray<KeyValuePair> *toArray();
374afd7b 513 StringList *keys();
3fe40613
VK
514};
515
516/**
400e55c4
VK
517 * NXCP message class
518 */
b368969c 519class NXCPMessage;
400e55c4
VK
520
521/**
3fe40613
VK
522 * String map class
523 */
524class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
525{
526public:
527 StringMap() : StringMapBase(true) { }
fb986055 528 StringMap(const StringMap &src);
3fe40613 529 virtual ~StringMap();
c577e360 530
fb986055 531 StringMap& operator =(const StringMap &src);
40b7416c 532
3fe40613
VK
533 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
534 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
967893bb 535 void set(const TCHAR *key, UINT32 value);
5ad2167d 536
a6312bd6
VK
537 void addAll(StringMap *src);
538
3fe40613 539 const TCHAR *get(const TCHAR *key) { return (const TCHAR *)getObject(key); }
967893bb 540 UINT32 getULong(const TCHAR *key, UINT32 defaultValue);
6acb8479 541 bool getBoolean(const TCHAR *key, bool defaultValue);
5ad2167d 542
b368969c 543 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId);
8bad8476
VK
544};
545
3fe40613
VK
546/**
547 * String map template for holding objects as values
548 */
549template <class T> class StringObjectMap : public StringMapBase
550{
551private:
552 static void destructor(void *object) { delete (T*)object; }
6173bea8 553
3fe40613
VK
554public:
555 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
556
557 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
558 T *get(const TCHAR *key) { return (T*)getObject(key); }
3fe40613 559};
6173bea8 560
3fe40613
VK
561/**
562 * String list class
563 */
6173bea8
VK
564class LIBNETXMS_EXPORTABLE StringList
565{
566private:
567 int m_count;
568 int m_allocated;
569 TCHAR **m_values;
570
571public:
572 StringList();
01116597 573 StringList(const StringList *src);
22ee6d97 574 StringList(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
575 ~StringList();
576
577 void add(const TCHAR *value);
578 void addPreallocated(TCHAR *value);
967893bb
VK
579 void add(INT32 value);
580 void add(UINT32 value);
6173bea8 581 void add(INT64 value);
967893bb 582 void add(UINT64 value);
6173bea8 583 void add(double value);
24dc5346 584 void replace(int index, const TCHAR *value);
b0353a33
VK
585 void addOrReplace(int index, const TCHAR *value);
586 void addOrReplacePreallocated(int index, TCHAR *value);
6173bea8 587 void clear();
01116597
VK
588 int size() const { return m_count; }
589 const TCHAR *get(int index) const { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
590 int indexOf(const TCHAR *value) const;
591 int indexOfIgnoreCase(const TCHAR *value) const;
d51f2182 592 void remove(int index);
86c126f5 593 void addAll(const StringList *src);
93dbc1be 594 void merge(const StringList *src, bool matchCase);
7d25a5c5 595 TCHAR *join(const TCHAR *separator);
22ee6d97 596 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
6fbaa926 597 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId);
6173bea8
VK
598};
599
3fe40613 600/**
a58501c0
VK
601 * Entry of string set
602 */
603struct StringSetEntry;
604
605/**
f3603b79
VK
606 * NXCP message
607 */
b368969c 608class NXCPMessage;
f3603b79
VK
609
610/**
a58501c0
VK
611 * String set class
612 */
613class LIBNETXMS_EXPORTABLE StringSet
614{
615private:
616 StringSetEntry *m_data;
617
618public:
619 StringSet();
620 ~StringSet();
621
622 void add(const TCHAR *str);
f3603b79 623 void addPreallocated(TCHAR *str);
a58501c0
VK
624 void remove(const TCHAR *str);
625 void clear();
626
627 int size();
f3603b79 628 bool contains(const TCHAR *str);
5b521bfc 629 bool equals(StringSet *s);
a58501c0 630
fa493140 631 void addAll(StringSet *src);
f3603b79 632 void addAll(TCHAR **strings, int count);
d45f9179 633 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
d6c4cdc1 634 void addAllPreallocated(TCHAR **strings, int count);
a58501c0 635 void forEach(bool (*cb)(const TCHAR *, void *), void *userData);
f3603b79 636
b368969c
VK
637 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId);
638 void addAllFromMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId, bool clearBeforeAdd, bool toUppercase);
c262bc08 639
d45f9179 640 String join(const TCHAR *separator);
a58501c0
VK
641};
642
643/**
296ae03d
VK
644 * Opaque hash map entry structure
645 */
646struct HashMapEntry;
647
648/**
649 * Hash map base class (for fixed size non-pointer keys)
650 */
651class LIBNETXMS_EXPORTABLE HashMapBase
652{
653private:
654 HashMapEntry *m_data;
655 bool m_objectOwner;
656 unsigned int m_keylen;
657
658 HashMapEntry *find(const void *key);
659 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
660
661protected:
662 void (*m_objectDestructor)(void *);
663
664 HashMapBase(bool objectOwner, unsigned int keylen);
665
666 void *_get(const void *key);
667 void _set(const void *key, void *value);
668 void _remove(const void *key);
669
670 bool _contains(const void *key) { return find(key) != NULL; }
671
672public:
673 virtual ~HashMapBase();
674
675 void setOwner(bool owner) { m_objectOwner = owner; }
676 void clear();
677
678 int size();
679
53c3d1e7 680 EnumerationCallbackResult forEach(EnumerationCallbackResult (*cb)(const void *, const void *, void *), void *userData);
296ae03d
VK
681 const void *findElement(bool (*comparator)(const void *, const void *, void *), void *userData);
682};
683
684/**
685 * Hash map template for holding objects as values
686 */
687template <class K, class V> class HashMap : public HashMapBase
688{
689private:
690 static void destructor(void *object) { delete (V*)object; }
691
692public:
693 HashMap(bool objectOwner = false) : HashMapBase(objectOwner, sizeof(K)) { m_objectDestructor = destructor; }
694
695 V *get(const K& key) { return (V*)_get(&key); }
696 void set(const K& key, V *value) { _set(&key, (void *)value); }
697 void remove(const K& key) { _remove(&key); }
698 bool contains(const K& key) { return _contains(&key); }
699};
700
701/**
57f34c0a
VK
702 * Byte stream
703 */
704class LIBNETXMS_EXPORTABLE ByteStream
705{
706private:
707 BYTE *m_data;
708 size_t m_size;
709 size_t m_allocated;
710 size_t m_pos;
3cf3dedd 711 size_t m_allocationStep;
57f34c0a
VK
712
713public:
714 ByteStream(size_t initial = 8192);
715 ByteStream(const void *data, size_t size);
716 virtual ~ByteStream();
717
4b47d7d7
VK
718 static ByteStream *load(const TCHAR *file);
719
720 void seek(size_t pos) { if (pos <= m_size) m_pos = pos; }
57f34c0a
VK
721 size_t pos() { return m_pos; }
722 size_t size() { return m_size; }
723 bool eos() { return m_pos == m_size; }
724
3cf3dedd
VK
725 void setAllocationStep(size_t s) { m_allocationStep = s; }
726
727 const BYTE *buffer(size_t *size) { *size = m_size; return m_data; }
57f34c0a 728
4b47d7d7 729 void write(const void *data, size_t size);
57f34c0a
VK
730 void write(char c) { write(&c, 1); }
731 void write(BYTE b) { write(&b, 1); }
732 void write(INT16 n) { UINT16 x = htons((UINT16)n); write(&x, 2); }
733 void write(UINT16 n) { UINT16 x = htons(n); write(&x, 2); }
734 void write(INT32 n) { UINT32 x = htonl((UINT32)n); write(&x, 4); }
735 void write(UINT32 n) { UINT32 x = htonl(n); write(&x, 4); }
4b47d7d7
VK
736 void write(INT64 n) { UINT64 x = htonq((UINT64)n); write(&x, 8); }
737 void write(UINT64 n) { UINT64 x = htonq(n); write(&x, 8); }
738 void write(double n) { double x = htond(n); write(&x, 8); }
739 void writeString(const TCHAR *s);
57f34c0a 740
4b47d7d7 741 size_t read(void *buffer, size_t count);
57f34c0a
VK
742 char readChar() { return !eos() ? (char)m_data[m_pos++] : 0; }
743 BYTE readByte() { return !eos() ? m_data[m_pos++] : 0; }
744 INT16 readInt16();
745 UINT16 readUInt16();
746 INT32 readInt32();
747 UINT32 readUInt32();
4b47d7d7
VK
748 INT64 readInt64();
749 UINT64 readUInt64();
750 double readDouble();
751 TCHAR *readString();
752
753 bool save(int f);
57f34c0a
VK
754};
755
756/**
25fe6265
VK
757 * Auxilliary class for objects which counts references and
758 * destroys itself wheren reference count falls to 0
759 */
7f632dfe
VK
760class LIBNETXMS_EXPORTABLE RefCountObject
761{
762private:
7785322f 763 VolatileCounter m_refCount;
7f632dfe
VK
764
765public:
7785322f
VK
766 RefCountObject()
767 {
768 m_refCount = 1;
769 }
b549a0f8 770 virtual ~RefCountObject();
7785322f
VK
771
772 void incRefCount()
773 {
774 InterlockedIncrement(&m_refCount);
775 }
776
777 void decRefCount()
778 {
779 if (InterlockedDecrement(&m_refCount) == 0)
780 delete this;
781 }
7f632dfe
VK
782};
783
b368969c 784class NXCPMessage;
967893bb
VK
785
786/**
787 * Table column definition
788 */
789class LIBNETXMS_EXPORTABLE TableColumnDefinition
790{
791private:
792 TCHAR *m_name;
793 TCHAR *m_displayName;
794 INT32 m_dataType;
795 bool m_instanceColumn;
796
797public:
798 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
b368969c 799 TableColumnDefinition(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
800 TableColumnDefinition(TableColumnDefinition *src);
801 ~TableColumnDefinition();
802
b368969c 803 void fillMessage(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
804
805 const TCHAR *getName() { return m_name; }
806 const TCHAR *getDisplayName() { return m_displayName; }
807 INT32 getDataType() { return m_dataType; }
808 bool isInstanceColumn() { return m_instanceColumn; }
809
810 void setDataType(INT32 type) { m_dataType = type; }
811 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 812 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
813};
814
815/**
d6bbfa4e
VK
816 * Table cell
817 */
818class TableCell
819{
820private:
821 TCHAR *m_value;
822 int m_status;
823
824public:
825 TableCell() { m_value = NULL; m_status = -1; }
c83f6449
VK
826 TableCell(const TCHAR *value) { m_value = _tcsdup_ex(value); m_status = -1; }
827 TableCell(const TCHAR *value, int status) { m_value = _tcsdup_ex(value); m_status = status; }
828 TableCell(TableCell *src) { m_value = _tcsdup_ex(src->m_value); m_status = src->m_status; }
d6bbfa4e
VK
829 ~TableCell() { safe_free(m_value); }
830
c83f6449 831 void set(const TCHAR *value, int status) { safe_free(m_value); m_value = _tcsdup_ex(value); m_status = status; }
d6bbfa4e
VK
832 void setPreallocated(TCHAR *value, int status) { safe_free(m_value); m_value = value; m_status = status; }
833
834 const TCHAR *getValue() { return m_value; }
c83f6449 835 void setValue(const TCHAR *value) { safe_free(m_value); m_value = _tcsdup_ex(value); }
d6bbfa4e
VK
836 void setPreallocatedValue(TCHAR *value) { safe_free(m_value); m_value = value; }
837
838 int getStatus() { return m_status; }
839 void setStatus(int status) { m_status = status; }
840};
841
842/**
843 * Table row
844 */
845class TableRow
846{
847private:
848 ObjectArray<TableCell> *m_cells;
849 UINT32 m_objectId;
bac74bf7 850
d6bbfa4e
VK
851public:
852 TableRow(int columnCount);
853 TableRow(TableRow *src);
854 ~TableRow() { delete m_cells; }
855
856 void addColumn() { m_cells->add(new TableCell); }
857 void deleteColumn(int index) { m_cells->remove(index); }
bac74bf7 858
d6bbfa4e
VK
859 void set(int index, const TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->set(value, status); }
860 void setPreallocated(int index, TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocated(value, status); }
861
862 void setValue(int index, const TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setValue(value); }
863 void setPreallocatedValue(int index, TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocatedValue(value); }
864
865 void setStatus(int index, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setStatus(status); }
866
867 const TCHAR *getValue(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getValue() : NULL; }
868 int getStatus(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getStatus() : -1; }
869
870 UINT32 getObjectId() { return m_objectId; }
871 void setObjectId(UINT32 id) { m_objectId = id; }
872};
873
874/**
967893bb
VK
875 * Class for table data storage
876 */
a0ddfb29 877class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
878{
879private:
d6bbfa4e 880 ObjectArray<TableRow> *m_data;
967893bb
VK
881 ObjectArray<TableColumnDefinition> *m_columns;
882 TCHAR *m_title;
883 int m_source;
d6bbfa4e 884 bool m_extendedFormat;
967893bb 885
b368969c 886 void createFromMessage(NXCPMessage *msg);
967893bb 887 void destroy();
63ff3c9d 888 bool parseXML(const char *xml);
967893bb
VK
889
890public:
891 Table();
a0ddfb29 892 Table(Table *src);
b368969c 893 Table(NXCPMessage *msg);
b549a0f8 894 virtual ~Table();
967893bb 895
b368969c
VK
896 int fillMessage(NXCPMessage &msg, int offset, int rowLimit);
897 void updateFromMessage(NXCPMessage *msg);
967893bb 898
a0ddfb29
VK
899 void addAll(Table *src);
900 void copyRow(Table *src, int row);
967893bb 901
d6bbfa4e
VK
902 int getNumRows() { return m_data->size(); }
903 int getNumColumns() { return m_columns->size(); }
967893bb
VK
904 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
905 int getSource() { return m_source; }
906
d6bbfa4e
VK
907 bool isExtendedFormat() { return m_extendedFormat; }
908 void setExtendedFormat(bool ext) { m_extendedFormat = ext; }
909
967893bb
VK
910 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
911 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
912 int getColumnIndex(const TCHAR *name);
913 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
914
c83f6449 915 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = _tcsdup_ex(title); }
967893bb 916 void setSource(int source) { m_source = source; }
8dba1eac 917 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
967893bb
VK
918 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
919 int addRow();
920
25076d53
VK
921 void deleteRow(int row);
922 void deleteColumn(int col);
923
967893bb
VK
924 void setAt(int nRow, int nCol, INT32 nData);
925 void setAt(int nRow, int nCol, UINT32 dwData);
926 void setAt(int nRow, int nCol, double dData);
927 void setAt(int nRow, int nCol, INT64 nData);
928 void setAt(int nRow, int nCol, UINT64 qwData);
929 void setAt(int nRow, int nCol, const TCHAR *pszData);
930 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
931
d6bbfa4e
VK
932 void set(int nCol, INT32 nData) { setAt(getNumRows() - 1, nCol, nData); }
933 void set(int nCol, UINT32 dwData) { setAt(getNumRows() - 1, nCol, dwData); }
934 void set(int nCol, double dData) { setAt(getNumRows() - 1, nCol, dData); }
935 void set(int nCol, INT64 nData) { setAt(getNumRows() - 1, nCol, nData); }
936 void set(int nCol, UINT64 qwData) { setAt(getNumRows() - 1, nCol, qwData); }
d65c16a8
VK
937 void set(int nCol, const TCHAR *data) { setAt(getNumRows() - 1, nCol, data); }
938#ifdef UNICODE
939 void set(int nCol, const char *data) { setPreallocatedAt(getNumRows() - 1, nCol, WideStringFromMBString(data)); }
940#else
941 void set(int nCol, const WCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, MBStringFromWideString(data)); }
942#endif
943 void setPreallocated(int nCol, TCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, data); }
d6bbfa4e
VK
944
945 void setStatusAt(int row, int col, int status);
946 void setStatus(int col, int status) { setStatusAt(getNumRows() - 1, col, status); }
967893bb
VK
947
948 const TCHAR *getAsString(int nRow, int nCol);
949 INT32 getAsInt(int nRow, int nCol);
950 UINT32 getAsUInt(int nRow, int nCol);
951 INT64 getAsInt64(int nRow, int nCol);
952 UINT64 getAsUInt64(int nRow, int nCol);
953 double getAsDouble(int nRow, int nCol);
22aaa779 954
d6bbfa4e
VK
955 int getStatus(int nRow, int nCol);
956
22aaa779 957 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 958 int findRowByInstance(const TCHAR *instance);
d6bbfa4e
VK
959
960 UINT32 getObjectId(int row) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getObjectId() : 0; }
961 void setObjectId(int row, UINT32 id) { TableRow *r = m_data->get(row); if (r != NULL) r->setObjectId(id); }
a3d3f9d5 962
63ff3c9d
VK
963 static Table *createFromXML(const char *xml);
964 TCHAR *createXML();
967893bb
VK
965};
966
25fe6265 967/**
c11eee9b
VK
968 * sockaddr buffer
969 */
970union SockAddrBuffer
971{
972 struct sockaddr_in sa4;
973#ifdef WITH_IPV6
974 struct sockaddr_in6 sa6;
975#endif
976};
977
978/**
979 * sockaddr length calculation
980 */
981#ifdef WITH_IPV6
982#define SA_LEN(sa) (((sa)->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
983#else
984#define SA_LEN(sa) sizeof(struct sockaddr_in)
985#endif
986
987/**
5f6e8d09
VK
988 * Get port number from SockAddrBuffer
989 */
990#ifdef WITH_IPV6
991#define SA_PORT(sa) ((((struct sockaddr *)sa)->sa_family == AF_INET) ? ((struct sockaddr_in *)sa)->sin_port : ((struct sockaddr_in6 *)sa)->sin6_port)
992#else
993#define SA_PORT(sa) (((struct sockaddr_in *)sa)->sin_port)
994#endif
995
996/**
30592fa6
VK
997 * Compare addresses in sockaddr
998 */
999inline bool SocketAddressEquals(struct sockaddr *a1, struct sockaddr *a2)
1000{
1001 if (a1->sa_family != a2->sa_family)
1002 return false;
1003 if (a1->sa_family == AF_INET)
1004 return ((struct sockaddr_in *)a1)->sin_addr.s_addr == ((struct sockaddr_in *)a2)->sin_addr.s_addr;
1005#ifdef WITH_IPV6
1006 if (a1->sa_family == AF_INET6)
1007 return !memcmp(((struct sockaddr_in6 *)a1)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)a2)->sin6_addr.s6_addr, 16);
1008#endif
1009 return false;
1010}
1011
1012/**
9319c166
VK
1013 * IP address
1014 */
1015class LIBNETXMS_EXPORTABLE InetAddress
1016{
9319c166 1017private:
8c75ad41
VK
1018 short m_maskBits;
1019 short m_family;
1020 union
1021 {
1022 UINT32 v4;
1023 BYTE v6[16];
1024 } m_addr;
9319c166
VK
1025
1026public:
8c75ad41
VK
1027 InetAddress();
1028 InetAddress(UINT32 addr);
c75e9ee4 1029 InetAddress(UINT32 addr, UINT32 mask);
4203dcb5 1030 InetAddress(const BYTE *addr, int maskBits = 128);
9319c166 1031
8c75ad41
VK
1032 bool isAnyLocal() const;
1033 bool isLoopback() const;
1034 bool isMulticast() const;
1035 bool isBroadcast() const;
4203dcb5 1036 bool isLinkLocal() const;
8c75ad41 1037 bool isValid() const { return m_family != AF_UNSPEC; }
4203dcb5 1038 bool isValidUnicast() const { return isValid() && !isAnyLocal() && !isLoopback() && !isMulticast() && !isBroadcast() && !isLinkLocal(); }
9319c166 1039
8c75ad41 1040 int getFamily() const { return m_family; }
c7853753
VK
1041 UINT32 getAddressV4() const { return (m_family == AF_INET) ? m_addr.v4 : 0; }
1042 const BYTE *getAddressV6() const { return (m_family == AF_INET6) ? m_addr.v6 : (const BYTE *)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; }
9319c166 1043
8c75ad41 1044 bool contain(const InetAddress &a) const;
43709a0c 1045 bool sameSubnet(const InetAddress &a) const;
8c75ad41
VK
1046 bool equals(const InetAddress &a) const;
1047 int compareTo(const InetAddress &a) const;
9319c166 1048
8c75ad41
VK
1049 void setMaskBits(int m) { m_maskBits = m; }
1050 int getMaskBits() const { return m_maskBits; }
c75e9ee4 1051 int getHostBits() const { return (m_family == AF_INET) ? (32 - m_maskBits) : (128 - m_maskBits); }
9319c166 1052
463b1d34
VK
1053 InetAddress getSubnetAddress() const;
1054 bool isSubnetBroadcast(int maskBits) const;
ffabd7f8 1055
8c75ad41
VK
1056 String toString() const;
1057 TCHAR *toString(TCHAR *buffer) const;
9319c166 1058
43709a0c 1059 BYTE *buildHashKey(BYTE *key) const;
f3b94fc5 1060
43709a0c
VK
1061 TCHAR *getHostByAddr(TCHAR *buffer, size_t buflen) const;
1062
1063 struct sockaddr *fillSockAddr(SockAddrBuffer *buffer, UINT16 port = 0) const;
c11eee9b 1064
c7853753
VK
1065 static InetAddress resolveHostName(const WCHAR *hostname, int af = AF_INET);
1066 static InetAddress resolveHostName(const char *hostname, int af = AF_INET);
f3b94fc5
VK
1067 static InetAddress parse(const WCHAR *str);
1068 static InetAddress parse(const char *str);
8c75ad41 1069 static InetAddress createFromSockaddr(struct sockaddr *s);
dcc7a2b0
VK
1070
1071 static const InetAddress INVALID;
48313f37 1072 static const InetAddress LOOPBACK;
dcc7a2b0
VK
1073};
1074
1075/**
1076 * IP address list
1077 */
1078class LIBNETXMS_EXPORTABLE InetAddressList
1079{
1080private:
1081 ObjectArray<InetAddress> *m_list;
1082
1083 int indexOf(const InetAddress& addr) const;
1084
1085public:
1086 InetAddressList();
1087 ~InetAddressList();
1088
1089 void add(const InetAddress& addr);
c30c0c0f
VK
1090 void add(const InetAddressList& addrList);
1091 void replace(const InetAddress& addr);
dcc7a2b0 1092 void remove(const InetAddress& addr);
c30c0c0f 1093 void clear() { m_list->clear(); }
4203dcb5 1094 const InetAddress& get(int index) const { const InetAddress *a = m_list->get(index); return (a != NULL) ? *a : InetAddress::INVALID; }
dcc7a2b0
VK
1095
1096 int size() const { return m_list->size(); }
1097 bool hasAddress(const InetAddress& addr) const { return indexOf(addr) != -1; }
4203dcb5 1098 const InetAddress& findAddress(const InetAddress& addr) const { int idx = indexOf(addr); return (idx != -1) ? *m_list->get(idx) : InetAddress::INVALID; }
c34b611c 1099 const InetAddress& findSameSubnetAddress(const InetAddress& addr) const;
dcc7a2b0 1100 const InetAddress& getFirstUnicastAddress() const;
c34b611c
VK
1101 const InetAddress& getFirstUnicastAddressV4() const;
1102 bool hasValidUnicastAddress() const { return getFirstUnicastAddress().isValid(); }
c30c0c0f 1103 bool isLoopbackOnly() const;
dcc7a2b0
VK
1104
1105 const ObjectArray<InetAddress> *getList() const { return m_list; }
40c8f341
VK
1106
1107 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
9319c166
VK
1108};
1109
1110/**
25fe6265
VK
1111 * Network connection
1112 */
58ee2f43
VK
1113class LIBNETXMS_EXPORTABLE SocketConnection
1114{
1115protected:
1116 SOCKET m_socket;
1117 char m_data[4096];
1118 int m_dataPos;
1119
1120public:
1121 SocketConnection();
1122 virtual ~SocketConnection();
1123
967893bb
VK
1124 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1125 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
1126 void disconnect();
1127
967893bb
VK
1128 bool canRead(UINT32 timeout);
1129 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43 1130 bool waitForText(const char *text, int timeout);
cc022855 1131
58ee2f43 1132 int write(const char *pBuff, int nSize);
1671f85d
VK
1133 bool writeLine(const char *line);
1134
967893bb 1135 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
1136};
1137
030a0779
VK
1138/**
1139 * Telnet connection - handles all telnet negotiation
1140 */
def5b792
AK
1141class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
1142{
030a0779 1143protected:
967893bb
VK
1144 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1145 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 1146
def5b792 1147public:
967893bb 1148 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 1149
967893bb
VK
1150 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
1151 bool connect(UINT32 ip, WORD port, UINT32 timeout);
1152 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1153 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
1154};
1155
56fa1092
VK
1156/**
1157 * Postal address representation
1158 */
1159class LIBNETXMS_EXPORTABLE PostalAddress
1160{
1161private:
1162 TCHAR *m_country;
1163 TCHAR *m_city;
1164 TCHAR *m_streetAddress;
1165 TCHAR *m_postcode;
1166
1167public:
1168 PostalAddress();
1169 PostalAddress(const TCHAR *country, const TCHAR *city, const TCHAR *streetAddress, const TCHAR *postcode);
1170 ~PostalAddress();
1171
1172 const TCHAR *getCountry() { return CHECK_NULL_EX(m_country); }
1173 const TCHAR *getCity() { return CHECK_NULL_EX(m_city); }
1174 const TCHAR *getStreetAddress() { return CHECK_NULL_EX(m_streetAddress); }
1175 const TCHAR *getPostCode() { return CHECK_NULL_EX(m_postcode); }
1176
1177 void setCountry(const TCHAR *country) { safe_free(m_country); m_country = _tcsdup_ex(country); }
1178 void setCity(const TCHAR *city) { safe_free(m_city); m_city = _tcsdup_ex(city); }
1179 void setStreetAddress(const TCHAR *streetAddress) { safe_free(m_streetAddress); m_streetAddress = _tcsdup_ex(streetAddress); }
1180 void setPostCode(const TCHAR *postcode) { safe_free(m_postcode); m_postcode = _tcsdup_ex(postcode); }
1181};
1182
b160350d
VK
1183#endif /* __cplusplus */
1184
030a0779
VK
1185/**
1186 * Configuration item template for configuration loader
1187 */
4826c8a5
VK
1188typedef struct
1189{
e2e66145
VK
1190 TCHAR token[64];
1191 BYTE type;
1192 BYTE separator; // Separator character for lists
1193 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
e95680e5 1194 UINT64 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
e2e66145
VK
1195 UINT32 bufferPos; // Should be set to 0
1196 void *buffer;
1197 void *overrideIndicator;
4826c8a5 1198} NX_CFG_TEMPLATE;
dcb44c9a 1199
030a0779
VK
1200/**
1201 * Code translation structure
1202 */
1b56f3c5 1203typedef struct __CODE_TO_TEXT
bc858635
VK
1204{
1205 int code;
1206 const TCHAR *text;
1207} CODE_TO_TEXT;
1208
e2e66145
VK
1209/**
1210 * getopt() prototype if needed
1211 */
df9812a9 1212#if USE_BUNDLED_GETOPT
99e50bb7 1213#include <netxms_getopt.h>
3046f9a4
VK
1214#endif
1215
f2fdf1b5 1216//
3d1cb7d6
VK
1217// Structures for opendir() / readdir() / closedir()
1218//
1219
1220#ifdef _WIN32
1221
7f9e0c00 1222typedef struct dirent
3d1cb7d6
VK
1223{
1224 long d_ino; /* inode number (not used by MS-DOS) */
1225 int d_namlen; /* Name length */
1226 char d_name[257]; /* file name */
1227} _DIRECT;
1228
1229typedef struct _dir_struc
1230{
1231 char *start; /* Starting position */
1232 char *curr; /* Current position */
1233 long size; /* Size of string table */
1234 long nfiles; /* number if filenames in table */
7f9e0c00 1235 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
1236} DIR;
1237
5ea2db30
VK
1238#ifdef UNICODE
1239
1240typedef struct dirent_w
1241{
1242 long d_ino; /* inode number (not used by MS-DOS) */
1243 int d_namlen; /* Name length */
1244 WCHAR d_name[257]; /* file name */
1245} _DIRECTW;
1246
1247typedef struct _dir_struc_w
1248{
1249 WCHAR *start; /* Starting position */
1250 WCHAR *curr; /* Current position */
1251 long size; /* Size of string table */
1252 long nfiles; /* number if filenames in table */
1253 struct dirent_w dirstr; /* Directory structure to return */
1254} DIRW;
1255
1256#define _TDIR DIRW
1257#define _TDIRECT _DIRECTW
1258#define _tdirent dirent_w
1259
1260#else
1261
1262#define _TDIR DIR
1263#define _TDIRECT _DIRECT
1264#define _tdirent dirent
1265
37d62364
AK
1266#endif
1267
76b1d3d8
VK
1268#else /* not _WIN32 */
1269
1270typedef struct dirent_w
1271{
1272 long d_ino; /* inode number */
1273 WCHAR d_name[257]; /* file name */
1274} _DIRECTW;
1275
1276typedef struct _dir_struc_w
1277{
1278 DIR *dir; /* Original non-unicode structure */
1279 struct dirent_w dirstr; /* Directory structure to return */
1280} DIRW;
1281
3d1cb7d6
VK
1282#endif /* _WIN32 */
1283
1284
1285//
e9246d26
VK
1286// Functions
1287//
1288
b50f1100 1289#ifdef __cplusplus
18c575eb 1290
2e590d1a
VK
1291inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
1292{
912b994d 1293#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1294 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
912b994d 1295#else
2e590d1a
VK
1296 _tcsncpy(pszDest, pszSrc, nLen - 1);
1297 pszDest[nLen - 1] = 0;
912b994d 1298#endif
2e590d1a
VK
1299 return pszDest;
1300}
f3387429
VK
1301
1302#ifdef UNICODE
2e590d1a
VK
1303inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
1304{
f3387429 1305#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1306 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429 1307#else
2e590d1a
VK
1308 strncpy(pszDest, pszSrc, nLen - 1);
1309 pszDest[nLen - 1] = 0;
c81979bc 1310#endif
2e590d1a
VK
1311 return pszDest;
1312}
c81979bc 1313#else
f3387429 1314#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
1315#endif
1316
967893bb 1317int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
5e60b759
VK
1318int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
1319int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
18c575eb
VK
1320bool LIBNETXMS_EXPORTABLE RecvAll(SOCKET s, void *buffer, size_t size, UINT32 timeout);
1321
b42c9187
VK
1322SOCKET LIBNETXMS_EXPORTABLE ConnectToHost(const InetAddress& addr, UINT16 port, UINT32 timeout);
1323
b0608204 1324#endif /* __cplusplus */
c81979bc
VK
1325
1326#ifdef __cplusplus
e9246d26
VK
1327extern "C"
1328{
b50f1100 1329#endif
735bac5c 1330
076a55c4 1331#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc 1332#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
735bac5c
VK
1333void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
1334#define wcsupr __wcsupr
cc022855 1335#endif
735bac5c
VK
1336void LIBNETXMS_EXPORTABLE __strupr(char *in);
1337#define strupr __strupr
7e679c4b 1338#endif
cc022855 1339
735bac5c 1340void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
b8a8d064
VK
1341 int (*compare)(const void *, const void *, void *));
1342
735bac5c 1343INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
d35ed0aa 1344
735bac5c
VK
1345UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
1346UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
1347#ifdef UNICODE
1348#define FileSize FileSizeW
1349#else
1350#define FileSize FileSizeA
1351#endif
670c7f3d 1352
735bac5c
VK
1353int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1354TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 1355#ifdef UNICODE
735bac5c 1356char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
1357#else
1358#define IpToStrA IpToStr
e0f99bf0 1359#endif
8c75ad41 1360TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(const BYTE *addr, TCHAR *buffer);
735bac5c 1361TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
60557d06 1362
735bac5c
VK
1363UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *name);
1364UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameW(const WCHAR *name);
08b214c6 1365#ifdef UNICODE
cfe20cce 1366#define ResolveHostName ResolveHostNameW
08b214c6 1367#else
cfe20cce 1368#define ResolveHostName ResolveHostNameA
08b214c6 1369#endif
a1dc75b0 1370
735bac5c
VK
1371void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1372void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
31521248 1373
735bac5c
VK
1374WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *data, size_t size, WCHAR *pStr);
1375char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *data, size_t size, char *pStr);
d7970ee7
VK
1376#ifdef UNICODE
1377#define BinToStr BinToStrW
1378#else
1379#define BinToStr BinToStrA
1380#endif
1381
735bac5c
VK
1382size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1383size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
52db3ed1
VK
1384#ifdef UNICODE
1385#define StrToBin StrToBinW
1386#else
1387#define StrToBin StrToBinA
1388#endif
cc022855 1389
735bac5c 1390TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
b50f1100 1391
735bac5c
VK
1392void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1393void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
b07c50cc
VK
1394#ifdef UNICODE
1395#define StrStrip StrStripW
1396#else
1397#define StrStrip StrStripA
1398#endif
1399
735bac5c
VK
1400const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
1401const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
1402#ifdef UNICODE
1403#define ExtractWord ExtractWordW
1404#else
1405#define ExtractWord ExtractWordA
1406#endif
1407
735bac5c
VK
1408int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1409int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
3583a400
VK
1410#ifdef UNICODE
1411#define NumChars NumCharsW
1412#else
1413#define NumChars NumCharsA
1414#endif
ada97259 1415
735bac5c
VK
1416void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1417void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
35f836fe
VK
1418#ifdef UNICODE
1419#define RemoveTrailingCRLF RemoveTrailingCRLFW
1420#else
1421#define RemoveTrailingCRLF RemoveTrailingCRLFA
1422#endif
1423
4893b4aa
VK
1424BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *str, const char *expr, bool matchCase);
1425BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *str, const WCHAR *expr, bool matchCase);
cafe592d
VK
1426#ifdef UNICODE
1427#define RegexpMatch RegexpMatchW
1428#else
1429#define RegexpMatch RegexpMatchA
1430#endif
1431
735bac5c 1432const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
d77c193c 1433BOOL LIBNETXMS_EXPORTABLE CreateFolder(const TCHAR *directory);
818f8c94 1434TCHAR LIBNETXMS_EXPORTABLE *Trim(TCHAR *str);
735bac5c
VK
1435bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
1436TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
0a145c10 1437int LIBNETXMS_EXPORTABLE GetLastMonthDay(struct tm *currTime);
1438#ifdef __cplusplus
1439bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime = 0);
1440#else
1441bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime);
1442#endif
1443
ada97259 1444
f7794b68 1445#ifdef __cplusplus
735bac5c 1446BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
f7794b68 1447#endif
735bac5c
VK
1448BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
1449/* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1450const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
1451void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
1452BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 1453#ifdef UNICODE
735bac5c 1454BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
1455#else
1456#define LoadFileA LoadFile
1457#endif
1458
735bac5c
VK
1459UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1460void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
1461void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
1462void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
1463void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
1718cc2a 1464void LIBNETXMS_EXPORTABLE CalculateSHA256Hash(const unsigned char *data, size_t len, unsigned char *hash);
735bac5c
VK
1465BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1466BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
1467BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 1468
981d246a
VK
1469void LIBNETXMS_EXPORTABLE GenerateRandomBytes(BYTE *buffer, size_t size);
1470
735bac5c
VK
1471void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1472void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
4e2debcc 1473
f2436b76 1474bool LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd, size_t bufferLenght);
6b1282e7 1475
735bac5c 1476int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 1477
735bac5c
VK
1478HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
1479void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
1480void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 1481
735bac5c
VK
1482bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1483bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1484long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
289d126b 1485
735bac5c
VK
1486bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1487bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1488long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
289d126b
VK
1489
1490#ifdef UNICODE
1491#define ExtractNamedOptionValue ExtractNamedOptionValueW
1492#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1493#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1494#else
1495#define ExtractNamedOptionValue ExtractNamedOptionValueA
1496#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1497#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1498#endif
ae9cddaf 1499
bc858635 1500#ifdef __cplusplus
735bac5c 1501const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
bc858635 1502#else
735bac5c 1503const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
bc858635
VK
1504#endif
1505
aa13cfd9 1506#ifdef _WIN32
735bac5c
VK
1507TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
1508BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
1509INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 1510#endif
696fc54f 1511
de4c0511
VK
1512#if !HAVE_DAEMON || !HAVE_DECL_DAEMON
1513int LIBNETXMS_EXPORTABLE __daemon(int nochdir, int noclose);
1514#define daemon __daemon
696fc54f 1515#endif
4332e9d8 1516
735bac5c 1517UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1518
4332e9d8 1519#ifndef _WIN32
735bac5c
VK
1520BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
1521int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
1522 int cchWideChar, char *pByteStr, int cchByteChar,
1523 char *pDefaultChar, BOOL *pbUsedDefChar);
1524int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
1525 int cchByteChar, WCHAR *pWideCharStr,
1526 int cchWideChar);
04dbaf62 1527
6b93c954 1528#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
735bac5c 1529int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1530#endif
1531#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
735bac5c 1532UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1533#endif
1534#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
b6ec31bb 1535UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strdup(const UCS2CHAR *pStr);
4332e9d8 1536#endif
8b86c5dc 1537
735bac5c
VK
1538size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1539size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
1540UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1541char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1542
735bac5c
VK
1543int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1544int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1545int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1546int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1547int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1548int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1549
735bac5c
VK
1550int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1551int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
1552int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
1553int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1554int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
1555int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1556
3e39fcde 1557#endif /* _WIN32 */
8b86c5dc 1558
3d1058ed 1559#ifdef _WITH_ENCRYPTION
735bac5c 1560WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
3d1058ed
VK
1561#endif
1562
8b86c5dc 1563#ifdef UNICODE_UCS4
735bac5c
VK
1564size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1565size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1566size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1567UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1568WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1569#endif
8b86c5dc 1570
ebe98224
VK
1571size_t LIBNETXMS_EXPORTABLE utf8_to_mb(const char *src, int srcLen, char *dst, int dstLen);
1572size_t LIBNETXMS_EXPORTABLE mb_to_utf8(const char *src, int srcLen, char *dst, int dstLen);
1573char LIBNETXMS_EXPORTABLE *MBStringFromUTF8String(const char *s);
1574char LIBNETXMS_EXPORTABLE *UTF8StringFromMBString(const char *s);
1575
842c22f4 1576#if !defined(_WIN32) && !HAVE_WSTAT
735bac5c 1577int wstat(const WCHAR *_path, struct stat *_sbuf);
842c22f4
AK
1578#endif
1579
ef856d02 1580#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1581
f3387429 1582#if !HAVE_WPOPEN
735bac5c 1583FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
f3387429 1584#endif
8b86c5dc 1585#if !HAVE_WFOPEN
735bac5c 1586FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
8b86c5dc 1587#endif
8c2489aa 1588#if HAVE_FOPEN64 && !HAVE_WFOPEN64
735bac5c 1589FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
8c2489aa 1590#endif
8b86c5dc 1591#if !HAVE_WOPEN
735bac5c 1592int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1593#endif
f3387429 1594#if !HAVE_WCHMOD
735bac5c 1595int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
f3387429 1596#endif
b07c50cc 1597#if !HAVE_WCHDIR
735bac5c 1598int wchdir(const WCHAR *_path);
b07c50cc
VK
1599#endif
1600#if !HAVE_WMKDIR
735bac5c 1601int wmkdir(const WCHAR *_path, int mode);
b07c50cc
VK
1602#endif
1603#if !HAVE_WRMDIR
735bac5c 1604int wrmdir(const WCHAR *_path);
b07c50cc 1605#endif
ccc34207 1606#if !HAVE_WRENAME
735bac5c 1607int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
ccc34207
VK
1608#endif
1609#if !HAVE_WUNLINK
735bac5c 1610int wunlink(const WCHAR *_path);
ccc34207 1611#endif
b07c50cc 1612#if !HAVE_WREMOVE
735bac5c 1613int wremove(const WCHAR *_path);
b07c50cc
VK
1614#endif
1615#if !HAVE_WSYSTEM
735bac5c 1616int wsystem(const WCHAR *_cmd);
b07c50cc 1617#endif
dda7c270 1618#if !HAVE_WMKSTEMP
735bac5c 1619int wmkstemp(WCHAR *_template);
dda7c270 1620#endif
b07c50cc 1621#if !HAVE_WACCESS
735bac5c 1622int waccess(const WCHAR *_path, int mode);
b07c50cc 1623#endif
273da39a 1624#if !HAVE_WGETENV
735bac5c 1625WCHAR *wgetenv(const WCHAR *_string);
273da39a 1626#endif
f3387429 1627#if !HAVE_WCTIME
735bac5c 1628WCHAR *wctime(const time_t *timep);
f3387429 1629#endif
0186b5da 1630#if !HAVE_PUTWS
735bac5c 1631int putws(const WCHAR *s);
0186b5da 1632#endif
9757ca46 1633#if !HAVE_WCSERROR && (HAVE_STRERROR || HAVE_DECL_STRERROR)
735bac5c 1634WCHAR *wcserror(int errnum);
d49d7500
VK
1635#endif
1636#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06 1637#if HAVE_POSIX_STRERROR_R
735bac5c 1638int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
b0823d06 1639#else
735bac5c 1640WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
d49d7500 1641#endif
b0823d06 1642#endif
e05f8aaf 1643
ef856d02 1644#endif /* UNICODE && !_WIN32*/
e05f8aaf 1645
8b86c5dc 1646#if !HAVE_STRTOLL
735bac5c 1647INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
8b86c5dc
VK
1648#endif
1649#if !HAVE_STRTOULL
735bac5c 1650UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1651#endif
e05f8aaf
VK
1652
1653#if !HAVE_WCSTOLL
735bac5c 1654INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf
VK
1655#endif
1656#if !HAVE_WCSTOULL
735bac5c 1657UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1658#endif
b50f1100 1659
2f56be29 1660#if !HAVE_WCSLWR && !defined(_WIN32)
735bac5c 1661WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
fdf1ed50
VK
1662#endif
1663
4d978b4a 1664#if !HAVE_WCSCASECMP && !defined(_WIN32)
735bac5c 1665int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
4d978b4a
VK
1666#endif
1667
f4d2c9ba 1668#if !HAVE_WCSNCASECMP && !defined(_WIN32)
735bac5c 1669int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
f4d2c9ba
VK
1670#endif
1671
9138dfe2
VK
1672#ifndef _WIN32
1673
1674#if HAVE_ITOA && !HAVE__ITOA
1675#define _itoa itoa
1676#undef HAVE__ITOA
1677#define HAVE__ITOA 1
1678#endif
1679#if !HAVE__ITOA && !defined(_WIN32)
1680char LIBNETXMS_EXPORTABLE *_itoa(int value, char *str, int base);
1681#endif
1682
1683#if HAVE_ITOW && !HAVE__ITOW
1684#define _itow itow
1685#undef HAVE__ITOW
1686#define HAVE__ITOW 1
1687#endif
1688#if !HAVE__ITOW && !defined(_WIN32)
1689WCHAR LIBNETXMS_EXPORTABLE *_itow(int value, WCHAR *str, int base);
1690#endif
1691
1692#endif /* _WIN32 */
1693
3d1cb7d6 1694#ifdef _WIN32
5ea2db30 1695#ifdef UNICODE
735bac5c
VK
1696DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1697struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1698int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
5ea2db30
VK
1699
1700#define _topendir wopendir
1701#define _treaddir wreaddir
1702#define _tclosedir wclosedir
1703#else
1704#define _topendir opendir
1705#define _treaddir readdir
1706#define _tclosedir closedir
1707#endif
1708
735bac5c
VK
1709DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
1710struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
1711int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1712
76b1d3d8
VK
1713#else /* not _WIN32 */
1714
735bac5c
VK
1715DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1716struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1717int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
76b1d3d8 1718
37d62364 1719#endif
3d1cb7d6 1720
7f9e0c00 1721#if defined(_WIN32) || !(HAVE_SCANDIR)
735bac5c
VK
1722int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1723 int (*select)(const struct dirent *),
1724 int (*compar)(const struct dirent **, const struct dirent **));
1725int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
7f9e0c00
VK
1726#endif
1727
3bf0df9f
VK
1728TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
1729
967893bb 1730BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1731 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1732void LIBNETXMS_EXPORTABLE nxlog_close(void);
1733void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1734BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1735BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1736
1737typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1738void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1739
1740void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
6e383343
VK
1741void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
1742#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1743 __attribute__ ((format(printf, 1, 2)))
1744#endif
1745;
9f2902ac 1746
37a7a862
VK
1747#ifdef _WIN32
1748int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1749int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1750#ifdef UNICODE
1751#define _tmkstemp wmkstemp
1752#else
1753#define _tmkstemp mkstemp
1754#endif
1755#endif
1756
9b13553b
VK
1757#ifndef _WIN32
1758int strcat_s(char *dst, size_t dstSize, const char *src);
1759int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1760#endif
1761
c57d020a
VK
1762#if !HAVE_STRPTIME
1763char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
1764#endif
1765
1766#if !HAVE_TIMEGM
1767time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
1768#endif
1769
6f95ef89
VK
1770#if !HAVE_INET_PTON
1771int LIBNETXMS_EXPORTABLE nx_inet_pton(int af, const char *src, void *dst);
1772#define inet_pton nx_inet_pton
1773#endif
1774
b06ae508 1775int LIBNETXMS_EXPORTABLE GetSleepTime(int hour, int minute, int second);
b60584cf
VK
1776time_t LIBNETXMS_EXPORTABLE ParseDateTimeA(const char *text, time_t defaultValue);
1777time_t LIBNETXMS_EXPORTABLE ParseDateTimeW(const WCHAR *text, time_t defaultValue);
1778
1779#ifdef UNICODE
1780#define ParseDateTime ParseDateTimeW
1781#else
1782#define ParseDateTime ParseDateTimeA
1783#endif
b06ae508 1784
b50f1100 1785#ifdef __cplusplus
e9246d26 1786}
b50f1100 1787#endif
e9246d26 1788
a7ff20a5
VK
1789
1790//
b06ae508 1791// C++ only functions
a7ff20a5
VK
1792//
1793
1794#ifdef __cplusplus
1795
a97f02e1
VK
1796enum nxDirectoryType
1797{
1798 nxDirBin = 0,
1799 nxDirData = 1,
1800 nxDirEtc = 2,
1039d7ee
VK
1801 nxDirLib = 3,
1802 nxDirShare = 4
a97f02e1
VK
1803};
1804
5962498b
VK
1805TCHAR LIBNETXMS_EXPORTABLE *GetHeapInfo();
1806
a97f02e1
VK
1807void LIBNETXMS_EXPORTABLE GetNetXMSDirectory(nxDirectoryType type, TCHAR *dir);
1808
43709a0c
VK
1809UINT32 LIBNETXMS_EXPORTABLE IcmpPing(const InetAddress& addr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
1810
240816b0
VK
1811TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1812String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1813const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1814int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1815UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1816bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1817
1818#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1819void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1820#endif
1821
a7ff20a5
VK
1822#endif
1823
e9246d26 1824#endif /* _nms_util_h_ */