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