fixed agent build error; special configuration search path for ZeroShell
[public/netxms.git] / include / nms_util.h
CommitLineData
e9246d26 1/*
7ac71e60 2** NetXMS - Network Management System
36e44abe 3** Copyright (C) 2003-2011 Victor Kirhenshtein
e9246d26
VK
4**
5** This program is free software; you can redistribute it and/or modify
68f384ea
VK
6** it under the terms of the GNU Lesser General Public License as published
7** by the Free Software Foundation; either version 3 of the License, or
e9246d26
VK
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
68f384ea 15** You should have received a copy of the GNU Lesser General Public License
e9246d26
VK
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
fb7ce4b9 19** File: nms_util.h
e9246d26
VK
20**
21**/
22
23#ifndef _nms_util_h_
24#define _nms_util_h_
25
64584976
VK
26#ifdef _WIN32
27#ifdef LIBNETXMS_EXPORTS
28#define LIBNETXMS_EXPORTABLE __declspec(dllexport)
29#else
30#define LIBNETXMS_EXPORTABLE __declspec(dllimport)
31#endif
32#else /* _WIN32 */
33#define LIBNETXMS_EXPORTABLE
34#endif
35
36
e9246d26
VK
37#include <nms_common.h>
38#include <nms_cscp.h>
953dfe76 39#include <nms_threads.h>
64584976 40#include <time.h>
e9246d26 41
6d8aee95
VK
42#if HAVE_BYTESWAP_H
43#include <byteswap.h>
44#endif
45
92502d5f
VK
46#include <base64.h>
47
6d8aee95
VK
48
49//
92502d5f 50// Serial communications
6d8aee95
VK
51//
52
53#ifdef _WIN32
54
55#define FLOW_NONE 0
56#define FLOW_SOFTWARE 1
57#define FLOW_HARDWARE 2
58
59#else /* _WIN32 */
60
70fed300
AK
61#ifdef HAVE_TERMIOS_H
62# include <termios.h>
63#else
64# error termios.h not found
65#endif
66
6d8aee95
VK
67#endif /* _WIN32 */
68
69
70//
66c04cc7
VK
71// Return codes for IcmpPing()
72//
73
74#define ICMP_SUCCESS 0
75#define ICMP_UNREACHEABLE 1
76#define ICMP_TIMEOUT 2
77#define ICMP_RAW_SOCK_FAILED 3
416816ed 78#define ICMP_API_ERROR 3
66c04cc7
VK
79
80
81//
4826c8a5 82// Token types for configuration loader
dcb44c9a
VK
83//
84
4826c8a5
VK
85#define CT_LONG 0
86#define CT_STRING 1
87#define CT_STRING_LIST 2
88#define CT_END_OF_LIST 3
89#define CT_BOOLEAN 4
90#define CT_WORD 5
fc935a60 91#define CT_IGNORE 6
dda7c270 92#define CT_MB_STRING 7
4826c8a5 93
605fc935
VK
94/**
95 * Return codes for NxLoadConfig()
96 */
4826c8a5
VK
97#define NXCFG_ERR_OK 0
98#define NXCFG_ERR_NOFILE 1
99#define NXCFG_ERR_SYNTAX 2
100
605fc935
VK
101/**
102 * nxlog_open() flags
103 */
967893bb
VK
104#define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
105#define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
106#define NXLOG_IS_OPEN ((UINT32)0x80000000)
9f2902ac 107
605fc935
VK
108/**
109 * nxlog rotation policy
110 */
5945d50a
VK
111#define NXLOG_ROTATION_DISABLED 0
112#define NXLOG_ROTATION_DAILY 1
113#define NXLOG_ROTATION_BY_SIZE 2
114
605fc935
VK
115/**
116 * _tcsdup() replacement
117 */
8ff13508
VK
118#if defined(_WIN32) && defined(USE_WIN32_HEAP)
119#ifdef __cplusplus
120extern "C" {
121#endif
35f836fe 122char LIBNETXMS_EXPORTABLE *nx__tcsdup(const char *src);
8ff13508
VK
123WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
124#ifdef __cplusplus
125}
126#endif
127#endif
128
605fc935
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
25fe6265 399/**
e8e76b4f
VK
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
417/**
25fe6265
VK
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
636//
3046f9a4
VK
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
645//
f2fdf1b5
VK
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
662//
3d1cb7d6
VK
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
731//
e9246d26
VK
732// Functions
733//
734
43d92b4c 735
babb8280 736#if WORDS_BIGENDIAN
43d92b4c
VK
737#define htonq(x) (x)
738#define ntohq(x) (x)
9f20696e
VK
739#define htond(x) (x)
740#define ntohd(x) (x)
babb8280
VK
741#define SwapWideString(x)
742#else
ca6ad6bb
VK
743#ifdef HAVE_HTONLL
744#define htonq(x) htonll(x)
745#else
babb8280 746#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
747#endif
748#ifdef HAVE_NTOHLL
749#define ntohq(x) ntohll(x)
750#else
babb8280 751#define ntohq(x) __bswap_64(x)
ca6ad6bb 752#endif
babb8280
VK
753#define htond(x) __bswap_double(x)
754#define ntohd(x) __bswap_double(x)
755#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 756#endif
15929b31 757
55b33d4c
VK
758#ifdef UNDER_CE
759#define close(x) CloseHandle((HANDLE)(x))
760#endif
761
b50f1100 762#ifdef __cplusplus
4299401e 763 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
c81979bc 764 {
912b994d
VK
765#if defined(_WIN32) && (_MSC_VER >= 1400)
766 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
767#else
c81979bc
VK
768 _tcsncpy(pszDest, pszSrc, nLen - 1);
769 pszDest[nLen - 1] = 0;
912b994d 770#endif
c81979bc
VK
771 return pszDest;
772 }
f3387429
VK
773
774#ifdef UNICODE
775 inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
776 {
777#if defined(_WIN32) && (_MSC_VER >= 1400)
6646dca4 778 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429
VK
779#else
780 strncpy(pszDest, pszSrc, nLen - 1);
781 pszDest[nLen - 1] = 0;
c81979bc 782#endif
f3387429
VK
783 return pszDest;
784 }
c81979bc 785#else
f3387429 786#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
787#endif
788
967893bb 789int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
7b8b337e
VK
790int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int, MUTEX);
791int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
967893bb 792 size_t nSize, int nFlags, UINT32 dwTimeout);
c81979bc
VK
793#endif
794
795#ifdef __cplusplus
e9246d26
VK
796extern "C"
797{
b50f1100 798#endif
c5b523e2 799#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
967893bb 800 UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
c5b523e2 801#endif
f52be742 802#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
967893bb 803 UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
64584976 804#endif
9f20696e 805 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
273da39a 806 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 807
076a55c4 808#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc
VK
809#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
810 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
811#endif
812 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 813#endif
ab588c84 814
b8a8d064
VK
815 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
816 int (*compare)(const void *, const void *, void *));
817
670c7f3d 818 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
d35ed0aa 819
967893bb
VK
820 UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
821 UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
822#ifdef UNICODE
823#define FileSize FileSizeW
824#else
825#define FileSize FileSizeA
826#endif
670c7f3d 827
967893bb
VK
828 int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
829 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 830#ifdef UNICODE
967893bb 831 char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
832#else
833#define IpToStrA IpToStr
e0f99bf0 834#endif
36e44abe 835 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
60557d06
VK
836 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
837
967893bb 838 UINT32 LIBNETXMS_EXPORTABLE ResolveHostName(const TCHAR *pszName);
08b214c6 839#ifdef UNICODE
967893bb 840 UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *pszName);
08b214c6
VK
841#else
842#define ResolveHostNameA ResolveHostName
843#endif
a1dc75b0 844
967893bb
VK
845 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, UINT32 dwSize);
846 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, UINT32 dwSize);
31521248 847
967893bb
VK
848 WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *pData, UINT32 dwSize, WCHAR *pStr);
849 char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *pData, UINT32 dwSize, char *pStr);
d7970ee7
VK
850#ifdef UNICODE
851#define BinToStr BinToStrW
852#else
853#define BinToStr BinToStrA
854#endif
855
967893bb 856 UINT32 LIBNETXMS_EXPORTABLE StrToBin(const TCHAR *pStr, BYTE *pData, UINT32 dwSize);
f42b8099 857 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *pData, TCHAR *pStr);
b50f1100 858
b07c50cc
VK
859 void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
860 void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
861#ifdef UNICODE
862#define StrStrip StrStripW
863#else
864#define StrStrip StrStripA
865#endif
866
ada97259 867 const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
cafe592d 868 const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
869#ifdef UNICODE
870#define ExtractWord ExtractWordW
871#else
872#define ExtractWord ExtractWordA
873#endif
874
3583a400
VK
875 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
876 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
877#ifdef UNICODE
878#define NumChars NumCharsW
879#else
880#define NumChars NumCharsA
881#endif
ada97259 882
35f836fe
VK
883 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
884 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
885#ifdef UNICODE
886#define RemoveTrailingCRLF RemoveTrailingCRLFW
887#else
888#define RemoveTrailingCRLF RemoveTrailingCRLFA
889#endif
890
cafe592d
VK
891 BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *pszStr, const char *pszExpr, BOOL bMatchCase);
892 BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *pszStr, const WCHAR *pszExpr, BOOL bMatchCase);
893#ifdef UNICODE
894#define RegexpMatch RegexpMatchW
895#else
896#define RegexpMatch RegexpMatchA
897#endif
898
a73a4403 899 const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize);
ada97259 900 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
240816b0 901 bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
ada97259
VK
902 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
903
f7794b68
VK
904#ifdef __cplusplus
905 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
906#endif
fa2b47a7 907 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
7618e362 908 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1a5e0c22 909 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
c3cfc039 910 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
967893bb 911 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 912#ifdef UNICODE
967893bb 913 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
914#else
915#define LoadFileA LoadFile
916#endif
917
967893bb 918 UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, UINT32 dwSize, UINT32 dwCRC);
a3c76aba 919 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 920 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
a3c76aba 921 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 922 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
3aa0061c
VK
923 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
924 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
967893bb 925 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 926
4e2debcc
VK
927 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
928 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
929
6b1282e7
VK
930 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
931
967893bb
VK
932 UINT32 LIBNETXMS_EXPORTABLE IcmpPing(UINT32 dwAddr, int iNumRetries, UINT32 dwTimeout,
933 UINT32 *pdwRTT, UINT32 dwPacketSize);
4826c8a5 934
3aa0061c 935 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 936
4560d8bd 937 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
aa13cfd9 938 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
dda7c270 939 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 940
2e06d08b
VK
941 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValue(const TCHAR *optString, const TCHAR *option, TCHAR *buffer, int bufSize);
942 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBool(const TCHAR *optString, const TCHAR *option, BOOL defVal);
943 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsInt(const TCHAR *optString, const TCHAR *option, long defVal);
ae9cddaf 944
bc858635
VK
945#ifdef __cplusplus
946 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
947#else
948 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
949#endif
950
aa13cfd9 951#ifdef _WIN32
967893bb 952 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
ccc34207 953 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
11606542 954 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 955#endif
696fc54f
VK
956
957#if !(HAVE_DAEMON)
b1a57444 958 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 959#endif
4332e9d8 960
967893bb 961 UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 962
4332e9d8 963#ifndef _WIN32
664c7c49 964 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
967893bb 965 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
4332e9d8
VK
966 int cchWideChar, char *pByteStr, int cchByteChar,
967 char *pDefaultChar, BOOL *pbUsedDefChar);
967893bb 968 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
4332e9d8
VK
969 int cchByteChar, WCHAR *pWideCharStr,
970 int cchWideChar);
04dbaf62 971
6b93c954 972#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
8b86c5dc 973 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
974#endif
975#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
8b86c5dc 976 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
977#endif
978#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
35f836fe 979 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
4332e9d8 980#endif
8b86c5dc 981
53c96e2d
VK
982 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
983 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
39945910
VK
984 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
985 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 986
6e17f325
VK
987 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
988 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
989 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
990 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
991 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
992 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 993
25575d38
VK
994 int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
995 int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
7ff399a5 996 int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
25575d38
VK
997 int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
998 int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
7ff399a5 999 int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1000
3e39fcde 1001#endif /* _WIN32 */
8b86c5dc 1002
3aa0061c 1003 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
c16afb3d 1004 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
3aa0061c
VK
1005 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
1006 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
8b86c5dc 1007
3d1058ed
VK
1008#ifdef _WITH_ENCRYPTION
1009 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
1010#endif
1011
8b86c5dc 1012#ifdef UNICODE_UCS4
53c96e2d
VK
1013 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1014 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1015 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
d9b66a56
VK
1016 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1017 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1018#endif
8b86c5dc 1019
842c22f4
AK
1020#if !defined(_WIN32) && !HAVE_WSTAT
1021 int wstat(const WCHAR *_path, struct stat *_sbuf);
1022#endif
1023
ef856d02 1024#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1025
f3387429
VK
1026#if !HAVE_WPOPEN
1027 FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
1028#endif
8b86c5dc
VK
1029#if !HAVE_WFOPEN
1030 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
1031#endif
1032#if !HAVE_WOPEN
1033 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1034#endif
f3387429
VK
1035#if !HAVE_WCHMOD
1036 int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
1037#endif
b07c50cc
VK
1038#if !HAVE_WCHDIR
1039 int wchdir(const WCHAR *_path);
1040#endif
1041#if !HAVE_WMKDIR
1042 int wmkdir(const WCHAR *_path, int mode);
1043#endif
1044#if !HAVE_WRMDIR
1045 int wrmdir(const WCHAR *_path);
1046#endif
ccc34207
VK
1047#if !HAVE_WRENAME
1048 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
1049#endif
1050#if !HAVE_WUNLINK
1051 int wunlink(const WCHAR *_path);
1052#endif
b07c50cc
VK
1053#if !HAVE_WREMOVE
1054 int wremove(const WCHAR *_path);
1055#endif
1056#if !HAVE_WSYSTEM
1057 int wsystem(const WCHAR *_cmd);
1058#endif
dda7c270
VK
1059#if !HAVE_WMKSTEMP
1060 int wmkstemp(const WCHAR *_template);
1061#endif
b07c50cc
VK
1062#if !HAVE_WACCESS
1063 int waccess(const WCHAR *_path, int mode);
1064#endif
273da39a
VK
1065#if !HAVE_WGETENV
1066 WCHAR *wgetenv(const WCHAR *_string);
1067#endif
f3387429
VK
1068#if !HAVE_WCTIME
1069 WCHAR *wctime(const time_t *timep);
1070#endif
0186b5da
VK
1071#if !HAVE_PUTWS
1072 int putws(const WCHAR *s);
1073#endif
d49d7500
VK
1074#if !HAVE_WCSERROR && HAVE_STRERROR
1075 WCHAR *wcserror(int errnum);
1076#endif
1077#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06
VK
1078#if HAVE_POSIX_STRERROR_R
1079 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1080#else
d49d7500
VK
1081 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1082#endif
b0823d06 1083#endif
e05f8aaf 1084
ef856d02 1085#endif /* UNICODE && !_WIN32*/
e05f8aaf 1086
8b86c5dc
VK
1087#if !HAVE_STRTOLL
1088 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
1089#endif
1090#if !HAVE_STRTOULL
967893bb 1091 UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1092#endif
e05f8aaf
VK
1093
1094#if !HAVE_WCSTOLL
1095 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
1096#endif
1097#if !HAVE_WCSTOULL
967893bb 1098 UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1099#endif
b50f1100 1100
fa88627b 1101#if !HAVE_WCSDUP && !defined(_WIN32)
37861c72
VK
1102 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
1103#endif
1104
3d1cb7d6 1105#ifdef _WIN32
5ea2db30
VK
1106#ifdef UNICODE
1107 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1108 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1109 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1110
1111#define _topendir wopendir
1112#define _treaddir wreaddir
1113#define _tclosedir wclosedir
1114#else
1115#define _topendir opendir
1116#define _treaddir readdir
1117#define _tclosedir closedir
1118#endif
1119
3d1cb7d6 1120 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 1121 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6 1122 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1123
76b1d3d8
VK
1124#else /* not _WIN32 */
1125
1126 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1127 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1128 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1129
37d62364 1130#endif
3d1cb7d6 1131
7f9e0c00
VK
1132#if defined(_WIN32) || !(HAVE_SCANDIR)
1133 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1134 int (*select)(const struct dirent *),
1135 int (*compar)(const struct dirent **, const struct dirent **));
1136 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
1137#endif
1138
55b33d4c
VK
1139#ifdef UNDER_CE
1140 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1141 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1142 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
1143#endif
1144
967893bb 1145BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1146 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1147void LIBNETXMS_EXPORTABLE nxlog_close(void);
1148void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1149BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1150BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1151
1152typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1153void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1154
1155void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
1156void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...);
9f2902ac 1157
37a7a862
VK
1158#ifdef _WIN32
1159int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1160int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1161#ifdef UNICODE
1162#define _tmkstemp wmkstemp
1163#else
1164#define _tmkstemp mkstemp
1165#endif
1166#endif
1167
9b13553b
VK
1168#ifndef _WIN32
1169int strcat_s(char *dst, size_t dstSize, const char *src);
1170int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1171#endif
1172
b50f1100 1173#ifdef __cplusplus
e9246d26 1174}
b50f1100 1175#endif
e9246d26 1176
a7ff20a5
VK
1177
1178//
1179// C++ only finctions
1180//
1181
1182#ifdef __cplusplus
1183
240816b0
VK
1184TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1185String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1186const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1187int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1188UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1189bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1190
1191#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1192void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1193#endif
1194
1195void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
967893bb
VK
1196 void (* sendTrap1)(UINT32, const TCHAR *, const char *, va_list),
1197 void (* sendTrap2)(UINT32, const TCHAR *, int, TCHAR **),
1198 BOOL (* sendFile)(void *, UINT32, const TCHAR *, long),
53c96e2d 1199 BOOL (* pushData)(const TCHAR *, const TCHAR *));
a7ff20a5
VK
1200
1201#endif
1202
e9246d26 1203#endif /* _nms_util_h_ */