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