added Deliberant and Synology MIBs
[public/netxms.git] / include / nms_util.h
CommitLineData
cc022855 1/*
7ac71e60 2** NetXMS - Network Management System
6f95ef89 3** Copyright (C) 2003-2015 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
4b47d7d7
VK
48/*** Byte swapping ***/
49#if WORDS_BIGENDIAN
50#define htonq(x) (x)
51#define ntohq(x) (x)
52#define htond(x) (x)
53#define ntohd(x) (x)
54#define SwapWideString(x)
55#else
56#ifdef HAVE_HTONLL
57#define htonq(x) htonll(x)
58#else
59#define htonq(x) __bswap_64(x)
60#endif
61#ifdef HAVE_NTOHLL
62#define ntohq(x) ntohll(x)
63#else
64#define ntohq(x) __bswap_64(x)
65#endif
66#define htond(x) __bswap_double(x)
67#define ntohd(x) __bswap_double(x)
68#define SwapWideString(x) __bswap_wstr(x)
69#endif
6d8aee95 70
4b47d7d7
VK
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75#if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
76UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
77#endif
78#if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
79UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
80#endif
81double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
82void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
6d8aee95 83
4b47d7d7
VK
84#ifdef __cplusplus
85}
86#endif
87
88
89/*** Serial communications ***/
6d8aee95
VK
90#ifdef _WIN32
91
92#define FLOW_NONE 0
93#define FLOW_SOFTWARE 1
94#define FLOW_HARDWARE 2
95
96#else /* _WIN32 */
97
70fed300
AK
98#ifdef HAVE_TERMIOS_H
99# include <termios.h>
100#else
101# error termios.h not found
102#endif
103
6d8aee95
VK
104#endif /* _WIN32 */
105
e2e66145
VK
106/**
107 * Return codes for IcmpPing()
108 */
66c04cc7
VK
109#define ICMP_SUCCESS 0
110#define ICMP_UNREACHEABLE 1
111#define ICMP_TIMEOUT 2
112#define ICMP_RAW_SOCK_FAILED 3
5418d4bb 113#define ICMP_API_ERROR 4
66c04cc7 114
e2e66145
VK
115/**
116 * Token types for configuration loader
117 */
4826c8a5
VK
118#define CT_LONG 0
119#define CT_STRING 1
120#define CT_STRING_LIST 2
121#define CT_END_OF_LIST 3
122#define CT_BOOLEAN 4
123#define CT_WORD 5
fc935a60 124#define CT_IGNORE 6
dda7c270 125#define CT_MB_STRING 7
e95680e5 126#define CT_BOOLEAN64 8
4826c8a5 127
605fc935 128/**
e2e66145
VK
129 * Uninitialized value for override indicator
130 */
131#define NXCONFIG_UNINITIALIZED_VALUE (-1)
132
133/**
605fc935
VK
134 * Return codes for NxLoadConfig()
135 */
4826c8a5
VK
136#define NXCFG_ERR_OK 0
137#define NXCFG_ERR_NOFILE 1
138#define NXCFG_ERR_SYNTAX 2
139
605fc935
VK
140/**
141 * nxlog_open() flags
142 */
4e0e77e6
VK
143#define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
144#define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
145#define NXLOG_BACKGROUND_WRITER ((UINT32)0x00000004)
146#define NXLOG_IS_OPEN ((UINT32)0x80000000)
9f2902ac 147
605fc935
VK
148/**
149 * nxlog rotation policy
150 */
5945d50a
VK
151#define NXLOG_ROTATION_DISABLED 0
152#define NXLOG_ROTATION_DAILY 1
153#define NXLOG_ROTATION_BY_SIZE 2
154
605fc935
VK
155/**
156 * _tcsdup() replacement
157 */
8ff13508
VK
158#if defined(_WIN32) && defined(USE_WIN32_HEAP)
159#ifdef __cplusplus
160extern "C" {
161#endif
fd72502a 162char LIBNETXMS_EXPORTABLE *nx_strdup(const char *src);
8ff13508
VK
163WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
164#ifdef __cplusplus
165}
166#endif
167#endif
168
605fc935 169/**
f4d2c9ba
VK
170 * Custom wcsdup
171 */
172#if !HAVE_WCSDUP && !defined(_WIN32)
fd72502a 173
f4d2c9ba
VK
174#ifdef __cplusplus
175extern "C" {
176#endif
177 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
178#ifdef __cplusplus
179}
180#endif
fd72502a
VK
181
182#elif defined(_AIX)
183
184// Some AIX versions have broken wcsdup() so we use internal implementation
185#ifdef __cplusplus
186extern "C" {
187#endif
188 WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
189#ifdef __cplusplus
190}
191#endif
192
193#define wcsdup nx_wcsdup
194
f4d2c9ba
VK
195#endif
196
c012b7b7
VK
197#ifdef __cplusplus
198
f4d2c9ba 199/**
c83f6449
VK
200 * Extended tcsdup: returns NULL if NULL pointer passed
201 */
202inline TCHAR *_tcsdup_ex(const TCHAR *s)
203{
204 return (s != NULL) ? _tcsdup(s) : NULL;
205}
206
c012b7b7
VK
207#endif
208
c83f6449 209/**
d65c16a8
VK
210 * String conversion helpers
211 */
212#ifdef __cplusplus
213extern "C" {
214#endif
215WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
216WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
217char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
218char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
219#ifdef __cplusplus
220}
221#endif
222
223/**
605fc935
VK
224 * Class for serial communications
225 */
b160350d
VK
226#ifdef __cplusplus
227
228#ifndef _WIN32
229enum
230{
231 NOPARITY,
232 ODDPARITY,
233 EVENPARITY,
234 ONESTOPBIT,
235 TWOSTOPBITS
236};
237
70fed300
AK
238enum
239{
240 FLOW_NONE,
241 FLOW_HARDWARE,
242 FLOW_SOFTWARE
243};
244
b160350d
VK
245#ifndef INVALID_HANDLE_VALUE
246#define INVALID_HANDLE_VALUE (-1)
247#endif
248#endif /* _WIN32 */
249
250class LIBNETXMS_EXPORTABLE Serial
251{
b160350d
VK
252private:
253 TCHAR *m_pszPort;
254 int m_nTimeout;
255 int m_nSpeed;
256 int m_nDataBits;
257 int m_nStopBits;
258 int m_nParity;
70fed300 259 int m_nFlowControl;
b160350d
VK
260
261#ifndef _WIN32
262 int m_hPort;
70fed300 263 struct termios m_originalSettings;
b160350d
VK
264#else
265 HANDLE m_hPort;
266#endif
b160350d 267
826fcc1f
VK
268public:
269 Serial();
270 ~Serial();
271
272 bool open(const TCHAR *pszPort);
273 void close();
274 void setTimeout(int nTimeout);
275 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
276 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
c3f71625 277 int readToMark(char *buff, int size, const char **marks, char **occurence);
826fcc1f
VK
278 bool write(const char *pBuff, int nSize);
279 void flush();
280 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
281 bool restart();
282};
ff214673 283
3fe40613
VK
284/**
285 * Dynamic string class
286 */
8bad8476
VK
287class LIBNETXMS_EXPORTABLE String
288{
289protected:
4e0e77e6
VK
290 TCHAR *m_buffer;
291 size_t m_length;
292 size_t m_allocated;
dcc7a2b0 293 size_t m_allocationStep;
8bad8476
VK
294
295public:
c577e360
VK
296 static const int npos;
297
8bad8476 298 String();
f3cdd1c5 299 String(const TCHAR *init);
643c9dcb 300 String(const String &src);
8bad8476
VK
301 ~String();
302
4e0e77e6
VK
303 TCHAR *getBuffer() { return m_buffer; }
304 void setBuffer(TCHAR *buffer);
8bad8476 305
dcc7a2b0
VK
306 size_t getAllocationStep() { return m_allocationStep; }
307 void setAllocationStep(size_t step) { m_allocationStep = step; }
4e0e77e6
VK
308
309 const String& operator =(const TCHAR *str);
49bf9d8d 310 const String& operator =(const String &src);
1d34c533
VK
311 const String& operator +=(const TCHAR *str);
312 const String& operator +=(const String &str);
4e0e77e6 313 operator const TCHAR*() { return CHECK_NULL_EX(m_buffer); }
8bad8476 314
08b214c6
VK
315 char *getUTF8String();
316
4e0e77e6
VK
317 void append(const TCHAR *str) { if (str != NULL) append(str, _tcslen(str)); }
318 void append(const TCHAR *str, size_t len);
f98a8402 319 void append(const TCHAR c) { append(&c, 1); }
5f648670
VK
320 void append(INT32 n);
321 void append(UINT32 n);
322 void append(INT64 n);
323 void append(UINT64 n);
4e0e77e6
VK
324
325 void appendPreallocated(TCHAR *str) { if (str != NULL) { append(str); free(str); } }
326
327 void appendMBString(const char *str, size_t len, int nCodePage);
328 void appendWideString(const WCHAR *str, size_t len);
c577e360 329
4e0e77e6
VK
330 void appendFormattedString(const TCHAR *format, ...);
331 void appendFormattedStringV(const TCHAR *format, va_list args);
ff175e91 332
4e0e77e6 333 void clear();
c577e360 334
4e0e77e6
VK
335 size_t length() const { return m_length; }
336 bool isEmpty() const { return m_length == 0; }
c577e360 337
4e0e77e6
VK
338 TCHAR *substring(int nStart, int nLen, TCHAR *pszBuffer = NULL);
339 int find(const TCHAR *str, int nStart = 0);
1e1450ef 340
6bcafa2d
VK
341 void escapeCharacter(int ch, int esc);
342 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
0bbab9d3
VK
343 void trim();
344 void shrink(int chars = 1);
c577e360
VK
345};
346
3fe40613 347/**
f9861ff9
VK
348 * Dynamic array class
349 */
350class LIBNETXMS_EXPORTABLE Array
351{
352private:
353 int m_size;
354 int m_allocated;
355 int m_grow;
356 size_t m_elementSize;
357 void **m_data;
358 bool m_objectOwner;
359
360 void internalRemove(int index, bool allowDestruction);
361 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
362
363protected:
364 bool m_storePointers;
365 void (*m_objectDestructor)(void *);
366
367 Array(void *data, int initial, int grow, size_t elementSize);
dcc7a2b0 368 void *__getBuffer() const { return m_data; }
f9861ff9
VK
369
370public:
371 Array(int initial = 0, int grow = 16, bool owner = false);
372 virtual ~Array();
373
374 int add(void *element);
dcc7a2b0
VK
375 void *get(int index) const { return ((index >= 0) && (index < m_size)) ? (m_storePointers ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
376 int indexOf(void *element) const;
f9861ff9
VK
377 void set(int index, void *element);
378 void replace(int index, void *element);
379 void remove(int index) { internalRemove(index, true); }
380 void remove(void *element) { internalRemove(indexOf(element), true); }
381 void unlink(int index) { internalRemove(index, false); }
382 void unlink(void *element) { internalRemove(indexOf(element), false); }
383 void clear();
384 void sort(int (*cb)(const void *, const void *));
dcc7a2b0 385 void *find(const void *key, int (*cb)(const void *, const void *)) const;
f9861ff9 386
dcc7a2b0 387 int size() const { return m_size; }
f9861ff9
VK
388
389 void setOwner(bool owner) { m_objectOwner = owner; }
dcc7a2b0 390 bool isOwner() const { return m_objectOwner; }
f9861ff9
VK
391};
392
393/**
394 * Template class for dynamic array which holds objects
395 */
396template <class T> class ObjectArray : public Array
397{
398private:
399 static void destructor(void *object) { delete (T*)object; }
400
401public:
402 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
403 virtual ~ObjectArray() { }
404
405 int add(T *object) { return Array::add((void *)object); }
dcc7a2b0
VK
406 T *get(int index) const { return (T*)Array::get(index); }
407 int indexOf(T *object) const { return Array::indexOf((void *)object); }
dfed892b 408 bool contains(T *object) const { return indexOf(object) >= 0; }
f9861ff9
VK
409 void set(int index, T *object) { Array::set(index, (void *)object); }
410 void replace(int index, T *object) { Array::replace(index, (void *)object); }
411 void remove(int index) { Array::remove(index); }
412 void remove(T *object) { Array::remove((void *)object); }
413 void unlink(int index) { Array::unlink(index); }
414 void unlink(T *object) { Array::unlink((void *)object); }
415};
416
417/**
418 * Template class for dynamic array which holds scalar values
419 */
420template <class T> class IntegerArray : public Array
421{
422private:
423 static void destructor(void *element) { }
424
425public:
426 IntegerArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; m_storePointers = (sizeof(T) == sizeof(void *)); }
427 virtual ~IntegerArray() { }
428
429 int add(T value) { return Array::add(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
e5390fb5 430 T get(int index) const { if (m_storePointers) return CAST_FROM_POINTER(Array::get(index), T); T *p = (T*)Array::get(index); return (p != NULL) ? *p : 0; }
dcc7a2b0 431 int indexOf(T value) const { return Array::indexOf(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
dfed892b 432 bool contains(T value) const { return indexOf(value) >= 0; }
f9861ff9
VK
433 void set(int index, T value) { Array::set(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
434 void replace(int index, T value) { Array::replace(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
435
dcc7a2b0 436 T *getBuffer() const { return (T*)__getBuffer(); }
f9861ff9
VK
437};
438
439/**
440 * Auxilliary class to hold dynamically allocated array of structures
441 */
442template <class T> class StructArray : public Array
443{
444private:
445 static void destructor(void *element) { }
446
447public:
448 StructArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; }
449 StructArray(T *data, int size) : Array(data, size, 16, sizeof(T)) { m_objectDestructor = destructor; }
450 virtual ~StructArray() { }
451
452 int add(T *element) { return Array::add((void *)element); }
dcc7a2b0
VK
453 T *get(int index) const { return (T*)Array::get(index); }
454 int indexOf(T *element) const { return Array::indexOf((void *)element); }
dfed892b 455 bool contains(T *element) const { return indexOf(element) >= 0; }
f9861ff9
VK
456 void set(int index, T *element) { Array::set(index, (void *)element); }
457 void replace(int index, T *element) { Array::replace(index, (void *)element); }
458 void remove(int index) { Array::remove(index); }
459 void remove(T *element) { Array::remove((void *)element); }
460 void unlink(int index) { Array::unlink(index); }
461 void unlink(T *element) { Array::unlink((void *)element); }
462
dcc7a2b0 463 T *getBuffer() const { return (T*)__getBuffer(); }
f9861ff9
VK
464};
465
466/**
467 * Entry of string map (internal)
400e55c4
VK
468 */
469struct StringMapEntry;
470
471/**
f9861ff9
VK
472 * Key/value pair
473 */
474struct KeyValuePair
475{
476 const TCHAR *key;
477 const void *value;
478};
479
480/**
3fe40613
VK
481 * String maps base class
482 */
483class LIBNETXMS_EXPORTABLE StringMapBase
c577e360
VK
484{
485protected:
400e55c4 486 StringMapEntry *m_data;
3fe40613 487 bool m_objectOwner;
55bdca5a 488 bool m_ignoreCase;
3fe40613 489 void (*m_objectDestructor)(void *);
c577e360 490
400e55c4 491 StringMapEntry *find(const TCHAR *key);
3fe40613
VK
492 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
493 void *getObject(const TCHAR *key);
494 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
c577e360
VK
495
496public:
3fe40613
VK
497 StringMapBase(bool objectOwner);
498 virtual ~StringMapBase();
499
55bdca5a 500 void setOwner(bool owner) { m_objectOwner = owner; }
91a581c5 501 void setIgnoreCase(bool ignore);
55bdca5a 502
3fe40613
VK
503 void remove(const TCHAR *key);
504 void clear();
505
c1a30973 506 int size();
400e55c4
VK
507 bool contains(const TCHAR *key) { return find(key) != NULL; }
508
509 bool forEach(bool (*cb)(const TCHAR *, const void *, void *), void *userData);
beab0421 510 const void *findElement(bool (*comparator)(const TCHAR *, const void *, void *), void *userData);
f9861ff9
VK
511
512 StructArray<KeyValuePair> *toArray();
3fe40613
VK
513};
514
515/**
400e55c4
VK
516 * NXCP message class
517 */
b368969c 518class NXCPMessage;
400e55c4
VK
519
520/**
3fe40613
VK
521 * String map class
522 */
523class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
524{
525public:
526 StringMap() : StringMapBase(true) { }
fb986055 527 StringMap(const StringMap &src);
3fe40613 528 virtual ~StringMap();
c577e360 529
fb986055 530 StringMap& operator =(const StringMap &src);
40b7416c 531
3fe40613
VK
532 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
533 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
967893bb 534 void set(const TCHAR *key, UINT32 value);
5ad2167d 535
a6312bd6
VK
536 void addAll(StringMap *src);
537
3fe40613 538 const TCHAR *get(const TCHAR *key) { return (const TCHAR *)getObject(key); }
967893bb 539 UINT32 getULong(const TCHAR *key, UINT32 defaultValue);
6acb8479 540 bool getBoolean(const TCHAR *key, bool defaultValue);
5ad2167d 541
b368969c 542 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId);
8bad8476
VK
543};
544
3fe40613
VK
545/**
546 * String map template for holding objects as values
547 */
548template <class T> class StringObjectMap : public StringMapBase
549{
550private:
551 static void destructor(void *object) { delete (T*)object; }
6173bea8 552
3fe40613
VK
553public:
554 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
555
556 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
557 T *get(const TCHAR *key) { return (T*)getObject(key); }
3fe40613 558};
6173bea8 559
3fe40613
VK
560/**
561 * String list class
562 */
6173bea8
VK
563class LIBNETXMS_EXPORTABLE StringList
564{
565private:
566 int m_count;
567 int m_allocated;
568 TCHAR **m_values;
569
570public:
571 StringList();
01116597 572 StringList(const StringList *src);
22ee6d97 573 StringList(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
574 ~StringList();
575
576 void add(const TCHAR *value);
577 void addPreallocated(TCHAR *value);
967893bb
VK
578 void add(INT32 value);
579 void add(UINT32 value);
6173bea8 580 void add(INT64 value);
967893bb 581 void add(UINT64 value);
6173bea8 582 void add(double value);
24dc5346 583 void replace(int index, const TCHAR *value);
b0353a33
VK
584 void addOrReplace(int index, const TCHAR *value);
585 void addOrReplacePreallocated(int index, TCHAR *value);
6173bea8 586 void clear();
01116597
VK
587 int size() const { return m_count; }
588 const TCHAR *get(int index) const { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
589 int indexOf(const TCHAR *value) const;
590 int indexOfIgnoreCase(const TCHAR *value) const;
d51f2182 591 void remove(int index);
86c126f5 592 void addAll(const StringList *src);
93dbc1be 593 void merge(const StringList *src, bool matchCase);
7d25a5c5 594 TCHAR *join(const TCHAR *separator);
22ee6d97 595 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
6173bea8
VK
596};
597
3fe40613 598/**
a58501c0
VK
599 * Entry of string set
600 */
601struct StringSetEntry;
602
603/**
f3603b79
VK
604 * NXCP message
605 */
b368969c 606class NXCPMessage;
f3603b79
VK
607
608/**
a58501c0
VK
609 * String set class
610 */
611class LIBNETXMS_EXPORTABLE StringSet
612{
613private:
614 StringSetEntry *m_data;
615
616public:
617 StringSet();
618 ~StringSet();
619
620 void add(const TCHAR *str);
f3603b79 621 void addPreallocated(TCHAR *str);
a58501c0
VK
622 void remove(const TCHAR *str);
623 void clear();
624
625 int size();
f3603b79 626 bool contains(const TCHAR *str);
5b521bfc 627 bool equals(StringSet *s);
a58501c0 628
fa493140 629 void addAll(StringSet *src);
f3603b79 630 void addAll(TCHAR **strings, int count);
d6c4cdc1 631 void addAllPreallocated(TCHAR **strings, int count);
a58501c0 632 void forEach(bool (*cb)(const TCHAR *, void *), void *userData);
f3603b79 633
b368969c
VK
634 void fillMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId);
635 void addAllFromMessage(NXCPMessage *msg, UINT32 baseId, UINT32 countId, bool clearBeforeAdd, bool toUppercase);
c262bc08
VK
636
637 String getAll(const TCHAR *separator);
a58501c0
VK
638};
639
640/**
57f34c0a
VK
641 * Byte stream
642 */
643class LIBNETXMS_EXPORTABLE ByteStream
644{
645private:
646 BYTE *m_data;
647 size_t m_size;
648 size_t m_allocated;
649 size_t m_pos;
650
651public:
652 ByteStream(size_t initial = 8192);
653 ByteStream(const void *data, size_t size);
654 virtual ~ByteStream();
655
4b47d7d7
VK
656 static ByteStream *load(const TCHAR *file);
657
658 void seek(size_t pos) { if (pos <= m_size) m_pos = pos; }
57f34c0a
VK
659 size_t pos() { return m_pos; }
660 size_t size() { return m_size; }
661 bool eos() { return m_pos == m_size; }
662
663 BYTE *buffer(size_t *size) { *size = m_size; return m_data; }
664
4b47d7d7 665 void write(const void *data, size_t size);
57f34c0a
VK
666 void write(char c) { write(&c, 1); }
667 void write(BYTE b) { write(&b, 1); }
668 void write(INT16 n) { UINT16 x = htons((UINT16)n); write(&x, 2); }
669 void write(UINT16 n) { UINT16 x = htons(n); write(&x, 2); }
670 void write(INT32 n) { UINT32 x = htonl((UINT32)n); write(&x, 4); }
671 void write(UINT32 n) { UINT32 x = htonl(n); write(&x, 4); }
4b47d7d7
VK
672 void write(INT64 n) { UINT64 x = htonq((UINT64)n); write(&x, 8); }
673 void write(UINT64 n) { UINT64 x = htonq(n); write(&x, 8); }
674 void write(double n) { double x = htond(n); write(&x, 8); }
675 void writeString(const TCHAR *s);
57f34c0a 676
4b47d7d7 677 size_t read(void *buffer, size_t count);
57f34c0a
VK
678 char readChar() { return !eos() ? (char)m_data[m_pos++] : 0; }
679 BYTE readByte() { return !eos() ? m_data[m_pos++] : 0; }
680 INT16 readInt16();
681 UINT16 readUInt16();
682 INT32 readInt32();
683 UINT32 readUInt32();
4b47d7d7
VK
684 INT64 readInt64();
685 UINT64 readUInt64();
686 double readDouble();
687 TCHAR *readString();
688
689 bool save(int f);
57f34c0a
VK
690};
691
692/**
25fe6265
VK
693 * Auxilliary class for objects which counts references and
694 * destroys itself wheren reference count falls to 0
695 */
7f632dfe
VK
696class LIBNETXMS_EXPORTABLE RefCountObject
697{
698private:
7785322f 699 VolatileCounter m_refCount;
7f632dfe
VK
700
701public:
7785322f
VK
702 RefCountObject()
703 {
704 m_refCount = 1;
705 }
b549a0f8 706 virtual ~RefCountObject();
7785322f
VK
707
708 void incRefCount()
709 {
710 InterlockedIncrement(&m_refCount);
711 }
712
713 void decRefCount()
714 {
715 if (InterlockedDecrement(&m_refCount) == 0)
716 delete this;
717 }
7f632dfe
VK
718};
719
b368969c 720class NXCPMessage;
967893bb
VK
721
722/**
723 * Table column definition
724 */
725class LIBNETXMS_EXPORTABLE TableColumnDefinition
726{
727private:
728 TCHAR *m_name;
729 TCHAR *m_displayName;
730 INT32 m_dataType;
731 bool m_instanceColumn;
732
733public:
734 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
b368969c 735 TableColumnDefinition(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
736 TableColumnDefinition(TableColumnDefinition *src);
737 ~TableColumnDefinition();
738
b368969c 739 void fillMessage(NXCPMessage *msg, UINT32 baseId);
967893bb
VK
740
741 const TCHAR *getName() { return m_name; }
742 const TCHAR *getDisplayName() { return m_displayName; }
743 INT32 getDataType() { return m_dataType; }
744 bool isInstanceColumn() { return m_instanceColumn; }
745
746 void setDataType(INT32 type) { m_dataType = type; }
747 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
a0ddfb29 748 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
967893bb
VK
749};
750
751/**
d6bbfa4e
VK
752 * Table cell
753 */
754class TableCell
755{
756private:
757 TCHAR *m_value;
758 int m_status;
759
760public:
761 TableCell() { m_value = NULL; m_status = -1; }
c83f6449
VK
762 TableCell(const TCHAR *value) { m_value = _tcsdup_ex(value); m_status = -1; }
763 TableCell(const TCHAR *value, int status) { m_value = _tcsdup_ex(value); m_status = status; }
764 TableCell(TableCell *src) { m_value = _tcsdup_ex(src->m_value); m_status = src->m_status; }
d6bbfa4e
VK
765 ~TableCell() { safe_free(m_value); }
766
c83f6449 767 void set(const TCHAR *value, int status) { safe_free(m_value); m_value = _tcsdup_ex(value); m_status = status; }
d6bbfa4e
VK
768 void setPreallocated(TCHAR *value, int status) { safe_free(m_value); m_value = value; m_status = status; }
769
770 const TCHAR *getValue() { return m_value; }
c83f6449 771 void setValue(const TCHAR *value) { safe_free(m_value); m_value = _tcsdup_ex(value); }
d6bbfa4e
VK
772 void setPreallocatedValue(TCHAR *value) { safe_free(m_value); m_value = value; }
773
774 int getStatus() { return m_status; }
775 void setStatus(int status) { m_status = status; }
776};
777
778/**
779 * Table row
780 */
781class TableRow
782{
783private:
784 ObjectArray<TableCell> *m_cells;
785 UINT32 m_objectId;
bac74bf7 786
d6bbfa4e
VK
787public:
788 TableRow(int columnCount);
789 TableRow(TableRow *src);
790 ~TableRow() { delete m_cells; }
791
792 void addColumn() { m_cells->add(new TableCell); }
793 void deleteColumn(int index) { m_cells->remove(index); }
bac74bf7 794
d6bbfa4e
VK
795 void set(int index, const TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->set(value, status); }
796 void setPreallocated(int index, TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocated(value, status); }
797
798 void setValue(int index, const TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setValue(value); }
799 void setPreallocatedValue(int index, TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocatedValue(value); }
800
801 void setStatus(int index, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setStatus(status); }
802
803 const TCHAR *getValue(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getValue() : NULL; }
804 int getStatus(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getStatus() : -1; }
805
806 UINT32 getObjectId() { return m_objectId; }
807 void setObjectId(UINT32 id) { m_objectId = id; }
808};
809
810/**
967893bb
VK
811 * Class for table data storage
812 */
a0ddfb29 813class LIBNETXMS_EXPORTABLE Table : public RefCountObject
967893bb
VK
814{
815private:
d6bbfa4e 816 ObjectArray<TableRow> *m_data;
967893bb
VK
817 ObjectArray<TableColumnDefinition> *m_columns;
818 TCHAR *m_title;
819 int m_source;
d6bbfa4e 820 bool m_extendedFormat;
967893bb 821
b368969c 822 void createFromMessage(NXCPMessage *msg);
967893bb
VK
823 void destroy();
824
825public:
826 Table();
a0ddfb29 827 Table(Table *src);
b368969c 828 Table(NXCPMessage *msg);
b549a0f8 829 virtual ~Table();
967893bb 830
b368969c
VK
831 int fillMessage(NXCPMessage &msg, int offset, int rowLimit);
832 void updateFromMessage(NXCPMessage *msg);
967893bb 833
a0ddfb29
VK
834 void addAll(Table *src);
835 void copyRow(Table *src, int row);
967893bb 836
d6bbfa4e
VK
837 int getNumRows() { return m_data->size(); }
838 int getNumColumns() { return m_columns->size(); }
967893bb
VK
839 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
840 int getSource() { return m_source; }
841
d6bbfa4e
VK
842 bool isExtendedFormat() { return m_extendedFormat; }
843 void setExtendedFormat(bool ext) { m_extendedFormat = ext; }
844
967893bb
VK
845 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
846 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
847 int getColumnIndex(const TCHAR *name);
848 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
849
c83f6449 850 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = _tcsdup_ex(title); }
967893bb 851 void setSource(int source) { m_source = source; }
8dba1eac 852 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
967893bb
VK
853 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
854 int addRow();
855
25076d53
VK
856 void deleteRow(int row);
857 void deleteColumn(int col);
858
967893bb
VK
859 void setAt(int nRow, int nCol, INT32 nData);
860 void setAt(int nRow, int nCol, UINT32 dwData);
861 void setAt(int nRow, int nCol, double dData);
862 void setAt(int nRow, int nCol, INT64 nData);
863 void setAt(int nRow, int nCol, UINT64 qwData);
864 void setAt(int nRow, int nCol, const TCHAR *pszData);
865 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
866
d6bbfa4e
VK
867 void set(int nCol, INT32 nData) { setAt(getNumRows() - 1, nCol, nData); }
868 void set(int nCol, UINT32 dwData) { setAt(getNumRows() - 1, nCol, dwData); }
869 void set(int nCol, double dData) { setAt(getNumRows() - 1, nCol, dData); }
870 void set(int nCol, INT64 nData) { setAt(getNumRows() - 1, nCol, nData); }
871 void set(int nCol, UINT64 qwData) { setAt(getNumRows() - 1, nCol, qwData); }
d65c16a8
VK
872 void set(int nCol, const TCHAR *data) { setAt(getNumRows() - 1, nCol, data); }
873#ifdef UNICODE
874 void set(int nCol, const char *data) { setPreallocatedAt(getNumRows() - 1, nCol, WideStringFromMBString(data)); }
875#else
876 void set(int nCol, const WCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, MBStringFromWideString(data)); }
877#endif
878 void setPreallocated(int nCol, TCHAR *data) { setPreallocatedAt(getNumRows() - 1, nCol, data); }
d6bbfa4e
VK
879
880 void setStatusAt(int row, int col, int status);
881 void setStatus(int col, int status) { setStatusAt(getNumRows() - 1, col, status); }
967893bb
VK
882
883 const TCHAR *getAsString(int nRow, int nCol);
884 INT32 getAsInt(int nRow, int nCol);
885 UINT32 getAsUInt(int nRow, int nCol);
886 INT64 getAsInt64(int nRow, int nCol);
887 UINT64 getAsUInt64(int nRow, int nCol);
888 double getAsDouble(int nRow, int nCol);
22aaa779 889
d6bbfa4e
VK
890 int getStatus(int nRow, int nCol);
891
22aaa779 892 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
a0ddfb29 893 int findRowByInstance(const TCHAR *instance);
d6bbfa4e
VK
894
895 UINT32 getObjectId(int row) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getObjectId() : 0; }
896 void setObjectId(int row, UINT32 id) { TableRow *r = m_data->get(row); if (r != NULL) r->setObjectId(id); }
967893bb
VK
897};
898
25fe6265 899/**
c11eee9b
VK
900 * sockaddr buffer
901 */
902union SockAddrBuffer
903{
904 struct sockaddr_in sa4;
905#ifdef WITH_IPV6
906 struct sockaddr_in6 sa6;
907#endif
908};
909
910/**
911 * sockaddr length calculation
912 */
913#ifdef WITH_IPV6
914#define SA_LEN(sa) (((sa)->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
915#else
916#define SA_LEN(sa) sizeof(struct sockaddr_in)
917#endif
918
919/**
30592fa6
VK
920 * Compare addresses in sockaddr
921 */
922inline bool SocketAddressEquals(struct sockaddr *a1, struct sockaddr *a2)
923{
924 if (a1->sa_family != a2->sa_family)
925 return false;
926 if (a1->sa_family == AF_INET)
927 return ((struct sockaddr_in *)a1)->sin_addr.s_addr == ((struct sockaddr_in *)a2)->sin_addr.s_addr;
928#ifdef WITH_IPV6
929 if (a1->sa_family == AF_INET6)
930 return !memcmp(((struct sockaddr_in6 *)a1)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)a2)->sin6_addr.s6_addr, 16);
931#endif
932 return false;
933}
934
935/**
9319c166
VK
936 * IP address
937 */
938class LIBNETXMS_EXPORTABLE InetAddress
939{
9319c166 940private:
8c75ad41
VK
941 short m_maskBits;
942 short m_family;
943 union
944 {
945 UINT32 v4;
946 BYTE v6[16];
947 } m_addr;
9319c166
VK
948
949public:
8c75ad41
VK
950 InetAddress();
951 InetAddress(UINT32 addr);
c75e9ee4 952 InetAddress(UINT32 addr, UINT32 mask);
4203dcb5 953 InetAddress(const BYTE *addr, int maskBits = 128);
9319c166 954
8c75ad41
VK
955 bool isAnyLocal() const;
956 bool isLoopback() const;
957 bool isMulticast() const;
958 bool isBroadcast() const;
4203dcb5 959 bool isLinkLocal() const;
8c75ad41 960 bool isValid() const { return m_family != AF_UNSPEC; }
4203dcb5 961 bool isValidUnicast() const { return isValid() && !isAnyLocal() && !isLoopback() && !isMulticast() && !isBroadcast() && !isLinkLocal(); }
9319c166 962
8c75ad41 963 int getFamily() const { return m_family; }
c7853753
VK
964 UINT32 getAddressV4() const { return (m_family == AF_INET) ? m_addr.v4 : 0; }
965 const BYTE *getAddressV6() const { return (m_family == AF_INET6) ? m_addr.v6 : (const BYTE *)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; }
9319c166 966
8c75ad41 967 bool contain(const InetAddress &a) const;
43709a0c 968 bool sameSubnet(const InetAddress &a) const;
8c75ad41
VK
969 bool equals(const InetAddress &a) const;
970 int compareTo(const InetAddress &a) const;
9319c166 971
8c75ad41
VK
972 void setMaskBits(int m) { m_maskBits = m; }
973 int getMaskBits() const { return m_maskBits; }
c75e9ee4 974 int getHostBits() const { return (m_family == AF_INET) ? (32 - m_maskBits) : (128 - m_maskBits); }
9319c166 975
463b1d34
VK
976 InetAddress getSubnetAddress() const;
977 bool isSubnetBroadcast(int maskBits) const;
ffabd7f8 978
8c75ad41
VK
979 String toString() const;
980 TCHAR *toString(TCHAR *buffer) const;
9319c166 981
43709a0c 982 BYTE *buildHashKey(BYTE *key) const;
f3b94fc5 983
43709a0c
VK
984 TCHAR *getHostByAddr(TCHAR *buffer, size_t buflen) const;
985
986 struct sockaddr *fillSockAddr(SockAddrBuffer *buffer, UINT16 port = 0) const;
c11eee9b 987
c7853753
VK
988 static InetAddress resolveHostName(const WCHAR *hostname, int af = AF_INET);
989 static InetAddress resolveHostName(const char *hostname, int af = AF_INET);
f3b94fc5
VK
990 static InetAddress parse(const WCHAR *str);
991 static InetAddress parse(const char *str);
8c75ad41 992 static InetAddress createFromSockaddr(struct sockaddr *s);
dcc7a2b0
VK
993
994 static const InetAddress INVALID;
48313f37 995 static const InetAddress LOOPBACK;
dcc7a2b0
VK
996};
997
998/**
999 * IP address list
1000 */
1001class LIBNETXMS_EXPORTABLE InetAddressList
1002{
1003private:
1004 ObjectArray<InetAddress> *m_list;
1005
1006 int indexOf(const InetAddress& addr) const;
1007
1008public:
1009 InetAddressList();
1010 ~InetAddressList();
1011
1012 void add(const InetAddress& addr);
c30c0c0f
VK
1013 void add(const InetAddressList& addrList);
1014 void replace(const InetAddress& addr);
dcc7a2b0 1015 void remove(const InetAddress& addr);
c30c0c0f 1016 void clear() { m_list->clear(); }
4203dcb5 1017 const InetAddress& get(int index) const { const InetAddress *a = m_list->get(index); return (a != NULL) ? *a : InetAddress::INVALID; }
dcc7a2b0
VK
1018
1019 int size() const { return m_list->size(); }
1020 bool hasAddress(const InetAddress& addr) const { return indexOf(addr) != -1; }
4203dcb5 1021 const InetAddress& findAddress(const InetAddress& addr) const { int idx = indexOf(addr); return (idx != -1) ? *m_list->get(idx) : InetAddress::INVALID; }
c34b611c 1022 const InetAddress& findSameSubnetAddress(const InetAddress& addr) const;
dcc7a2b0 1023 const InetAddress& getFirstUnicastAddress() const;
c34b611c
VK
1024 const InetAddress& getFirstUnicastAddressV4() const;
1025 bool hasValidUnicastAddress() const { return getFirstUnicastAddress().isValid(); }
c30c0c0f 1026 bool isLoopbackOnly() const;
dcc7a2b0
VK
1027
1028 const ObjectArray<InetAddress> *getList() const { return m_list; }
40c8f341
VK
1029
1030 void fillMessage(NXCPMessage *msg, UINT32 sizeFieldId, UINT32 baseFieldId) const;
9319c166
VK
1031};
1032
1033/**
25fe6265
VK
1034 * Network connection
1035 */
58ee2f43
VK
1036class LIBNETXMS_EXPORTABLE SocketConnection
1037{
1038protected:
1039 SOCKET m_socket;
1040 char m_data[4096];
1041 int m_dataPos;
1042
1043public:
1044 SocketConnection();
1045 virtual ~SocketConnection();
1046
967893bb
VK
1047 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1048 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
58ee2f43
VK
1049 void disconnect();
1050
967893bb
VK
1051 bool canRead(UINT32 timeout);
1052 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
58ee2f43 1053 bool waitForText(const char *text, int timeout);
cc022855 1054
58ee2f43 1055 int write(const char *pBuff, int nSize);
1671f85d
VK
1056 bool writeLine(const char *line);
1057
967893bb 1058 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
58ee2f43
VK
1059};
1060
030a0779
VK
1061/**
1062 * Telnet connection - handles all telnet negotiation
1063 */
def5b792
AK
1064class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
1065{
030a0779 1066protected:
967893bb
VK
1067 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
1068 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
030a0779 1069
def5b792 1070public:
967893bb 1071 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
d4e9d325 1072
967893bb
VK
1073 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
1074 bool connect(UINT32 ip, WORD port, UINT32 timeout);
1075 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
1076 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
def5b792
AK
1077};
1078
56fa1092
VK
1079/**
1080 * Postal address representation
1081 */
1082class LIBNETXMS_EXPORTABLE PostalAddress
1083{
1084private:
1085 TCHAR *m_country;
1086 TCHAR *m_city;
1087 TCHAR *m_streetAddress;
1088 TCHAR *m_postcode;
1089
1090public:
1091 PostalAddress();
1092 PostalAddress(const TCHAR *country, const TCHAR *city, const TCHAR *streetAddress, const TCHAR *postcode);
1093 ~PostalAddress();
1094
1095 const TCHAR *getCountry() { return CHECK_NULL_EX(m_country); }
1096 const TCHAR *getCity() { return CHECK_NULL_EX(m_city); }
1097 const TCHAR *getStreetAddress() { return CHECK_NULL_EX(m_streetAddress); }
1098 const TCHAR *getPostCode() { return CHECK_NULL_EX(m_postcode); }
1099
1100 void setCountry(const TCHAR *country) { safe_free(m_country); m_country = _tcsdup_ex(country); }
1101 void setCity(const TCHAR *city) { safe_free(m_city); m_city = _tcsdup_ex(city); }
1102 void setStreetAddress(const TCHAR *streetAddress) { safe_free(m_streetAddress); m_streetAddress = _tcsdup_ex(streetAddress); }
1103 void setPostCode(const TCHAR *postcode) { safe_free(m_postcode); m_postcode = _tcsdup_ex(postcode); }
1104};
1105
b160350d
VK
1106#endif /* __cplusplus */
1107
030a0779
VK
1108/**
1109 * Configuration item template for configuration loader
1110 */
4826c8a5
VK
1111typedef struct
1112{
e2e66145
VK
1113 TCHAR token[64];
1114 BYTE type;
1115 BYTE separator; // Separator character for lists
1116 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
e95680e5 1117 UINT64 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
e2e66145
VK
1118 UINT32 bufferPos; // Should be set to 0
1119 void *buffer;
1120 void *overrideIndicator;
4826c8a5 1121} NX_CFG_TEMPLATE;
dcb44c9a 1122
030a0779
VK
1123/**
1124 * Code translation structure
1125 */
1b56f3c5 1126typedef struct __CODE_TO_TEXT
bc858635
VK
1127{
1128 int code;
1129 const TCHAR *text;
1130} CODE_TO_TEXT;
1131
e2e66145
VK
1132/**
1133 * getopt() prototype if needed
1134 */
df9812a9 1135#if USE_BUNDLED_GETOPT
99e50bb7 1136#include <netxms_getopt.h>
3046f9a4
VK
1137#endif
1138
1139
1140//
f2fdf1b5
VK
1141// Win32 API functions missing under WinCE
1142//
1143
c32b805a 1144#if defined(UNDER_CE) && defined(__cplusplus)
f2fdf1b5
VK
1145
1146inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
1147{
1148 SYSTEMTIME sysTime;
cc022855 1149
f2fdf1b5
VK
1150 GetSystemTime(&sysTime);
1151 SystemTimeToFileTime(&sysTime, pFt);
1152}
1153
1154#endif // UNDER_CE
1155
1156
1157//
3d1cb7d6
VK
1158// Structures for opendir() / readdir() / closedir()
1159//
1160
1161#ifdef _WIN32
1162
7f9e0c00 1163typedef struct dirent
3d1cb7d6
VK
1164{
1165 long d_ino; /* inode number (not used by MS-DOS) */
1166 int d_namlen; /* Name length */
1167 char d_name[257]; /* file name */
1168} _DIRECT;
1169
1170typedef struct _dir_struc
1171{
1172 char *start; /* Starting position */
1173 char *curr; /* Current position */
1174 long size; /* Size of string table */
1175 long nfiles; /* number if filenames in table */
7f9e0c00 1176 struct dirent dirstr; /* Directory structure to return */
3d1cb7d6
VK
1177} DIR;
1178
5ea2db30
VK
1179#ifdef UNICODE
1180
1181typedef struct dirent_w
1182{
1183 long d_ino; /* inode number (not used by MS-DOS) */
1184 int d_namlen; /* Name length */
1185 WCHAR d_name[257]; /* file name */
1186} _DIRECTW;
1187
1188typedef struct _dir_struc_w
1189{
1190 WCHAR *start; /* Starting position */
1191 WCHAR *curr; /* Current position */
1192 long size; /* Size of string table */
1193 long nfiles; /* number if filenames in table */
1194 struct dirent_w dirstr; /* Directory structure to return */
1195} DIRW;
1196
1197#define _TDIR DIRW
1198#define _TDIRECT _DIRECTW
1199#define _tdirent dirent_w
1200
1201#else
1202
1203#define _TDIR DIR
1204#define _TDIRECT _DIRECT
1205#define _tdirent dirent
1206
37d62364
AK
1207#endif
1208
76b1d3d8
VK
1209#else /* not _WIN32 */
1210
1211typedef struct dirent_w
1212{
1213 long d_ino; /* inode number */
1214 WCHAR d_name[257]; /* file name */
1215} _DIRECTW;
1216
1217typedef struct _dir_struc_w
1218{
1219 DIR *dir; /* Original non-unicode structure */
1220 struct dirent_w dirstr; /* Directory structure to return */
1221} DIRW;
1222
3d1cb7d6
VK
1223#endif /* _WIN32 */
1224
1225
1226//
e9246d26
VK
1227// Functions
1228//
1229
55b33d4c
VK
1230#ifdef UNDER_CE
1231#define close(x) CloseHandle((HANDLE)(x))
1232#endif
1233
b50f1100 1234#ifdef __cplusplus
18c575eb 1235
2e590d1a
VK
1236inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
1237{
912b994d 1238#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1239 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
912b994d 1240#else
2e590d1a
VK
1241 _tcsncpy(pszDest, pszSrc, nLen - 1);
1242 pszDest[nLen - 1] = 0;
912b994d 1243#endif
2e590d1a
VK
1244 return pszDest;
1245}
f3387429
VK
1246
1247#ifdef UNICODE
2e590d1a
VK
1248inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
1249{
f3387429 1250#if defined(_WIN32) && (_MSC_VER >= 1400)
2e590d1a 1251 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
f3387429 1252#else
2e590d1a
VK
1253 strncpy(pszDest, pszSrc, nLen - 1);
1254 pszDest[nLen - 1] = 0;
c81979bc 1255#endif
2e590d1a
VK
1256 return pszDest;
1257}
c81979bc 1258#else
f3387429 1259#define nx_strncpy_mb nx_strncpy
7b8b337e
VK
1260#endif
1261
967893bb 1262int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
5e60b759
VK
1263int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
1264int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
18c575eb
VK
1265bool LIBNETXMS_EXPORTABLE RecvAll(SOCKET s, void *buffer, size_t size, UINT32 timeout);
1266
b0608204 1267#endif /* __cplusplus */
c81979bc
VK
1268
1269#ifdef __cplusplus
e9246d26
VK
1270extern "C"
1271{
b50f1100 1272#endif
735bac5c 1273
076a55c4 1274#if !defined(_WIN32) && !defined(_NETWARE)
8b86c5dc 1275#if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
735bac5c
VK
1276void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
1277#define wcsupr __wcsupr
cc022855 1278#endif
735bac5c
VK
1279void LIBNETXMS_EXPORTABLE __strupr(char *in);
1280#define strupr __strupr
7e679c4b 1281#endif
cc022855 1282
735bac5c 1283void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
b8a8d064
VK
1284 int (*compare)(const void *, const void *, void *));
1285
735bac5c 1286INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
d35ed0aa 1287
735bac5c
VK
1288UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
1289UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
d35ed0aa
VK
1290#ifdef UNICODE
1291#define FileSize FileSizeW
1292#else
1293#define FileSize FileSizeA
1294#endif
670c7f3d 1295
735bac5c
VK
1296int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1297TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
e0f99bf0 1298#ifdef UNICODE
735bac5c 1299char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
17b35ccc
VK
1300#else
1301#define IpToStrA IpToStr
e0f99bf0 1302#endif
8c75ad41 1303TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(const BYTE *addr, TCHAR *buffer);
735bac5c 1304TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
60557d06 1305
735bac5c
VK
1306UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *name);
1307UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameW(const WCHAR *name);
08b214c6 1308#ifdef UNICODE
cfe20cce 1309#define ResolveHostName ResolveHostNameW
08b214c6 1310#else
cfe20cce 1311#define ResolveHostName ResolveHostNameA
08b214c6 1312#endif
a1dc75b0 1313
735bac5c
VK
1314void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1315void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
31521248 1316
735bac5c
VK
1317WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *data, size_t size, WCHAR *pStr);
1318char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *data, size_t size, char *pStr);
d7970ee7
VK
1319#ifdef UNICODE
1320#define BinToStr BinToStrW
1321#else
1322#define BinToStr BinToStrA
1323#endif
1324
735bac5c
VK
1325size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1326size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
52db3ed1
VK
1327#ifdef UNICODE
1328#define StrToBin StrToBinW
1329#else
1330#define StrToBin StrToBinA
1331#endif
cc022855 1332
735bac5c 1333TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
b50f1100 1334
735bac5c
VK
1335void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1336void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
b07c50cc
VK
1337#ifdef UNICODE
1338#define StrStrip StrStripW
1339#else
1340#define StrStrip StrStripA
1341#endif
1342
735bac5c
VK
1343const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
1344const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
ada97259
VK
1345#ifdef UNICODE
1346#define ExtractWord ExtractWordW
1347#else
1348#define ExtractWord ExtractWordA
1349#endif
1350
735bac5c
VK
1351int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1352int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
3583a400
VK
1353#ifdef UNICODE
1354#define NumChars NumCharsW
1355#else
1356#define NumChars NumCharsA
1357#endif
ada97259 1358
735bac5c
VK
1359void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1360void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
35f836fe
VK
1361#ifdef UNICODE
1362#define RemoveTrailingCRLF RemoveTrailingCRLFW
1363#else
1364#define RemoveTrailingCRLF RemoveTrailingCRLFA
1365#endif
1366
4893b4aa
VK
1367BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *str, const char *expr, bool matchCase);
1368BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *str, const WCHAR *expr, bool matchCase);
cafe592d
VK
1369#ifdef UNICODE
1370#define RegexpMatch RegexpMatchW
1371#else
1372#define RegexpMatch RegexpMatchA
1373#endif
1374
735bac5c 1375const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
d77c193c 1376BOOL LIBNETXMS_EXPORTABLE CreateFolder(const TCHAR *directory);
818f8c94 1377TCHAR LIBNETXMS_EXPORTABLE *Trim(TCHAR *str);
735bac5c
VK
1378bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
1379TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
ada97259 1380
f7794b68 1381#ifdef __cplusplus
735bac5c 1382BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
f7794b68 1383#endif
735bac5c
VK
1384BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
1385/* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1386const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
1387void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
1388BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
08b214c6 1389#ifdef UNICODE
735bac5c 1390BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
08b214c6
VK
1391#else
1392#define LoadFileA LoadFile
1393#endif
1394
735bac5c
VK
1395UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1396void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
1397void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
1398void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
1399void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
1400BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1401BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
1402BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
66c04cc7 1403
735bac5c
VK
1404void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1405void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
4e2debcc 1406
735bac5c 1407BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
6b1282e7 1408
735bac5c 1409int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
aa13cfd9 1410
735bac5c
VK
1411HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
1412void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
1413void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
aa13cfd9 1414
735bac5c
VK
1415bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1416bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1417long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
289d126b 1418
735bac5c
VK
1419bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1420bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1421long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
289d126b
VK
1422
1423#ifdef UNICODE
1424#define ExtractNamedOptionValue ExtractNamedOptionValueW
1425#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1426#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1427#else
1428#define ExtractNamedOptionValue ExtractNamedOptionValueA
1429#define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1430#define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1431#endif
ae9cddaf 1432
bc858635 1433#ifdef __cplusplus
735bac5c 1434const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
bc858635 1435#else
735bac5c 1436const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
bc858635
VK
1437#endif
1438
aa13cfd9 1439#ifdef _WIN32
735bac5c
VK
1440TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
1441BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
1442INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
aa13cfd9 1443#endif
696fc54f 1444
de4c0511
VK
1445#if !HAVE_DAEMON || !HAVE_DECL_DAEMON
1446int LIBNETXMS_EXPORTABLE __daemon(int nochdir, int noclose);
1447#define daemon __daemon
696fc54f 1448#endif
4332e9d8 1449
735bac5c 1450UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
268774a0 1451
4332e9d8 1452#ifndef _WIN32
735bac5c
VK
1453BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
1454int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
1455 int cchWideChar, char *pByteStr, int cchByteChar,
1456 char *pDefaultChar, BOOL *pbUsedDefChar);
1457int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
1458 int cchByteChar, WCHAR *pWideCharStr,
1459 int cchWideChar);
04dbaf62 1460
6b93c954 1461#if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
735bac5c 1462int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
6b93c954
VK
1463#endif
1464#if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
735bac5c 1465UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
6b93c954
VK
1466#endif
1467#if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
b6ec31bb 1468UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strdup(const UCS2CHAR *pStr);
4332e9d8 1469#endif
8b86c5dc 1470
735bac5c
VK
1471size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1472size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
1473UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1474char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
8b86c5dc 1475
735bac5c
VK
1476int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1477int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1478int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1479int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1480int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1481int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
04dbaf62 1482
735bac5c
VK
1483int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1484int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
1485int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
1486int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1487int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
1488int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
25575d38 1489
3e39fcde 1490#endif /* _WIN32 */
8b86c5dc 1491
3d1058ed 1492#ifdef _WITH_ENCRYPTION
735bac5c 1493WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
3d1058ed
VK
1494#endif
1495
8b86c5dc 1496#ifdef UNICODE_UCS4
735bac5c
VK
1497size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1498size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1499size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1500UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1501WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
b50f1100 1502#endif
8b86c5dc 1503
ebe98224
VK
1504size_t LIBNETXMS_EXPORTABLE utf8_to_mb(const char *src, int srcLen, char *dst, int dstLen);
1505size_t LIBNETXMS_EXPORTABLE mb_to_utf8(const char *src, int srcLen, char *dst, int dstLen);
1506char LIBNETXMS_EXPORTABLE *MBStringFromUTF8String(const char *s);
1507char LIBNETXMS_EXPORTABLE *UTF8StringFromMBString(const char *s);
1508
842c22f4 1509#if !defined(_WIN32) && !HAVE_WSTAT
735bac5c 1510int wstat(const WCHAR *_path, struct stat *_sbuf);
842c22f4
AK
1511#endif
1512
ef856d02 1513#if defined(UNICODE) && !defined(_WIN32)
d9b66a56 1514
f3387429 1515#if !HAVE_WPOPEN
735bac5c 1516FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
f3387429 1517#endif
8b86c5dc 1518#if !HAVE_WFOPEN
735bac5c 1519FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
8b86c5dc 1520#endif
8c2489aa 1521#if HAVE_FOPEN64 && !HAVE_WFOPEN64
735bac5c 1522FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
8c2489aa 1523#endif
8b86c5dc 1524#if !HAVE_WOPEN
735bac5c 1525int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
b50f1100 1526#endif
f3387429 1527#if !HAVE_WCHMOD
735bac5c 1528int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
f3387429 1529#endif
b07c50cc 1530#if !HAVE_WCHDIR
735bac5c 1531int wchdir(const WCHAR *_path);
b07c50cc
VK
1532#endif
1533#if !HAVE_WMKDIR
735bac5c 1534int wmkdir(const WCHAR *_path, int mode);
b07c50cc
VK
1535#endif
1536#if !HAVE_WRMDIR
735bac5c 1537int wrmdir(const WCHAR *_path);
b07c50cc 1538#endif
ccc34207 1539#if !HAVE_WRENAME
735bac5c 1540int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
ccc34207
VK
1541#endif
1542#if !HAVE_WUNLINK
735bac5c 1543int wunlink(const WCHAR *_path);
ccc34207 1544#endif
b07c50cc 1545#if !HAVE_WREMOVE
735bac5c 1546int wremove(const WCHAR *_path);
b07c50cc
VK
1547#endif
1548#if !HAVE_WSYSTEM
735bac5c 1549int wsystem(const WCHAR *_cmd);
b07c50cc 1550#endif
dda7c270 1551#if !HAVE_WMKSTEMP
735bac5c 1552int wmkstemp(WCHAR *_template);
dda7c270 1553#endif
b07c50cc 1554#if !HAVE_WACCESS
735bac5c 1555int waccess(const WCHAR *_path, int mode);
b07c50cc 1556#endif
273da39a 1557#if !HAVE_WGETENV
735bac5c 1558WCHAR *wgetenv(const WCHAR *_string);
273da39a 1559#endif
f3387429 1560#if !HAVE_WCTIME
735bac5c 1561WCHAR *wctime(const time_t *timep);
f3387429 1562#endif
0186b5da 1563#if !HAVE_PUTWS
735bac5c 1564int putws(const WCHAR *s);
0186b5da 1565#endif
d49d7500 1566#if !HAVE_WCSERROR && HAVE_STRERROR
735bac5c 1567WCHAR *wcserror(int errnum);
d49d7500
VK
1568#endif
1569#if !HAVE_WCSERROR_R && HAVE_STRERROR_R
b0823d06 1570#if HAVE_POSIX_STRERROR_R
735bac5c 1571int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
b0823d06 1572#else
735bac5c 1573WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
d49d7500 1574#endif
b0823d06 1575#endif
e05f8aaf 1576
ef856d02 1577#endif /* UNICODE && !_WIN32*/
e05f8aaf 1578
8b86c5dc 1579#if !HAVE_STRTOLL
735bac5c 1580INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
8b86c5dc
VK
1581#endif
1582#if !HAVE_STRTOULL
735bac5c 1583UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
8b86c5dc 1584#endif
e05f8aaf
VK
1585
1586#if !HAVE_WCSTOLL
735bac5c 1587INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf
VK
1588#endif
1589#if !HAVE_WCSTOULL
735bac5c 1590UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
e05f8aaf 1591#endif
b50f1100 1592
2f56be29 1593#if !HAVE_WCSLWR && !defined(_WIN32)
735bac5c 1594WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
fdf1ed50
VK
1595#endif
1596
4d978b4a 1597#if !HAVE_WCSCASECMP && !defined(_WIN32)
735bac5c 1598int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
4d978b4a
VK
1599#endif
1600
f4d2c9ba 1601#if !HAVE_WCSNCASECMP && !defined(_WIN32)
735bac5c 1602int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
f4d2c9ba
VK
1603#endif
1604
9138dfe2
VK
1605#ifndef _WIN32
1606
1607#if HAVE_ITOA && !HAVE__ITOA
1608#define _itoa itoa
1609#undef HAVE__ITOA
1610#define HAVE__ITOA 1
1611#endif
1612#if !HAVE__ITOA && !defined(_WIN32)
1613char LIBNETXMS_EXPORTABLE *_itoa(int value, char *str, int base);
1614#endif
1615
1616#if HAVE_ITOW && !HAVE__ITOW
1617#define _itow itow
1618#undef HAVE__ITOW
1619#define HAVE__ITOW 1
1620#endif
1621#if !HAVE__ITOW && !defined(_WIN32)
1622WCHAR LIBNETXMS_EXPORTABLE *_itow(int value, WCHAR *str, int base);
1623#endif
1624
1625#endif /* _WIN32 */
1626
3d1cb7d6 1627#ifdef _WIN32
5ea2db30 1628#ifdef UNICODE
735bac5c
VK
1629DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1630struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1631int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
5ea2db30
VK
1632
1633#define _topendir wopendir
1634#define _treaddir wreaddir
1635#define _tclosedir wclosedir
1636#else
1637#define _topendir opendir
1638#define _treaddir readdir
1639#define _tclosedir closedir
1640#endif
1641
735bac5c
VK
1642DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
1643struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
1644int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
5ea2db30 1645
76b1d3d8
VK
1646#else /* not _WIN32 */
1647
735bac5c
VK
1648DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1649struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1650int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
76b1d3d8 1651
37d62364 1652#endif
3d1cb7d6 1653
7f9e0c00 1654#if defined(_WIN32) || !(HAVE_SCANDIR)
735bac5c
VK
1655int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1656 int (*select)(const struct dirent *),
1657 int (*compar)(const struct dirent **, const struct dirent **));
1658int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
7f9e0c00
VK
1659#endif
1660
3bf0df9f
VK
1661TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
1662
55b33d4c 1663#ifdef UNDER_CE
3bf0df9f
VK
1664int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1665int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1666int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
55b33d4c
VK
1667#endif
1668
967893bb 1669BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
d49d7500 1670 unsigned int msgCount, const TCHAR **messages);
9f2902ac
VK
1671void LIBNETXMS_EXPORTABLE nxlog_close(void);
1672void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
5945d50a 1673BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
4addc3a3 1674BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
9113e749
VK
1675
1676typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1677void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
f669df41
VK
1678
1679void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
6e383343
VK
1680void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
1681#if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1682 __attribute__ ((format(printf, 1, 2)))
1683#endif
1684;
9f2902ac 1685
37a7a862
VK
1686#ifdef _WIN32
1687int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1688int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1689#ifdef UNICODE
1690#define _tmkstemp wmkstemp
1691#else
1692#define _tmkstemp mkstemp
1693#endif
1694#endif
1695
9b13553b
VK
1696#ifndef _WIN32
1697int strcat_s(char *dst, size_t dstSize, const char *src);
1698int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1699#endif
1700
c57d020a
VK
1701#if !HAVE_STRPTIME
1702char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
1703#endif
1704
1705#if !HAVE_TIMEGM
1706time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
1707#endif
1708
6f95ef89
VK
1709#if !HAVE_INET_PTON
1710int LIBNETXMS_EXPORTABLE nx_inet_pton(int af, const char *src, void *dst);
1711#define inet_pton nx_inet_pton
1712#endif
1713
b06ae508
VK
1714int LIBNETXMS_EXPORTABLE GetSleepTime(int hour, int minute, int second);
1715
b50f1100 1716#ifdef __cplusplus
e9246d26 1717}
b50f1100 1718#endif
e9246d26 1719
a7ff20a5
VK
1720
1721//
b06ae508 1722// C++ only functions
a7ff20a5
VK
1723//
1724
1725#ifdef __cplusplus
1726
43709a0c
VK
1727UINT32 LIBNETXMS_EXPORTABLE IcmpPing(const InetAddress& addr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
1728
240816b0
VK
1729TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1730String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
53c96e2d
VK
1731const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1732int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
967893bb 1733UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
53c96e2d
VK
1734bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1735
1736#if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
9113e749 1737void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
53c96e2d
VK
1738#endif
1739
a7ff20a5
VK
1740#endif
1741
e9246d26 1742#endif /* _nms_util_h_ */