Merge branch 'develop' of 10.3.0.4:public/netxms into develop
[public/netxms.git] / include / nms_util.h
CommitLineData
e9246d26 1/*
7ac71e60 2** NetXMS - Network Management System
b0608204 3** Copyright (C) 2003-2013 Victor Kirhenshtein
e9246d26
VK
4**
5** This program is free software; you can redistribute it and/or modify
68f384ea
VK
6** it under the terms of the GNU Lesser General Public License as published
7** by the Free Software Foundation; either version 3 of the License, or
e9246d26
VK
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
68f384ea 15** You should have received a copy of the GNU Lesser General Public License
e9246d26
VK
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
fb7ce4b9 19** File: nms_util.h
e9246d26
VK
20**
21**/
22
23#ifndef _nms_util_h_
24#define _nms_util_h_
25
64584976
VK
26#ifdef _WIN32
27#ifdef LIBNETXMS_EXPORTS
28#define LIBNETXMS_EXPORTABLE __declspec(dllexport)
29#else
30#define LIBNETXMS_EXPORTABLE __declspec(dllimport)
31#endif
32#else /* _WIN32 */
33#define LIBNETXMS_EXPORTABLE
34#endif
35
36
e9246d26
VK
37#include <nms_common.h>
38#include <nms_cscp.h>
953dfe76 39#include <nms_threads.h>
64584976 40#include <time.h>
e9246d26 41
6d8aee95
VK
42#if HAVE_BYTESWAP_H
43#include <byteswap.h>
44#endif
45
92502d5f
VK
46#include <base64.h>
47
6d8aee95
VK
48
49//
92502d5f 50// Serial communications
6d8aee95
VK
51//
52
53#ifdef _WIN32
54
55#define FLOW_NONE 0
56#define FLOW_SOFTWARE 1
57#define FLOW_HARDWARE 2
58
59#else /* _WIN32 */
60
70fed300
AK
61#ifdef HAVE_TERMIOS_H
62# include <termios.h>
63#else
64# error termios.h not found
65#endif
66
6d8aee95
VK
67#endif /* _WIN32 */
68
e2e66145
VK
69/**
70 * Return codes for IcmpPing()
71 */
66c04cc7
VK
72#define ICMP_SUCCESS 0
73#define ICMP_UNREACHEABLE 1
74#define ICMP_TIMEOUT 2
75#define ICMP_RAW_SOCK_FAILED 3
416816ed 76#define ICMP_API_ERROR 3
66c04cc7 77
e2e66145
VK
78/**
79 * Token types for configuration loader
80 */
4826c8a5
VK
81#define CT_LONG 0
82#define CT_STRING 1
83#define CT_STRING_LIST 2
84#define CT_END_OF_LIST 3
85#define CT_BOOLEAN 4
86#define CT_WORD 5
fc935a60 87#define CT_IGNORE 6
dda7c270 88#define CT_MB_STRING 7
4826c8a5 89
605fc935 90/**
e2e66145
VK
91 * Uninitialized value for override indicator
92 */
93#define NXCONFIG_UNINITIALIZED_VALUE (-1)
94
95/**
605fc935
VK
96 * Return codes for NxLoadConfig()
97 */
4826c8a5
VK
98#define NXCFG_ERR_OK 0
99#define NXCFG_ERR_NOFILE 1
100#define NXCFG_ERR_SYNTAX 2
101
605fc935
VK
102/**
103 * nxlog_open() flags
104 */
967893bb
VK
105#define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
106#define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
107#define NXLOG_IS_OPEN ((UINT32)0x80000000)
9f2902ac 108
605fc935
VK
109/**
110 * nxlog rotation policy
111 */
5945d50a
VK
112#define NXLOG_ROTATION_DISABLED 0
113#define NXLOG_ROTATION_DAILY 1
114#define NXLOG_ROTATION_BY_SIZE 2
115
605fc935
VK
116/**
117 * _tcsdup() replacement
118 */
8ff13508
VK
119#if defined(_WIN32) && defined(USE_WIN32_HEAP)
120#ifdef __cplusplus
121extern "C" {
122#endif
35f836fe 123char LIBNETXMS_EXPORTABLE *nx__tcsdup(const char *src);
8ff13508
VK
124WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
125#ifdef __cplusplus
126}
127#endif
128#endif
129
605fc935 130/**
f4d2c9ba
VK
131 * Custom wcsdup
132 */
133#if !HAVE_WCSDUP && !defined(_WIN32)
134#ifdef __cplusplus
135extern "C" {
136#endif
137 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
138#ifdef __cplusplus
139}
140#endif
141#endif
142
143/**
605fc935
VK
144 * Class for serial communications
145 */
b160350d
VK
146#ifdef __cplusplus
147
148#ifndef _WIN32
149enum
150{
151 NOPARITY,
152 ODDPARITY,
153 EVENPARITY,
154 ONESTOPBIT,
155 TWOSTOPBITS
156};
157
70fed300
AK
158enum
159{
160 FLOW_NONE,
161 FLOW_HARDWARE,
162 FLOW_SOFTWARE
163};
164
b160350d
VK
165#ifndef INVALID_HANDLE_VALUE
166#define INVALID_HANDLE_VALUE (-1)
167#endif
168#endif /* _WIN32 */
169
170class LIBNETXMS_EXPORTABLE Serial
171{
b160350d
VK
172private:
173 TCHAR *m_pszPort;
174 int m_nTimeout;
175 int m_nSpeed;
176 int m_nDataBits;
177 int m_nStopBits;
178 int m_nParity;
70fed300 179 int m_nFlowControl;
b160350d
VK
180
181#ifndef _WIN32
182 int m_hPort;
70fed300 183 struct termios m_originalSettings;
b160350d
VK
184#else
185 HANDLE m_hPort;
186#endif
b160350d 187
826fcc1f
VK
188public:
189 Serial();
190 ~Serial();
191
192 bool open(const TCHAR *pszPort);
193 void close();
194 void setTimeout(int nTimeout);
195 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
196 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
c3f71625 197 int readToMark(char *buff, int size, const char **marks, char **occurence);
826fcc1f
VK
198 bool write(const char *pBuff, int nSize);
199 void flush();
200 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
201 bool restart();
202};
ff214673 203
3fe40613
VK
204/**
205 * Dynamic string class
206 */
8bad8476
VK
207class LIBNETXMS_EXPORTABLE String
208{
209protected:
210 TCHAR *m_pszBuffer;
967893bb 211 UINT32 m_dwBufSize;
8bad8476
VK
212
213public:
c577e360
VK
214 static const int npos;
215
8bad8476 216 String();
f3cdd1c5 217 String(const TCHAR *init);
643c9dcb 218 String(const String &src);
8bad8476
VK
219 ~String();
220
7618e362 221 TCHAR *getBuffer() { return m_pszBuffer; }
ce7565e7 222 void setBuffer(TCHAR *pszBuffer);
8bad8476 223
2391d4fd 224 const String& operator =(const TCHAR *pszStr);
49bf9d8d 225 const String& operator =(const String &src);
1d34c533
VK
226 const String& operator +=(const TCHAR *str);
227 const String& operator +=(const String &str);
ce7565e7 228 operator const TCHAR*() { return CHECK_NULL_EX(m_pszBuffer); }
8bad8476 229
08b214c6
VK
230 char *getUTF8String();
231
967893bb 232 void addString(const TCHAR *pStr, UINT32 dwLen);
ce7565e7 233 void addDynamicString(TCHAR *pszStr) { if (pszStr != NULL) { *this += pszStr; free(pszStr); } }
c577e360 234
967893bb
VK
235 void addMultiByteString(const char *pStr, UINT32 dwSize, int nCodePage);
236 void addWideCharString(const WCHAR *pStr, UINT32 dwSize);
ff175e91 237
ce7565e7 238 void addFormattedString(const TCHAR *format, ...);
893dfdd7 239 void addFormattedStringV(const TCHAR *format, va_list args);
c577e360 240
967893bb 241 UINT32 getSize() { return m_dwBufSize > 0 ? m_dwBufSize - 1 : 0; }
ce7565e7 242 BOOL isEmpty() { return m_dwBufSize <= 1; }
c577e360 243
ce7565e7
VK
244 TCHAR *subStr(int nStart, int nLen, TCHAR *pszBuffer);
245 TCHAR *subStr(int nStart, int nLen) { return subStr(nStart, nLen, NULL); }
246 int find(const TCHAR *pszStr, int nStart = 0);
1e1450ef 247
6bcafa2d
VK
248 void escapeCharacter(int ch, int esc);
249 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
0bbab9d3
VK
250 void trim();
251 void shrink(int chars = 1);
c577e360
VK
252};
253
3fe40613
VK
254/**
255 * String maps base class
256 */
257class LIBNETXMS_EXPORTABLE StringMapBase
c577e360
VK
258{
259protected:
967893bb 260 UINT32 m_size;
fb986055 261 TCHAR **m_keys;
3fe40613
VK
262 void **m_values;
263 bool m_objectOwner;
55bdca5a 264 bool m_ignoreCase;
3fe40613 265 void (*m_objectDestructor)(void *);
c577e360 266
967893bb 267 UINT32 find(const TCHAR *key);
3fe40613
VK
268 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
269 void *getObject(const TCHAR *key);
270 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
c577e360
VK
271
272public:
3fe40613
VK
273 StringMapBase(bool objectOwner);
274 virtual ~StringMapBase();
275
55bdca5a
VK
276 void setOwner(bool owner) { m_objectOwner = owner; }
277 void setIgnoreCase(bool ignore) { m_ignoreCase = ignore; }
278
3fe40613
VK
279 void remove(const TCHAR *key);
280 void clear();
281
967893bb
VK
282 UINT32 getSize() { return m_size; }
283 const TCHAR *getKeyByIndex(UINT32 idx) { return (idx < m_size) ? CHECK_NULL_EX(m_keys[idx]) : NULL; }
3fe40613
VK
284};
285
286/**
287 * String map class
288 */
289class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
290{
291public:
292 StringMap() : StringMapBase(true) { }
fb986055 293 StringMap(const StringMap &src);
3fe40613 294 virtual ~StringMap();
c577e360 295
fb986055 296 StringMap& operator =(const StringMap &src);
40b7416c 297
3fe40613
VK
298 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
299 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
967893bb 300 void set(const TCHAR *key, UINT32 value);
5ad2167d 301
3fe40613 302 const TCHAR *get(const TCHAR *key) { return (const TCHAR *)getObject(key); }
967893bb 303 UINT32 getULong(const TCHAR *key, UINT32 defaultValue);
6acb8479 304 bool getBoolean(const TCHAR *key, bool defaultValue);
5ad2167d 305
967893bb 306 const TCHAR *getValueByIndex(UINT32 idx) { return (idx < m_size) ? CHECK_NULL_EX((TCHAR *)m_values[idx]) : NULL; }
8bad8476
VK
307};
308
3fe40613
VK
309/**
310 * String map template for holding objects as values
311 */
312template <class T> class StringObjectMap : public StringMapBase
313{
314private:
315 static void destructor(void *object) { delete (T*)object; }
6173bea8 316
3fe40613
VK
317public:
318 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
319
320 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
321 T *get(const TCHAR *key) { return (T*)getObject(key); }
967893bb 322 T *getValueByIndex(UINT32 idx) { return (idx < m_size) ? (T *)m_values[idx] : NULL; }
3fe40613 323};
6173bea8 324
3fe40613
VK
325/**
326 * String list class
327 */
6173bea8
VK
328class LIBNETXMS_EXPORTABLE StringList
329{
330private:
331 int m_count;
332 int m_allocated;
333 TCHAR **m_values;
334
335public:
336 StringList();
86c126f5 337 StringList(StringList *src);
22ee6d97 338 StringList(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
339 ~StringList();
340
341 void add(const TCHAR *value);
342 void addPreallocated(TCHAR *value);
967893bb
VK
343 void add(INT32 value);
344 void add(UINT32 value);
6173bea8 345 void add(INT64 value);
967893bb 346 void add(UINT64 value);
6173bea8 347 void add(double value);
24dc5346 348 void replace(int index, const TCHAR *value);
6173bea8
VK
349 void clear();
350 int getSize() { return m_count; }
351 const TCHAR *getValue(int index) { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
ed806a1a
VK
352 int getIndex(const TCHAR *value);
353 int getIndexIgnoreCase(const TCHAR *value);
d51f2182 354 void remove(int index);
86c126f5 355 void addAll(const StringList *src);
93dbc1be 356 void merge(const StringList *src, bool matchCase);
7d25a5c5 357 TCHAR *join(const TCHAR *separator);
22ee6d97 358 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
359};
360
3fe40613 361/**
a58501c0
VK
362 * Entry of string set
363 */
364struct StringSetEntry;
365
366/**
367 * String set class
368 */
369class LIBNETXMS_EXPORTABLE StringSet
370{
371private:
372 StringSetEntry *m_data;
373
374public:
375 StringSet();
376 ~StringSet();
377
378 void add(const TCHAR *str);
379 void remove(const TCHAR *str);
380 void clear();
381
382 int size();
383 bool exist(const TCHAR *str);
384
fa493140 385 void addAll(StringSet *src);
a58501c0
VK
386 void forEach(bool (*cb)(const TCHAR *, void *), void *userData);
387};
388
389/**
3fe40613
VK
390 * Dynamic array class
391 */
0d75ea88
VK
392class LIBNETXMS_EXPORTABLE Array
393{
394private:
395 int m_size;
396 int m_allocated;
397 int m_grow;
ac5b1642 398 size_t m_elementSize;
0d75ea88
VK
399 void **m_data;
400 bool m_objectOwner;
401
16d6f798 402 void internalRemove(int index, bool allowDestruction);
cfa7c21f 403 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
16d6f798 404
0d75ea88 405protected:
cfa7c21f 406 void (*m_objectDestructor)(void *);
0d75ea88 407
ac5b1642 408 Array(void *data, int initial, int grow, size_t elementSize);
8da70db3 409
0d75ea88
VK
410public:
411 Array(int initial = 0, int grow = 16, bool owner = false);
412 virtual ~Array();
413
8da70db3
VK
414 int add(void *element);
415 void *get(int index) { return ((index >= 0) && (index < m_size)) ? ((m_elementSize <= sizeof(void *)) ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
416 int indexOf(void *element);
417 void set(int index, void *element);
418 void replace(int index, void *element);
16d6f798 419 void remove(int index) { internalRemove(index, true); }
8da70db3 420 void remove(void *element) { internalRemove(indexOf(element), true); }
16d6f798 421 void unlink(int index) { internalRemove(index, false); }
8da70db3 422 void unlink(void *element) { internalRemove(indexOf(element), false); }
0d75ea88
VK
423 void clear();
424
425 int size() { return m_size; }
426
427 void setOwner(bool owner) { m_objectOwner = owner; }
428 bool isOwner() { return m_objectOwner; }
429};
430
3fe40613
VK
431/**
432 * Template class for dynamic array which holds objects
433 */
0d75ea88
VK
434template <class T> class ObjectArray : public Array
435{
cfa7c21f
VK
436private:
437 static void destructor(void *object) { delete (T*)object; }
0d75ea88
VK
438
439public:
cfa7c21f 440 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
0d75ea88
VK
441 virtual ~ObjectArray() { }
442
47a89886 443 int add(T *object) { return Array::add((void *)object); }
0d75ea88 444 T *get(int index) { return (T*)Array::get(index); }
74d4ba34 445 int indexOf(T *object) { return Array::indexOf((void *)object); }
47a89886
VK
446 void set(int index, T *object) { Array::set(index, (void *)object); }
447 void replace(int index, T *object) { Array::replace(index, (void *)object); }
74d4ba34
VK
448 void remove(int index) { Array::remove(index); }
449 void remove(T *object) { Array::remove((void *)object); }
450 void unlink(int index) { Array::unlink(index); }
451 void unlink(T *object) { Array::unlink((void *)object); }
0d75ea88
VK
452};
453
25fe6265 454/**
e8e76b4f
VK
455 * Template class for dynamic array which holds scalar values
456 */
457template <class T> class IntegerArray : public Array
458{
459private:
8da70db3 460 static void destructor(void *element) { }
e8e76b4f
VK
461
462public:
463 IntegerArray(int initial = 0, int grow = 16) : Array(initial, grow, false) { m_objectDestructor = destructor; }
464 virtual ~IntegerArray() { }
465
466 int add(T value) { return Array::add(CAST_TO_POINTER(value, void *)); }
467 T get(int index) { return CAST_FROM_POINTER(Array::get(index), T); }
468 void set(int index, T value) { Array::set(index, CAST_TO_POINTER(value, void *)); }
469 void replace(int index, T value) { Array::replace(index, CAST_TO_POINTER(value, void *)); }
470};
471
472/**
25fe6265
VK
473 * Auxilliary class to hold dynamically allocated array of structures
474 */
8da70db3 475template <class T> class StructArray : public Array
cc8ce218
VK
476{
477private:
8da70db3 478 static void destructor(void *element) { }
cc8ce218
VK
479
480public:
8da70db3
VK
481 StructArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; }
482 StructArray(T *data, int size) : Array(data, size, 16, sizeof(T)) { m_objectDestructor = destructor; }
483 virtual ~StructArray() { }
0105c251 484
8da70db3
VK
485 int add(T *element) { return Array::add((void *)element); }
486 T *get(int index) { return (T*)Array::get(index); }
487 int indexOf(T *element) { return Array::indexOf((void *)element); }
488 void set(int index, T *element) { Array::set(index, (void *)element); }
489 void replace(int index, T *element) { Array::replace(index, (void *)element); }
490 void remove(int index) { Array::remove(index); }
491 void remove(T *element) { Array::remove((void *)element); }
492 void unlink(int index) { Array::unlink(index); }
493 void unlink(T *element) { Array::unlink((void *)element); }
cc8ce218
VK
494};
495
25fe6265
VK
496/**
497 * Auxilliary class for objects which counts references and
498 * destroys itself wheren reference count falls to 0
499 */
7f632dfe
VK
500class LIBNETXMS_EXPORTABLE RefCountObject
501{
502private:
7785322f 503 VolatileCounter m_refCount;
7f632dfe
VK
504
505public:
7785322f
VK
506 RefCountObject()
507 {
508 m_refCount = 1;
509 }
b549a0f8 510 virtual ~RefCountObject();
7785322f
VK
511
512 void incRefCount()
513 {
514 InterlockedIncrement(&m_refCount);
515 }
516
517 void decRefCount()
518 {
519 if (InterlockedDecrement(&m_refCount) == 0)
520 delete this;
521 }
7f632dfe
VK
522};
523
967893bb
VK
524class CSCPMessage;
525
526/**
527 * Table column definition
528 */
529class LIBNETXMS_EXPORTABLE TableColumnDefinition
530{
531private:
532 TCHAR *m_name;
533 TCHAR *m_displayName;
534 INT32 m_dataType;
535 bool m_instanceColumn;
536
537public:
538 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
539 TableColumnDefinition(CSCPMessage *msg, UINT32 baseId);
540 TableColumnDefinition(TableColumnDefinition *src);
541 ~TableColumnDefinition();
542
543 void fillMessage(CSCPMessage *msg, UINT32 baseId);
544
545 const TCHAR *getName() { return m_name; }
546 const TCHAR *getDisplayName() { return m_displayName; }
547 INT32 getDataType() { return m_dataType; }
548 bool isInstanceColumn() { return m_instanceColumn; }
549
550 void setDataType(INT32 type) { m_dataType = type; }
551 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 552 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
553};
554
555/**
556 * Class for table data storage
557 */
a0ddfb29 558class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
559{
560private:
561 int m_nNumRows;
562 int m_nNumCols;
f68b2b51 563 TCHAR **m_data;
967893bb
VK
564 ObjectArray<TableColumnDefinition> *m_columns;
565 TCHAR *m_title;
566 int m_source;
567
568 void createFromMessage(CSCPMessage *msg);
569 void destroy();
570
571public:
572 Table();
a0ddfb29 573 Table(Table *src);
967893bb 574 Table(CSCPMessage *msg);
b549a0f8 575 virtual ~Table();
967893bb
VK
576
577 int fillMessage(CSCPMessage &msg, int offset, int rowLimit);
578 void updateFromMessage(CSCPMessage *msg);
579
a0ddfb29
VK
580 void addAll(Table *src);
581 void copyRow(Table *src, int row);
967893bb
VK
582
583 int getNumRows() { return m_nNumRows; }
584 int getNumColumns() { return m_nNumCols; }
585 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
586 int getSource() { return m_source; }
587
588 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
589 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
590 int getColumnIndex(const TCHAR *name);
591 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
592
593 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = (title != NULL) ? _tcsdup(title) : NULL; }
594 void setSource(int source) { m_source = source; }
8dba1eac 595 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
967893bb
VK
596 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
597 int addRow();
598
25076d53
VK
599 void deleteRow(int row);
600 void deleteColumn(int col);
601
967893bb
VK
602 void setAt(int nRow, int nCol, INT32 nData);
603 void setAt(int nRow, int nCol, UINT32 dwData);
604 void setAt(int nRow, int nCol, double dData);
605 void setAt(int nRow, int nCol, INT64 nData);
606 void setAt(int nRow, int nCol, UINT64 qwData);
607 void setAt(int nRow, int nCol, const TCHAR *pszData);
608 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
609
610 void set(int nCol, INT32 nData) { setAt(m_nNumRows - 1, nCol, nData); }
611 void set(int nCol, UINT32 dwData) { setAt(m_nNumRows - 1, nCol, dwData); }
612 void set(int nCol, double dData) { setAt(m_nNumRows - 1, nCol, dData); }
613 void set(int nCol, INT64 nData) { setAt(m_nNumRows - 1, nCol, nData); }
614 void set(int nCol, UINT64 qwData) { setAt(m_nNumRows - 1, nCol, qwData); }
615 void set(int nCol, const TCHAR *pszData) { setAt(m_nNumRows - 1, nCol, pszData); }
616 void setPreallocated(int nCol, TCHAR *pszData) { setPreallocatedAt(m_nNumRows - 1, nCol, pszData); }
617
618 const TCHAR *getAsString(int nRow, int nCol);
619 INT32 getAsInt(int nRow, int nCol);
620 UINT32 getAsUInt(int nRow, int nCol);
621 INT64 getAsInt64(int nRow, int nCol);
622 UINT64 getAsUInt64(int nRow, int nCol);
623 double getAsDouble(int nRow, int nCol);
22aaa779
VK
624
625 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 626 int findRowByInstance(const TCHAR *instance);
967893bb
VK
627};
628
25fe6265
VK
629/**
630 * Network connection
631 */
58ee2f43
VK
632class LIBNETXMS_EXPORTABLE SocketConnection
633{
634protected:
635 SOCKET m_socket;
636 char m_data[4096];
637 int m_dataPos;
638
639public:
640 SocketConnection();
641 virtual ~SocketConnection();
642
967893bb
VK
643 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
644 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
645 void disconnect();
646
967893bb
VK
647 bool canRead(UINT32 timeout);
648 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43
VK
649 bool waitForText(const char *text, int timeout);
650
651 int write(const char *pBuff, int nSize);
1671f85d
VK
652 bool writeLine(const char *line);
653
967893bb 654 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
655};
656
030a0779
VK
657/**
658 * Telnet connection - handles all telnet negotiation
659 */
def5b792
AK
660class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
661{
030a0779 662protected:
967893bb
VK
663 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
664 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 665
def5b792 666public:
967893bb 667 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 668
967893bb
VK
669 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
670 bool connect(UINT32 ip, WORD port, UINT32 timeout);
671 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
672 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
673};
674
b160350d
VK
675#endif /* __cplusplus */
676
030a0779
VK
677/**
678 * Configuration item template for configuration loader
679 */
4826c8a5
VK
680typedef struct
681{
e2e66145
VK
682 TCHAR token[64];
683 BYTE type;
684 BYTE separator; // Separator character for lists
685 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
686 UINT32 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
687 UINT32 bufferPos; // Should be set to 0
688 void *buffer;
689 void *overrideIndicator;
4826c8a5 690} NX_CFG_TEMPLATE;
dcb44c9a 691
030a0779
VK
692/**
693 * Code translation structure
694 */
bc858635
VK
695typedef struct __CODE_TO_TEXT
696{
697 int code;
698 const TCHAR *text;
699} CODE_TO_TEXT;
700
e2e66145
VK
701/**
702 * getopt() prototype if needed
703 */
df9812a9 704#if USE_BUNDLED_GETOPT
99e50bb7 705#include <netxms_getopt.h>
3046f9a4
VK
706#endif
707
708
709//
f2fdf1b5
VK
710// Win32 API functions missing under WinCE
711//
712
c32b805a 713#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
714
715inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
716{
717 SYSTEMTIME sysTime;
718
719 GetSystemTime(&sysTime);
720 SystemTimeToFileTime(&sysTime, pFt);
721}
722
723#endif // UNDER_CE
724
725
726//
3d1cb7d6
VK
727// Structures for opendir() / readdir() / closedir()
728//
729
730#ifdef _WIN32
731
7f9e0c00 732typedef struct dirent
3d1cb7d6
VK
733{
734 long d_ino; /* inode number (not used by MS-DOS) */
735 int d_namlen; /* Name length */
736 char d_name[257]; /* file name */
737} _DIRECT;
738
739typedef struct _dir_struc
740{
741 char *start; /* Starting position */
742 char *curr; /* Current position */
743 long size; /* Size of string table */
744 long nfiles; /* number if filenames in table */
7f9e0c00 745 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
746} DIR;
747
5ea2db30
VK
748#ifdef UNICODE
749
750typedef struct dirent_w
751{
752 long d_ino; /* inode number (not used by MS-DOS) */
753 int d_namlen; /* Name length */
754 WCHAR d_name[257]; /* file name */
755} _DIRECTW;
756
757typedef struct _dir_struc_w
758{
759 WCHAR *start; /* Starting position */
760 WCHAR *curr; /* Current position */
761 long size; /* Size of string table */
762 long nfiles; /* number if filenames in table */
763 struct dirent_w dirstr; /* Directory structure to return */
764} DIRW;
765
766#define _TDIR DIRW
767#define _TDIRECT _DIRECTW
768#define _tdirent dirent_w
769
770#else
771
772#define _TDIR DIR
773#define _TDIRECT _DIRECT
774#define _tdirent dirent
775
37d62364
AK
776#endif
777
76b1d3d8
VK
778#else /* not _WIN32 */
779
780typedef struct dirent_w
781{
782 long d_ino; /* inode number */
783 WCHAR d_name[257]; /* file name */
784} _DIRECTW;
785
786typedef struct _dir_struc_w
787{
788 DIR *dir; /* Original non-unicode structure */
789 struct dirent_w dirstr; /* Directory structure to return */
790} DIRW;
791
3d1cb7d6
VK
792#endif /* _WIN32 */
793
794
795//
e9246d26
VK
796// Functions
797//
798
babb8280 799#if WORDS_BIGENDIAN
43d92b4c
VK
800#define htonq(x) (x)
801#define ntohq(x) (x)
9f20696e
VK
802#define htond(x) (x)
803#define ntohd(x) (x)
babb8280
VK
804#define SwapWideString(x)
805#else
ca6ad6bb
VK
806#ifdef HAVE_HTONLL
807#define htonq(x) htonll(x)
808#else
babb8280 809#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
810#endif
811#ifdef HAVE_NTOHLL
812#define ntohq(x) ntohll(x)
813#else
babb8280 814#define ntohq(x) __bswap_64(x)
ca6ad6bb 815#endif
babb8280
VK
816#define htond(x) __bswap_double(x)
817#define ntohd(x) __bswap_double(x)
818#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 819#endif
15929b31 820
55b33d4c
VK
821#ifdef UNDER_CE
822#define close(x) CloseHandle((HANDLE)(x))
823#endif
824
b50f1100 825#ifdef __cplusplus
4299401e 826 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
c81979bc 827 {
912b994d
VK
828#if defined(_WIN32) && (_MSC_VER >= 1400)
829 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
830#else
c81979bc
VK
831 _tcsncpy(pszDest, pszSrc, nLen - 1);
832 pszDest[nLen - 1] = 0;
912b994d 833#endif
c81979bc
VK
834 return pszDest;
835 }
f3387429
VK
836
837#ifdef UNICODE
838 inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
839 {
840#if defined(_WIN32) && (_MSC_VER >= 1400)
6646dca4 841 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429
VK
842#else
843 strncpy(pszDest, pszSrc, nLen - 1);
844 pszDest[nLen - 1] = 0;
c81979bc 845#endif
f3387429
VK
846 return pszDest;
847 }
c81979bc 848#else
f3387429 849#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
850#endif
851
967893bb 852int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
5e60b759
VK
853int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
854int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
b0608204 855#endif /* __cplusplus */
c81979bc
VK
856
857#ifdef __cplusplus
e9246d26
VK
858extern "C"
859{
b50f1100 860#endif
c5b523e2 861#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
967893bb 862 UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
c5b523e2 863#endif
f52be742 864#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
967893bb 865 UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
64584976 866#endif
9f20696e 867 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
273da39a 868 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 869
076a55c4 870#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc
VK
871#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
872 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
873#endif
874 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 875#endif
ab588c84 876
b8a8d064
VK
877 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
878 int (*compare)(const void *, const void *, void *));
879
244c65ef 880 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
d35ed0aa 881
967893bb
VK
882 UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
883 UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
884#ifdef UNICODE
885#define FileSize FileSizeW
886#else
887#define FileSize FileSizeA
888#endif
670c7f3d 889
967893bb
VK
890 int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
891 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 892#ifdef UNICODE
967893bb 893 char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
894#else
895#define IpToStrA IpToStr
e0f99bf0 896#endif
36e44abe 897 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
60557d06
VK
898 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
899
967893bb 900 UINT32 LIBNETXMS_EXPORTABLE ResolveHostName(const TCHAR *pszName);
08b214c6 901#ifdef UNICODE
967893bb 902 UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *pszName);
08b214c6
VK
903#else
904#define ResolveHostNameA ResolveHostName
905#endif
a1dc75b0 906
967893bb
VK
907 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, UINT32 dwSize);
908 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, UINT32 dwSize);
31521248 909
967893bb
VK
910 WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *pData, UINT32 dwSize, WCHAR *pStr);
911 char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *pData, UINT32 dwSize, char *pStr);
d7970ee7
VK
912#ifdef UNICODE
913#define BinToStr BinToStrW
914#else
915#define BinToStr BinToStrA
916#endif
917
52db3ed1
VK
918 UINT32 LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *pData, UINT32 dwSize);
919 UINT32 LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *pData, UINT32 dwSize);
920#ifdef UNICODE
921#define StrToBin StrToBinW
922#else
923#define StrToBin StrToBinA
924#endif
925
f42b8099 926 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *pData, TCHAR *pStr);
b50f1100 927
b07c50cc
VK
928 void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
929 void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
930#ifdef UNICODE
931#define StrStrip StrStripW
932#else
933#define StrStrip StrStripA
934#endif
935
ada97259 936 const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
cafe592d 937 const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
938#ifdef UNICODE
939#define ExtractWord ExtractWordW
940#else
941#define ExtractWord ExtractWordA
942#endif
943
3583a400
VK
944 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
945 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
946#ifdef UNICODE
947#define NumChars NumCharsW
948#else
949#define NumChars NumCharsA
950#endif
ada97259 951
35f836fe
VK
952 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
953 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
954#ifdef UNICODE
955#define RemoveTrailingCRLF RemoveTrailingCRLFW
956#else
957#define RemoveTrailingCRLF RemoveTrailingCRLFA
958#endif
959
cafe592d
VK
960 BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *pszStr, const char *pszExpr, BOOL bMatchCase);
961 BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *pszStr, const WCHAR *pszExpr, BOOL bMatchCase);
962#ifdef UNICODE
963#define RegexpMatch RegexpMatchW
964#else
965#define RegexpMatch RegexpMatchA
966#endif
967
a73a4403 968 const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize);
ada97259 969 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
240816b0 970 bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
ada97259
VK
971 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
972
f7794b68
VK
973#ifdef __cplusplus
974 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
975#endif
fa2b47a7 976 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
7618e362 977 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1a5e0c22 978 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
c3cfc039 979 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
967893bb 980 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 981#ifdef UNICODE
967893bb 982 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
983#else
984#define LoadFileA LoadFile
985#endif
986
967893bb 987 UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, UINT32 dwSize, UINT32 dwCRC);
a3c76aba 988 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 989 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
a3c76aba 990 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 991 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
3aa0061c
VK
992 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
993 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
967893bb 994 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 995
4e2debcc
VK
996 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
997 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
998
6b1282e7
VK
999 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
1000
967893bb
VK
1001 UINT32 LIBNETXMS_EXPORTABLE IcmpPing(UINT32 dwAddr, int iNumRetries, UINT32 dwTimeout,
1002 UINT32 *pdwRTT, UINT32 dwPacketSize);
4826c8a5 1003
3aa0061c 1004 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 1005
4560d8bd 1006 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
aa13cfd9 1007 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
dda7c270 1008 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 1009
289d126b
VK
1010 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1011 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1012 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
1013
1014 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1015 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1016 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
1017
1018#ifdef UNICODE
1019#define ExtractNamedOptionValue ExtractNamedOptionValueW
1020#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1021#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1022#else
1023#define ExtractNamedOptionValue ExtractNamedOptionValueA
1024#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1025#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1026#endif
ae9cddaf 1027
bc858635
VK
1028#ifdef __cplusplus
1029 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
1030#else
1031 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
1032#endif
1033
aa13cfd9 1034#ifdef _WIN32
967893bb 1035 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
ccc34207 1036 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
11606542 1037 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 1038#endif
696fc54f
VK
1039
1040#if !(HAVE_DAEMON)
b1a57444 1041 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 1042#endif
4332e9d8 1043
967893bb 1044 UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1045
4332e9d8 1046#ifndef _WIN32
664c7c49 1047 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
967893bb 1048 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
4332e9d8
VK
1049 int cchWideChar, char *pByteStr, int cchByteChar,
1050 char *pDefaultChar, BOOL *pbUsedDefChar);
967893bb 1051 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
4332e9d8
VK
1052 int cchByteChar, WCHAR *pWideCharStr,
1053 int cchWideChar);
04dbaf62 1054
6b93c954 1055#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
8b86c5dc 1056 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1057#endif
1058#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
8b86c5dc 1059 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1060#endif
1061#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
35f836fe 1062 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
4332e9d8 1063#endif
8b86c5dc 1064
53c96e2d
VK
1065 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1066 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
39945910
VK
1067 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1068 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1069
6e17f325
VK
1070 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1071 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1072 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1073 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1074 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1075 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1076
25575d38
VK
1077 int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1078 int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
7ff399a5 1079 int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
25575d38
VK
1080 int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1081 int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
7ff399a5 1082 int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1083
3e39fcde 1084#endif /* _WIN32 */
8b86c5dc 1085
3aa0061c 1086 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
c16afb3d 1087 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
3aa0061c
VK
1088 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
1089 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
8b86c5dc 1090
3d1058ed
VK
1091#ifdef _WITH_ENCRYPTION
1092 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
1093#endif
1094
8b86c5dc 1095#ifdef UNICODE_UCS4
53c96e2d
VK
1096 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1097 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1098 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
d9b66a56
VK
1099 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1100 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1101#endif
8b86c5dc 1102
842c22f4
AK
1103#if !defined(_WIN32) && !HAVE_WSTAT
1104 int wstat(const WCHAR *_path, struct stat *_sbuf);
1105#endif
1106
ef856d02 1107#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1108
f3387429
VK
1109#if !HAVE_WPOPEN
1110 FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
1111#endif
8b86c5dc
VK
1112#if !HAVE_WFOPEN
1113 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
1114#endif
1115#if !HAVE_WOPEN
1116 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1117#endif
f3387429
VK
1118#if !HAVE_WCHMOD
1119 int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
1120#endif
b07c50cc
VK
1121#if !HAVE_WCHDIR
1122 int wchdir(const WCHAR *_path);
1123#endif
1124#if !HAVE_WMKDIR
1125 int wmkdir(const WCHAR *_path, int mode);
1126#endif
1127#if !HAVE_WRMDIR
1128 int wrmdir(const WCHAR *_path);
1129#endif
ccc34207
VK
1130#if !HAVE_WRENAME
1131 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
1132#endif
1133#if !HAVE_WUNLINK
1134 int wunlink(const WCHAR *_path);
1135#endif
b07c50cc
VK
1136#if !HAVE_WREMOVE
1137 int wremove(const WCHAR *_path);
1138#endif
1139#if !HAVE_WSYSTEM
1140 int wsystem(const WCHAR *_cmd);
1141#endif
dda7c270
VK
1142#if !HAVE_WMKSTEMP
1143 int wmkstemp(const WCHAR *_template);
1144#endif
b07c50cc
VK
1145#if !HAVE_WACCESS
1146 int waccess(const WCHAR *_path, int mode);
1147#endif
273da39a
VK
1148#if !HAVE_WGETENV
1149 WCHAR *wgetenv(const WCHAR *_string);
1150#endif
f3387429
VK
1151#if !HAVE_WCTIME
1152 WCHAR *wctime(const time_t *timep);
1153#endif
0186b5da
VK
1154#if !HAVE_PUTWS
1155 int putws(const WCHAR *s);
1156#endif
d49d7500
VK
1157#if !HAVE_WCSERROR && HAVE_STRERROR
1158 WCHAR *wcserror(int errnum);
1159#endif
1160#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06
VK
1161#if HAVE_POSIX_STRERROR_R
1162 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1163#else
d49d7500
VK
1164 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1165#endif
b0823d06 1166#endif
e05f8aaf 1167
ef856d02 1168#endif /* UNICODE && !_WIN32*/
e05f8aaf 1169
8b86c5dc
VK
1170#if !HAVE_STRTOLL
1171 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
1172#endif
1173#if !HAVE_STRTOULL
967893bb 1174 UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1175#endif
e05f8aaf
VK
1176
1177#if !HAVE_WCSTOLL
1178 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
1179#endif
1180#if !HAVE_WCSTOULL
967893bb 1181 UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1182#endif
b50f1100 1183
4d978b4a
VK
1184#if !HAVE_WCSCASECMP && !defined(_WIN32)
1185 int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
1186#endif
1187
f4d2c9ba
VK
1188#if !HAVE_WCSNCASECMP && !defined(_WIN32)
1189 int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
1190#endif
1191
3d1cb7d6 1192#ifdef _WIN32
5ea2db30
VK
1193#ifdef UNICODE
1194 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1195 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1196 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1197
1198#define _topendir wopendir
1199#define _treaddir wreaddir
1200#define _tclosedir wclosedir
1201#else
1202#define _topendir opendir
1203#define _treaddir readdir
1204#define _tclosedir closedir
1205#endif
1206
3d1cb7d6 1207 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 1208 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6 1209 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1210
76b1d3d8
VK
1211#else /* not _WIN32 */
1212
1213 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1214 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1215 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1216
37d62364 1217#endif
3d1cb7d6 1218
7f9e0c00
VK
1219#if defined(_WIN32) || !(HAVE_SCANDIR)
1220 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1221 int (*select)(const struct dirent *),
1222 int (*compar)(const struct dirent **, const struct dirent **));
1223 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
1224#endif
1225
3bf0df9f
VK
1226TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
1227
55b33d4c 1228#ifdef UNDER_CE
3bf0df9f
VK
1229int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1230int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1231int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
55b33d4c
VK
1232#endif
1233
967893bb 1234BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1235 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1236void LIBNETXMS_EXPORTABLE nxlog_close(void);
1237void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1238BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1239BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1240
1241typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1242void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1243
1244void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
6e383343
VK
1245void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
1246#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1247 __attribute__ ((format(printf, 1, 2)))
1248#endif
1249;
9f2902ac 1250
37a7a862
VK
1251#ifdef _WIN32
1252int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1253int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1254#ifdef UNICODE
1255#define _tmkstemp wmkstemp
1256#else
1257#define _tmkstemp mkstemp
1258#endif
1259#endif
1260
9b13553b
VK
1261#ifndef _WIN32
1262int strcat_s(char *dst, size_t dstSize, const char *src);
1263int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1264#endif
1265
b50f1100 1266#ifdef __cplusplus
e9246d26 1267}
b50f1100 1268#endif
e9246d26 1269
a7ff20a5
VK
1270
1271//
1272// C++ only finctions
1273//
1274
1275#ifdef __cplusplus
1276
240816b0
VK
1277TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1278String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1279const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1280int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1281UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1282bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1283
1284#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1285void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1286#endif
1287
1288void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
967893bb
VK
1289 void (* sendTrap1)(UINT32, const TCHAR *, const char *, va_list),
1290 void (* sendTrap2)(UINT32, const TCHAR *, int, TCHAR **),
9fa031cd 1291 bool (* sendFile)(void *, UINT32, const TCHAR *, long, long),
4e46505f 1292 bool (* pushData)(const TCHAR *, const TCHAR *, UINT32));
a7ff20a5
VK
1293
1294#endif
1295
e9246d26 1296#endif /* _nms_util_h_ */