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