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