Merge branch 'master' of 10.3.0.4:public/netxms
[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
VK
404
405public:
406 Array(int initial = 0, int grow = 16, bool owner = false);
407 virtual ~Array();
408
409 int add(void *object);
410 void *get(int index) { return ((index >= 0) && (index < m_size)) ? m_data[index] : NULL; }
74d4ba34 411 int indexOf(void *object);
47a89886 412 void set(int index, void *object);
24dc5346 413 void replace(int index, void *object);
16d6f798 414 void remove(int index) { internalRemove(index, true); }
74d4ba34 415 void remove(void *object) { internalRemove(indexOf(object), true); }
16d6f798 416 void unlink(int index) { internalRemove(index, false); }
74d4ba34 417 void unlink(void *object) { internalRemove(indexOf(object), false); }
0d75ea88
VK
418 void clear();
419
420 int size() { return m_size; }
421
422 void setOwner(bool owner) { m_objectOwner = owner; }
423 bool isOwner() { return m_objectOwner; }
424};
425
3fe40613
VK
426/**
427 * Template class for dynamic array which holds objects
428 */
0d75ea88
VK
429template <class T> class ObjectArray : public Array
430{
cfa7c21f
VK
431private:
432 static void destructor(void *object) { delete (T*)object; }
0d75ea88
VK
433
434public:
cfa7c21f 435 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
0d75ea88
VK
436 virtual ~ObjectArray() { }
437
47a89886 438 int add(T *object) { return Array::add((void *)object); }
0d75ea88 439 T *get(int index) { return (T*)Array::get(index); }
74d4ba34 440 int indexOf(T *object) { return Array::indexOf((void *)object); }
47a89886
VK
441 void set(int index, T *object) { Array::set(index, (void *)object); }
442 void replace(int index, T *object) { Array::replace(index, (void *)object); }
74d4ba34
VK
443 void remove(int index) { Array::remove(index); }
444 void remove(T *object) { Array::remove((void *)object); }
445 void unlink(int index) { Array::unlink(index); }
446 void unlink(T *object) { Array::unlink((void *)object); }
0d75ea88
VK
447};
448
25fe6265 449/**
e8e76b4f
VK
450 * Template class for dynamic array which holds scalar values
451 */
452template <class T> class IntegerArray : public Array
453{
454private:
455 static void destructor(void *object) { }
456
457public:
458 IntegerArray(int initial = 0, int grow = 16) : Array(initial, grow, false) { m_objectDestructor = destructor; }
459 virtual ~IntegerArray() { }
460
461 int add(T value) { return Array::add(CAST_TO_POINTER(value, void *)); }
462 T get(int index) { return CAST_FROM_POINTER(Array::get(index), T); }
463 void set(int index, T value) { Array::set(index, CAST_TO_POINTER(value, void *)); }
464 void replace(int index, T value) { Array::replace(index, CAST_TO_POINTER(value, void *)); }
465};
466
467/**
25fe6265
VK
468 * Auxilliary class to hold dynamically allocated array of structures
469 */
cc8ce218
VK
470template <class T> class StructArray
471{
472private:
0105c251
VK
473 int m_size;
474 int m_allocated;
475 int m_grow;
cc8ce218
VK
476 T *m_data;
477
478public:
0105c251 479 StructArray(T *data, int size) { m_data = data; m_size = size; }
cc8ce218
VK
480 ~StructArray() { safe_free(m_data); }
481
0105c251
VK
482 int size() { return m_size; }
483
cc8ce218 484 T *get(int index) { return ((index >= 0) && (index < m_count)) ? &m_data[index] : NULL; }
0105c251 485 void replace(int index, T *value) { if ((index >= 0) && (index < m_count)) memcpy(&m_data[index], value, sizeof(T)); }
cc8ce218
VK
486};
487
25fe6265
VK
488/**
489 * Auxilliary class for objects which counts references and
490 * destroys itself wheren reference count falls to 0
491 */
7f632dfe
VK
492class LIBNETXMS_EXPORTABLE RefCountObject
493{
494private:
7785322f 495 VolatileCounter m_refCount;
7f632dfe
VK
496
497public:
7785322f
VK
498 RefCountObject()
499 {
500 m_refCount = 1;
501 }
b549a0f8 502 virtual ~RefCountObject();
7785322f
VK
503
504 void incRefCount()
505 {
506 InterlockedIncrement(&m_refCount);
507 }
508
509 void decRefCount()
510 {
511 if (InterlockedDecrement(&m_refCount) == 0)
512 delete this;
513 }
7f632dfe
VK
514};
515
967893bb
VK
516class CSCPMessage;
517
518/**
519 * Table column definition
520 */
521class LIBNETXMS_EXPORTABLE TableColumnDefinition
522{
523private:
524 TCHAR *m_name;
525 TCHAR *m_displayName;
526 INT32 m_dataType;
527 bool m_instanceColumn;
528
529public:
530 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
531 TableColumnDefinition(CSCPMessage *msg, UINT32 baseId);
532 TableColumnDefinition(TableColumnDefinition *src);
533 ~TableColumnDefinition();
534
535 void fillMessage(CSCPMessage *msg, UINT32 baseId);
536
537 const TCHAR *getName() { return m_name; }
538 const TCHAR *getDisplayName() { return m_displayName; }
539 INT32 getDataType() { return m_dataType; }
540 bool isInstanceColumn() { return m_instanceColumn; }
541
542 void setDataType(INT32 type) { m_dataType = type; }
543 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 544 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
545};
546
547/**
548 * Class for table data storage
549 */
a0ddfb29 550class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
551{
552private:
553 int m_nNumRows;
554 int m_nNumCols;
555 TCHAR **m_ppData;
556 ObjectArray<TableColumnDefinition> *m_columns;
557 TCHAR *m_title;
558 int m_source;
559
560 void createFromMessage(CSCPMessage *msg);
561 void destroy();
562
563public:
564 Table();
a0ddfb29 565 Table(Table *src);
967893bb 566 Table(CSCPMessage *msg);
b549a0f8 567 virtual ~Table();
967893bb
VK
568
569 int fillMessage(CSCPMessage &msg, int offset, int rowLimit);
570 void updateFromMessage(CSCPMessage *msg);
571
a0ddfb29
VK
572 void addAll(Table *src);
573 void copyRow(Table *src, int row);
967893bb
VK
574
575 int getNumRows() { return m_nNumRows; }
576 int getNumColumns() { return m_nNumCols; }
577 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
578 int getSource() { return m_source; }
579
580 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
581 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
582 int getColumnIndex(const TCHAR *name);
583 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
584
585 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = (title != NULL) ? _tcsdup(title) : NULL; }
586 void setSource(int source) { m_source = source; }
8dba1eac 587 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
967893bb
VK
588 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
589 int addRow();
590
25076d53
VK
591 void deleteRow(int row);
592 void deleteColumn(int col);
593
967893bb
VK
594 void setAt(int nRow, int nCol, INT32 nData);
595 void setAt(int nRow, int nCol, UINT32 dwData);
596 void setAt(int nRow, int nCol, double dData);
597 void setAt(int nRow, int nCol, INT64 nData);
598 void setAt(int nRow, int nCol, UINT64 qwData);
599 void setAt(int nRow, int nCol, const TCHAR *pszData);
600 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
601
602 void set(int nCol, INT32 nData) { setAt(m_nNumRows - 1, nCol, nData); }
603 void set(int nCol, UINT32 dwData) { setAt(m_nNumRows - 1, nCol, dwData); }
604 void set(int nCol, double dData) { setAt(m_nNumRows - 1, nCol, dData); }
605 void set(int nCol, INT64 nData) { setAt(m_nNumRows - 1, nCol, nData); }
606 void set(int nCol, UINT64 qwData) { setAt(m_nNumRows - 1, nCol, qwData); }
607 void set(int nCol, const TCHAR *pszData) { setAt(m_nNumRows - 1, nCol, pszData); }
608 void setPreallocated(int nCol, TCHAR *pszData) { setPreallocatedAt(m_nNumRows - 1, nCol, pszData); }
609
610 const TCHAR *getAsString(int nRow, int nCol);
611 INT32 getAsInt(int nRow, int nCol);
612 UINT32 getAsUInt(int nRow, int nCol);
613 INT64 getAsInt64(int nRow, int nCol);
614 UINT64 getAsUInt64(int nRow, int nCol);
615 double getAsDouble(int nRow, int nCol);
22aaa779
VK
616
617 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 618 int findRowByInstance(const TCHAR *instance);
967893bb
VK
619};
620
25fe6265
VK
621/**
622 * Network connection
623 */
58ee2f43
VK
624class LIBNETXMS_EXPORTABLE SocketConnection
625{
626protected:
627 SOCKET m_socket;
628 char m_data[4096];
629 int m_dataPos;
630
631public:
632 SocketConnection();
633 virtual ~SocketConnection();
634
967893bb
VK
635 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
636 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
637 void disconnect();
638
967893bb
VK
639 bool canRead(UINT32 timeout);
640 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43
VK
641 bool waitForText(const char *text, int timeout);
642
643 int write(const char *pBuff, int nSize);
1671f85d
VK
644 bool writeLine(const char *line);
645
967893bb 646 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
647};
648
030a0779
VK
649/**
650 * Telnet connection - handles all telnet negotiation
651 */
def5b792
AK
652class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
653{
030a0779 654protected:
967893bb
VK
655 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
656 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 657
def5b792 658public:
967893bb 659 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 660
967893bb
VK
661 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
662 bool connect(UINT32 ip, WORD port, UINT32 timeout);
663 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
664 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
665};
666
b160350d
VK
667#endif /* __cplusplus */
668
030a0779
VK
669/**
670 * Configuration item template for configuration loader
671 */
4826c8a5
VK
672typedef struct
673{
018fda4d 674 TCHAR szToken[64];
4826c8a5
VK
675 BYTE iType;
676 BYTE cSeparator; // Separator character for lists
677 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
967893bb
VK
678 UINT32 dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
679 UINT32 dwBufferPos; // Should be set to 0
4826c8a5
VK
680 void *pBuffer;
681} NX_CFG_TEMPLATE;
dcb44c9a 682
030a0779
VK
683/**
684 * Code translation structure
685 */
bc858635
VK
686typedef struct __CODE_TO_TEXT
687{
688 int code;
689 const TCHAR *text;
690} CODE_TO_TEXT;
691
692
693//
3046f9a4
VK
694// getopt() prototype if needed
695//
696
df9812a9 697#if USE_BUNDLED_GETOPT
99e50bb7 698#include <netxms_getopt.h>
3046f9a4
VK
699#endif
700
701
702//
f2fdf1b5
VK
703// Win32 API functions missing under WinCE
704//
705
c32b805a 706#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
707
708inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
709{
710 SYSTEMTIME sysTime;
711
712 GetSystemTime(&sysTime);
713 SystemTimeToFileTime(&sysTime, pFt);
714}
715
716#endif // UNDER_CE
717
718
719//
3d1cb7d6
VK
720// Structures for opendir() / readdir() / closedir()
721//
722
723#ifdef _WIN32
724
7f9e0c00 725typedef struct dirent
3d1cb7d6
VK
726{
727 long d_ino; /* inode number (not used by MS-DOS) */
728 int d_namlen; /* Name length */
729 char d_name[257]; /* file name */
730} _DIRECT;
731
732typedef struct _dir_struc
733{
734 char *start; /* Starting position */
735 char *curr; /* Current position */
736 long size; /* Size of string table */
737 long nfiles; /* number if filenames in table */
7f9e0c00 738 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
739} DIR;
740
5ea2db30
VK
741#ifdef UNICODE
742
743typedef struct dirent_w
744{
745 long d_ino; /* inode number (not used by MS-DOS) */
746 int d_namlen; /* Name length */
747 WCHAR d_name[257]; /* file name */
748} _DIRECTW;
749
750typedef struct _dir_struc_w
751{
752 WCHAR *start; /* Starting position */
753 WCHAR *curr; /* Current position */
754 long size; /* Size of string table */
755 long nfiles; /* number if filenames in table */
756 struct dirent_w dirstr; /* Directory structure to return */
757} DIRW;
758
759#define _TDIR DIRW
760#define _TDIRECT _DIRECTW
761#define _tdirent dirent_w
762
763#else
764
765#define _TDIR DIR
766#define _TDIRECT _DIRECT
767#define _tdirent dirent
768
37d62364
AK
769#endif
770
76b1d3d8
VK
771#else /* not _WIN32 */
772
773typedef struct dirent_w
774{
775 long d_ino; /* inode number */
776 WCHAR d_name[257]; /* file name */
777} _DIRECTW;
778
779typedef struct _dir_struc_w
780{
781 DIR *dir; /* Original non-unicode structure */
782 struct dirent_w dirstr; /* Directory structure to return */
783} DIRW;
784
3d1cb7d6
VK
785#endif /* _WIN32 */
786
787
788//
e9246d26
VK
789// Functions
790//
791
babb8280 792#if WORDS_BIGENDIAN
43d92b4c
VK
793#define htonq(x) (x)
794#define ntohq(x) (x)
9f20696e
VK
795#define htond(x) (x)
796#define ntohd(x) (x)
babb8280
VK
797#define SwapWideString(x)
798#else
ca6ad6bb
VK
799#ifdef HAVE_HTONLL
800#define htonq(x) htonll(x)
801#else
babb8280 802#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
803#endif
804#ifdef HAVE_NTOHLL
805#define ntohq(x) ntohll(x)
806#else
babb8280 807#define ntohq(x) __bswap_64(x)
ca6ad6bb 808#endif
babb8280
VK
809#define htond(x) __bswap_double(x)
810#define ntohd(x) __bswap_double(x)
811#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 812#endif
15929b31 813
55b33d4c
VK
814#ifdef UNDER_CE
815#define close(x) CloseHandle((HANDLE)(x))
816#endif
817
b50f1100 818#ifdef __cplusplus
4299401e 819 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
c81979bc 820 {
912b994d
VK
821#if defined(_WIN32) && (_MSC_VER >= 1400)
822 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
823#else
c81979bc
VK
824 _tcsncpy(pszDest, pszSrc, nLen - 1);
825 pszDest[nLen - 1] = 0;
912b994d 826#endif
c81979bc
VK
827 return pszDest;
828 }
f3387429
VK
829
830#ifdef UNICODE
831 inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
832 {
833#if defined(_WIN32) && (_MSC_VER >= 1400)
6646dca4 834 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429
VK
835#else
836 strncpy(pszDest, pszSrc, nLen - 1);
837 pszDest[nLen - 1] = 0;
c81979bc 838#endif
f3387429
VK
839 return pszDest;
840 }
c81979bc 841#else
f3387429 842#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
843#endif
844
967893bb 845int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
7b8b337e 846int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int, MUTEX);
d78be64d 847int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff, size_t nSize, int nFlags, UINT32 dwTimeout);
b0608204 848#endif /* __cplusplus */
c81979bc
VK
849
850#ifdef __cplusplus
e9246d26
VK
851extern "C"
852{
b50f1100 853#endif
c5b523e2 854#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
967893bb 855 UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
c5b523e2 856#endif
f52be742 857#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
967893bb 858 UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
64584976 859#endif
9f20696e 860 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
273da39a 861 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 862
076a55c4 863#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc
VK
864#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
865 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
866#endif
867 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 868#endif
ab588c84 869
b8a8d064
VK
870 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
871 int (*compare)(const void *, const void *, void *));
872
670c7f3d 873 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
d35ed0aa 874
967893bb
VK
875 UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
876 UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
877#ifdef UNICODE
878#define FileSize FileSizeW
879#else
880#define FileSize FileSizeA
881#endif
670c7f3d 882
967893bb
VK
883 int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
884 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 885#ifdef UNICODE
967893bb 886 char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
887#else
888#define IpToStrA IpToStr
e0f99bf0 889#endif
36e44abe 890 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
60557d06
VK
891 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
892
967893bb 893 UINT32 LIBNETXMS_EXPORTABLE ResolveHostName(const TCHAR *pszName);
08b214c6 894#ifdef UNICODE
967893bb 895 UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *pszName);
08b214c6
VK
896#else
897#define ResolveHostNameA ResolveHostName
898#endif
a1dc75b0 899
967893bb
VK
900 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, UINT32 dwSize);
901 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, UINT32 dwSize);
31521248 902
967893bb
VK
903 WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *pData, UINT32 dwSize, WCHAR *pStr);
904 char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *pData, UINT32 dwSize, char *pStr);
d7970ee7
VK
905#ifdef UNICODE
906#define BinToStr BinToStrW
907#else
908#define BinToStr BinToStrA
909#endif
910
52db3ed1
VK
911 UINT32 LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *pData, UINT32 dwSize);
912 UINT32 LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *pData, UINT32 dwSize);
913#ifdef UNICODE
914#define StrToBin StrToBinW
915#else
916#define StrToBin StrToBinA
917#endif
918
f42b8099 919 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *pData, TCHAR *pStr);
b50f1100 920
b07c50cc
VK
921 void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
922 void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
923#ifdef UNICODE
924#define StrStrip StrStripW
925#else
926#define StrStrip StrStripA
927#endif
928
ada97259 929 const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
cafe592d 930 const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
931#ifdef UNICODE
932#define ExtractWord ExtractWordW
933#else
934#define ExtractWord ExtractWordA
935#endif
936
3583a400
VK
937 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
938 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
939#ifdef UNICODE
940#define NumChars NumCharsW
941#else
942#define NumChars NumCharsA
943#endif
ada97259 944
35f836fe
VK
945 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
946 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
947#ifdef UNICODE
948#define RemoveTrailingCRLF RemoveTrailingCRLFW
949#else
950#define RemoveTrailingCRLF RemoveTrailingCRLFA
951#endif
952
cafe592d
VK
953 BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *pszStr, const char *pszExpr, BOOL bMatchCase);
954 BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *pszStr, const WCHAR *pszExpr, BOOL bMatchCase);
955#ifdef UNICODE
956#define RegexpMatch RegexpMatchW
957#else
958#define RegexpMatch RegexpMatchA
959#endif
960
a73a4403 961 const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize);
ada97259 962 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
240816b0 963 bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
ada97259
VK
964 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
965
f7794b68
VK
966#ifdef __cplusplus
967 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
968#endif
fa2b47a7 969 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
7618e362 970 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1a5e0c22 971 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
c3cfc039 972 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
967893bb 973 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 974#ifdef UNICODE
967893bb 975 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
976#else
977#define LoadFileA LoadFile
978#endif
979
967893bb 980 UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, UINT32 dwSize, UINT32 dwCRC);
a3c76aba 981 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 982 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
a3c76aba 983 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 984 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
3aa0061c
VK
985 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
986 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
967893bb 987 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 988
4e2debcc
VK
989 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
990 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
991
6b1282e7
VK
992 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
993
967893bb
VK
994 UINT32 LIBNETXMS_EXPORTABLE IcmpPing(UINT32 dwAddr, int iNumRetries, UINT32 dwTimeout,
995 UINT32 *pdwRTT, UINT32 dwPacketSize);
4826c8a5 996
3aa0061c 997 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 998
4560d8bd 999 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
aa13cfd9 1000 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
dda7c270 1001 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 1002
289d126b
VK
1003 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1004 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1005 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
1006
1007 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1008 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1009 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
1010
1011#ifdef UNICODE
1012#define ExtractNamedOptionValue ExtractNamedOptionValueW
1013#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1014#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1015#else
1016#define ExtractNamedOptionValue ExtractNamedOptionValueA
1017#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1018#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1019#endif
ae9cddaf 1020
bc858635
VK
1021#ifdef __cplusplus
1022 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
1023#else
1024 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
1025#endif
1026
aa13cfd9 1027#ifdef _WIN32
967893bb 1028 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
ccc34207 1029 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
11606542 1030 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 1031#endif
696fc54f
VK
1032
1033#if !(HAVE_DAEMON)
b1a57444 1034 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 1035#endif
4332e9d8 1036
967893bb 1037 UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1038
4332e9d8 1039#ifndef _WIN32
664c7c49 1040 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
967893bb 1041 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
4332e9d8
VK
1042 int cchWideChar, char *pByteStr, int cchByteChar,
1043 char *pDefaultChar, BOOL *pbUsedDefChar);
967893bb 1044 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
4332e9d8
VK
1045 int cchByteChar, WCHAR *pWideCharStr,
1046 int cchWideChar);
04dbaf62 1047
6b93c954 1048#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
8b86c5dc 1049 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1050#endif
1051#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
8b86c5dc 1052 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1053#endif
1054#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
35f836fe 1055 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
4332e9d8 1056#endif
8b86c5dc 1057
53c96e2d
VK
1058 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1059 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
39945910
VK
1060 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1061 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1062
6e17f325
VK
1063 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1064 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1065 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1066 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1067 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1068 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1069
25575d38
VK
1070 int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1071 int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
7ff399a5 1072 int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
25575d38
VK
1073 int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1074 int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
7ff399a5 1075 int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1076
3e39fcde 1077#endif /* _WIN32 */
8b86c5dc 1078
3aa0061c 1079 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
c16afb3d 1080 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
3aa0061c
VK
1081 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
1082 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
8b86c5dc 1083
3d1058ed
VK
1084#ifdef _WITH_ENCRYPTION
1085 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
1086#endif
1087
8b86c5dc 1088#ifdef UNICODE_UCS4
53c96e2d
VK
1089 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1090 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1091 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
d9b66a56
VK
1092 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1093 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1094#endif
8b86c5dc 1095
842c22f4
AK
1096#if !defined(_WIN32) && !HAVE_WSTAT
1097 int wstat(const WCHAR *_path, struct stat *_sbuf);
1098#endif
1099
ef856d02 1100#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1101
f3387429
VK
1102#if !HAVE_WPOPEN
1103 FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
1104#endif
8b86c5dc
VK
1105#if !HAVE_WFOPEN
1106 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
1107#endif
1108#if !HAVE_WOPEN
1109 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1110#endif
f3387429
VK
1111#if !HAVE_WCHMOD
1112 int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
1113#endif
b07c50cc
VK
1114#if !HAVE_WCHDIR
1115 int wchdir(const WCHAR *_path);
1116#endif
1117#if !HAVE_WMKDIR
1118 int wmkdir(const WCHAR *_path, int mode);
1119#endif
1120#if !HAVE_WRMDIR
1121 int wrmdir(const WCHAR *_path);
1122#endif
ccc34207
VK
1123#if !HAVE_WRENAME
1124 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
1125#endif
1126#if !HAVE_WUNLINK
1127 int wunlink(const WCHAR *_path);
1128#endif
b07c50cc
VK
1129#if !HAVE_WREMOVE
1130 int wremove(const WCHAR *_path);
1131#endif
1132#if !HAVE_WSYSTEM
1133 int wsystem(const WCHAR *_cmd);
1134#endif
dda7c270
VK
1135#if !HAVE_WMKSTEMP
1136 int wmkstemp(const WCHAR *_template);
1137#endif
b07c50cc
VK
1138#if !HAVE_WACCESS
1139 int waccess(const WCHAR *_path, int mode);
1140#endif
273da39a
VK
1141#if !HAVE_WGETENV
1142 WCHAR *wgetenv(const WCHAR *_string);
1143#endif
f3387429
VK
1144#if !HAVE_WCTIME
1145 WCHAR *wctime(const time_t *timep);
1146#endif
0186b5da
VK
1147#if !HAVE_PUTWS
1148 int putws(const WCHAR *s);
1149#endif
d49d7500
VK
1150#if !HAVE_WCSERROR && HAVE_STRERROR
1151 WCHAR *wcserror(int errnum);
1152#endif
1153#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06
VK
1154#if HAVE_POSIX_STRERROR_R
1155 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1156#else
d49d7500
VK
1157 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1158#endif
b0823d06 1159#endif
e05f8aaf 1160
ef856d02 1161#endif /* UNICODE && !_WIN32*/
e05f8aaf 1162
8b86c5dc
VK
1163#if !HAVE_STRTOLL
1164 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
1165#endif
1166#if !HAVE_STRTOULL
967893bb 1167 UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1168#endif
e05f8aaf
VK
1169
1170#if !HAVE_WCSTOLL
1171 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
1172#endif
1173#if !HAVE_WCSTOULL
967893bb 1174 UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1175#endif
b50f1100 1176
4d978b4a
VK
1177#if !HAVE_WCSCASECMP && !defined(_WIN32)
1178 int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
1179#endif
1180
f4d2c9ba
VK
1181#if !HAVE_WCSNCASECMP && !defined(_WIN32)
1182 int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
1183#endif
1184
3d1cb7d6 1185#ifdef _WIN32
5ea2db30
VK
1186#ifdef UNICODE
1187 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1188 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1189 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1190
1191#define _topendir wopendir
1192#define _treaddir wreaddir
1193#define _tclosedir wclosedir
1194#else
1195#define _topendir opendir
1196#define _treaddir readdir
1197#define _tclosedir closedir
1198#endif
1199
3d1cb7d6 1200 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 1201 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6 1202 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1203
76b1d3d8
VK
1204#else /* not _WIN32 */
1205
1206 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1207 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1208 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1209
37d62364 1210#endif
3d1cb7d6 1211
7f9e0c00
VK
1212#if defined(_WIN32) || !(HAVE_SCANDIR)
1213 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1214 int (*select)(const struct dirent *),
1215 int (*compar)(const struct dirent **, const struct dirent **));
1216 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
1217#endif
1218
55b33d4c
VK
1219#ifdef UNDER_CE
1220 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1221 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1222 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
1223#endif
1224
967893bb 1225BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1226 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1227void LIBNETXMS_EXPORTABLE nxlog_close(void);
1228void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1229BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1230BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1231
1232typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1233void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1234
1235void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
1236void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...);
9f2902ac 1237
37a7a862
VK
1238#ifdef _WIN32
1239int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1240int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1241#ifdef UNICODE
1242#define _tmkstemp wmkstemp
1243#else
1244#define _tmkstemp mkstemp
1245#endif
1246#endif
1247
9b13553b
VK
1248#ifndef _WIN32
1249int strcat_s(char *dst, size_t dstSize, const char *src);
1250int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1251#endif
1252
b50f1100 1253#ifdef __cplusplus
e9246d26 1254}
b50f1100 1255#endif
e9246d26 1256
a7ff20a5
VK
1257
1258//
1259// C++ only finctions
1260//
1261
1262#ifdef __cplusplus
1263
240816b0
VK
1264TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1265String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1266const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1267int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1268UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1269bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1270
1271#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1272void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1273#endif
1274
1275void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
967893bb
VK
1276 void (* sendTrap1)(UINT32, const TCHAR *, const char *, va_list),
1277 void (* sendTrap2)(UINT32, const TCHAR *, int, TCHAR **),
0137b9f7
VK
1278 bool (* sendFile)(void *, UINT32, const TCHAR *, long),
1279 bool (* pushData)(const TCHAR *, const TCHAR *));
a7ff20a5
VK
1280
1281#endif
1282
e9246d26 1283#endif /* _nms_util_h_ */