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