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