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