TelnetConnection added
[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
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 */
9f2902ac
VK
104#define NXLOG_USE_SYSLOG ((DWORD)0x00000001)
105#define NXLOG_PRINT_TO_STDOUT ((DWORD)0x00000002)
106#define NXLOG_IS_OPEN ((DWORD)0x80000000)
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
826fcc1f
VK
189/**
190 * Class for table data storage
191 */
ccc34207
VK
192class CSCPMessage;
193
ff214673
VK
194class LIBNETXMS_EXPORTABLE Table
195{
196private:
197 int m_nNumRows;
198 int m_nNumCols;
199 TCHAR **m_ppData;
200 TCHAR **m_ppColNames;
e31c8e60
VK
201 LONG *m_colFormats;
202 TCHAR *m_title;
ff214673 203
0f506caa
VK
204 void createFromMessage(CSCPMessage *msg);
205 void destroy();
206
ff214673
VK
207public:
208 Table();
ccc34207 209 Table(CSCPMessage *msg);
ff214673
VK
210 ~Table();
211
3d5610eb 212 int fillMessage(CSCPMessage &msg, int offset, int rowLimit);
0f506caa 213 void updateFromMessage(CSCPMessage *msg);
9e126e57 214
e31c8e60
VK
215 int getNumRows() { return m_nNumRows; }
216 int getNumColumns() { return m_nNumCols; }
217 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
9e126e57 218
e31c8e60
VK
219 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_nNumCols)) ? m_ppColNames[col] : NULL; }
220 LONG getColumnFormat(int col) { return ((col >= 0) && (col < m_nNumCols)) ? m_colFormats[col] : 0; }
221
222 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = (title != NULL) ? _tcsdup(title) : NULL; }
223 int addColumn(const TCHAR *name, LONG format = 0);
224 int addRow();
9e126e57
VK
225
226 void setAt(int nRow, int nCol, LONG nData);
227 void setAt(int nRow, int nCol, DWORD dwData);
228 void setAt(int nRow, int nCol, double dData);
229 void setAt(int nRow, int nCol, INT64 nData);
230 void setAt(int nRow, int nCol, QWORD qwData);
231 void setAt(int nRow, int nCol, const TCHAR *pszData);
232 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
233
234 void set(int nCol, LONG nData) { setAt(m_nNumRows - 1, nCol, nData); }
235 void set(int nCol, DWORD dwData) { setAt(m_nNumRows - 1, nCol, dwData); }
236 void set(int nCol, double dData) { setAt(m_nNumRows - 1, nCol, dData); }
237 void set(int nCol, INT64 nData) { setAt(m_nNumRows - 1, nCol, nData); }
238 void set(int nCol, QWORD qwData) { setAt(m_nNumRows - 1, nCol, qwData); }
239 void set(int nCol, const TCHAR *pszData) { setAt(m_nNumRows - 1, nCol, pszData); }
240 void setPreallocated(int nCol, TCHAR *pszData) { setPreallocatedAt(m_nNumRows - 1, nCol, pszData); }
241
242 const TCHAR *getAsString(int nRow, int nCol);
243 LONG getAsInt(int nRow, int nCol);
244 DWORD getAsUInt(int nRow, int nCol);
245 INT64 getAsInt64(int nRow, int nCol);
246 QWORD getAsUInt64(int nRow, int nCol);
247 double getAsDouble(int nRow, int nCol);
ff214673
VK
248};
249
3fe40613
VK
250/**
251 * Dynamic string class
252 */
8bad8476
VK
253class LIBNETXMS_EXPORTABLE String
254{
255protected:
256 TCHAR *m_pszBuffer;
257 DWORD m_dwBufSize;
258
259public:
c577e360
VK
260 static const int npos;
261
8bad8476 262 String();
f3cdd1c5 263 String(const TCHAR *init);
643c9dcb 264 String(const String &src);
8bad8476
VK
265 ~String();
266
7618e362 267 TCHAR *getBuffer() { return m_pszBuffer; }
ce7565e7 268 void setBuffer(TCHAR *pszBuffer);
8bad8476 269
2391d4fd 270 const String& operator =(const TCHAR *pszStr);
49bf9d8d 271 const String& operator =(const String &src);
2391d4fd 272 const String& operator +=(const TCHAR *pszStr);
ce7565e7 273 operator const TCHAR*() { return CHECK_NULL_EX(m_pszBuffer); }
8bad8476 274
08b214c6
VK
275 char *getUTF8String();
276
ce7565e7
VK
277 void addString(const TCHAR *pStr, DWORD dwLen);
278 void addDynamicString(TCHAR *pszStr) { if (pszStr != NULL) { *this += pszStr; free(pszStr); } }
c577e360 279
ce7565e7
VK
280 void addMultiByteString(const char *pStr, DWORD dwSize, int nCodePage);
281 void addWideCharString(const WCHAR *pStr, DWORD dwSize);
ff175e91 282
ce7565e7 283 void addFormattedString(const TCHAR *format, ...);
893dfdd7 284 void addFormattedStringV(const TCHAR *format, va_list args);
c577e360 285
ce7565e7
VK
286 DWORD getSize() { return m_dwBufSize > 0 ? m_dwBufSize - 1 : 0; }
287 BOOL isEmpty() { return m_dwBufSize <= 1; }
c577e360 288
ce7565e7
VK
289 TCHAR *subStr(int nStart, int nLen, TCHAR *pszBuffer);
290 TCHAR *subStr(int nStart, int nLen) { return subStr(nStart, nLen, NULL); }
291 int find(const TCHAR *pszStr, int nStart = 0);
1e1450ef 292
6bcafa2d
VK
293 void escapeCharacter(int ch, int esc);
294 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
0bbab9d3
VK
295 void trim();
296 void shrink(int chars = 1);
c577e360
VK
297};
298
3fe40613
VK
299/**
300 * String maps base class
301 */
302class LIBNETXMS_EXPORTABLE StringMapBase
c577e360
VK
303{
304protected:
fb986055
VK
305 DWORD m_size;
306 TCHAR **m_keys;
3fe40613
VK
307 void **m_values;
308 bool m_objectOwner;
309 void (*m_objectDestructor)(void *);
c577e360 310
fb986055 311 DWORD find(const TCHAR *key);
3fe40613
VK
312 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
313 void *getObject(const TCHAR *key);
314 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
c577e360
VK
315
316public:
3fe40613
VK
317 StringMapBase(bool objectOwner);
318 virtual ~StringMapBase();
319
320 void remove(const TCHAR *key);
321 void clear();
322
323 DWORD getSize() { return m_size; }
324 const TCHAR *getKeyByIndex(DWORD idx) { return (idx < m_size) ? CHECK_NULL_EX(m_keys[idx]) : NULL; }
325};
326
327/**
328 * String map class
329 */
330class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
331{
332public:
333 StringMap() : StringMapBase(true) { }
fb986055 334 StringMap(const StringMap &src);
3fe40613 335 virtual ~StringMap();
c577e360 336
fb986055 337 StringMap& operator =(const StringMap &src);
40b7416c 338
3fe40613
VK
339 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
340 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
5ad2167d
VK
341 void set(const TCHAR *key, DWORD value);
342
3fe40613 343 const TCHAR *get(const TCHAR *key) { return (const TCHAR *)getObject(key); }
5ad2167d 344 DWORD getULong(const TCHAR *key, DWORD defaultValue);
6acb8479 345 bool getBoolean(const TCHAR *key, bool defaultValue);
5ad2167d 346
3fe40613 347 const TCHAR *getValueByIndex(DWORD idx) { return (idx < m_size) ? CHECK_NULL_EX((TCHAR *)m_values[idx]) : NULL; }
8bad8476
VK
348};
349
3fe40613
VK
350/**
351 * String map template for holding objects as values
352 */
353template <class T> class StringObjectMap : public StringMapBase
354{
355private:
356 static void destructor(void *object) { delete (T*)object; }
6173bea8 357
3fe40613
VK
358public:
359 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
360
361 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
362 T *get(const TCHAR *key) { return (T*)getObject(key); }
363 T *getValueByIndex(DWORD idx) { return (idx < m_size) ? (T *)m_values[idx] : NULL; }
364};
6173bea8 365
3fe40613
VK
366/**
367 * String list class
368 */
6173bea8
VK
369class LIBNETXMS_EXPORTABLE StringList
370{
371private:
372 int m_count;
373 int m_allocated;
374 TCHAR **m_values;
375
376public:
377 StringList();
378 ~StringList();
379
380 void add(const TCHAR *value);
381 void addPreallocated(TCHAR *value);
382 void add(LONG value);
383 void add(DWORD value);
384 void add(INT64 value);
385 void add(QWORD value);
386 void add(double value);
24dc5346 387 void replace(int index, const TCHAR *value);
6173bea8
VK
388 void clear();
389 int getSize() { return m_count; }
390 const TCHAR *getValue(int index) { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
ed806a1a
VK
391 int getIndex(const TCHAR *value);
392 int getIndexIgnoreCase(const TCHAR *value);
d51f2182 393 void remove(int index);
6173bea8
VK
394};
395
3fe40613
VK
396/**
397 * Dynamic array class
398 */
0d75ea88
VK
399class LIBNETXMS_EXPORTABLE Array
400{
401private:
402 int m_size;
403 int m_allocated;
404 int m_grow;
405 void **m_data;
406 bool m_objectOwner;
407
16d6f798 408 void internalRemove(int index, bool allowDestruction);
cfa7c21f 409 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
16d6f798 410
0d75ea88 411protected:
cfa7c21f 412 void (*m_objectDestructor)(void *);
0d75ea88
VK
413
414public:
415 Array(int initial = 0, int grow = 16, bool owner = false);
416 virtual ~Array();
417
418 int add(void *object);
419 void *get(int index) { return ((index >= 0) && (index < m_size)) ? m_data[index] : NULL; }
47a89886 420 void set(int index, void *object);
24dc5346 421 void replace(int index, void *object);
16d6f798
VK
422 void remove(int index) { internalRemove(index, true); }
423 void unlink(int index) { internalRemove(index, false); }
0d75ea88
VK
424 void clear();
425
426 int size() { return m_size; }
427
428 void setOwner(bool owner) { m_objectOwner = owner; }
429 bool isOwner() { return m_objectOwner; }
430};
431
3fe40613
VK
432/**
433 * Template class for dynamic array which holds objects
434 */
0d75ea88
VK
435template <class T> class ObjectArray : public Array
436{
cfa7c21f
VK
437private:
438 static void destructor(void *object) { delete (T*)object; }
0d75ea88
VK
439
440public:
cfa7c21f 441 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
0d75ea88
VK
442 virtual ~ObjectArray() { }
443
47a89886 444 int add(T *object) { return Array::add((void *)object); }
0d75ea88 445 T *get(int index) { return (T*)Array::get(index); }
47a89886
VK
446 void set(int index, T *object) { Array::set(index, (void *)object); }
447 void replace(int index, T *object) { Array::replace(index, (void *)object); }
0d75ea88
VK
448};
449
25fe6265
VK
450/**
451 * Auxilliary class to hold dynamically allocated array of structures
452 */
cc8ce218
VK
453template <class T> class StructArray
454{
455private:
456 int m_count;
457 T *m_data;
458
459public:
460 StructArray(T *data, int count) { m_data = data; m_count = count; }
461 ~StructArray() { safe_free(m_data); }
462
463 int size() { return m_count; }
464 T *get(int index) { return ((index >= 0) && (index < m_count)) ? &m_data[index] : NULL; }
465};
466
25fe6265
VK
467/**
468 * Auxilliary class for objects which counts references and
469 * destroys itself wheren reference count falls to 0
470 */
7f632dfe
VK
471class LIBNETXMS_EXPORTABLE RefCountObject
472{
473private:
474 int m_refCount;
475 MUTEX m_mutex;
476
477public:
478 RefCountObject();
479 virtual ~RefCountObject();
480
481 void incRefCount();
482 void decRefCount();
483};
484
25fe6265
VK
485/**
486 * Network connection
487 */
58ee2f43
VK
488class LIBNETXMS_EXPORTABLE SocketConnection
489{
490protected:
491 SOCKET m_socket;
492 char m_data[4096];
493 int m_dataPos;
494
495public:
496 SocketConnection();
497 virtual ~SocketConnection();
498
499 bool connectTCP(const TCHAR *hostName, WORD port, DWORD timeout);
500 void disconnect();
501
502 bool canRead(DWORD timeout);
def5b792 503 virtual int read(char *pBuff, int nSize, DWORD timeout = INFINITE);
58ee2f43
VK
504 bool waitForText(const char *text, int timeout);
505
506 int write(const char *pBuff, int nSize);
1671f85d
VK
507 bool writeLine(const char *line);
508
509 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, DWORD timeout);
58ee2f43
VK
510};
511
def5b792
AK
512class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
513{
514public:
515 bool connect(const TCHAR *hostName, WORD port, DWORD timeout);
516 int read(char *pBuff, int nSize, DWORD timeout = INFINITE);
517
518 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, DWORD timeout);
519
520protected:
521 bool connectTCP(const TCHAR *hostName, WORD port, DWORD timeout);
522};
523
b160350d
VK
524#endif /* __cplusplus */
525
526
4826c8a5
VK
527//
528// Configuration item template for configuration loader
529//
530
531typedef struct
532{
018fda4d 533 TCHAR szToken[64];
4826c8a5
VK
534 BYTE iType;
535 BYTE cSeparator; // Separator character for lists
536 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
537 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
538 DWORD dwBufferPos; // Should be set to 0
539 void *pBuffer;
540} NX_CFG_TEMPLATE;
dcb44c9a
VK
541
542
bc858635
VK
543//
544// Code translation structure
545//
546
547typedef struct __CODE_TO_TEXT
548{
549 int code;
550 const TCHAR *text;
551} CODE_TO_TEXT;
552
553
3046f9a4
VK
554//
555// getopt() prototype if needed
556//
557
df9812a9 558#if USE_BUNDLED_GETOPT
99e50bb7 559#include <netxms_getopt.h>
3046f9a4
VK
560#endif
561
562
f2fdf1b5
VK
563//
564// Win32 API functions missing under WinCE
565//
566
c32b805a 567#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
568
569inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
570{
571 SYSTEMTIME sysTime;
572
573 GetSystemTime(&sysTime);
574 SystemTimeToFileTime(&sysTime, pFt);
575}
576
577#endif // UNDER_CE
578
579
3d1cb7d6
VK
580//
581// Structures for opendir() / readdir() / closedir()
582//
583
584#ifdef _WIN32
585
7f9e0c00 586typedef struct dirent
3d1cb7d6
VK
587{
588 long d_ino; /* inode number (not used by MS-DOS) */
589 int d_namlen; /* Name length */
590 char d_name[257]; /* file name */
591} _DIRECT;
592
593typedef struct _dir_struc
594{
595 char *start; /* Starting position */
596 char *curr; /* Current position */
597 long size; /* Size of string table */
598 long nfiles; /* number if filenames in table */
7f9e0c00 599 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
600} DIR;
601
5ea2db30
VK
602#ifdef UNICODE
603
604typedef struct dirent_w
605{
606 long d_ino; /* inode number (not used by MS-DOS) */
607 int d_namlen; /* Name length */
608 WCHAR d_name[257]; /* file name */
609} _DIRECTW;
610
611typedef struct _dir_struc_w
612{
613 WCHAR *start; /* Starting position */
614 WCHAR *curr; /* Current position */
615 long size; /* Size of string table */
616 long nfiles; /* number if filenames in table */
617 struct dirent_w dirstr; /* Directory structure to return */
618} DIRW;
619
620#define _TDIR DIRW
621#define _TDIRECT _DIRECTW
622#define _tdirent dirent_w
623
624#else
625
626#define _TDIR DIR
627#define _TDIRECT _DIRECT
628#define _tdirent dirent
629
37d62364
AK
630#endif
631
76b1d3d8
VK
632#else /* not _WIN32 */
633
634typedef struct dirent_w
635{
636 long d_ino; /* inode number */
637 WCHAR d_name[257]; /* file name */
638} _DIRECTW;
639
640typedef struct _dir_struc_w
641{
642 DIR *dir; /* Original non-unicode structure */
643 struct dirent_w dirstr; /* Directory structure to return */
644} DIRW;
645
3d1cb7d6
VK
646#endif /* _WIN32 */
647
648
e9246d26
VK
649//
650// Functions
651//
652
43d92b4c 653
babb8280 654#if WORDS_BIGENDIAN
43d92b4c
VK
655#define htonq(x) (x)
656#define ntohq(x) (x)
9f20696e
VK
657#define htond(x) (x)
658#define ntohd(x) (x)
babb8280
VK
659#define SwapWideString(x)
660#else
ca6ad6bb
VK
661#ifdef HAVE_HTONLL
662#define htonq(x) htonll(x)
663#else
babb8280 664#define htonq(x) __bswap_64(x)
ca6ad6bb
VK
665#endif
666#ifdef HAVE_NTOHLL
667#define ntohq(x) ntohll(x)
668#else
babb8280 669#define ntohq(x) __bswap_64(x)
ca6ad6bb 670#endif
babb8280
VK
671#define htond(x) __bswap_double(x)
672#define ntohd(x) __bswap_double(x)
673#define SwapWideString(x) __bswap_wstr(x)
43d92b4c 674#endif
15929b31 675
55b33d4c
VK
676#ifdef UNDER_CE
677#define close(x) CloseHandle((HANDLE)(x))
678#endif
679
c81979bc 680#ifdef __cplusplus
4299401e 681 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
c81979bc 682 {
912b994d
VK
683#if defined(_WIN32) && (_MSC_VER >= 1400)
684 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
685#else
c81979bc
VK
686 _tcsncpy(pszDest, pszSrc, nLen - 1);
687 pszDest[nLen - 1] = 0;
912b994d 688#endif
c81979bc
VK
689 return pszDest;
690 }
f3387429
VK
691
692#ifdef UNICODE
693 inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
694 {
695#if defined(_WIN32) && (_MSC_VER >= 1400)
6646dca4 696 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429
VK
697#else
698 strncpy(pszDest, pszSrc, nLen - 1);
699 pszDest[nLen - 1] = 0;
c81979bc 700#endif
f3387429
VK
701 return pszDest;
702 }
c81979bc 703#else
f3387429 704#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
705#endif
706
7b8b337e
VK
707int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, DWORD timeout);
708int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int, MUTEX);
709int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
710 size_t nSize, int nFlags, DWORD dwTimeout);
c81979bc
VK
711#endif
712
b50f1100 713#ifdef __cplusplus
e9246d26
VK
714extern "C"
715{
b50f1100 716#endif
c5b523e2
VK
717#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
718 DWORD LIBNETXMS_EXPORTABLE __bswap_32(DWORD dwVal);
719#endif
f52be742 720#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
64584976
VK
721 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
722#endif
9f20696e 723 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
273da39a 724 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
64584976 725
076a55c4 726#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc
VK
727#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
728 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
729#endif
730 void LIBNETXMS_EXPORTABLE strupr(char *in);
7e679c4b 731#endif
ab588c84 732
b8a8d064
VK
733 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
734 int (*compare)(const void *, const void *, void *));
735
670c7f3d 736 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
d35ed0aa
VK
737
738 QWORD LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
739 QWORD LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
740#ifdef UNICODE
741#define FileSize FileSizeW
742#else
743#define FileSize FileSizeA
744#endif
670c7f3d 745
64584976 746 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
018fda4d 747 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
e0f99bf0
VK
748#ifdef UNICODE
749 char LIBNETXMS_EXPORTABLE *IpToStrA(DWORD dwAddr, char *szBuffer);
17b35ccc
VK
750#else
751#define IpToStrA IpToStr
e0f99bf0 752#endif
36e44abe 753 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
60557d06
VK
754 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
755
3aa0061c 756 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(const TCHAR *pszName);
08b214c6
VK
757#ifdef UNICODE
758 DWORD LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *pszName);
759#else
760#define ResolveHostNameA ResolveHostName
761#endif
a1dc75b0 762
64584976 763 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
f218dc6d 764 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
31521248 765
d7970ee7
VK
766 WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *pData, DWORD dwSize, WCHAR *pStr);
767 char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *pData, DWORD dwSize, char *pStr);
768#ifdef UNICODE
769#define BinToStr BinToStrW
770#else
771#define BinToStr BinToStrA
772#endif
773
3aa0061c 774 DWORD LIBNETXMS_EXPORTABLE StrToBin(const TCHAR *pStr, BYTE *pData, DWORD dwSize);
f42b8099 775 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *pData, TCHAR *pStr);
b50f1100 776
b07c50cc
VK
777 void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
778 void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
779#ifdef UNICODE
780#define StrStrip StrStripW
781#else
782#define StrStrip StrStripA
783#endif
784
ada97259 785 const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
cafe592d 786 const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
787#ifdef UNICODE
788#define ExtractWord ExtractWordW
789#else
790#define ExtractWord ExtractWordA
791#endif
792
3583a400
VK
793 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
794 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
795#ifdef UNICODE
796#define NumChars NumCharsW
797#else
798#define NumChars NumCharsA
799#endif
ada97259 800
35f836fe
VK
801 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
802 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
803#ifdef UNICODE
804#define RemoveTrailingCRLF RemoveTrailingCRLFW
805#else
806#define RemoveTrailingCRLF RemoveTrailingCRLFA
cafe592d
VK
807#endif
808
809 BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *pszStr, const char *pszExpr, BOOL bMatchCase);
810 BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *pszStr, const WCHAR *pszExpr, BOOL bMatchCase);
811#ifdef UNICODE
812#define RegexpMatch RegexpMatchW
813#else
814#define RegexpMatch RegexpMatchA
35f836fe
VK
815#endif
816
a73a4403 817 const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize);
ada97259 818 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
240816b0 819 bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
ada97259
VK
820 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
821
f7794b68
VK
822#ifdef __cplusplus
823 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
824#endif
fa2b47a7 825 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
7618e362 826 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1a5e0c22 827 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
c3cfc039 828 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
3aa0061c 829 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, DWORD *pdwFileSize);
08b214c6
VK
830#ifdef UNICODE
831 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, DWORD *pdwFileSize);
832#else
833#define LoadFileA LoadFile
834#endif
835
6120112b 836 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
a3c76aba 837 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 838 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
a3c76aba 839 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
e0d4b618 840 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
3aa0061c
VK
841 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
842 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
843 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, DWORD *pResult);
66c04cc7 844
4e2debcc
VK
845 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
846 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
847
6b1282e7
VK
848 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
849
46ef501a
VK
850 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
851 DWORD *pdwRTT, DWORD dwPacketSize);
4826c8a5 852
3aa0061c 853 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 854
4560d8bd 855 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
aa13cfd9 856 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
dda7c270 857 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 858
2e06d08b
VK
859 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValue(const TCHAR *optString, const TCHAR *option, TCHAR *buffer, int bufSize);
860 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBool(const TCHAR *optString, const TCHAR *option, BOOL defVal);
861 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsInt(const TCHAR *optString, const TCHAR *option, long defVal);
ae9cddaf 862
bc858635
VK
863#ifdef __cplusplus
864 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
865#else
866 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
867#endif
868
aa13cfd9 869#ifdef _WIN32
4299401e 870 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, size_t iBufSize);
ccc34207 871 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
11606542 872 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 873#endif
696fc54f
VK
874
875#if !(HAVE_DAEMON)
b1a57444 876 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
696fc54f 877#endif
4332e9d8 878
3aa0061c 879 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 880
4332e9d8 881#ifndef _WIN32
664c7c49 882 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
3aa0061c 883 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, const WCHAR *pWideCharStr,
4332e9d8
VK
884 int cchWideChar, char *pByteStr, int cchByteChar,
885 char *pDefaultChar, BOOL *pbUsedDefChar);
3aa0061c 886 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, const char *pByteStr,
4332e9d8
VK
887 int cchByteChar, WCHAR *pWideCharStr,
888 int cchWideChar);
04dbaf62 889
6b93c954 890#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
8b86c5dc 891 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
892#endif
893#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
8b86c5dc 894 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
895#endif
896#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
35f836fe 897 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
4332e9d8 898#endif
8b86c5dc 899
53c96e2d
VK
900 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
901 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
39945910
VK
902 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
903 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 904
6e17f325
VK
905 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
906 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
907 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
908 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
909 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
910 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 911
25575d38
VK
912 int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
913 int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
7ff399a5 914 int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
25575d38
VK
915 int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
916 int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
7ff399a5 917 int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 918
3e39fcde 919#endif /* _WIN32 */
8b86c5dc 920
3aa0061c 921 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
c16afb3d 922 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
3aa0061c
VK
923 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
924 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
8b86c5dc 925
3d1058ed
VK
926#ifdef _WITH_ENCRYPTION
927 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
928#endif
929
8b86c5dc 930#ifdef UNICODE_UCS4
53c96e2d
VK
931 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
932 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
933 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
d9b66a56
VK
934 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
935 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 936#endif
8b86c5dc 937
842c22f4
AK
938#if !defined(_WIN32) && !HAVE_WSTAT
939 int wstat(const WCHAR *_path, struct stat *_sbuf);
940#endif
941
ef856d02 942#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 943
f3387429
VK
944#if !HAVE_WPOPEN
945 FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
946#endif
8b86c5dc
VK
947#if !HAVE_WFOPEN
948 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
949#endif
950#if !HAVE_WOPEN
951 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 952#endif
f3387429
VK
953#if !HAVE_WCHMOD
954 int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
955#endif
b07c50cc
VK
956#if !HAVE_WCHDIR
957 int wchdir(const WCHAR *_path);
958#endif
959#if !HAVE_WMKDIR
960 int wmkdir(const WCHAR *_path, int mode);
961#endif
962#if !HAVE_WRMDIR
963 int wrmdir(const WCHAR *_path);
964#endif
ccc34207
VK
965#if !HAVE_WRENAME
966 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
967#endif
968#if !HAVE_WUNLINK
969 int wunlink(const WCHAR *_path);
970#endif
b07c50cc
VK
971#if !HAVE_WREMOVE
972 int wremove(const WCHAR *_path);
973#endif
974#if !HAVE_WSYSTEM
975 int wsystem(const WCHAR *_cmd);
976#endif
dda7c270
VK
977#if !HAVE_WMKSTEMP
978 int wmkstemp(const WCHAR *_template);
979#endif
b07c50cc
VK
980#if !HAVE_WACCESS
981 int waccess(const WCHAR *_path, int mode);
982#endif
273da39a
VK
983#if !HAVE_WGETENV
984 WCHAR *wgetenv(const WCHAR *_string);
985#endif
f3387429
VK
986#if !HAVE_WCTIME
987 WCHAR *wctime(const time_t *timep);
988#endif
0186b5da
VK
989#if !HAVE_PUTWS
990 int putws(const WCHAR *s);
991#endif
d49d7500
VK
992#if !HAVE_WCSERROR && HAVE_STRERROR
993 WCHAR *wcserror(int errnum);
994#endif
995#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06
VK
996#if HAVE_POSIX_STRERROR_R
997 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
998#else
d49d7500
VK
999 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1000#endif
b0823d06 1001#endif
e05f8aaf 1002
ef856d02 1003#endif /* UNICODE && !_WIN32*/
e05f8aaf 1004
8b86c5dc
VK
1005#if !HAVE_STRTOLL
1006 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
1007#endif
1008#if !HAVE_STRTOULL
1009 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
1010#endif
e05f8aaf
VK
1011
1012#if !HAVE_WCSTOLL
1013 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
1014#endif
1015#if !HAVE_WCSTOULL
1016 QWORD LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
1017#endif
b50f1100 1018
fa88627b 1019#if !HAVE_WCSDUP && !defined(_WIN32)
37861c72
VK
1020 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
1021#endif
1022
55e62b45
VK
1023#if !HAVE_STRLWR && !defined(_WIN32)
1024 char LIBNETXMS_EXPORTABLE *strlwr(char *str);
1025 WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
1026#endif
1027
3d1cb7d6 1028#ifdef _WIN32
5ea2db30
VK
1029#ifdef UNICODE
1030 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1031 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1032 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1033
1034#define _topendir wopendir
1035#define _treaddir wreaddir
1036#define _tclosedir wclosedir
1037#else
1038#define _topendir opendir
1039#define _treaddir readdir
1040#define _tclosedir closedir
1041#endif
1042
3d1cb7d6 1043 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
7f9e0c00 1044 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
3d1cb7d6 1045 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1046
76b1d3d8
VK
1047#else /* not _WIN32 */
1048
1049 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1050 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1051 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1052
37d62364 1053#endif
3d1cb7d6 1054
7f9e0c00
VK
1055#if defined(_WIN32) || !(HAVE_SCANDIR)
1056 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1057 int (*select)(const struct dirent *),
1058 int (*compar)(const struct dirent **, const struct dirent **));
1059 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
1060#endif
1061
55b33d4c
VK
1062#ifdef UNDER_CE
1063 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1064 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1065 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
1066#endif
1067
9f2902ac 1068BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, DWORD flags, const TCHAR *msgModule,
d49d7500 1069 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1070void LIBNETXMS_EXPORTABLE nxlog_close(void);
1071void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1072BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1073BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
f669df41
VK
1074void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(void (*writer)(const TCHAR *, ...));
1075
1076void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
1077void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...);
9f2902ac 1078
37a7a862
VK
1079#ifdef _WIN32
1080int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1081int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1082#ifdef UNICODE
1083#define _tmkstemp wmkstemp
1084#else
1085#define _tmkstemp mkstemp
1086#endif
1087#endif
1088
9b13553b
VK
1089#ifndef _WIN32
1090int strcat_s(char *dst, size_t dstSize, const char *src);
1091int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1092#endif
1093
b50f1100 1094#ifdef __cplusplus
e9246d26 1095}
b50f1100 1096#endif
e9246d26 1097
a7ff20a5
VK
1098
1099//
1100// C++ only finctions
1101//
1102
1103#ifdef __cplusplus
1104
240816b0
VK
1105TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1106String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1107const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1108int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
1109DWORD LIBNETXMS_EXPORTABLE XMLGetAttrDWORD(const char **attrs, const char *name, DWORD defVal);
1110bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1111
1112#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
1113void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
1114 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
1115#endif
1116
1117void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
2dd24569
VK
1118 void (* sendTrap1)(DWORD, const TCHAR *, const char *, va_list),
1119 void (* sendTrap2)(DWORD, const TCHAR *, int, TCHAR **),
53c96e2d
VK
1120 BOOL (* sendFile)(void *, DWORD, const TCHAR *, long),
1121 BOOL (* pushData)(const TCHAR *, const TCHAR *));
a7ff20a5
VK
1122
1123#endif
1124
e9246d26 1125#endif /* _nms_util_h_ */