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