implemented JSON serialization for user DB objects
[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() const;
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 /**
1370 * Create JSON string from wide character string
1371 */
1372 inline json_t *json_string_w(const WCHAR *s)
1373 {
1374 if (s == NULL)
1375 return json_null();
1376 char *us = UTF8StringFromWideString(s);
1377 json_t *js = json_string(us);
1378 free(us);
1379 return js;
1380 }
1381
1382 #ifdef UNICODE
1383 #define json_string_t json_string_w
1384 #else
1385 #define json_string_t json_string_a
1386 #endif
1387
1388 /**
1389 * Create JSON array from integer array
1390 */
1391 template<typename T> json_t LIBNETXMS_EXPORTABLE *json_integer_array(const T *values, size_t size)
1392 {
1393 json_t *a = json_array();
1394 for(size_t i = 0; i < size; i++)
1395 json_array_append_new(a, json_integer(values[i]));
1396 return a;
1397 }
1398
1399 /**
1400 * Serialize ObjectArray as JSON
1401 */
1402 template<typename T> json_t *json_object_array(ObjectArray<T> *a)
1403 {
1404 json_t *root = json_array();
1405 if (a != NULL)
1406 {
1407 for(int i = 0; i < a->size(); i++)
1408 {
1409 T *e = a->get(i);
1410 if (e != NULL)
1411 json_array_append_new(root, e->toJson());
1412 }
1413 }
1414 return root;
1415 }
1416
1417 /**
1418 * sockaddr buffer
1419 */
1420 union SockAddrBuffer
1421 {
1422 struct sockaddr_in sa4;
1423 #ifdef WITH_IPV6
1424 struct sockaddr_in6 sa6;
1425 #endif
1426 };
1427
1428 /**
1429 * sockaddr length calculation
1430 */
1431 #ifdef WITH_IPV6
1432 #define SA_LEN(sa) (((sa)->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
1433 #else
1434 #define SA_LEN(sa) sizeof(struct sockaddr_in)
1435 #endif
1436
1437 /**
1438 * Get port number from SockAddrBuffer
1439 */
1440 #ifdef WITH_IPV6
1441 #define SA_PORT(sa) ((((struct sockaddr *)sa)->sa_family == AF_INET) ? ((struct sockaddr_in *)sa)->sin_port : ((struct sockaddr_in6 *)sa)->sin6_port)
1442 #else
1443 #define SA_PORT(sa) (((struct sockaddr_in *)sa)->sin_port)
1444 #endif
1445
1446 /**
1447 * Compare addresses in sockaddr
1448 */
1449 inline bool SocketAddressEquals(struct sockaddr *a1, struct sockaddr *a2)
1450 {
1451 if (a1->sa_family != a2->sa_family)
1452 return false;
1453 if (a1->sa_family == AF_INET)
1454 return ((struct sockaddr_in *)a1)->sin_addr.s_addr == ((struct sockaddr_in *)a2)->sin_addr.s_addr;
1455 #ifdef WITH_IPV6
1456 if (a1->sa_family == AF_INET6)
1457 return !memcmp(((struct sockaddr_in6 *)a1)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)a2)->sin6_addr.s6_addr, 16);
1458 #endif
1459 return false;
1460 }
1461
1462 /**
1463 * IP address
1464 */
1465 class LIBNETXMS_EXPORTABLE InetAddress
1466 {
1467 private:
1468 short m_maskBits;
1469 short m_family;
1470 union
1471 {
1472 UINT32 v4;
1473 BYTE v6[16];
1474 } m_addr;
1475
1476 public:
1477 InetAddress();
1478 InetAddress(UINT32 addr);
1479 InetAddress(UINT32 addr, UINT32 mask);
1480 InetAddress(const BYTE *addr, int maskBits = 128);
1481
1482 bool isAnyLocal() const;
1483 bool isLoopback() const;
1484 bool isMulticast() const;
1485 bool isBroadcast() const;
1486 bool isLinkLocal() const;
1487 bool isValid() const { return m_family != AF_UNSPEC; }
1488 bool isValidUnicast() const { return isValid() && !isAnyLocal() && !isLoopback() && !isMulticast() && !isBroadcast() && !isLinkLocal(); }
1489
1490 int getFamily() const { return m_family; }
1491 UINT32 getAddressV4() const { return (m_family == AF_INET) ? m_addr.v4 : 0; }
1492 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"; }
1493
1494 bool contain(const InetAddress &a) const;
1495 bool sameSubnet(const InetAddress &a) const;
1496 bool equals(const InetAddress &a) const;
1497 int compareTo(const InetAddress &a) const;
1498
1499 void setMaskBits(int m) { m_maskBits = m; }
1500 int getMaskBits() const { return m_maskBits; }
1501 int getHostBits() const { return (m_family == AF_INET) ? (32 - m_maskBits) : (128 - m_maskBits); }
1502
1503 InetAddress getSubnetAddress() const;
1504 InetAddress getSubnetBroadcast() const;
1505 bool isSubnetBroadcast(int maskBits) const;
1506
1507 String toString() const;
1508 TCHAR *toString(TCHAR *buffer) const;
1509 #ifdef UNICODE
1510 char *toStringA(char *buffer) const;
1511 #else
1512 char *toStringA(char *buffer) const { return toString(buffer); }
1513 #endif
1514
1515 json_t *toJson() const;
1516
1517 BYTE *buildHashKey(BYTE *key) const;
1518
1519 TCHAR *getHostByAddr(TCHAR *buffer, size_t buflen) const;
1520
1521 struct sockaddr *fillSockAddr(SockAddrBuffer *buffer, UINT16 port = 0) const;
1522
1523 static InetAddress resolveHostName(const WCHAR *hostname, int af = AF_INET);
1524 static InetAddress resolveHostName(const char *hostname, int af = AF_INET);
1525 static InetAddress parse(const WCHAR *str);
1526 static InetAddress parse(const char *str);
1527 static InetAddress createFromSockaddr(struct sockaddr *s);
1528
1529 static const InetAddress INVALID;
1530 static const InetAddress LOOPBACK;
1531 };
1532
1533 /**
1534 * IP address list
1535 */
1536 class LIBNETXMS_EXPORTABLE InetAddressList
1537 {
1538 private:
1539 ObjectArray<InetAddress> *m_list;
1540
1541 int indexOf(const InetAddress& addr) const;
1542
1543 public:
1544 InetAddressList();
1545 ~InetAddressList();
1546
1547 void add(const InetAddress& addr);
1548 void add(const InetAddressList& addrList);
1549 void replace(const InetAddress& addr);
1550 void remove(const InetAddress& addr);
1551 void clear() { m_list->clear(); }
1552 const InetAddress& get(int index) const { const InetAddress *a = m_list->get(index); return (a != NULL) ? *a : InetAddress::INVALID; }
1553
1554 int size() const { return m_list->size(); }
1555 bool hasAddress(const InetAddress& addr) const { return indexOf(addr) != -1; }
1556 const InetAddress& findAddress(const InetAddress& addr) const { int idx = indexOf(addr); return (idx != -1) ? *m_list->get(idx) : InetAddress::INVALID; }
1557 const InetAddress& findSameSubnetAddress(const InetAddress& addr) const;
1558 const InetAddress& getFirstUnicastAddress() const;
1559 const InetAddress& getFirstUnicastAddressV4() const;
1560 bool hasValidUnicastAddress() const { return getFirstUnicastAddress().isValid(); }
1561 bool isLoopbackOnly() const;
1562
1563 const ObjectArray<InetAddress> *getList() const { return m_list; }
1564
1565 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
1566
1567 json_t *toJson() const { return json_object_array(m_list); }
1568 };
1569
1570 /**
1571 * Network connection
1572 */
1573 class LIBNETXMS_EXPORTABLE SocketConnection
1574 {
1575 protected:
1576 SOCKET m_socket;
1577 char m_data[4096];
1578 int m_dataPos;
1579
1580 public:
1581 SocketConnection();
1582 virtual ~SocketConnection();
1583
1584 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1585 bool connectTCP(const InetAddress& ip, WORD port, UINT32 timeout);
1586 void disconnect();
1587
1588 bool canRead(UINT32 timeout);
1589 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1590 bool waitForText(const char *text, int timeout);
1591
1592 int write(const char *pBuff, int nSize);
1593 bool writeLine(const char *line);
1594
1595 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
1596 };
1597
1598 /**
1599 * Telnet connection - handles all telnet negotiation
1600 */
1601 class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
1602 {
1603 protected:
1604 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1605 bool connectTCP(const InetAddress& ip, WORD port, UINT32 timeout);
1606
1607 public:
1608 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
1609
1610 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
1611 bool connect(const InetAddress& ip, WORD port, UINT32 timeout);
1612 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1613 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
1614 };
1615
1616 /**
1617 * Postal address representation
1618 */
1619 class LIBNETXMS_EXPORTABLE PostalAddress
1620 {
1621 private:
1622 TCHAR *m_country;
1623 TCHAR *m_city;
1624 TCHAR *m_streetAddress;
1625 TCHAR *m_postcode;
1626
1627 public:
1628 PostalAddress();
1629 PostalAddress(const TCHAR *country, const TCHAR *city, const TCHAR *streetAddress, const TCHAR *postcode);
1630 ~PostalAddress();
1631
1632 const TCHAR *getCountry() const { return CHECK_NULL_EX(m_country); }
1633 const TCHAR *getCity() const { return CHECK_NULL_EX(m_city); }
1634 const TCHAR *getStreetAddress() const { return CHECK_NULL_EX(m_streetAddress); }
1635 const TCHAR *getPostCode() const { return CHECK_NULL_EX(m_postcode); }
1636
1637 json_t *toJson() const;
1638
1639 void setCountry(const TCHAR *country) { free(m_country); m_country = _tcsdup_ex(country); }
1640 void setCity(const TCHAR *city) { free(m_city); m_city = _tcsdup_ex(city); }
1641 void setStreetAddress(const TCHAR *streetAddress) { free(m_streetAddress); m_streetAddress = _tcsdup_ex(streetAddress); }
1642 void setPostCode(const TCHAR *postcode) { free(m_postcode); m_postcode = _tcsdup_ex(postcode); }
1643 };
1644
1645 /**
1646 * Max number of polled sockets
1647 */
1648 #define SOCKET_POLLER_MAX_SOCKETS 16
1649
1650 /**
1651 * Socket poller
1652 */
1653 class LIBNETXMS_EXPORTABLE SocketPoller
1654 {
1655 private:
1656 bool m_write;
1657 int m_count;
1658 #if HAVE_POLL
1659 struct pollfd m_sockets[SOCKET_POLLER_MAX_SOCKETS];
1660 #else
1661 fd_set m_sockets;
1662 #ifndef _WIN32
1663 SOCKET m_maxfd;
1664 #endif
1665 #endif
1666
1667 public:
1668 SocketPoller(bool write = false);
1669 ~SocketPoller();
1670
1671 bool add(SOCKET s);
1672 int poll(UINT32 timeout);
1673 bool isSet(SOCKET s);
1674 void reset();
1675 };
1676
1677 /**
1678 * Abstract communication channel
1679 */
1680 class LIBNETXMS_EXPORTABLE AbstractCommChannel : public RefCountObject
1681 {
1682 protected:
1683 virtual ~AbstractCommChannel();
1684
1685 public:
1686 AbstractCommChannel();
1687
1688 virtual int send(const void *data, size_t size, MUTEX mutex = INVALID_MUTEX_HANDLE) = 0;
1689 virtual int recv(void *buffer, size_t size, UINT32 timeout = INFINITE) = 0;
1690 virtual int poll(UINT32 timeout, bool write = false) = 0;
1691 virtual int shutdown() = 0;
1692 virtual void close() = 0;
1693 };
1694
1695 /**
1696 * Socket communication channel
1697 */
1698 class LIBNETXMS_EXPORTABLE SocketCommChannel : public AbstractCommChannel
1699 {
1700 private:
1701 SOCKET m_socket;
1702 bool m_owner;
1703
1704 protected:
1705 virtual ~SocketCommChannel();
1706
1707 public:
1708 SocketCommChannel(SOCKET socket, bool owner = true);
1709
1710 virtual int send(const void *data, size_t size, MUTEX mutex = INVALID_MUTEX_HANDLE);
1711 virtual int recv(void *buffer, size_t size, UINT32 timeout = INFINITE);
1712 virtual int poll(UINT32 timeout, bool write = false);
1713 virtual int shutdown();
1714 virtual void close();
1715 };
1716
1717 #endif /* __cplusplus */
1718
1719 /**
1720 * Configuration item template for configuration loader
1721 */
1722 typedef struct
1723 {
1724 TCHAR token[64];
1725 BYTE type;
1726 BYTE separator; // Separator character for lists
1727 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
1728 UINT64 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
1729 UINT32 bufferPos; // Should be set to 0
1730 void *buffer;
1731 void *overrideIndicator;
1732 } NX_CFG_TEMPLATE;
1733
1734 /**
1735 * Code translation structure
1736 */
1737 typedef struct __CODE_TO_TEXT
1738 {
1739 int code;
1740 const TCHAR *text;
1741 } CODE_TO_TEXT;
1742
1743 /**
1744 * getopt() prototype if needed
1745 */
1746 #if USE_BUNDLED_GETOPT
1747 #include <netxms_getopt.h>
1748 #endif
1749
1750 //
1751 // Structures for opendir() / readdir() / closedir()
1752 //
1753
1754 #ifdef _WIN32
1755
1756 #define DT_UNKNOWN 0
1757 #define DT_REG 1
1758 #define DT_DIR 2
1759
1760 typedef struct dirent
1761 {
1762 long d_ino; /* inode number (not used by MS-DOS) */
1763 unsigned char d_type; /* file type */
1764 int d_namlen; /* Name length */
1765 char d_name[MAX_PATH]; /* file name */
1766 } _DIRECT;
1767
1768 typedef struct _dir_struc
1769 {
1770 HANDLE handle;
1771 struct dirent dirstr; /* Directory structure to return */
1772 } DIR;
1773
1774 #ifdef UNICODE
1775
1776 typedef struct dirent_w
1777 {
1778 long d_ino; /* inode number (not used by MS-DOS) */
1779 unsigned char d_type; /* file type */
1780 int d_namlen; /* Name length */
1781 WCHAR d_name[MAX_PATH]; /* file name */
1782 } _DIRECTW;
1783
1784 typedef struct _dir_struc_w
1785 {
1786 HANDLE handle;
1787 struct dirent_w dirstr; /* Directory structure to return */
1788 } DIRW;
1789
1790 #define _TDIR DIRW
1791 #define _TDIRECT _DIRECTW
1792 #define _tdirent dirent_w
1793
1794 #else
1795
1796 #define _TDIR DIR
1797 #define _TDIRECT _DIRECT
1798 #define _tdirent dirent
1799
1800 #endif
1801
1802 #else /* not _WIN32 */
1803
1804 typedef struct dirent_w
1805 {
1806 long d_ino; /* inode number */
1807 #if HAVE_DIRENT_D_TYPE
1808 unsigned char d_type; /* file type */
1809 #endif
1810 WCHAR d_name[257]; /* file name */
1811 } _DIRECTW;
1812
1813 typedef struct _dir_struc_w
1814 {
1815 DIR *dir; /* Original non-unicode structure */
1816 struct dirent_w dirstr; /* Directory structure to return */
1817 } DIRW;
1818
1819 #endif /* _WIN32 */
1820
1821
1822 //
1823 // Functions
1824 //
1825
1826 #ifdef __cplusplus
1827
1828 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
1829 {
1830 #if defined(_WIN32) && (_MSC_VER >= 1400) && !defined(__clang__)
1831 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
1832 #else
1833 _tcsncpy(pszDest, pszSrc, nLen - 1);
1834 pszDest[nLen - 1] = 0;
1835 #endif
1836 return pszDest;
1837 }
1838
1839 #ifdef UNICODE
1840 inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
1841 {
1842 #if defined(_WIN32) && (_MSC_VER >= 1400) && !defined(__clang__)
1843 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
1844 #else
1845 strncpy(pszDest, pszSrc, nLen - 1);
1846 pszDest[nLen - 1] = 0;
1847 #endif
1848 return pszDest;
1849 }
1850 #else
1851 #define nx_strncpy_mb nx_strncpy
1852 #endif
1853
1854 int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
1855 int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
1856 int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
1857 bool LIBNETXMS_EXPORTABLE RecvAll(SOCKET s, void *buffer, size_t size, UINT32 timeout);
1858
1859 SOCKET LIBNETXMS_EXPORTABLE ConnectToHost(const InetAddress& addr, UINT16 port, UINT32 timeout);
1860
1861 #endif /* __cplusplus */
1862
1863 #ifdef __cplusplus
1864 extern "C"
1865 {
1866 #endif
1867
1868 void LIBNETXMS_EXPORTABLE InitNetXMSProcess(bool commandLineTool);
1869
1870 #ifndef _WIN32
1871 #if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
1872 void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
1873 #define wcsupr __wcsupr
1874 #endif
1875 void LIBNETXMS_EXPORTABLE __strupr(char *in);
1876 #define strupr __strupr
1877 #endif /* _WIN32 */
1878
1879 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
1880 int (*compare)(const void *, const void *, void *));
1881
1882 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
1883
1884 UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
1885 UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
1886 #ifdef UNICODE
1887 #define FileSize FileSizeW
1888 #else
1889 #define FileSize FileSizeA
1890 #endif
1891
1892 int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1893 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
1894 #ifdef UNICODE
1895 char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
1896 #else
1897 #define IpToStrA IpToStr
1898 #endif
1899 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(const BYTE *addr, TCHAR *buffer);
1900 #ifdef UNICODE
1901 char LIBNETXMS_EXPORTABLE *Ip6ToStrA(const BYTE *addr, char *buffer);
1902 #else
1903 #define Ip6ToStrA Ip6ToStr
1904 #endif
1905 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
1906
1907 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1908 void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
1909
1910 WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const void *data, size_t size, WCHAR *str);
1911 char LIBNETXMS_EXPORTABLE *BinToStrA(const void *data, size_t size, char *str);
1912 #ifdef UNICODE
1913 #define BinToStr BinToStrW
1914 #else
1915 #define BinToStr BinToStrA
1916 #endif
1917
1918 size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1919 size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
1920 #ifdef UNICODE
1921 #define StrToBin StrToBinW
1922 #else
1923 #define StrToBin StrToBinA
1924 #endif
1925
1926 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
1927
1928 void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1929 void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
1930 #ifdef UNICODE
1931 #define StrStrip StrStripW
1932 #else
1933 #define StrStrip StrStripA
1934 #endif
1935
1936 const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
1937 const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
1938 #ifdef UNICODE
1939 #define ExtractWord ExtractWordW
1940 #else
1941 #define ExtractWord ExtractWordA
1942 #endif
1943
1944 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1945 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
1946 #ifdef UNICODE
1947 #define NumChars NumCharsW
1948 #else
1949 #define NumChars NumCharsA
1950 #endif
1951
1952 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1953 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
1954 #ifdef UNICODE
1955 #define RemoveTrailingCRLF RemoveTrailingCRLFW
1956 #else
1957 #define RemoveTrailingCRLF RemoveTrailingCRLFA
1958 #endif
1959
1960 bool LIBNETXMS_EXPORTABLE RegexpMatchA(const char *str, const char *expr, bool matchCase);
1961 bool LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *str, const WCHAR *expr, bool matchCase);
1962 #ifdef UNICODE
1963 #define RegexpMatch RegexpMatchW
1964 #else
1965 #define RegexpMatch RegexpMatchA
1966 #endif
1967
1968 const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
1969 BOOL LIBNETXMS_EXPORTABLE CreateFolder(const TCHAR *directory);
1970 bool LIBNETXMS_EXPORTABLE SetLastModificationTime(TCHAR *fileName, time_t lastModDate);
1971 TCHAR LIBNETXMS_EXPORTABLE *Trim(TCHAR *str);
1972 bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
1973 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
1974 int LIBNETXMS_EXPORTABLE GetLastMonthDay(struct tm *currTime);
1975 #ifdef __cplusplus
1976 bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime = 0);
1977 #else
1978 bool LIBNETXMS_EXPORTABLE MatchScheduleElement(TCHAR *pszPattern, int nValue, int maxValue, struct tm *localTime, time_t currTime);
1979 #endif
1980
1981
1982 #ifdef __cplusplus
1983 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
1984 #endif
1985 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
1986 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1987 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
1988 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
1989 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
1990 #ifdef UNICODE
1991 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
1992 #else
1993 #define LoadFileA LoadFile
1994 #endif
1995
1996 UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1997 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
1998 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
1999 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
2000 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
2001 void LIBNETXMS_EXPORTABLE CalculateSHA256Hash(const unsigned char *data, size_t len, unsigned char *hash);
2002 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
2003 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
2004 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
2005
2006 void LIBNETXMS_EXPORTABLE GenerateRandomBytes(BYTE *buffer, size_t size);
2007
2008 void LIBNETXMS_EXPORTABLE LogOpenSSLErrorStack(int level);
2009
2010 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
2011 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
2012
2013 bool LIBNETXMS_EXPORTABLE DecryptPasswordW(const WCHAR *login, const WCHAR *encryptedPasswd, WCHAR *decryptedPasswd, size_t bufferLenght);
2014 bool LIBNETXMS_EXPORTABLE DecryptPasswordA(const char *login, const char *encryptedPasswd, char *decryptedPasswd, size_t bufferLenght);
2015 #ifdef UNICODE
2016 #define DecryptPassword DecryptPasswordW
2017 #else
2018 #define DecryptPassword DecryptPasswordA
2019 #endif
2020
2021 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
2022
2023 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
2024 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
2025 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
2026
2027 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
2028 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
2029 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
2030
2031 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
2032 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
2033 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
2034
2035 #ifdef UNICODE
2036 #define ExtractNamedOptionValue ExtractNamedOptionValueW
2037 #define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
2038 #define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
2039 #else
2040 #define ExtractNamedOptionValue ExtractNamedOptionValueA
2041 #define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
2042 #define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
2043 #endif
2044
2045 #ifdef __cplusplus
2046 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
2047 #else
2048 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
2049 #endif
2050
2051 #ifdef _WIN32
2052 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
2053 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
2054 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
2055 #endif
2056
2057 TCHAR LIBNETXMS_EXPORTABLE *GetLastSocketErrorText(TCHAR *buffer, size_t size);
2058
2059 #if !HAVE_DAEMON || !HAVE_DECL_DAEMON
2060 int LIBNETXMS_EXPORTABLE __daemon(int nochdir, int noclose);
2061 #define daemon __daemon
2062 #endif
2063
2064 UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
2065
2066 #ifndef _WIN32
2067
2068 bool LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
2069 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
2070 int cchWideChar, char *pByteStr, int cchByteChar,
2071 char *pDefaultChar, BOOL *pbUsedDefChar);
2072 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
2073 int cchByteChar, WCHAR *pWideCharStr,
2074 int cchWideChar);
2075
2076 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
2077 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
2078 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
2079 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
2080 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
2081 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
2082
2083 int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
2084 int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
2085 int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
2086 int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
2087 int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
2088 int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
2089
2090 #endif /* _WIN32 */
2091
2092 #ifdef _WITH_ENCRYPTION
2093 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
2094 #endif
2095
2096 #if !defined(_WIN32) && !HAVE_WSTAT
2097 int wstat(const WCHAR *_path, struct stat *_sbuf);
2098 #endif
2099
2100 #if defined(UNICODE) && !defined(_WIN32)
2101
2102 #if !HAVE_WPOPEN
2103 FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
2104 #endif
2105 #if !HAVE_WFOPEN
2106 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
2107 #endif
2108 #if HAVE_FOPEN64 && !HAVE_WFOPEN64
2109 FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
2110 #endif
2111 #if !HAVE_WOPEN
2112 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
2113 #endif
2114 #if !HAVE_WCHMOD
2115 int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
2116 #endif
2117 #if !HAVE_WCHDIR
2118 int wchdir(const WCHAR *_path);
2119 #endif
2120 #if !HAVE_WMKDIR
2121 int wmkdir(const WCHAR *_path, int mode);
2122 #endif
2123 #if !HAVE_WUTIME
2124 int wutime(const WCHAR *_path, struct utimbuf *buf);
2125 #endif
2126 #if !HAVE_WRMDIR
2127 int wrmdir(const WCHAR *_path);
2128 #endif
2129 #if !HAVE_WRENAME
2130 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
2131 #endif
2132 #if !HAVE_WUNLINK
2133 int wunlink(const WCHAR *_path);
2134 #endif
2135 #if !HAVE_WREMOVE
2136 int wremove(const WCHAR *_path);
2137 #endif
2138 #if !HAVE_WSYSTEM
2139 int wsystem(const WCHAR *_cmd);
2140 #endif
2141 #if !HAVE_WMKSTEMP
2142 int wmkstemp(WCHAR *_template);
2143 #endif
2144 #if !HAVE_WACCESS
2145 int waccess(const WCHAR *_path, int mode);
2146 #endif
2147 #if !HAVE_WGETENV
2148 WCHAR *wgetenv(const WCHAR *_string);
2149 #endif
2150 #if !HAVE_WCTIME
2151 WCHAR *wctime(const time_t *timep);
2152 #endif
2153 #if !HAVE_PUTWS
2154 int putws(const WCHAR *s);
2155 #endif
2156 #if !HAVE_WCSERROR && (HAVE_STRERROR || HAVE_DECL_STRERROR)
2157 WCHAR *wcserror(int errnum);
2158 #endif
2159 #if !HAVE_WCSERROR_R && HAVE_STRERROR_R
2160 #if HAVE_POSIX_STRERROR_R
2161 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
2162 #else
2163 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
2164 #endif
2165 #endif
2166
2167 #endif /* UNICODE && !_WIN32*/
2168
2169 #if !HAVE_STRTOLL
2170 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
2171 #endif
2172 #if !HAVE_STRTOULL
2173 UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
2174 #endif
2175
2176 #if !HAVE_WCSTOLL
2177 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
2178 #endif
2179 #if !HAVE_WCSTOULL
2180 UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
2181 #endif
2182
2183 #if !HAVE_STRLWR && !defined(_WIN32)
2184 char LIBNETXMS_EXPORTABLE *strlwr(char *str);
2185 #endif
2186
2187 #if !HAVE_WCSLWR && !defined(_WIN32)
2188 WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
2189 #endif
2190
2191 #if !HAVE_WCSCASECMP && !defined(_WIN32)
2192 int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
2193 #endif
2194
2195 #if !HAVE_WCSNCASECMP && !defined(_WIN32)
2196 int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
2197 #endif
2198
2199 #if !defined(_WIN32) && (!HAVE_WCSFTIME || !WORKING_WCSFTIME)
2200 size_t LIBNETXMS_EXPORTABLE nx_wcsftime(WCHAR *buffer, size_t bufsize, const WCHAR *format, const struct tm *t);
2201 #undef wcsftime
2202 #define wcsftime nx_wcsftime
2203 #endif
2204
2205 #ifndef _WIN32
2206
2207 #if HAVE_ITOA && !HAVE__ITOA
2208 #define _itoa itoa
2209 #undef HAVE__ITOA
2210 #define HAVE__ITOA 1
2211 #endif
2212 #if !HAVE__ITOA && !defined(_WIN32)
2213 char LIBNETXMS_EXPORTABLE *_itoa(int value, char *str, int base);
2214 #endif
2215
2216 #if HAVE_ITOW && !HAVE__ITOW
2217 #define _itow itow
2218 #undef HAVE__ITOW
2219 #define HAVE__ITOW 1
2220 #endif
2221 #if !HAVE__ITOW && !defined(_WIN32)
2222 WCHAR LIBNETXMS_EXPORTABLE *_itow(int value, WCHAR *str, int base);
2223 #endif
2224
2225 #endif /* _WIN32 */
2226
2227 #ifdef _WIN32
2228 #ifdef UNICODE
2229 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *path);
2230 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *p);
2231 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *p);
2232
2233 #define _topendir wopendir
2234 #define _treaddir wreaddir
2235 #define _tclosedir wclosedir
2236 #else
2237 #define _topendir opendir
2238 #define _treaddir readdir
2239 #define _tclosedir closedir
2240 #endif
2241
2242 DIR LIBNETXMS_EXPORTABLE *opendir(const char *path);
2243 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *p);
2244 int LIBNETXMS_EXPORTABLE closedir(DIR *p);
2245
2246 #else /* not _WIN32 */
2247
2248 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
2249 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
2250 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
2251
2252 #endif
2253
2254 #if defined(_WIN32) || !(HAVE_SCANDIR)
2255 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
2256 int (*select)(const struct dirent *),
2257 int (*compar)(const struct dirent **, const struct dirent **));
2258 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
2259 #endif
2260
2261 TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
2262
2263 bool LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
2264 unsigned int msgCount, const TCHAR **messages, DWORD debugMsg);
2265 void LIBNETXMS_EXPORTABLE nxlog_close(void);
2266 void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
2267 void LIBNETXMS_EXPORTABLE nxlog_debug(int level, const TCHAR *format, ...);
2268 void LIBNETXMS_EXPORTABLE nxlog_debug2(int level, const TCHAR *format, va_list args);
2269 bool LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, UINT64 maxLogSize, int historySize, const TCHAR *dailySuffix);
2270 bool LIBNETXMS_EXPORTABLE nxlog_rotate();
2271 void LIBNETXMS_EXPORTABLE nxlog_set_debug_level(int level);
2272 int LIBNETXMS_EXPORTABLE nxlog_get_debug_level();
2273
2274 typedef void (*NxLogDebugWriter)(const TCHAR *);
2275 void LIBNETXMS_EXPORTABLE nxlog_set_debug_writer(NxLogDebugWriter writer);
2276
2277 typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
2278 void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
2279
2280 void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
2281 void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
2282 #if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
2283 __attribute__ ((format(printf, 1, 2)))
2284 #endif
2285 ;
2286
2287 #ifdef _WIN32
2288 int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
2289 int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
2290 #ifdef UNICODE
2291 #define _tmkstemp wmkstemp
2292 #else
2293 #define _tmkstemp mkstemp
2294 #endif
2295 #endif
2296
2297 #ifndef _WIN32
2298 int strcat_s(char *dst, size_t dstSize, const char *src);
2299 int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
2300 #endif
2301
2302 #if !HAVE_STRPTIME
2303 char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
2304 #endif
2305
2306 #if !HAVE_TIMEGM
2307 time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
2308 #endif
2309
2310 #if !HAVE_INET_PTON
2311 int LIBNETXMS_EXPORTABLE nx_inet_pton(int af, const char *src, void *dst);
2312 #define inet_pton nx_inet_pton
2313 #endif
2314
2315 int LIBNETXMS_EXPORTABLE GetSleepTime(int hour, int minute, int second);
2316 time_t LIBNETXMS_EXPORTABLE ParseDateTimeA(const char *text, time_t defaultValue);
2317 time_t LIBNETXMS_EXPORTABLE ParseDateTimeW(const WCHAR *text, time_t defaultValue);
2318
2319 #ifdef UNICODE
2320 #define ParseDateTime ParseDateTimeW
2321 #else
2322 #define ParseDateTime ParseDateTimeA
2323 #endif
2324
2325 #ifdef __cplusplus
2326 }
2327 #endif
2328
2329
2330 //
2331 // C++ only functions
2332 //
2333
2334 #ifdef __cplusplus
2335
2336 enum nxDirectoryType
2337 {
2338 nxDirBin = 0,
2339 nxDirData = 1,
2340 nxDirEtc = 2,
2341 nxDirLib = 3,
2342 nxDirShare = 4
2343 };
2344
2345 TCHAR LIBNETXMS_EXPORTABLE *GetHeapInfo();
2346
2347 void LIBNETXMS_EXPORTABLE GetNetXMSDirectory(nxDirectoryType type, TCHAR *dir);
2348 TCHAR LIBNETXMS_EXPORTABLE *FindJavaRuntime(TCHAR *buffer, size_t size);
2349
2350 UINT32 LIBNETXMS_EXPORTABLE IcmpPing(const InetAddress& addr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
2351
2352 TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
2353 String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
2354 const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
2355 int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
2356 UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
2357 bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
2358
2359 String LIBNETXMS_EXPORTABLE EscapeStringForJSON(const TCHAR *s);
2360 String LIBNETXMS_EXPORTABLE EscapeStringForAgent(const TCHAR *s);
2361
2362 StringList LIBNETXMS_EXPORTABLE *ParseCommandLine(const TCHAR *cmdline);
2363
2364 #if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
2365 void LIBNETXMS_EXPORTABLE BlockAllSignals(bool processWide, bool allowInterrupt);
2366 void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
2367 #endif
2368
2369 #endif
2370
2371 #endif /* _nms_util_h_ */