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