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