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