added InetAddress::isSubnetBroadcast()
[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
605fc935 91/**
e2e66145
VK
92 * Uninitialized value for override indicator
93 */
94#define NXCONFIG_UNINITIALIZED_VALUE (-1)
95
96/**
605fc935
VK
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
605fc935 132/**
f4d2c9ba
VK
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
f4d2c9ba 162/**
c83f6449
VK
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
c83f6449 172/**
d65c16a8
VK
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
186/**
605fc935
VK
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;
256 int 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
4e0e77e6
VK
269 int getAllocationStep() { return m_allocationStep; }
270 void setAllocationStep(int step) { m_allocationStep = step; }
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
3fe40613 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);
326 void *__getBuffer() { return m_data; }
327
328public:
329 Array(int initial = 0, int grow = 16, bool owner = false);
330 virtual ~Array();
331
332 int add(void *element);
333 void *get(int index) { return ((index >= 0) && (index < m_size)) ? (m_storePointers ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
334 int indexOf(void *element);
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 *));
343 void *find(const void *key, int (*cb)(const void *, const void *));
344
345 int size() { return m_size; }
346
347 void setOwner(bool owner) { m_objectOwner = owner; }
348 bool isOwner() { return m_objectOwner; }
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); }
364 T *get(int index) { return (T*)Array::get(index); }
365 int indexOf(T *object) { return Array::indexOf((void *)object); }
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); }
387 T get(int index) { return m_storePointers ? CAST_FROM_POINTER(Array::get(index), T) : *((T*)Array::get(index)); }
388 int indexOf(T value) { return Array::indexOf(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
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
392 T *getBuffer() { return (T*)__getBuffer(); }
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); }
409 T *get(int index) { return (T*)Array::get(index); }
410 int indexOf(T *element) { return Array::indexOf((void *)element); }
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
418 T *getBuffer() { return (T*)__getBuffer(); }
419};
420
421/**
422 * Entry of string map (internal)
400e55c4
VK
423 */
424struct StringMapEntry;
425
426/**
f9861ff9
VK
427 * Key/value pair
428 */
429struct KeyValuePair
430{
431 const TCHAR *key;
432 const void *value;
433};
434
435/**
3fe40613
VK
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
470/**
400e55c4
VK
471 * NXCP message class
472 */
b368969c 473class NXCPMessage;
400e55c4
VK
474
475/**
3fe40613
VK
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
3fe40613 551/**
a58501c0
VK
552 * Entry of string set
553 */
554struct StringSetEntry;
555
556/**
f3603b79
VK
557 * NXCP message
558 */
b368969c 559class NXCPMessage;
f3603b79
VK
560
561/**
a58501c0
VK
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
593/**
57f34c0a
VK
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
635/**
25fe6265
VK
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
694/**
d6bbfa4e
VK
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
753/**
967893bb
VK
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
25fe6265 842/**
c11eee9b
VK
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
862/**
9319c166
VK
863 * IP address
864 */
865class LIBNETXMS_EXPORTABLE InetAddress
866{
9319c166 867private:
8c75ad41
VK
868 short m_maskBits;
869 short m_family;
870 union
871 {
872 UINT32 v4;
873 BYTE v6[16];
874 } m_addr;
9319c166
VK
875
876public:
8c75ad41
VK
877 InetAddress();
878 InetAddress(UINT32 addr);
879 InetAddress(BYTE *addr);
9319c166 880
8c75ad41
VK
881 bool isAnyLocal() const;
882 bool isLoopback() const;
883 bool isMulticast() const;
884 bool isBroadcast() const;
885 bool isValid() const { return m_family != AF_UNSPEC; }
9319c166 886
8c75ad41 887 int getFamily() const { return m_family; }
c7853753
VK
888 UINT32 getAddressV4() const { return (m_family == AF_INET) ? m_addr.v4 : 0; }
889 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 890
8c75ad41 891 bool contain(const InetAddress &a) const;
43709a0c 892 bool sameSubnet(const InetAddress &a) const;
8c75ad41
VK
893 bool equals(const InetAddress &a) const;
894 int compareTo(const InetAddress &a) const;
9319c166 895
8c75ad41
VK
896 void setMaskBits(int m) { m_maskBits = m; }
897 int getMaskBits() const { return m_maskBits; }
9319c166 898
463b1d34
VK
899 InetAddress getSubnetAddress() const;
900 bool isSubnetBroadcast(int maskBits) const;
ffabd7f8 901
8c75ad41
VK
902 String toString() const;
903 TCHAR *toString(TCHAR *buffer) const;
9319c166 904
43709a0c 905 BYTE *buildHashKey(BYTE *key) const;
f3b94fc5 906
43709a0c
VK
907 TCHAR *getHostByAddr(TCHAR *buffer, size_t buflen) const;
908
909 struct sockaddr *fillSockAddr(SockAddrBuffer *buffer, UINT16 port = 0) const;
c11eee9b 910
c7853753
VK
911 static InetAddress resolveHostName(const WCHAR *hostname, int af = AF_INET);
912 static InetAddress resolveHostName(const char *hostname, int af = AF_INET);
f3b94fc5
VK
913 static InetAddress parse(const WCHAR *str);
914 static InetAddress parse(const char *str);
8c75ad41 915 static InetAddress createFromSockaddr(struct sockaddr *s);
9319c166
VK
916};
917
918/**
25fe6265
VK
919 * Network connection
920 */
58ee2f43
VK
921class LIBNETXMS_EXPORTABLE SocketConnection
922{
923protected:
924 SOCKET m_socket;
925 char m_data[4096];
926 int m_dataPos;
927
928public:
929 SocketConnection();
930 virtual ~SocketConnection();
931
967893bb
VK
932 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
933 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
934 void disconnect();
935
967893bb
VK
936 bool canRead(UINT32 timeout);
937 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43 938 bool waitForText(const char *text, int timeout);
cc022855 939
58ee2f43 940 int write(const char *pBuff, int nSize);
1671f85d
VK
941 bool writeLine(const char *line);
942
967893bb 943 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
944};
945
030a0779
VK
946/**
947 * Telnet connection - handles all telnet negotiation
948 */
def5b792
AK
949class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
950{
030a0779 951protected:
967893bb
VK
952 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
953 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 954
def5b792 955public:
967893bb 956 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 957
967893bb
VK
958 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
959 bool connect(UINT32 ip, WORD port, UINT32 timeout);
960 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
961 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
962};
963
56fa1092
VK
964/**
965 * Postal address representation
966 */
967class LIBNETXMS_EXPORTABLE PostalAddress
968{
969private:
970 TCHAR *m_country;
971 TCHAR *m_city;
972 TCHAR *m_streetAddress;
973 TCHAR *m_postcode;
974
975public:
976 PostalAddress();
977 PostalAddress(const TCHAR *country, const TCHAR *city, const TCHAR *streetAddress, const TCHAR *postcode);
978 ~PostalAddress();
979
980 const TCHAR *getCountry() { return CHECK_NULL_EX(m_country); }
981 const TCHAR *getCity() { return CHECK_NULL_EX(m_city); }
982 const TCHAR *getStreetAddress() { return CHECK_NULL_EX(m_streetAddress); }
983 const TCHAR *getPostCode() { return CHECK_NULL_EX(m_postcode); }
984
985 void setCountry(const TCHAR *country) { safe_free(m_country); m_country = _tcsdup_ex(country); }
986 void setCity(const TCHAR *city) { safe_free(m_city); m_city = _tcsdup_ex(city); }
987 void setStreetAddress(const TCHAR *streetAddress) { safe_free(m_streetAddress); m_streetAddress = _tcsdup_ex(streetAddress); }
988 void setPostCode(const TCHAR *postcode) { safe_free(m_postcode); m_postcode = _tcsdup_ex(postcode); }
989};
990
b160350d
VK
991#endif /* __cplusplus */
992
030a0779
VK
993/**
994 * Configuration item template for configuration loader
995 */
4826c8a5
VK
996typedef struct
997{
e2e66145
VK
998 TCHAR token[64];
999 BYTE type;
1000 BYTE separator; // Separator character for lists
1001 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
e95680e5 1002 UINT64 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
e2e66145
VK
1003 UINT32 bufferPos; // Should be set to 0
1004 void *buffer;
1005 void *overrideIndicator;
4826c8a5 1006} NX_CFG_TEMPLATE;
dcb44c9a 1007
030a0779
VK
1008/**
1009 * Code translation structure
1010 */
1b56f3c5 1011typedef struct __CODE_TO_TEXT
bc858635
VK
1012{
1013 int code;
1014 const TCHAR *text;
1015} CODE_TO_TEXT;
1016
e2e66145
VK
1017/**
1018 * getopt() prototype if needed
1019 */
df9812a9 1020#if USE_BUNDLED_GETOPT
99e50bb7 1021#include <netxms_getopt.h>
3046f9a4
VK
1022#endif
1023
1024
1025//
f2fdf1b5
VK
1026// Win32 API functions missing under WinCE
1027//
1028
c32b805a 1029#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
1030
1031inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
1032{
1033 SYSTEMTIME sysTime;
cc022855 1034
f2fdf1b5
VK
1035 GetSystemTime(&sysTime);
1036 SystemTimeToFileTime(&sysTime, pFt);
1037}
1038
1039#endif // UNDER_CE
1040
1041
1042//
3d1cb7d6
VK
1043// Structures for opendir() / readdir() / closedir()
1044//
1045
1046#ifdef _WIN32
1047
7f9e0c00 1048typedef struct dirent
3d1cb7d6
VK
1049{
1050 long d_ino; /* inode number (not used by MS-DOS) */
1051 int d_namlen; /* Name length */
1052 char d_name[257]; /* file name */
1053} _DIRECT;
1054
1055typedef struct _dir_struc
1056{
1057 char *start; /* Starting position */
1058 char *curr; /* Current position */
1059 long size; /* Size of string table */
1060 long nfiles; /* number if filenames in table */
7f9e0c00 1061 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
1062} DIR;
1063
5ea2db30
VK
1064#ifdef UNICODE
1065
1066typedef struct dirent_w
1067{
1068 long d_ino; /* inode number (not used by MS-DOS) */
1069 int d_namlen; /* Name length */
1070 WCHAR d_name[257]; /* file name */
1071} _DIRECTW;
1072
1073typedef struct _dir_struc_w
1074{
1075 WCHAR *start; /* Starting position */
1076 WCHAR *curr; /* Current position */
1077 long size; /* Size of string table */
1078 long nfiles; /* number if filenames in table */
1079 struct dirent_w dirstr; /* Directory structure to return */
1080} DIRW;
1081
1082#define _TDIR DIRW
1083#define _TDIRECT _DIRECTW
1084#define _tdirent dirent_w
1085
1086#else
1087
1088#define _TDIR DIR
1089#define _TDIRECT _DIRECT
1090#define _tdirent dirent
1091
37d62364
AK
1092#endif
1093
76b1d3d8
VK
1094#else /* not _WIN32 */
1095
1096typedef struct dirent_w
1097{
1098 long d_ino; /* inode number */
1099 WCHAR d_name[257]; /* file name */
1100} _DIRECTW;
1101
1102typedef struct _dir_struc_w
1103{
1104 DIR *dir; /* Original non-unicode structure */
1105 struct dirent_w dirstr; /* Directory structure to return */
1106} DIRW;
1107
3d1cb7d6
VK
1108#endif /* _WIN32 */
1109
1110
1111//
e9246d26
VK
1112// Functions
1113//
1114
babb8280 1115#if WORDS_BIGENDIAN
43d92b4c
VK
1116#define htonq(x) (x)
1117#define ntohq(x) (x)
9f20696e
VK
1118#define htond(x) (x)
1119#define ntohd(x) (x)
babb8280
VK
1120#define SwapWideString(x)
1121#else
ca6ad6bb
VK
1122#ifdef HAVE_HTONLL
1123#define htonq(x) htonll(x)
1124#else
babb8280 1125#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
1126#endif
1127#ifdef HAVE_NTOHLL
1128#define ntohq(x) ntohll(x)
1129#else
babb8280 1130#define ntohq(x) __bswap_64(x)
ca6ad6bb 1131#endif
babb8280
VK
1132#define htond(x) __bswap_double(x)
1133#define ntohd(x) __bswap_double(x)
1134#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 1135#endif
15929b31 1136
55b33d4c
VK
1137#ifdef UNDER_CE
1138#define close(x) CloseHandle((HANDLE)(x))
1139#endif
1140
b50f1100 1141#ifdef __cplusplus
2e590d1a
VK
1142inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
1143{
912b994d 1144#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1145 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
912b994d 1146#else
2e590d1a
VK
1147 _tcsncpy(pszDest, pszSrc, nLen - 1);
1148 pszDest[nLen - 1] = 0;
912b994d 1149#endif
2e590d1a
VK
1150 return pszDest;
1151}
f3387429
VK
1152
1153#ifdef UNICODE
2e590d1a
VK
1154inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
1155{
f3387429 1156#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1157 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429 1158#else
2e590d1a
VK
1159 strncpy(pszDest, pszSrc, nLen - 1);
1160 pszDest[nLen - 1] = 0;
c81979bc 1161#endif
2e590d1a
VK
1162 return pszDest;
1163}
c81979bc 1164#else
f3387429 1165#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
1166#endif
1167
967893bb 1168int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
5e60b759
VK
1169int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
1170int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
b0608204 1171#endif /* __cplusplus */
c81979bc
VK
1172
1173#ifdef __cplusplus
e9246d26
VK
1174extern "C"
1175{
b50f1100 1176#endif
735bac5c 1177
c5b523e2 1178#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
735bac5c 1179UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
c5b523e2 1180#endif
f52be742 1181#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
735bac5c 1182UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
64584976 1183#endif
735bac5c
VK
1184double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
1185void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 1186
076a55c4 1187#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc 1188#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
735bac5c
VK
1189void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
1190#define wcsupr __wcsupr
cc022855 1191#endif
735bac5c
VK
1192void LIBNETXMS_EXPORTABLE __strupr(char *in);
1193#define strupr __strupr
7e679c4b 1194#endif
cc022855 1195
735bac5c 1196void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
b8a8d064
VK
1197 int (*compare)(const void *, const void *, void *));
1198
735bac5c 1199INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
d35ed0aa 1200
735bac5c
VK
1201UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
1202UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
1203#ifdef UNICODE
1204#define FileSize FileSizeW
1205#else
1206#define FileSize FileSizeA
1207#endif
670c7f3d 1208
735bac5c
VK
1209int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1210TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 1211#ifdef UNICODE
735bac5c 1212char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
1213#else
1214#define IpToStrA IpToStr
e0f99bf0 1215#endif
8c75ad41 1216TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(const BYTE *addr, TCHAR *buffer);
735bac5c 1217TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
60557d06 1218
735bac5c
VK
1219UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *name);
1220UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameW(const WCHAR *name);
08b214c6 1221#ifdef UNICODE
cfe20cce 1222#define ResolveHostName ResolveHostNameW
08b214c6 1223#else
cfe20cce 1224#define ResolveHostName ResolveHostNameA
08b214c6 1225#endif
a1dc75b0 1226
735bac5c
VK
1227void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1228void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
31521248 1229
735bac5c
VK
1230WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *data, size_t size, WCHAR *pStr);
1231char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *data, size_t size, char *pStr);
d7970ee7
VK
1232#ifdef UNICODE
1233#define BinToStr BinToStrW
1234#else
1235#define BinToStr BinToStrA
1236#endif
1237
735bac5c
VK
1238size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1239size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
52db3ed1
VK
1240#ifdef UNICODE
1241#define StrToBin StrToBinW
1242#else
1243#define StrToBin StrToBinA
1244#endif
cc022855 1245
735bac5c 1246TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
b50f1100 1247
735bac5c
VK
1248void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1249void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
b07c50cc
VK
1250#ifdef UNICODE
1251#define StrStrip StrStripW
1252#else
1253#define StrStrip StrStripA
1254#endif
1255
735bac5c
VK
1256const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
1257const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
1258#ifdef UNICODE
1259#define ExtractWord ExtractWordW
1260#else
1261#define ExtractWord ExtractWordA
1262#endif
1263
735bac5c
VK
1264int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1265int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
3583a400
VK
1266#ifdef UNICODE
1267#define NumChars NumCharsW
1268#else
1269#define NumChars NumCharsA
1270#endif
ada97259 1271
735bac5c
VK
1272void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1273void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
35f836fe
VK
1274#ifdef UNICODE
1275#define RemoveTrailingCRLF RemoveTrailingCRLFW
1276#else
1277#define RemoveTrailingCRLF RemoveTrailingCRLFA
1278#endif
1279
4893b4aa
VK
1280BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *str, const char *expr, bool matchCase);
1281BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *str, const WCHAR *expr, bool matchCase);
cafe592d
VK
1282#ifdef UNICODE
1283#define RegexpMatch RegexpMatchW
1284#else
1285#define RegexpMatch RegexpMatchA
1286#endif
1287
735bac5c 1288const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
818f8c94 1289TCHAR LIBNETXMS_EXPORTABLE *Trim(TCHAR *str);
735bac5c
VK
1290bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
1291TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
ada97259 1292
f7794b68 1293#ifdef __cplusplus
735bac5c 1294BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
f7794b68 1295#endif
735bac5c
VK
1296BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
1297/* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1298const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
1299void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
1300BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 1301#ifdef UNICODE
735bac5c 1302BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
1303#else
1304#define LoadFileA LoadFile
1305#endif
1306
735bac5c
VK
1307UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1308void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
1309void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
1310void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
1311void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
1312BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1313BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
1314BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 1315
735bac5c
VK
1316void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1317void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
4e2debcc 1318
735bac5c 1319BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
6b1282e7 1320
735bac5c 1321int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 1322
735bac5c
VK
1323HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
1324void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
1325void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 1326
735bac5c
VK
1327bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1328bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1329long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
289d126b 1330
735bac5c
VK
1331bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1332bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1333long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
289d126b
VK
1334
1335#ifdef UNICODE
1336#define ExtractNamedOptionValue ExtractNamedOptionValueW
1337#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1338#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1339#else
1340#define ExtractNamedOptionValue ExtractNamedOptionValueA
1341#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1342#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1343#endif
ae9cddaf 1344
bc858635 1345#ifdef __cplusplus
735bac5c 1346const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
bc858635 1347#else
735bac5c 1348const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
bc858635
VK
1349#endif
1350
aa13cfd9 1351#ifdef _WIN32
735bac5c
VK
1352TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
1353BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
1354INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 1355#endif
696fc54f
VK
1356
1357#if !(HAVE_DAEMON)
735bac5c 1358int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 1359#endif
4332e9d8 1360
735bac5c 1361UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1362
4332e9d8 1363#ifndef _WIN32
735bac5c
VK
1364BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
1365int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
1366 int cchWideChar, char *pByteStr, int cchByteChar,
1367 char *pDefaultChar, BOOL *pbUsedDefChar);
1368int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
1369 int cchByteChar, WCHAR *pWideCharStr,
1370 int cchWideChar);
04dbaf62 1371
6b93c954 1372#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
735bac5c 1373int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1374#endif
1375#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
735bac5c 1376UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1377#endif
1378#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
b6ec31bb 1379UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strdup(const UCS2CHAR *pStr);
4332e9d8 1380#endif
8b86c5dc 1381
735bac5c
VK
1382size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1383size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
1384UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1385char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1386
735bac5c
VK
1387int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1388int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1389int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1390int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1391int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1392int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1393
735bac5c
VK
1394int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1395int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
1396int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
1397int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1398int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
1399int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1400
3e39fcde 1401#endif /* _WIN32 */
8b86c5dc 1402
3d1058ed 1403#ifdef _WITH_ENCRYPTION
735bac5c 1404WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
3d1058ed
VK
1405#endif
1406
8b86c5dc 1407#ifdef UNICODE_UCS4
735bac5c
VK
1408size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1409size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1410size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1411UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1412WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1413#endif
8b86c5dc 1414
842c22f4 1415#if !defined(_WIN32) && !HAVE_WSTAT
735bac5c 1416int wstat(const WCHAR *_path, struct stat *_sbuf);
842c22f4
AK
1417#endif
1418
ef856d02 1419#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1420
f3387429 1421#if !HAVE_WPOPEN
735bac5c 1422FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
f3387429 1423#endif
8b86c5dc 1424#if !HAVE_WFOPEN
735bac5c 1425FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
8b86c5dc 1426#endif
8c2489aa 1427#if HAVE_FOPEN64 && !HAVE_WFOPEN64
735bac5c 1428FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
8c2489aa 1429#endif
8b86c5dc 1430#if !HAVE_WOPEN
735bac5c 1431int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1432#endif
f3387429 1433#if !HAVE_WCHMOD
735bac5c 1434int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
f3387429 1435#endif
b07c50cc 1436#if !HAVE_WCHDIR
735bac5c 1437int wchdir(const WCHAR *_path);
b07c50cc
VK
1438#endif
1439#if !HAVE_WMKDIR
735bac5c 1440int wmkdir(const WCHAR *_path, int mode);
b07c50cc
VK
1441#endif
1442#if !HAVE_WRMDIR
735bac5c 1443int wrmdir(const WCHAR *_path);
b07c50cc 1444#endif
ccc34207 1445#if !HAVE_WRENAME
735bac5c 1446int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
ccc34207
VK
1447#endif
1448#if !HAVE_WUNLINK
735bac5c 1449int wunlink(const WCHAR *_path);
ccc34207 1450#endif
b07c50cc 1451#if !HAVE_WREMOVE
735bac5c 1452int wremove(const WCHAR *_path);
b07c50cc
VK
1453#endif
1454#if !HAVE_WSYSTEM
735bac5c 1455int wsystem(const WCHAR *_cmd);
b07c50cc 1456#endif
dda7c270 1457#if !HAVE_WMKSTEMP
735bac5c 1458int wmkstemp(WCHAR *_template);
dda7c270 1459#endif
b07c50cc 1460#if !HAVE_WACCESS
735bac5c 1461int waccess(const WCHAR *_path, int mode);
b07c50cc 1462#endif
273da39a 1463#if !HAVE_WGETENV
735bac5c 1464WCHAR *wgetenv(const WCHAR *_string);
273da39a 1465#endif
f3387429 1466#if !HAVE_WCTIME
735bac5c 1467WCHAR *wctime(const time_t *timep);
f3387429 1468#endif
0186b5da 1469#if !HAVE_PUTWS
735bac5c 1470int putws(const WCHAR *s);
0186b5da 1471#endif
d49d7500 1472#if !HAVE_WCSERROR && HAVE_STRERROR
735bac5c 1473WCHAR *wcserror(int errnum);
d49d7500
VK
1474#endif
1475#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06 1476#if HAVE_POSIX_STRERROR_R
735bac5c 1477int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
b0823d06 1478#else
735bac5c 1479WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
d49d7500 1480#endif
b0823d06 1481#endif
e05f8aaf 1482
ef856d02 1483#endif /* UNICODE && !_WIN32*/
e05f8aaf 1484
8b86c5dc 1485#if !HAVE_STRTOLL
735bac5c 1486INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
8b86c5dc
VK
1487#endif
1488#if !HAVE_STRTOULL
735bac5c 1489UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1490#endif
e05f8aaf
VK
1491
1492#if !HAVE_WCSTOLL
735bac5c 1493INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf
VK
1494#endif
1495#if !HAVE_WCSTOULL
735bac5c 1496UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1497#endif
b50f1100 1498
2f56be29 1499#if !HAVE_WCSLWR && !defined(_WIN32)
735bac5c 1500WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
fdf1ed50
VK
1501#endif
1502
4d978b4a 1503#if !HAVE_WCSCASECMP && !defined(_WIN32)
735bac5c 1504int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
4d978b4a
VK
1505#endif
1506
f4d2c9ba 1507#if !HAVE_WCSNCASECMP && !defined(_WIN32)
735bac5c 1508int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
f4d2c9ba
VK
1509#endif
1510
3d1cb7d6 1511#ifdef _WIN32
5ea2db30 1512#ifdef UNICODE
735bac5c
VK
1513DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1514struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1515int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
5ea2db30
VK
1516
1517#define _topendir wopendir
1518#define _treaddir wreaddir
1519#define _tclosedir wclosedir
1520#else
1521#define _topendir opendir
1522#define _treaddir readdir
1523#define _tclosedir closedir
1524#endif
1525
735bac5c
VK
1526DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
1527struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
1528int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1529
76b1d3d8
VK
1530#else /* not _WIN32 */
1531
735bac5c
VK
1532DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1533struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1534int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
76b1d3d8 1535
37d62364 1536#endif
3d1cb7d6 1537
7f9e0c00 1538#if defined(_WIN32) || !(HAVE_SCANDIR)
735bac5c
VK
1539int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1540 int (*select)(const struct dirent *),
1541 int (*compar)(const struct dirent **, const struct dirent **));
1542int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
7f9e0c00
VK
1543#endif
1544
3bf0df9f
VK
1545TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
1546
55b33d4c 1547#ifdef UNDER_CE
3bf0df9f
VK
1548int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1549int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1550int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
55b33d4c
VK
1551#endif
1552
967893bb 1553BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1554 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1555void LIBNETXMS_EXPORTABLE nxlog_close(void);
1556void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1557BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1558BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1559
1560typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1561void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1562
1563void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
6e383343
VK
1564void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
1565#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1566 __attribute__ ((format(printf, 1, 2)))
1567#endif
1568;
9f2902ac 1569
37a7a862
VK
1570#ifdef _WIN32
1571int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1572int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1573#ifdef UNICODE
1574#define _tmkstemp wmkstemp
1575#else
1576#define _tmkstemp mkstemp
1577#endif
1578#endif
1579
9b13553b
VK
1580#ifndef _WIN32
1581int strcat_s(char *dst, size_t dstSize, const char *src);
1582int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1583#endif
1584
c57d020a
VK
1585#if !HAVE_STRPTIME
1586char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
1587#endif
1588
1589#if !HAVE_TIMEGM
1590time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
1591#endif
1592
6f95ef89
VK
1593#if !HAVE_INET_PTON
1594int LIBNETXMS_EXPORTABLE nx_inet_pton(int af, const char *src, void *dst);
1595#define inet_pton nx_inet_pton
1596#endif
1597
b50f1100 1598#ifdef __cplusplus
e9246d26 1599}
b50f1100 1600#endif
e9246d26 1601
a7ff20a5
VK
1602
1603//
1604// C++ only finctions
1605//
1606
1607#ifdef __cplusplus
1608
43709a0c
VK
1609UINT32 LIBNETXMS_EXPORTABLE IcmpPing(const InetAddress& addr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
1610
240816b0
VK
1611TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1612String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1613const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1614int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1615UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1616bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1617
1618#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1619void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1620#endif
1621
a7ff20a5
VK
1622#endif
1623
e9246d26 1624#endif /* _nms_util_h_ */