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