changes in string set API
[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);
d45f9179 631 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
d6c4cdc1 632 void addAllPreallocated(TCHAR **strings, int count);
a58501c0 633 void forEach(bool (*cb)(const TCHAR *, void *), void *userData);
f3603b79 634
b368969c
VK
635 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId);
636 void addAllFromMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId, bool clearBeforeAdd, bool toUppercase);
c262bc08 637
d45f9179 638 String join(const TCHAR *separator);
a58501c0
VK
639};
640
641/**
57f34c0a
VK
642 * Byte stream
643 */
644class LIBNETXMS_EXPORTABLE ByteStream
645{
646private:
647 BYTE *m_data;
648 size_t m_size;
649 size_t m_allocated;
650 size_t m_pos;
651
652public:
653 ByteStream(size_t initial = 8192);
654 ByteStream(const void *data, size_t size);
655 virtual ~ByteStream();
656
4b47d7d7
VK
657 static ByteStream *load(const TCHAR *file);
658
659 void seek(size_t pos) { if (pos <= m_size) m_pos = pos; }
57f34c0a
VK
660 size_t pos() { return m_pos; }
661 size_t size() { return m_size; }
662 bool eos() { return m_pos == m_size; }
663
664 BYTE *buffer(size_t *size) { *size = m_size; return m_data; }
665
4b47d7d7 666 void write(const void *data, size_t size);
57f34c0a
VK
667 void write(char c) { write(&c, 1); }
668 void write(BYTE b) { write(&b, 1); }
669 void write(INT16 n) { UINT16 x = htons((UINT16)n); write(&x, 2); }
670 void write(UINT16 n) { UINT16 x = htons(n); write(&x, 2); }
671 void write(INT32 n) { UINT32 x = htonl((UINT32)n); write(&x, 4); }
672 void write(UINT32 n) { UINT32 x = htonl(n); write(&x, 4); }
4b47d7d7
VK
673 void write(INT64 n) { UINT64 x = htonq((UINT64)n); write(&x, 8); }
674 void write(UINT64 n) { UINT64 x = htonq(n); write(&x, 8); }
675 void write(double n) { double x = htond(n); write(&x, 8); }
676 void writeString(const TCHAR *s);
57f34c0a 677
4b47d7d7 678 size_t read(void *buffer, size_t count);
57f34c0a
VK
679 char readChar() { return !eos() ? (char)m_data[m_pos++] : 0; }
680 BYTE readByte() { return !eos() ? m_data[m_pos++] : 0; }
681 INT16 readInt16();
682 UINT16 readUInt16();
683 INT32 readInt32();
684 UINT32 readUInt32();
4b47d7d7
VK
685 INT64 readInt64();
686 UINT64 readUInt64();
687 double readDouble();
688 TCHAR *readString();
689
690 bool save(int f);
57f34c0a
VK
691};
692
693/**
25fe6265
VK
694 * Auxilliary class for objects which counts references and
695 * destroys itself wheren reference count falls to 0
696 */
7f632dfe
VK
697class LIBNETXMS_EXPORTABLE RefCountObject
698{
699private:
7785322f 700 VolatileCounter m_refCount;
7f632dfe
VK
701
702public:
7785322f
VK
703 RefCountObject()
704 {
705 m_refCount = 1;
706 }
b549a0f8 707 virtual ~RefCountObject();
7785322f
VK
708
709 void incRefCount()
710 {
711 InterlockedIncrement(&m_refCount);
712 }
713
714 void decRefCount()
715 {
716 if (InterlockedDecrement(&m_refCount) == 0)
717 delete this;
718 }
7f632dfe
VK
719};
720
b368969c 721class NXCPMessage;
967893bb
VK
722
723/**
724 * Table column definition
725 */
726class LIBNETXMS_EXPORTABLE TableColumnDefinition
727{
728private:
729 TCHAR *m_name;
730 TCHAR *m_displayName;
731 INT32 m_dataType;
732 bool m_instanceColumn;
733
734public:
735 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
b368969c 736 TableColumnDefinition(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
737 TableColumnDefinition(TableColumnDefinition *src);
738 ~TableColumnDefinition();
739
b368969c 740 void fillMessage(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
741
742 const TCHAR *getName() { return m_name; }
743 const TCHAR *getDisplayName() { return m_displayName; }
744 INT32 getDataType() { return m_dataType; }
745 bool isInstanceColumn() { return m_instanceColumn; }
746
747 void setDataType(INT32 type) { m_dataType = type; }
748 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 749 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
750};
751
752/**
d6bbfa4e
VK
753 * Table cell
754 */
755class TableCell
756{
757private:
758 TCHAR *m_value;
759 int m_status;
760
761public:
762 TableCell() { m_value = NULL; m_status = -1; }
c83f6449
VK
763 TableCell(const TCHAR *value) { m_value = _tcsdup_ex(value); m_status = -1; }
764 TableCell(const TCHAR *value, int status) { m_value = _tcsdup_ex(value); m_status = status; }
765 TableCell(TableCell *src) { m_value = _tcsdup_ex(src->m_value); m_status = src->m_status; }
d6bbfa4e
VK
766 ~TableCell() { safe_free(m_value); }
767
c83f6449 768 void set(const TCHAR *value, int status) { safe_free(m_value); m_value = _tcsdup_ex(value); m_status = status; }
d6bbfa4e
VK
769 void setPreallocated(TCHAR *value, int status) { safe_free(m_value); m_value = value; m_status = status; }
770
771 const TCHAR *getValue() { return m_value; }
c83f6449 772 void setValue(const TCHAR *value) { safe_free(m_value); m_value = _tcsdup_ex(value); }
d6bbfa4e
VK
773 void setPreallocatedValue(TCHAR *value) { safe_free(m_value); m_value = value; }
774
775 int getStatus() { return m_status; }
776 void setStatus(int status) { m_status = status; }
777};
778
779/**
780 * Table row
781 */
782class TableRow
783{
784private:
785 ObjectArray<TableCell> *m_cells;
786 UINT32 m_objectId;
bac74bf7 787
d6bbfa4e
VK
788public:
789 TableRow(int columnCount);
790 TableRow(TableRow *src);
791 ~TableRow() { delete m_cells; }
792
793 void addColumn() { m_cells->add(new TableCell); }
794 void deleteColumn(int index) { m_cells->remove(index); }
bac74bf7 795
d6bbfa4e
VK
796 void set(int index, const TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->set(value, status); }
797 void setPreallocated(int index, TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocated(value, status); }
798
799 void setValue(int index, const TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setValue(value); }
800 void setPreallocatedValue(int index, TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocatedValue(value); }
801
802 void setStatus(int index, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setStatus(status); }
803
804 const TCHAR *getValue(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getValue() : NULL; }
805 int getStatus(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getStatus() : -1; }
806
807 UINT32 getObjectId() { return m_objectId; }
808 void setObjectId(UINT32 id) { m_objectId = id; }
809};
810
811/**
967893bb
VK
812 * Class for table data storage
813 */
a0ddfb29 814class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
815{
816private:
d6bbfa4e 817 ObjectArray<TableRow> *m_data;
967893bb
VK
818 ObjectArray<TableColumnDefinition> *m_columns;
819 TCHAR *m_title;
820 int m_source;
d6bbfa4e 821 bool m_extendedFormat;
967893bb 822
b368969c 823 void createFromMessage(NXCPMessage *msg);
967893bb
VK
824 void destroy();
825
826public:
827 Table();
a0ddfb29 828 Table(Table *src);
b368969c 829 Table(NXCPMessage *msg);
b549a0f8 830 virtual ~Table();
967893bb 831
b368969c
VK
832 int fillMessage(NXCPMessage &msg, int offset, int rowLimit);
833 void updateFromMessage(NXCPMessage *msg);
967893bb 834
a0ddfb29
VK
835 void addAll(Table *src);
836 void copyRow(Table *src, int row);
967893bb 837
d6bbfa4e
VK
838 int getNumRows() { return m_data->size(); }
839 int getNumColumns() { return m_columns->size(); }
967893bb
VK
840 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
841 int getSource() { return m_source; }
842
d6bbfa4e
VK
843 bool isExtendedFormat() { return m_extendedFormat; }
844 void setExtendedFormat(bool ext) { m_extendedFormat = ext; }
845
967893bb
VK
846 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
847 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
848 int getColumnIndex(const TCHAR *name);
849 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
850
c83f6449 851 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = _tcsdup_ex(title); }
967893bb 852 void setSource(int source) { m_source = source; }
8dba1eac 853 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
967893bb
VK
854 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
855 int addRow();
856
25076d53
VK
857 void deleteRow(int row);
858 void deleteColumn(int col);
859
967893bb
VK
860 void setAt(int nRow, int nCol, INT32 nData);
861 void setAt(int nRow, int nCol, UINT32 dwData);
862 void setAt(int nRow, int nCol, double dData);
863 void setAt(int nRow, int nCol, INT64 nData);
864 void setAt(int nRow, int nCol, UINT64 qwData);
865 void setAt(int nRow, int nCol, const TCHAR *pszData);
866 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
867
d6bbfa4e
VK
868 void set(int nCol, INT32 nData) { setAt(getNumRows() - 1, nCol, nData); }
869 void set(int nCol, UINT32 dwData) { setAt(getNumRows() - 1, nCol, dwData); }
870 void set(int nCol, double dData) { setAt(getNumRows() - 1, nCol, dData); }
871 void set(int nCol, INT64 nData) { setAt(getNumRows() - 1, nCol, nData); }
872 void set(int nCol, UINT64 qwData) { setAt(getNumRows() - 1, nCol, qwData); }
d65c16a8
VK
873 void set(int nCol, const TCHAR *data) { setAt(getNumRows() - 1, nCol, data); }
874#ifdef UNICODE
875 void set(int nCol, const char *data) { setPreallocatedAt(getNumRows() - 1, nCol, WideStringFromMBString(data)); }
876#else
877 void set(int nCol, const WCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, MBStringFromWideString(data)); }
878#endif
879 void setPreallocated(int nCol, TCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, data); }
d6bbfa4e
VK
880
881 void setStatusAt(int row, int col, int status);
882 void setStatus(int col, int status) { setStatusAt(getNumRows() - 1, col, status); }
967893bb
VK
883
884 const TCHAR *getAsString(int nRow, int nCol);
885 INT32 getAsInt(int nRow, int nCol);
886 UINT32 getAsUInt(int nRow, int nCol);
887 INT64 getAsInt64(int nRow, int nCol);
888 UINT64 getAsUInt64(int nRow, int nCol);
889 double getAsDouble(int nRow, int nCol);
22aaa779 890
d6bbfa4e
VK
891 int getStatus(int nRow, int nCol);
892
22aaa779 893 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 894 int findRowByInstance(const TCHAR *instance);
d6bbfa4e
VK
895
896 UINT32 getObjectId(int row) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getObjectId() : 0; }
897 void setObjectId(int row, UINT32 id) { TableRow *r = m_data->get(row); if (r != NULL) r->setObjectId(id); }
967893bb
VK
898};
899
25fe6265 900/**
c11eee9b
VK
901 * sockaddr buffer
902 */
903union SockAddrBuffer
904{
905 struct sockaddr_in sa4;
906#ifdef WITH_IPV6
907 struct sockaddr_in6 sa6;
908#endif
909};
910
911/**
912 * sockaddr length calculation
913 */
914#ifdef WITH_IPV6
915#define SA_LEN(sa) (((sa)->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
916#else
917#define SA_LEN(sa) sizeof(struct sockaddr_in)
918#endif
919
920/**
5f6e8d09
VK
921 * Get port number from SockAddrBuffer
922 */
923#ifdef WITH_IPV6
924#define SA_PORT(sa) ((((struct sockaddr *)sa)->sa_family == AF_INET) ? ((struct sockaddr_in *)sa)->sin_port : ((struct sockaddr_in6 *)sa)->sin6_port)
925#else
926#define SA_PORT(sa) (((struct sockaddr_in *)sa)->sin_port)
927#endif
928
929/**
30592fa6
VK
930 * Compare addresses in sockaddr
931 */
932inline bool SocketAddressEquals(struct sockaddr *a1, struct sockaddr *a2)
933{
934 if (a1->sa_family != a2->sa_family)
935 return false;
936 if (a1->sa_family == AF_INET)
937 return ((struct sockaddr_in *)a1)->sin_addr.s_addr == ((struct sockaddr_in *)a2)->sin_addr.s_addr;
938#ifdef WITH_IPV6
939 if (a1->sa_family == AF_INET6)
940 return !memcmp(((struct sockaddr_in6 *)a1)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)a2)->sin6_addr.s6_addr, 16);
941#endif
942 return false;
943}
944
945/**
9319c166
VK
946 * IP address
947 */
948class LIBNETXMS_EXPORTABLE InetAddress
949{
9319c166 950private:
8c75ad41
VK
951 short m_maskBits;
952 short m_family;
953 union
954 {
955 UINT32 v4;
956 BYTE v6[16];
957 } m_addr;
9319c166
VK
958
959public:
8c75ad41
VK
960 InetAddress();
961 InetAddress(UINT32 addr);
c75e9ee4 962 InetAddress(UINT32 addr, UINT32 mask);
4203dcb5 963 InetAddress(const BYTE *addr, int maskBits = 128);
9319c166 964
8c75ad41
VK
965 bool isAnyLocal() const;
966 bool isLoopback() const;
967 bool isMulticast() const;
968 bool isBroadcast() const;
4203dcb5 969 bool isLinkLocal() const;
8c75ad41 970 bool isValid() const { return m_family != AF_UNSPEC; }
4203dcb5 971 bool isValidUnicast() const { return isValid() && !isAnyLocal() && !isLoopback() && !isMulticast() && !isBroadcast() && !isLinkLocal(); }
9319c166 972
8c75ad41 973 int getFamily() const { return m_family; }
c7853753
VK
974 UINT32 getAddressV4() const { return (m_family == AF_INET) ? m_addr.v4 : 0; }
975 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 976
8c75ad41 977 bool contain(const InetAddress &a) const;
43709a0c 978 bool sameSubnet(const InetAddress &a) const;
8c75ad41
VK
979 bool equals(const InetAddress &a) const;
980 int compareTo(const InetAddress &a) const;
9319c166 981
8c75ad41
VK
982 void setMaskBits(int m) { m_maskBits = m; }
983 int getMaskBits() const { return m_maskBits; }
c75e9ee4 984 int getHostBits() const { return (m_family == AF_INET) ? (32 - m_maskBits) : (128 - m_maskBits); }
9319c166 985
463b1d34
VK
986 InetAddress getSubnetAddress() const;
987 bool isSubnetBroadcast(int maskBits) const;
ffabd7f8 988
8c75ad41
VK
989 String toString() const;
990 TCHAR *toString(TCHAR *buffer) const;
9319c166 991
43709a0c 992 BYTE *buildHashKey(BYTE *key) const;
f3b94fc5 993
43709a0c
VK
994 TCHAR *getHostByAddr(TCHAR *buffer, size_t buflen) const;
995
996 struct sockaddr *fillSockAddr(SockAddrBuffer *buffer, UINT16 port = 0) const;
c11eee9b 997
c7853753
VK
998 static InetAddress resolveHostName(const WCHAR *hostname, int af = AF_INET);
999 static InetAddress resolveHostName(const char *hostname, int af = AF_INET);
f3b94fc5
VK
1000 static InetAddress parse(const WCHAR *str);
1001 static InetAddress parse(const char *str);
8c75ad41 1002 static InetAddress createFromSockaddr(struct sockaddr *s);
dcc7a2b0
VK
1003
1004 static const InetAddress INVALID;
48313f37 1005 static const InetAddress LOOPBACK;
dcc7a2b0
VK
1006};
1007
1008/**
1009 * IP address list
1010 */
1011class LIBNETXMS_EXPORTABLE InetAddressList
1012{
1013private:
1014 ObjectArray<InetAddress> *m_list;
1015
1016 int indexOf(const InetAddress& addr) const;
1017
1018public:
1019 InetAddressList();
1020 ~InetAddressList();
1021
1022 void add(const InetAddress& addr);
c30c0c0f
VK
1023 void add(const InetAddressList& addrList);
1024 void replace(const InetAddress& addr);
dcc7a2b0 1025 void remove(const InetAddress& addr);
c30c0c0f 1026 void clear() { m_list->clear(); }
4203dcb5 1027 const InetAddress& get(int index) const { const InetAddress *a = m_list->get(index); return (a != NULL) ? *a : InetAddress::INVALID; }
dcc7a2b0
VK
1028
1029 int size() const { return m_list->size(); }
1030 bool hasAddress(const InetAddress& addr) const { return indexOf(addr) != -1; }
4203dcb5 1031 const InetAddress& findAddress(const InetAddress& addr) const { int idx = indexOf(addr); return (idx != -1) ? *m_list->get(idx) : InetAddress::INVALID; }
c34b611c 1032 const InetAddress& findSameSubnetAddress(const InetAddress& addr) const;
dcc7a2b0 1033 const InetAddress& getFirstUnicastAddress() const;
c34b611c
VK
1034 const InetAddress& getFirstUnicastAddressV4() const;
1035 bool hasValidUnicastAddress() const { return getFirstUnicastAddress().isValid(); }
c30c0c0f 1036 bool isLoopbackOnly() const;
dcc7a2b0
VK
1037
1038 const ObjectArray<InetAddress> *getList() const { return m_list; }
40c8f341
VK
1039
1040 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
9319c166
VK
1041};
1042
1043/**
25fe6265
VK
1044 * Network connection
1045 */
58ee2f43
VK
1046class LIBNETXMS_EXPORTABLE SocketConnection
1047{
1048protected:
1049 SOCKET m_socket;
1050 char m_data[4096];
1051 int m_dataPos;
1052
1053public:
1054 SocketConnection();
1055 virtual ~SocketConnection();
1056
967893bb
VK
1057 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1058 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
1059 void disconnect();
1060
967893bb
VK
1061 bool canRead(UINT32 timeout);
1062 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43 1063 bool waitForText(const char *text, int timeout);
cc022855 1064
58ee2f43 1065 int write(const char *pBuff, int nSize);
1671f85d
VK
1066 bool writeLine(const char *line);
1067
967893bb 1068 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
1069};
1070
030a0779
VK
1071/**
1072 * Telnet connection - handles all telnet negotiation
1073 */
def5b792
AK
1074class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
1075{
030a0779 1076protected:
967893bb
VK
1077 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1078 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 1079
def5b792 1080public:
967893bb 1081 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 1082
967893bb
VK
1083 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
1084 bool connect(UINT32 ip, WORD port, UINT32 timeout);
1085 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1086 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
1087};
1088
56fa1092
VK
1089/**
1090 * Postal address representation
1091 */
1092class LIBNETXMS_EXPORTABLE PostalAddress
1093{
1094private:
1095 TCHAR *m_country;
1096 TCHAR *m_city;
1097 TCHAR *m_streetAddress;
1098 TCHAR *m_postcode;
1099
1100public:
1101 PostalAddress();
1102 PostalAddress(const TCHAR *country, const TCHAR *city, const TCHAR *streetAddress, const TCHAR *postcode);
1103 ~PostalAddress();
1104
1105 const TCHAR *getCountry() { return CHECK_NULL_EX(m_country); }
1106 const TCHAR *getCity() { return CHECK_NULL_EX(m_city); }
1107 const TCHAR *getStreetAddress() { return CHECK_NULL_EX(m_streetAddress); }
1108 const TCHAR *getPostCode() { return CHECK_NULL_EX(m_postcode); }
1109
1110 void setCountry(const TCHAR *country) { safe_free(m_country); m_country = _tcsdup_ex(country); }
1111 void setCity(const TCHAR *city) { safe_free(m_city); m_city = _tcsdup_ex(city); }
1112 void setStreetAddress(const TCHAR *streetAddress) { safe_free(m_streetAddress); m_streetAddress = _tcsdup_ex(streetAddress); }
1113 void setPostCode(const TCHAR *postcode) { safe_free(m_postcode); m_postcode = _tcsdup_ex(postcode); }
1114};
1115
b160350d
VK
1116#endif /* __cplusplus */
1117
030a0779
VK
1118/**
1119 * Configuration item template for configuration loader
1120 */
4826c8a5
VK
1121typedef struct
1122{
e2e66145
VK
1123 TCHAR token[64];
1124 BYTE type;
1125 BYTE separator; // Separator character for lists
1126 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
e95680e5 1127 UINT64 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
e2e66145
VK
1128 UINT32 bufferPos; // Should be set to 0
1129 void *buffer;
1130 void *overrideIndicator;
4826c8a5 1131} NX_CFG_TEMPLATE;
dcb44c9a 1132
030a0779
VK
1133/**
1134 * Code translation structure
1135 */
1b56f3c5 1136typedef struct __CODE_TO_TEXT
bc858635
VK
1137{
1138 int code;
1139 const TCHAR *text;
1140} CODE_TO_TEXT;
1141
e2e66145
VK
1142/**
1143 * getopt() prototype if needed
1144 */
df9812a9 1145#if USE_BUNDLED_GETOPT
99e50bb7 1146#include <netxms_getopt.h>
3046f9a4
VK
1147#endif
1148
1149
1150//
f2fdf1b5
VK
1151// Win32 API functions missing under WinCE
1152//
1153
c32b805a 1154#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
1155
1156inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
1157{
1158 SYSTEMTIME sysTime;
cc022855 1159
f2fdf1b5
VK
1160 GetSystemTime(&sysTime);
1161 SystemTimeToFileTime(&sysTime, pFt);
1162}
1163
1164#endif // UNDER_CE
1165
1166
1167//
3d1cb7d6
VK
1168// Structures for opendir() / readdir() / closedir()
1169//
1170
1171#ifdef _WIN32
1172
7f9e0c00 1173typedef struct dirent
3d1cb7d6
VK
1174{
1175 long d_ino; /* inode number (not used by MS-DOS) */
1176 int d_namlen; /* Name length */
1177 char d_name[257]; /* file name */
1178} _DIRECT;
1179
1180typedef struct _dir_struc
1181{
1182 char *start; /* Starting position */
1183 char *curr; /* Current position */
1184 long size; /* Size of string table */
1185 long nfiles; /* number if filenames in table */
7f9e0c00 1186 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
1187} DIR;
1188
5ea2db30
VK
1189#ifdef UNICODE
1190
1191typedef struct dirent_w
1192{
1193 long d_ino; /* inode number (not used by MS-DOS) */
1194 int d_namlen; /* Name length */
1195 WCHAR d_name[257]; /* file name */
1196} _DIRECTW;
1197
1198typedef struct _dir_struc_w
1199{
1200 WCHAR *start; /* Starting position */
1201 WCHAR *curr; /* Current position */
1202 long size; /* Size of string table */
1203 long nfiles; /* number if filenames in table */
1204 struct dirent_w dirstr; /* Directory structure to return */
1205} DIRW;
1206
1207#define _TDIR DIRW
1208#define _TDIRECT _DIRECTW
1209#define _tdirent dirent_w
1210
1211#else
1212
1213#define _TDIR DIR
1214#define _TDIRECT _DIRECT
1215#define _tdirent dirent
1216
37d62364
AK
1217#endif
1218
76b1d3d8
VK
1219#else /* not _WIN32 */
1220
1221typedef struct dirent_w
1222{
1223 long d_ino; /* inode number */
1224 WCHAR d_name[257]; /* file name */
1225} _DIRECTW;
1226
1227typedef struct _dir_struc_w
1228{
1229 DIR *dir; /* Original non-unicode structure */
1230 struct dirent_w dirstr; /* Directory structure to return */
1231} DIRW;
1232
3d1cb7d6
VK
1233#endif /* _WIN32 */
1234
1235
1236//
e9246d26
VK
1237// Functions
1238//
1239
55b33d4c
VK
1240#ifdef UNDER_CE
1241#define close(x) CloseHandle((HANDLE)(x))
1242#endif
1243
b50f1100 1244#ifdef __cplusplus
18c575eb 1245
2e590d1a
VK
1246inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
1247{
912b994d 1248#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1249 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
912b994d 1250#else
2e590d1a
VK
1251 _tcsncpy(pszDest, pszSrc, nLen - 1);
1252 pszDest[nLen - 1] = 0;
912b994d 1253#endif
2e590d1a
VK
1254 return pszDest;
1255}
f3387429
VK
1256
1257#ifdef UNICODE
2e590d1a
VK
1258inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
1259{
f3387429 1260#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1261 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429 1262#else
2e590d1a
VK
1263 strncpy(pszDest, pszSrc, nLen - 1);
1264 pszDest[nLen - 1] = 0;
c81979bc 1265#endif
2e590d1a
VK
1266 return pszDest;
1267}
c81979bc 1268#else
f3387429 1269#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
1270#endif
1271
967893bb 1272int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
5e60b759
VK
1273int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
1274int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
18c575eb
VK
1275bool LIBNETXMS_EXPORTABLE RecvAll(SOCKET s, void *buffer, size_t size, UINT32 timeout);
1276
b0608204 1277#endif /* __cplusplus */
c81979bc
VK
1278
1279#ifdef __cplusplus
e9246d26
VK
1280extern "C"
1281{
b50f1100 1282#endif
735bac5c 1283
076a55c4 1284#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc 1285#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
735bac5c
VK
1286void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
1287#define wcsupr __wcsupr
cc022855 1288#endif
735bac5c
VK
1289void LIBNETXMS_EXPORTABLE __strupr(char *in);
1290#define strupr __strupr
7e679c4b 1291#endif
cc022855 1292
735bac5c 1293void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
b8a8d064
VK
1294 int (*compare)(const void *, const void *, void *));
1295
735bac5c 1296INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
d35ed0aa 1297
735bac5c
VK
1298UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
1299UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
1300#ifdef UNICODE
1301#define FileSize FileSizeW
1302#else
1303#define FileSize FileSizeA
1304#endif
670c7f3d 1305
735bac5c
VK
1306int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1307TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 1308#ifdef UNICODE
735bac5c 1309char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
1310#else
1311#define IpToStrA IpToStr
e0f99bf0 1312#endif
8c75ad41 1313TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(const BYTE *addr, TCHAR *buffer);
735bac5c 1314TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
60557d06 1315
735bac5c
VK
1316UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *name);
1317UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameW(const WCHAR *name);
08b214c6 1318#ifdef UNICODE
cfe20cce 1319#define ResolveHostName ResolveHostNameW
08b214c6 1320#else
cfe20cce 1321#define ResolveHostName ResolveHostNameA
08b214c6 1322#endif
a1dc75b0 1323
735bac5c
VK
1324void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1325void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
31521248 1326
735bac5c
VK
1327WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *data, size_t size, WCHAR *pStr);
1328char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *data, size_t size, char *pStr);
d7970ee7
VK
1329#ifdef UNICODE
1330#define BinToStr BinToStrW
1331#else
1332#define BinToStr BinToStrA
1333#endif
1334
735bac5c
VK
1335size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1336size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
52db3ed1
VK
1337#ifdef UNICODE
1338#define StrToBin StrToBinW
1339#else
1340#define StrToBin StrToBinA
1341#endif
cc022855 1342
735bac5c 1343TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
b50f1100 1344
735bac5c
VK
1345void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1346void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
b07c50cc
VK
1347#ifdef UNICODE
1348#define StrStrip StrStripW
1349#else
1350#define StrStrip StrStripA
1351#endif
1352
735bac5c
VK
1353const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
1354const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
1355#ifdef UNICODE
1356#define ExtractWord ExtractWordW
1357#else
1358#define ExtractWord ExtractWordA
1359#endif
1360
735bac5c
VK
1361int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1362int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
3583a400
VK
1363#ifdef UNICODE
1364#define NumChars NumCharsW
1365#else
1366#define NumChars NumCharsA
1367#endif
ada97259 1368
735bac5c
VK
1369void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1370void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
35f836fe
VK
1371#ifdef UNICODE
1372#define RemoveTrailingCRLF RemoveTrailingCRLFW
1373#else
1374#define RemoveTrailingCRLF RemoveTrailingCRLFA
1375#endif
1376
4893b4aa
VK
1377BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *str, const char *expr, bool matchCase);
1378BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *str, const WCHAR *expr, bool matchCase);
cafe592d
VK
1379#ifdef UNICODE
1380#define RegexpMatch RegexpMatchW
1381#else
1382#define RegexpMatch RegexpMatchA
1383#endif
1384
735bac5c 1385const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
13aa082e 1386const TCHAR LIBNETXMS_EXPORTABLE *ExpandFromTimestamp(const TCHAR *name, TCHAR *buffer, size_t bufSize, time_t time);
d77c193c 1387BOOL LIBNETXMS_EXPORTABLE CreateFolder(const TCHAR *directory);
818f8c94 1388TCHAR LIBNETXMS_EXPORTABLE *Trim(TCHAR *str);
735bac5c
VK
1389bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
1390TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
ada97259 1391
f7794b68 1392#ifdef __cplusplus
735bac5c 1393BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
f7794b68 1394#endif
735bac5c
VK
1395BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
1396/* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1397const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
1398void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
1399BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 1400#ifdef UNICODE
735bac5c 1401BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
1402#else
1403#define LoadFileA LoadFile
1404#endif
1405
735bac5c
VK
1406UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1407void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
1408void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
1409void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
1410void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
1718cc2a 1411void LIBNETXMS_EXPORTABLE CalculateSHA256Hash(const unsigned char *data, size_t len, unsigned char *hash);
735bac5c
VK
1412BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1413BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
1414BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 1415
981d246a
VK
1416void LIBNETXMS_EXPORTABLE GenerateRandomBytes(BYTE *buffer, size_t size);
1417
735bac5c
VK
1418void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1419void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
4e2debcc 1420
735bac5c 1421BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
6b1282e7 1422
735bac5c 1423int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 1424
735bac5c
VK
1425HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
1426void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
1427void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 1428
735bac5c
VK
1429bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1430bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1431long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
289d126b 1432
735bac5c
VK
1433bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1434bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1435long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
289d126b
VK
1436
1437#ifdef UNICODE
1438#define ExtractNamedOptionValue ExtractNamedOptionValueW
1439#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1440#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1441#else
1442#define ExtractNamedOptionValue ExtractNamedOptionValueA
1443#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1444#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1445#endif
ae9cddaf 1446
bc858635 1447#ifdef __cplusplus
735bac5c 1448const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
bc858635 1449#else
735bac5c 1450const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
bc858635
VK
1451#endif
1452
aa13cfd9 1453#ifdef _WIN32
735bac5c
VK
1454TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
1455BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
1456INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 1457#endif
696fc54f 1458
de4c0511
VK
1459#if !HAVE_DAEMON || !HAVE_DECL_DAEMON
1460int LIBNETXMS_EXPORTABLE __daemon(int nochdir, int noclose);
1461#define daemon __daemon
696fc54f 1462#endif
4332e9d8 1463
735bac5c 1464UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1465
4332e9d8 1466#ifndef _WIN32
735bac5c
VK
1467BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
1468int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
1469 int cchWideChar, char *pByteStr, int cchByteChar,
1470 char *pDefaultChar, BOOL *pbUsedDefChar);
1471int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
1472 int cchByteChar, WCHAR *pWideCharStr,
1473 int cchWideChar);
04dbaf62 1474
6b93c954 1475#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
735bac5c 1476int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1477#endif
1478#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
735bac5c 1479UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1480#endif
1481#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
b6ec31bb 1482UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strdup(const UCS2CHAR *pStr);
4332e9d8 1483#endif
8b86c5dc 1484
735bac5c
VK
1485size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1486size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
1487UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1488char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1489
735bac5c
VK
1490int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1491int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1492int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1493int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1494int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1495int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1496
735bac5c
VK
1497int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1498int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
1499int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
1500int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1501int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
1502int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1503
3e39fcde 1504#endif /* _WIN32 */
8b86c5dc 1505
3d1058ed 1506#ifdef _WITH_ENCRYPTION
735bac5c 1507WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
3d1058ed
VK
1508#endif
1509
8b86c5dc 1510#ifdef UNICODE_UCS4
735bac5c
VK
1511size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1512size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1513size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1514UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1515WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1516#endif
8b86c5dc 1517
ebe98224
VK
1518size_t LIBNETXMS_EXPORTABLE utf8_to_mb(const char *src, int srcLen, char *dst, int dstLen);
1519size_t LIBNETXMS_EXPORTABLE mb_to_utf8(const char *src, int srcLen, char *dst, int dstLen);
1520char LIBNETXMS_EXPORTABLE *MBStringFromUTF8String(const char *s);
1521char LIBNETXMS_EXPORTABLE *UTF8StringFromMBString(const char *s);
1522
842c22f4 1523#if !defined(_WIN32) && !HAVE_WSTAT
735bac5c 1524int wstat(const WCHAR *_path, struct stat *_sbuf);
842c22f4
AK
1525#endif
1526
ef856d02 1527#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1528
f3387429 1529#if !HAVE_WPOPEN
735bac5c 1530FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
f3387429 1531#endif
8b86c5dc 1532#if !HAVE_WFOPEN
735bac5c 1533FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
8b86c5dc 1534#endif
8c2489aa 1535#if HAVE_FOPEN64 && !HAVE_WFOPEN64
735bac5c 1536FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
8c2489aa 1537#endif
8b86c5dc 1538#if !HAVE_WOPEN
735bac5c 1539int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1540#endif
f3387429 1541#if !HAVE_WCHMOD
735bac5c 1542int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
f3387429 1543#endif
b07c50cc 1544#if !HAVE_WCHDIR
735bac5c 1545int wchdir(const WCHAR *_path);
b07c50cc
VK
1546#endif
1547#if !HAVE_WMKDIR
735bac5c 1548int wmkdir(const WCHAR *_path, int mode);
b07c50cc
VK
1549#endif
1550#if !HAVE_WRMDIR
735bac5c 1551int wrmdir(const WCHAR *_path);
b07c50cc 1552#endif
ccc34207 1553#if !HAVE_WRENAME
735bac5c 1554int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
ccc34207
VK
1555#endif
1556#if !HAVE_WUNLINK
735bac5c 1557int wunlink(const WCHAR *_path);
ccc34207 1558#endif
b07c50cc 1559#if !HAVE_WREMOVE
735bac5c 1560int wremove(const WCHAR *_path);
b07c50cc
VK
1561#endif
1562#if !HAVE_WSYSTEM
735bac5c 1563int wsystem(const WCHAR *_cmd);
b07c50cc 1564#endif
dda7c270 1565#if !HAVE_WMKSTEMP
735bac5c 1566int wmkstemp(WCHAR *_template);
dda7c270 1567#endif
b07c50cc 1568#if !HAVE_WACCESS
735bac5c 1569int waccess(const WCHAR *_path, int mode);
b07c50cc 1570#endif
273da39a 1571#if !HAVE_WGETENV
735bac5c 1572WCHAR *wgetenv(const WCHAR *_string);
273da39a 1573#endif
f3387429 1574#if !HAVE_WCTIME
735bac5c 1575WCHAR *wctime(const time_t *timep);
f3387429 1576#endif
0186b5da 1577#if !HAVE_PUTWS
735bac5c 1578int putws(const WCHAR *s);
0186b5da 1579#endif
d49d7500 1580#if !HAVE_WCSERROR && HAVE_STRERROR
735bac5c 1581WCHAR *wcserror(int errnum);
d49d7500
VK
1582#endif
1583#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06 1584#if HAVE_POSIX_STRERROR_R
735bac5c 1585int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
b0823d06 1586#else
735bac5c 1587WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
d49d7500 1588#endif
b0823d06 1589#endif
e05f8aaf 1590
ef856d02 1591#endif /* UNICODE && !_WIN32*/
e05f8aaf 1592
8b86c5dc 1593#if !HAVE_STRTOLL
735bac5c 1594INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
8b86c5dc
VK
1595#endif
1596#if !HAVE_STRTOULL
735bac5c 1597UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1598#endif
e05f8aaf
VK
1599
1600#if !HAVE_WCSTOLL
735bac5c 1601INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf
VK
1602#endif
1603#if !HAVE_WCSTOULL
735bac5c 1604UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1605#endif
b50f1100 1606
2f56be29 1607#if !HAVE_WCSLWR && !defined(_WIN32)
735bac5c 1608WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
fdf1ed50
VK
1609#endif
1610
4d978b4a 1611#if !HAVE_WCSCASECMP && !defined(_WIN32)
735bac5c 1612int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
4d978b4a
VK
1613#endif
1614
f4d2c9ba 1615#if !HAVE_WCSNCASECMP && !defined(_WIN32)
735bac5c 1616int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
f4d2c9ba
VK
1617#endif
1618
9138dfe2
VK
1619#ifndef _WIN32
1620
1621#if HAVE_ITOA && !HAVE__ITOA
1622#define _itoa itoa
1623#undef HAVE__ITOA
1624#define HAVE__ITOA 1
1625#endif
1626#if !HAVE__ITOA && !defined(_WIN32)
1627char LIBNETXMS_EXPORTABLE *_itoa(int value, char *str, int base);
1628#endif
1629
1630#if HAVE_ITOW && !HAVE__ITOW
1631#define _itow itow
1632#undef HAVE__ITOW
1633#define HAVE__ITOW 1
1634#endif
1635#if !HAVE__ITOW && !defined(_WIN32)
1636WCHAR LIBNETXMS_EXPORTABLE *_itow(int value, WCHAR *str, int base);
1637#endif
1638
1639#endif /* _WIN32 */
1640
3d1cb7d6 1641#ifdef _WIN32
5ea2db30 1642#ifdef UNICODE
735bac5c
VK
1643DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1644struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1645int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
5ea2db30
VK
1646
1647#define _topendir wopendir
1648#define _treaddir wreaddir
1649#define _tclosedir wclosedir
1650#else
1651#define _topendir opendir
1652#define _treaddir readdir
1653#define _tclosedir closedir
1654#endif
1655
735bac5c
VK
1656DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
1657struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
1658int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1659
76b1d3d8
VK
1660#else /* not _WIN32 */
1661
735bac5c
VK
1662DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1663struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1664int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
76b1d3d8 1665
37d62364 1666#endif
3d1cb7d6 1667
7f9e0c00 1668#if defined(_WIN32) || !(HAVE_SCANDIR)
735bac5c
VK
1669int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1670 int (*select)(const struct dirent *),
1671 int (*compar)(const struct dirent **, const struct dirent **));
1672int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
7f9e0c00
VK
1673#endif
1674
3bf0df9f
VK
1675TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
1676
55b33d4c 1677#ifdef UNDER_CE
3bf0df9f
VK
1678int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1679int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1680int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
55b33d4c
VK
1681#endif
1682
967893bb 1683BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1684 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1685void LIBNETXMS_EXPORTABLE nxlog_close(void);
1686void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1687BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1688BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1689
1690typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1691void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1692
1693void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
6e383343
VK
1694void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
1695#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1696 __attribute__ ((format(printf, 1, 2)))
1697#endif
1698;
9f2902ac 1699
37a7a862
VK
1700#ifdef _WIN32
1701int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1702int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1703#ifdef UNICODE
1704#define _tmkstemp wmkstemp
1705#else
1706#define _tmkstemp mkstemp
1707#endif
1708#endif
1709
9b13553b
VK
1710#ifndef _WIN32
1711int strcat_s(char *dst, size_t dstSize, const char *src);
1712int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1713#endif
1714
c57d020a
VK
1715#if !HAVE_STRPTIME
1716char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
1717#endif
1718
1719#if !HAVE_TIMEGM
1720time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
1721#endif
1722
6f95ef89
VK
1723#if !HAVE_INET_PTON
1724int LIBNETXMS_EXPORTABLE nx_inet_pton(int af, const char *src, void *dst);
1725#define inet_pton nx_inet_pton
1726#endif
1727
b06ae508 1728int LIBNETXMS_EXPORTABLE GetSleepTime(int hour, int minute, int second);
b60584cf
VK
1729time_t LIBNETXMS_EXPORTABLE ParseDateTimeA(const char *text, time_t defaultValue);
1730time_t LIBNETXMS_EXPORTABLE ParseDateTimeW(const WCHAR *text, time_t defaultValue);
1731
1732#ifdef UNICODE
1733#define ParseDateTime ParseDateTimeW
1734#else
1735#define ParseDateTime ParseDateTimeA
1736#endif
b06ae508 1737
b50f1100 1738#ifdef __cplusplus
e9246d26 1739}
b50f1100 1740#endif
e9246d26 1741
a7ff20a5
VK
1742
1743//
b06ae508 1744// C++ only functions
a7ff20a5
VK
1745//
1746
1747#ifdef __cplusplus
1748
a97f02e1
VK
1749enum nxDirectoryType
1750{
1751 nxDirBin = 0,
1752 nxDirData = 1,
1753 nxDirEtc = 2,
1754 nxDirLib = 3
1755};
1756
1757void LIBNETXMS_EXPORTABLE GetNetXMSDirectory(nxDirectoryType type, TCHAR *dir);
1758
43709a0c
VK
1759UINT32 LIBNETXMS_EXPORTABLE IcmpPing(const InetAddress& addr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
1760
240816b0
VK
1761TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1762String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1763const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1764int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1765UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1766bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1767
1768#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1769void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1770#endif
1771
a7ff20a5
VK
1772#endif
1773
e9246d26 1774#endif /* _nms_util_h_ */