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