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