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