fixed conflicts with Cortex headers
[public/netxms.git] / include / nms_util.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2013 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
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
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 **
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nms_util.h
20 **
21 **/
22
23 #ifndef _nms_util_h_
24 #define _nms_util_h_
25
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
37 #include <nms_common.h>
38 #include <nms_cscp.h>
39 #include <nms_threads.h>
40 #include <time.h>
41
42 #if HAVE_BYTESWAP_H
43 #include <byteswap.h>
44 #endif
45
46 #include <base64.h>
47
48
49 //
50 // Serial communications
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
61 #ifdef HAVE_TERMIOS_H
62 # include <termios.h>
63 #else
64 # error termios.h not found
65 #endif
66
67 #endif /* _WIN32 */
68
69 /**
70 * Return codes for IcmpPing()
71 */
72 #define ICMP_SUCCESS 0
73 #define ICMP_UNREACHEABLE 1
74 #define ICMP_TIMEOUT 2
75 #define ICMP_RAW_SOCK_FAILED 3
76 #define ICMP_API_ERROR 4
77
78 /**
79 * Token types for configuration loader
80 */
81 #define CT_LONG 0
82 #define CT_STRING 1
83 #define CT_STRING_LIST 2
84 #define CT_END_OF_LIST 3
85 #define CT_BOOLEAN 4
86 #define CT_WORD 5
87 #define CT_IGNORE 6
88 #define CT_MB_STRING 7
89
90 /**
91 * Uninitialized value for override indicator
92 */
93 #define NXCONFIG_UNINITIALIZED_VALUE (-1)
94
95 /**
96 * Return codes for NxLoadConfig()
97 */
98 #define NXCFG_ERR_OK 0
99 #define NXCFG_ERR_NOFILE 1
100 #define NXCFG_ERR_SYNTAX 2
101
102 /**
103 * nxlog_open() flags
104 */
105 #define NXLOG_USE_SYSLOG ((UINT32)0x00000001)
106 #define NXLOG_PRINT_TO_STDOUT ((UINT32)0x00000002)
107 #define NXLOG_IS_OPEN ((UINT32)0x80000000)
108
109 /**
110 * nxlog rotation policy
111 */
112 #define NXLOG_ROTATION_DISABLED 0
113 #define NXLOG_ROTATION_DAILY 1
114 #define NXLOG_ROTATION_BY_SIZE 2
115
116 /**
117 * _tcsdup() replacement
118 */
119 #if defined(_WIN32) && defined(USE_WIN32_HEAP)
120 #ifdef __cplusplus
121 extern "C" {
122 #endif
123 char LIBNETXMS_EXPORTABLE *nx_strdup(const char *src);
124 WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
125 #ifdef __cplusplus
126 }
127 #endif
128 #endif
129
130 /**
131 * Custom wcsdup
132 */
133 #if !HAVE_WCSDUP && !defined(_WIN32)
134
135 #ifdef __cplusplus
136 extern "C" {
137 #endif
138 WCHAR LIBNETXMS_EXPORTABLE *wcsdup(const WCHAR *src);
139 #ifdef __cplusplus
140 }
141 #endif
142
143 #elif defined(_AIX)
144
145 // Some AIX versions have broken wcsdup() so we use internal implementation
146 #ifdef __cplusplus
147 extern "C" {
148 #endif
149 WCHAR LIBNETXMS_EXPORTABLE *nx_wcsdup(const WCHAR *src);
150 #ifdef __cplusplus
151 }
152 #endif
153
154 #define wcsdup nx_wcsdup
155
156 #endif
157
158 #ifdef __cplusplus
159
160 /**
161 * Extended tcsdup: returns NULL if NULL pointer passed
162 */
163 inline TCHAR *_tcsdup_ex(const TCHAR *s)
164 {
165 return (s != NULL) ? _tcsdup(s) : NULL;
166 }
167
168 #endif
169
170 /**
171 * Class for serial communications
172 */
173 #ifdef __cplusplus
174
175 #ifndef _WIN32
176 enum
177 {
178 NOPARITY,
179 ODDPARITY,
180 EVENPARITY,
181 ONESTOPBIT,
182 TWOSTOPBITS
183 };
184
185 enum
186 {
187 FLOW_NONE,
188 FLOW_HARDWARE,
189 FLOW_SOFTWARE
190 };
191
192 #ifndef INVALID_HANDLE_VALUE
193 #define INVALID_HANDLE_VALUE (-1)
194 #endif
195 #endif /* _WIN32 */
196
197 class LIBNETXMS_EXPORTABLE Serial
198 {
199 private:
200 TCHAR *m_pszPort;
201 int m_nTimeout;
202 int m_nSpeed;
203 int m_nDataBits;
204 int m_nStopBits;
205 int m_nParity;
206 int m_nFlowControl;
207
208 #ifndef _WIN32
209 int m_hPort;
210 struct termios m_originalSettings;
211 #else
212 HANDLE m_hPort;
213 #endif
214
215 public:
216 Serial();
217 ~Serial();
218
219 bool open(const TCHAR *pszPort);
220 void close();
221 void setTimeout(int nTimeout);
222 int read(char *pBuff, int nSize); /* waits up to timeout and do single read */
223 int readAll(char *pBuff, int nSize); /* read until timeout or out of space */
224 int readToMark(char *buff, int size, const char **marks, char **occurence);
225 bool write(const char *pBuff, int nSize);
226 void flush();
227 bool set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl = FLOW_NONE);
228 bool restart();
229 };
230
231 /**
232 * Dynamic string class
233 */
234 class LIBNETXMS_EXPORTABLE String
235 {
236 protected:
237 TCHAR *m_pszBuffer;
238 UINT32 m_dwBufSize;
239
240 public:
241 static const int npos;
242
243 String();
244 String(const TCHAR *init);
245 String(const String &src);
246 ~String();
247
248 TCHAR *getBuffer() { return m_pszBuffer; }
249 void setBuffer(TCHAR *pszBuffer);
250
251 const String& operator =(const TCHAR *pszStr);
252 const String& operator =(const String &src);
253 const String& operator +=(const TCHAR *str);
254 const String& operator +=(const String &str);
255 operator const TCHAR*() { return CHECK_NULL_EX(m_pszBuffer); }
256
257 char *getUTF8String();
258
259 void addString(const TCHAR *pStr, UINT32 dwLen);
260 void addDynamicString(TCHAR *pszStr) { if (pszStr != NULL) { *this += pszStr; free(pszStr); } }
261
262 void addMultiByteString(const char *pStr, UINT32 size, int nCodePage);
263 void addWideCharString(const WCHAR *pStr, UINT32 size);
264
265 void addFormattedString(const TCHAR *format, ...);
266 void addFormattedStringV(const TCHAR *format, va_list args);
267
268 UINT32 getSize() { return m_dwBufSize > 0 ? m_dwBufSize - 1 : 0; }
269 BOOL isEmpty() { return m_dwBufSize <= 1; }
270
271 TCHAR *subStr(int nStart, int nLen, TCHAR *pszBuffer);
272 TCHAR *subStr(int nStart, int nLen) { return subStr(nStart, nLen, NULL); }
273 int find(const TCHAR *pszStr, int nStart = 0);
274
275 void escapeCharacter(int ch, int esc);
276 void replace(const TCHAR *pszSrc, const TCHAR *pszDst);
277 void trim();
278 void shrink(int chars = 1);
279 };
280
281 /**
282 * String maps base class
283 */
284 class LIBNETXMS_EXPORTABLE StringMapBase
285 {
286 protected:
287 int m_size;
288 TCHAR **m_keys;
289 void **m_values;
290 bool m_objectOwner;
291 bool m_ignoreCase;
292 void (*m_objectDestructor)(void *);
293
294 UINT32 find(const TCHAR *key);
295 void setObject(TCHAR *key, void *value, bool keyPreAlloc);
296 void *getObject(const TCHAR *key);
297 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
298
299 public:
300 StringMapBase(bool objectOwner);
301 virtual ~StringMapBase();
302
303 void setOwner(bool owner) { m_objectOwner = owner; }
304 void setIgnoreCase(bool ignore) { m_ignoreCase = ignore; }
305
306 void remove(const TCHAR *key);
307 void clear();
308
309 int size() { return m_size; }
310 const TCHAR *getKeyByIndex(int idx) { return ((idx >= 0) && (idx < m_size)) ? CHECK_NULL_EX(m_keys[idx]) : NULL; }
311 };
312
313 /**
314 * String map class
315 */
316 class LIBNETXMS_EXPORTABLE StringMap : public StringMapBase
317 {
318 public:
319 StringMap() : StringMapBase(true) { }
320 StringMap(const StringMap &src);
321 virtual ~StringMap();
322
323 StringMap& operator =(const StringMap &src);
324
325 void set(const TCHAR *key, const TCHAR *value) { setObject((TCHAR *)key, _tcsdup(value), false); }
326 void setPreallocated(TCHAR *key, TCHAR *value) { setObject(key, value, true); }
327 void set(const TCHAR *key, UINT32 value);
328
329 void addAll(StringMap *src);
330
331 const TCHAR *get(const TCHAR *key) { return (const TCHAR *)getObject(key); }
332 UINT32 getULong(const TCHAR *key, UINT32 defaultValue);
333 bool getBoolean(const TCHAR *key, bool defaultValue);
334
335 const TCHAR *getValueByIndex(int idx) { return ((idx >= 0) && (idx < m_size)) ? CHECK_NULL_EX((TCHAR *)m_values[idx]) : NULL; }
336 };
337
338 /**
339 * String map template for holding objects as values
340 */
341 template <class T> class StringObjectMap : public StringMapBase
342 {
343 private:
344 static void destructor(void *object) { delete (T*)object; }
345
346 public:
347 StringObjectMap(bool objectOwner) : StringMapBase(objectOwner) { m_objectDestructor = destructor; }
348
349 void set(const TCHAR *key, T *object) { setObject((TCHAR *)key, (void *)object, false); }
350 T *get(const TCHAR *key) { return (T*)getObject(key); }
351 T *getValueByIndex(int idx) { return ((idx >= 0) && (idx < m_size)) ? (T *)m_values[idx] : NULL; }
352 };
353
354 /**
355 * String list class
356 */
357 class LIBNETXMS_EXPORTABLE StringList
358 {
359 private:
360 int m_count;
361 int m_allocated;
362 TCHAR **m_values;
363
364 public:
365 StringList();
366 StringList(StringList *src);
367 StringList(const TCHAR *src, const TCHAR *separator);
368 ~StringList();
369
370 void add(const TCHAR *value);
371 void addPreallocated(TCHAR *value);
372 void add(INT32 value);
373 void add(UINT32 value);
374 void add(INT64 value);
375 void add(UINT64 value);
376 void add(double value);
377 void replace(int index, const TCHAR *value);
378 void clear();
379 int size() { return m_count; }
380 const TCHAR *get(int index) { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
381 int indexOf(const TCHAR *value);
382 int indexOfIgnoreCase(const TCHAR *value);
383 void remove(int index);
384 void addAll(const StringList *src);
385 void merge(const StringList *src, bool matchCase);
386 TCHAR *join(const TCHAR *separator);
387 void splitAndAdd(const TCHAR *src, const TCHAR *separator);
388 };
389
390 /**
391 * Entry of string set
392 */
393 struct StringSetEntry;
394
395 /**
396 * NXCP message
397 */
398 class CSCPMessage;
399
400 /**
401 * String set class
402 */
403 class LIBNETXMS_EXPORTABLE StringSet
404 {
405 private:
406 StringSetEntry *m_data;
407
408 public:
409 StringSet();
410 ~StringSet();
411
412 void add(const TCHAR *str);
413 void addPreallocated(TCHAR *str);
414 void remove(const TCHAR *str);
415 void clear();
416
417 int size();
418 bool contains(const TCHAR *str);
419 bool equals(StringSet *s);
420
421 void addAll(StringSet *src);
422 void addAll(TCHAR **strings, int count);
423 void addAllPreallocated(TCHAR **strings, int count);
424 void forEach(bool (*cb)(const TCHAR *, void *), void *userData);
425
426 void fillMessage(CSCPMessage *msg, UINT32 baseId, UINT32 countId);
427 void addAllFromMessage(CSCPMessage *msg, UINT32 baseId, UINT32 countId, bool clearBeforeAdd, bool toUppercase);
428
429 String getAll(const TCHAR *separator);
430 };
431
432 /**
433 * Dynamic array class
434 */
435 class LIBNETXMS_EXPORTABLE Array
436 {
437 private:
438 int m_size;
439 int m_allocated;
440 int m_grow;
441 size_t m_elementSize;
442 void **m_data;
443 bool m_objectOwner;
444
445 void internalRemove(int index, bool allowDestruction);
446 void destroyObject(void *object) { if (object != NULL) m_objectDestructor(object); }
447
448 protected:
449 bool m_storePointers;
450 void (*m_objectDestructor)(void *);
451
452 Array(void *data, int initial, int grow, size_t elementSize);
453 void *__getBuffer() { return m_data; }
454
455 public:
456 Array(int initial = 0, int grow = 16, bool owner = false);
457 virtual ~Array();
458
459 int add(void *element);
460 void *get(int index) { return ((index >= 0) && (index < m_size)) ? (m_storePointers ? m_data[index] : (void *)((char *)m_data + index * m_elementSize)): NULL; }
461 int indexOf(void *element);
462 void set(int index, void *element);
463 void replace(int index, void *element);
464 void remove(int index) { internalRemove(index, true); }
465 void remove(void *element) { internalRemove(indexOf(element), true); }
466 void unlink(int index) { internalRemove(index, false); }
467 void unlink(void *element) { internalRemove(indexOf(element), false); }
468 void clear();
469 void sort(int (*cb)(const void *, const void *));
470
471 int size() { return m_size; }
472
473 void setOwner(bool owner) { m_objectOwner = owner; }
474 bool isOwner() { return m_objectOwner; }
475 };
476
477 /**
478 * Template class for dynamic array which holds objects
479 */
480 template <class T> class ObjectArray : public Array
481 {
482 private:
483 static void destructor(void *object) { delete (T*)object; }
484
485 public:
486 ObjectArray(int initial = 0, int grow = 16, bool owner = false) : Array(initial, grow, owner) { m_objectDestructor = destructor; }
487 virtual ~ObjectArray() { }
488
489 int add(T *object) { return Array::add((void *)object); }
490 T *get(int index) { return (T*)Array::get(index); }
491 int indexOf(T *object) { return Array::indexOf((void *)object); }
492 void set(int index, T *object) { Array::set(index, (void *)object); }
493 void replace(int index, T *object) { Array::replace(index, (void *)object); }
494 void remove(int index) { Array::remove(index); }
495 void remove(T *object) { Array::remove((void *)object); }
496 void unlink(int index) { Array::unlink(index); }
497 void unlink(T *object) { Array::unlink((void *)object); }
498 };
499
500 /**
501 * Template class for dynamic array which holds scalar values
502 */
503 template <class T> class IntegerArray : public Array
504 {
505 private:
506 static void destructor(void *element) { }
507
508 public:
509 IntegerArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; m_storePointers = (sizeof(T) == sizeof(void *)); }
510 virtual ~IntegerArray() { }
511
512 int add(T value) { return Array::add(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
513 T get(int index) { return m_storePointers ? CAST_FROM_POINTER(Array::get(index), T) : *((T*)Array::get(index)); }
514 int indexOf(T value) { return Array::indexOf(m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
515 void set(int index, T value) { Array::set(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
516 void replace(int index, T value) { Array::replace(index, m_storePointers ? CAST_TO_POINTER(value, void *) : &value); }
517
518 T *getBuffer() { return (T*)__getBuffer(); }
519 };
520
521 /**
522 * Auxilliary class to hold dynamically allocated array of structures
523 */
524 template <class T> class StructArray : public Array
525 {
526 private:
527 static void destructor(void *element) { }
528
529 public:
530 StructArray(int initial = 0, int grow = 16) : Array(NULL, initial, grow, sizeof(T)) { m_objectDestructor = destructor; }
531 StructArray(T *data, int size) : Array(data, size, 16, sizeof(T)) { m_objectDestructor = destructor; }
532 virtual ~StructArray() { }
533
534 int add(T *element) { return Array::add((void *)element); }
535 T *get(int index) { return (T*)Array::get(index); }
536 int indexOf(T *element) { return Array::indexOf((void *)element); }
537 void set(int index, T *element) { Array::set(index, (void *)element); }
538 void replace(int index, T *element) { Array::replace(index, (void *)element); }
539 void remove(int index) { Array::remove(index); }
540 void remove(T *element) { Array::remove((void *)element); }
541 void unlink(int index) { Array::unlink(index); }
542 void unlink(T *element) { Array::unlink((void *)element); }
543
544 T *getBuffer() { return (T*)__getBuffer(); }
545 };
546
547 /**
548 * Auxilliary class for objects which counts references and
549 * destroys itself wheren reference count falls to 0
550 */
551 class LIBNETXMS_EXPORTABLE RefCountObject
552 {
553 private:
554 VolatileCounter m_refCount;
555
556 public:
557 RefCountObject()
558 {
559 m_refCount = 1;
560 }
561 virtual ~RefCountObject();
562
563 void incRefCount()
564 {
565 InterlockedIncrement(&m_refCount);
566 }
567
568 void decRefCount()
569 {
570 if (InterlockedDecrement(&m_refCount) == 0)
571 delete this;
572 }
573 };
574
575 class CSCPMessage;
576
577 /**
578 * Table column definition
579 */
580 class LIBNETXMS_EXPORTABLE TableColumnDefinition
581 {
582 private:
583 TCHAR *m_name;
584 TCHAR *m_displayName;
585 INT32 m_dataType;
586 bool m_instanceColumn;
587
588 public:
589 TableColumnDefinition(const TCHAR *name, const TCHAR *displayName, INT32 dataType, bool isInstance);
590 TableColumnDefinition(CSCPMessage *msg, UINT32 baseId);
591 TableColumnDefinition(TableColumnDefinition *src);
592 ~TableColumnDefinition();
593
594 void fillMessage(CSCPMessage *msg, UINT32 baseId);
595
596 const TCHAR *getName() { return m_name; }
597 const TCHAR *getDisplayName() { return m_displayName; }
598 INT32 getDataType() { return m_dataType; }
599 bool isInstanceColumn() { return m_instanceColumn; }
600
601 void setDataType(INT32 type) { m_dataType = type; }
602 void setInstanceColumn(bool isInstance) { m_instanceColumn = isInstance; }
603 void setDisplayName(const TCHAR *name) { safe_free(m_displayName); m_displayName = _tcsdup(CHECK_NULL_EX(name)); }
604 };
605
606 /**
607 * Table cell
608 */
609 class TableCell
610 {
611 private:
612 TCHAR *m_value;
613 int m_status;
614
615 public:
616 TableCell() { m_value = NULL; m_status = -1; }
617 TableCell(const TCHAR *value) { m_value = _tcsdup_ex(value); m_status = -1; }
618 TableCell(const TCHAR *value, int status) { m_value = _tcsdup_ex(value); m_status = status; }
619 TableCell(TableCell *src) { m_value = _tcsdup_ex(src->m_value); m_status = src->m_status; }
620 ~TableCell() { safe_free(m_value); }
621
622 void set(const TCHAR *value, int status) { safe_free(m_value); m_value = _tcsdup_ex(value); m_status = status; }
623 void setPreallocated(TCHAR *value, int status) { safe_free(m_value); m_value = value; m_status = status; }
624
625 const TCHAR *getValue() { return m_value; }
626 void setValue(const TCHAR *value) { safe_free(m_value); m_value = _tcsdup_ex(value); }
627 void setPreallocatedValue(TCHAR *value) { safe_free(m_value); m_value = value; }
628
629 int getStatus() { return m_status; }
630 void setStatus(int status) { m_status = status; }
631 };
632
633 /**
634 * Table row
635 */
636 class TableRow
637 {
638 private:
639 ObjectArray<TableCell> *m_cells;
640 UINT32 m_objectId;
641
642 public:
643 TableRow(int columnCount);
644 TableRow(TableRow *src);
645 ~TableRow() { delete m_cells; }
646
647 void addColumn() { m_cells->add(new TableCell); }
648 void deleteColumn(int index) { m_cells->remove(index); }
649
650 void set(int index, const TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->set(value, status); }
651 void setPreallocated(int index, TCHAR *value, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocated(value, status); }
652
653 void setValue(int index, const TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setValue(value); }
654 void setPreallocatedValue(int index, TCHAR *value) { TableCell *c = m_cells->get(index); if (c != NULL) c->setPreallocatedValue(value); }
655
656 void setStatus(int index, int status) { TableCell *c = m_cells->get(index); if (c != NULL) c->setStatus(status); }
657
658 const TCHAR *getValue(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getValue() : NULL; }
659 int getStatus(int index) { TableCell *c = m_cells->get(index); return (c != NULL) ? c->getStatus() : -1; }
660
661 UINT32 getObjectId() { return m_objectId; }
662 void setObjectId(UINT32 id) { m_objectId = id; }
663 };
664
665 /**
666 * Class for table data storage
667 */
668 class LIBNETXMS_EXPORTABLE Table : public RefCountObject
669 {
670 private:
671 ObjectArray<TableRow> *m_data;
672 ObjectArray<TableColumnDefinition> *m_columns;
673 TCHAR *m_title;
674 int m_source;
675 bool m_extendedFormat;
676
677 void createFromMessage(CSCPMessage *msg);
678 void destroy();
679
680 public:
681 Table();
682 Table(Table *src);
683 Table(CSCPMessage *msg);
684 virtual ~Table();
685
686 int fillMessage(CSCPMessage &msg, int offset, int rowLimit);
687 void updateFromMessage(CSCPMessage *msg);
688
689 void addAll(Table *src);
690 void copyRow(Table *src, int row);
691
692 int getNumRows() { return m_data->size(); }
693 int getNumColumns() { return m_columns->size(); }
694 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
695 int getSource() { return m_source; }
696
697 bool isExtendedFormat() { return m_extendedFormat; }
698 void setExtendedFormat(bool ext) { m_extendedFormat = ext; }
699
700 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getName() : NULL; }
701 INT32 getColumnDataType(int col) { return ((col >= 0) && (col < m_columns->size())) ? m_columns->get(col)->getDataType() : 0; }
702 int getColumnIndex(const TCHAR *name);
703 ObjectArray<TableColumnDefinition> *getColumnDefinitions() { return m_columns; }
704
705 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = _tcsdup_ex(title); }
706 void setSource(int source) { m_source = source; }
707 int addColumn(const TCHAR *name, INT32 dataType = 0, const TCHAR *displayName = NULL, bool isInstance = false);
708 void setColumnDataType(int col, INT32 dataType) { if ((col >= 0) && (col < m_columns->size())) m_columns->get(col)->setDataType(dataType); }
709 int addRow();
710
711 void deleteRow(int row);
712 void deleteColumn(int col);
713
714 void setAt(int nRow, int nCol, INT32 nData);
715 void setAt(int nRow, int nCol, UINT32 dwData);
716 void setAt(int nRow, int nCol, double dData);
717 void setAt(int nRow, int nCol, INT64 nData);
718 void setAt(int nRow, int nCol, UINT64 qwData);
719 void setAt(int nRow, int nCol, const TCHAR *pszData);
720 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
721
722 void set(int nCol, INT32 nData) { setAt(getNumRows() - 1, nCol, nData); }
723 void set(int nCol, UINT32 dwData) { setAt(getNumRows() - 1, nCol, dwData); }
724 void set(int nCol, double dData) { setAt(getNumRows() - 1, nCol, dData); }
725 void set(int nCol, INT64 nData) { setAt(getNumRows() - 1, nCol, nData); }
726 void set(int nCol, UINT64 qwData) { setAt(getNumRows() - 1, nCol, qwData); }
727 void set(int nCol, const TCHAR *pszData) { setAt(getNumRows() - 1, nCol, pszData); }
728 void setPreallocated(int nCol, TCHAR *pszData) { setPreallocatedAt(getNumRows() - 1, nCol, pszData); }
729
730 void setStatusAt(int row, int col, int status);
731 void setStatus(int col, int status) { setStatusAt(getNumRows() - 1, col, status); }
732
733 const TCHAR *getAsString(int nRow, int nCol);
734 INT32 getAsInt(int nRow, int nCol);
735 UINT32 getAsUInt(int nRow, int nCol);
736 INT64 getAsInt64(int nRow, int nCol);
737 UINT64 getAsUInt64(int nRow, int nCol);
738 double getAsDouble(int nRow, int nCol);
739
740 int getStatus(int nRow, int nCol);
741
742 void buildInstanceString(int row, TCHAR *buffer, size_t bufLen);
743 int findRowByInstance(const TCHAR *instance);
744
745 UINT32 getObjectId(int row) { TableRow *r = m_data->get(row); return (r != NULL) ? r->getObjectId() : 0; }
746 void setObjectId(int row, UINT32 id) { TableRow *r = m_data->get(row); if (r != NULL) r->setObjectId(id); }
747 };
748
749 /**
750 * Network connection
751 */
752 class LIBNETXMS_EXPORTABLE SocketConnection
753 {
754 protected:
755 SOCKET m_socket;
756 char m_data[4096];
757 int m_dataPos;
758
759 public:
760 SocketConnection();
761 virtual ~SocketConnection();
762
763 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
764 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
765 void disconnect();
766
767 bool canRead(UINT32 timeout);
768 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
769 bool waitForText(const char *text, int timeout);
770
771 int write(const char *pBuff, int nSize);
772 bool writeLine(const char *line);
773
774 static SocketConnection *createTCPConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
775 };
776
777 /**
778 * Telnet connection - handles all telnet negotiation
779 */
780 class LIBNETXMS_EXPORTABLE TelnetConnection : public SocketConnection
781 {
782 protected:
783 bool connectTCP(const TCHAR *hostName, WORD port, UINT32 timeout);
784 bool connectTCP(UINT32 ip, WORD port, UINT32 timeout);
785
786 public:
787 static TelnetConnection *createConnection(const TCHAR *hostName, WORD port, UINT32 timeout);
788
789 bool connect(const TCHAR *hostName, WORD port, UINT32 timeout);
790 bool connect(UINT32 ip, WORD port, UINT32 timeout);
791 virtual int read(char *pBuff, int nSize, UINT32 timeout = INFINITE);
792 int readLine(char *buffer, int size, UINT32 timeout = INFINITE);
793 };
794
795 #endif /* __cplusplus */
796
797 /**
798 * Configuration item template for configuration loader
799 */
800 typedef struct
801 {
802 TCHAR token[64];
803 BYTE type;
804 BYTE separator; // Separator character for lists
805 WORD listElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
806 UINT32 bufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
807 UINT32 bufferPos; // Should be set to 0
808 void *buffer;
809 void *overrideIndicator;
810 } NX_CFG_TEMPLATE;
811
812 /**
813 * Code translation structure
814 */
815 typedef struct __CODE_TO_TEXT
816 {
817 int code;
818 const TCHAR *text;
819 } CODE_TO_TEXT;
820
821 /**
822 * getopt() prototype if needed
823 */
824 #if USE_BUNDLED_GETOPT
825 #include <netxms_getopt.h>
826 #endif
827
828
829 //
830 // Win32 API functions missing under WinCE
831 //
832
833 #if defined(UNDER_CE) && defined(__cplusplus)
834
835 inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
836 {
837 SYSTEMTIME sysTime;
838
839 GetSystemTime(&sysTime);
840 SystemTimeToFileTime(&sysTime, pFt);
841 }
842
843 #endif // UNDER_CE
844
845
846 //
847 // Structures for opendir() / readdir() / closedir()
848 //
849
850 #ifdef _WIN32
851
852 typedef struct dirent
853 {
854 long d_ino; /* inode number (not used by MS-DOS) */
855 int d_namlen; /* Name length */
856 char d_name[257]; /* file name */
857 } _DIRECT;
858
859 typedef struct _dir_struc
860 {
861 char *start; /* Starting position */
862 char *curr; /* Current position */
863 long size; /* Size of string table */
864 long nfiles; /* number if filenames in table */
865 struct dirent dirstr; /* Directory structure to return */
866 } DIR;
867
868 #ifdef UNICODE
869
870 typedef struct dirent_w
871 {
872 long d_ino; /* inode number (not used by MS-DOS) */
873 int d_namlen; /* Name length */
874 WCHAR d_name[257]; /* file name */
875 } _DIRECTW;
876
877 typedef struct _dir_struc_w
878 {
879 WCHAR *start; /* Starting position */
880 WCHAR *curr; /* Current position */
881 long size; /* Size of string table */
882 long nfiles; /* number if filenames in table */
883 struct dirent_w dirstr; /* Directory structure to return */
884 } DIRW;
885
886 #define _TDIR DIRW
887 #define _TDIRECT _DIRECTW
888 #define _tdirent dirent_w
889
890 #else
891
892 #define _TDIR DIR
893 #define _TDIRECT _DIRECT
894 #define _tdirent dirent
895
896 #endif
897
898 #else /* not _WIN32 */
899
900 typedef struct dirent_w
901 {
902 long d_ino; /* inode number */
903 WCHAR d_name[257]; /* file name */
904 } _DIRECTW;
905
906 typedef struct _dir_struc_w
907 {
908 DIR *dir; /* Original non-unicode structure */
909 struct dirent_w dirstr; /* Directory structure to return */
910 } DIRW;
911
912 #endif /* _WIN32 */
913
914
915 //
916 // Functions
917 //
918
919 #if WORDS_BIGENDIAN
920 #define htonq(x) (x)
921 #define ntohq(x) (x)
922 #define htond(x) (x)
923 #define ntohd(x) (x)
924 #define SwapWideString(x)
925 #else
926 #ifdef HAVE_HTONLL
927 #define htonq(x) htonll(x)
928 #else
929 #define htonq(x) __bswap_64(x)
930 #endif
931 #ifdef HAVE_NTOHLL
932 #define ntohq(x) ntohll(x)
933 #else
934 #define ntohq(x) __bswap_64(x)
935 #endif
936 #define htond(x) __bswap_double(x)
937 #define ntohd(x) __bswap_double(x)
938 #define SwapWideString(x) __bswap_wstr(x)
939 #endif
940
941 #ifdef UNDER_CE
942 #define close(x) CloseHandle((HANDLE)(x))
943 #endif
944
945 #ifdef __cplusplus
946 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
947 {
948 #if defined(_WIN32) && (_MSC_VER >= 1400)
949 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
950 #else
951 _tcsncpy(pszDest, pszSrc, nLen - 1);
952 pszDest[nLen - 1] = 0;
953 #endif
954 return pszDest;
955 }
956
957 #ifdef UNICODE
958 inline char *nx_strncpy_mb(char *pszDest, const char *pszSrc, size_t nLen)
959 {
960 #if defined(_WIN32) && (_MSC_VER >= 1400)
961 strncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
962 #else
963 strncpy(pszDest, pszSrc, nLen - 1);
964 pszDest[nLen - 1] = 0;
965 #endif
966 return pszDest;
967 }
968 #else
969 #define nx_strncpy_mb nx_strncpy
970 #endif
971
972 int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, UINT32 timeout);
973 int LIBNETXMS_EXPORTABLE SendEx(SOCKET hSocket, const void *data, size_t len, int flags, MUTEX mutex);
974 int LIBNETXMS_EXPORTABLE RecvEx(SOCKET hSocket, void *data, size_t len, int flags, UINT32 timeout);
975 #endif /* __cplusplus */
976
977 #ifdef __cplusplus
978 extern "C"
979 {
980 #endif
981
982 #if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
983 UINT32 LIBNETXMS_EXPORTABLE __bswap_32(UINT32 dwVal);
984 #endif
985 #if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
986 UINT64 LIBNETXMS_EXPORTABLE __bswap_64(UINT64 qwVal);
987 #endif
988 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
989 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
990
991 #if !defined(_WIN32) && !defined(_NETWARE)
992 #if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
993 void LIBNETXMS_EXPORTABLE __wcsupr(WCHAR *in);
994 #define wcsupr __wcsupr
995 #endif
996 void LIBNETXMS_EXPORTABLE __strupr(char *in);
997 #define strupr __strupr
998 #endif
999
1000 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
1001 int (*compare)(const void *, const void *, void *));
1002
1003 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs();
1004
1005 UINT64 LIBNETXMS_EXPORTABLE FileSizeW(const WCHAR *pszFileName);
1006 UINT64 LIBNETXMS_EXPORTABLE FileSizeA(const char *pszFileName);
1007 #ifdef UNICODE
1008 #define FileSize FileSizeW
1009 #else
1010 #define FileSize FileSizeA
1011 #endif
1012
1013 int LIBNETXMS_EXPORTABLE BitsInMask(UINT32 dwMask);
1014 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(UINT32 dwAddr, TCHAR *szBuffer);
1015 #ifdef UNICODE
1016 char LIBNETXMS_EXPORTABLE *IpToStrA(UINT32 dwAddr, char *szBuffer);
1017 #else
1018 #define IpToStrA IpToStr
1019 #endif
1020 TCHAR LIBNETXMS_EXPORTABLE *Ip6ToStr(BYTE *addr, TCHAR *buffer);
1021 TCHAR LIBNETXMS_EXPORTABLE *SockaddrToStr(struct sockaddr *addr, TCHAR *buffer);
1022
1023 UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameA(const char *name);
1024 UINT32 LIBNETXMS_EXPORTABLE ResolveHostNameW(const WCHAR *name);
1025 #ifdef UNICODE
1026 #define ResolveHostName ResolveHostNameW
1027 #else
1028 #define ResolveHostName ResolveHostNameA
1029 #endif
1030
1031 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *data, size_t size);
1032 void LIBNETXMS_EXPORTABLE nx_memswap(void *block1, void *block2, size_t size);
1033
1034 WCHAR LIBNETXMS_EXPORTABLE *BinToStrW(const BYTE *data, size_t size, WCHAR *pStr);
1035 char LIBNETXMS_EXPORTABLE *BinToStrA(const BYTE *data, size_t size, char *pStr);
1036 #ifdef UNICODE
1037 #define BinToStr BinToStrW
1038 #else
1039 #define BinToStr BinToStrA
1040 #endif
1041
1042 size_t LIBNETXMS_EXPORTABLE StrToBinW(const WCHAR *pStr, BYTE *data, size_t size);
1043 size_t LIBNETXMS_EXPORTABLE StrToBinA(const char *pStr, BYTE *data, size_t size);
1044 #ifdef UNICODE
1045 #define StrToBin StrToBinW
1046 #else
1047 #define StrToBin StrToBinA
1048 #endif
1049
1050 TCHAR LIBNETXMS_EXPORTABLE *MACToStr(const BYTE *data, TCHAR *pStr);
1051
1052 void LIBNETXMS_EXPORTABLE StrStripA(char *pszStr);
1053 void LIBNETXMS_EXPORTABLE StrStripW(WCHAR *pszStr);
1054 #ifdef UNICODE
1055 #define StrStrip StrStripW
1056 #else
1057 #define StrStrip StrStripA
1058 #endif
1059
1060 const char LIBNETXMS_EXPORTABLE *ExtractWordA(const char *line, char *buffer);
1061 const WCHAR LIBNETXMS_EXPORTABLE *ExtractWordW(const WCHAR *line, WCHAR *buffer);
1062 #ifdef UNICODE
1063 #define ExtractWord ExtractWordW
1064 #else
1065 #define ExtractWord ExtractWordA
1066 #endif
1067
1068 int LIBNETXMS_EXPORTABLE NumCharsA(const char *pszStr, char ch);
1069 int LIBNETXMS_EXPORTABLE NumCharsW(const WCHAR *pszStr, WCHAR ch);
1070 #ifdef UNICODE
1071 #define NumChars NumCharsW
1072 #else
1073 #define NumChars NumCharsA
1074 #endif
1075
1076 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFA(char *str);
1077 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLFW(WCHAR *str);
1078 #ifdef UNICODE
1079 #define RemoveTrailingCRLF RemoveTrailingCRLFW
1080 #else
1081 #define RemoveTrailingCRLF RemoveTrailingCRLFA
1082 #endif
1083
1084 BOOL LIBNETXMS_EXPORTABLE RegexpMatchA(const char *pszStr, const char *pszExpr, BOOL bMatchCase);
1085 BOOL LIBNETXMS_EXPORTABLE RegexpMatchW(const WCHAR *pszStr, const WCHAR *pszExpr, BOOL bMatchCase);
1086 #ifdef UNICODE
1087 #define RegexpMatch RegexpMatchW
1088 #else
1089 #define RegexpMatch RegexpMatchA
1090 #endif
1091
1092 const TCHAR LIBNETXMS_EXPORTABLE *ExpandFileName(const TCHAR *name, TCHAR *buffer, size_t bufSize, bool allowShellCommand);
1093 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
1094 bool LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *str, bool matchCase);
1095 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
1096
1097 #ifdef __cplusplus
1098 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
1099 #endif
1100 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
1101 /* deprecated */ void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
1102 const TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(const TCHAR *pszFileName);
1103 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
1104 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, UINT32 *pdwFileSize);
1105 #ifdef UNICODE
1106 BYTE LIBNETXMS_EXPORTABLE *LoadFileA(const char *pszFileName, UINT32 *pdwFileSize);
1107 #else
1108 #define LoadFileA LoadFile
1109 #endif
1110
1111 UINT32 LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *data, UINT32 size, UINT32 dwCRC);
1112 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
1113 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
1114 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
1115 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
1116 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
1117 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
1118 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, UINT32 *pResult);
1119
1120 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1121 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
1122
1123 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
1124
1125 UINT32 LIBNETXMS_EXPORTABLE IcmpPing(UINT32 dwAddr, int iNumRetries, UINT32 dwTimeout, UINT32 *pdwRTT, UINT32 dwPacketSize);
1126
1127 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
1128
1129 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
1130 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
1131 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const char *pszSymbol, TCHAR *pszErrorText);
1132
1133 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueW(const WCHAR *optString, const WCHAR *option, WCHAR *buffer, int bufSize);
1134 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolW(const WCHAR *optString, const WCHAR *option, bool defVal);
1135 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntW(const WCHAR *optString, const WCHAR *option, long defVal);
1136
1137 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueA(const char *optString, const char *option, char *buffer, int bufSize);
1138 bool LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBoolA(const char *optString, const char *option, bool defVal);
1139 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsIntA(const char *optString, const char *option, long defVal);
1140
1141 #ifdef UNICODE
1142 #define ExtractNamedOptionValue ExtractNamedOptionValueW
1143 #define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolW
1144 #define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntW
1145 #else
1146 #define ExtractNamedOptionValue ExtractNamedOptionValueA
1147 #define ExtractNamedOptionValueAsBool ExtractNamedOptionValueAsBoolA
1148 #define ExtractNamedOptionValueAsInt ExtractNamedOptionValueAsIntA
1149 #endif
1150
1151 #ifdef __cplusplus
1152 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
1153 #else
1154 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
1155 #endif
1156
1157 #ifdef _WIN32
1158 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(UINT32 dwError, TCHAR *pszBuffer, size_t iBufSize);
1159 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
1160 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
1161 #endif
1162
1163 #if !(HAVE_DAEMON)
1164 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
1165 #endif
1166
1167 UINT32 LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
1168
1169 #ifndef _WIN32
1170 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
1171 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, UINT32 dwFlags, const WCHAR *pWideCharStr,
1172 int cchWideChar, char *pByteStr, int cchByteChar,
1173 char *pDefaultChar, BOOL *pbUsedDefChar);
1174 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, UINT32 dwFlags, const char *pByteStr,
1175 int cchByteChar, WCHAR *pWideCharStr,
1176 int cchWideChar);
1177
1178 #if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
1179 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
1180 #endif
1181 #if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
1182 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
1183 #endif
1184 #if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
1185 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2__tcsdup(const UCS2CHAR *pStr);
1186 #endif
1187
1188 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1189 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, int srcLen, UCS2CHAR *dst, int dstLen);
1190 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
1191 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
1192
1193 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
1194 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
1195 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
1196 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
1197 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
1198 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
1199
1200 int LIBNETXMS_EXPORTABLE nx_wscanf(const WCHAR *format, ...);
1201 int LIBNETXMS_EXPORTABLE nx_fwscanf(FILE *fp, const WCHAR *format, ...);
1202 int LIBNETXMS_EXPORTABLE nx_swscanf(const WCHAR *str, const WCHAR *format, ...);
1203 int LIBNETXMS_EXPORTABLE nx_vwscanf(const WCHAR *format, va_list args);
1204 int LIBNETXMS_EXPORTABLE nx_vfwscanf(FILE *fp, const WCHAR *format, va_list args);
1205 int LIBNETXMS_EXPORTABLE nx_vswscanf(const WCHAR *str, const WCHAR *format, va_list args);
1206
1207 #endif /* _WIN32 */
1208
1209 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
1210 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
1211 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
1212 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
1213
1214 #ifdef _WITH_ENCRYPTION
1215 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
1216 #endif
1217
1218 #ifdef UNICODE_UCS4
1219 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, WCHAR *dst, int dstLen);
1220 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, int srcLen, UCS2CHAR *dst, int dstLen);
1221 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen);
1222 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
1223 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
1224 #endif
1225
1226 #if !defined(_WIN32) && !HAVE_WSTAT
1227 int wstat(const WCHAR *_path, struct stat *_sbuf);
1228 #endif
1229
1230 #if defined(UNICODE) && !defined(_WIN32)
1231
1232 #if !HAVE_WPOPEN
1233 FILE LIBNETXMS_EXPORTABLE *wpopen(const WCHAR *_command, const WCHAR *_type);
1234 #endif
1235 #if !HAVE_WFOPEN
1236 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
1237 #endif
1238 #if HAVE_FOPEN64 && !HAVE_WFOPEN64
1239 FILE LIBNETXMS_EXPORTABLE *wfopen64(const WCHAR *_name, const WCHAR *_type);
1240 #endif
1241 #if !HAVE_WOPEN
1242 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
1243 #endif
1244 #if !HAVE_WCHMOD
1245 int LIBNETXMS_EXPORTABLE wchmod(const WCHAR *_name, int mode);
1246 #endif
1247 #if !HAVE_WCHDIR
1248 int wchdir(const WCHAR *_path);
1249 #endif
1250 #if !HAVE_WMKDIR
1251 int wmkdir(const WCHAR *_path, int mode);
1252 #endif
1253 #if !HAVE_WRMDIR
1254 int wrmdir(const WCHAR *_path);
1255 #endif
1256 #if !HAVE_WRENAME
1257 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
1258 #endif
1259 #if !HAVE_WUNLINK
1260 int wunlink(const WCHAR *_path);
1261 #endif
1262 #if !HAVE_WREMOVE
1263 int wremove(const WCHAR *_path);
1264 #endif
1265 #if !HAVE_WSYSTEM
1266 int wsystem(const WCHAR *_cmd);
1267 #endif
1268 #if !HAVE_WMKSTEMP
1269 int wmkstemp(WCHAR *_template);
1270 #endif
1271 #if !HAVE_WACCESS
1272 int waccess(const WCHAR *_path, int mode);
1273 #endif
1274 #if !HAVE_WGETENV
1275 WCHAR *wgetenv(const WCHAR *_string);
1276 #endif
1277 #if !HAVE_WCTIME
1278 WCHAR *wctime(const time_t *timep);
1279 #endif
1280 #if !HAVE_PUTWS
1281 int putws(const WCHAR *s);
1282 #endif
1283 #if !HAVE_WCSERROR && HAVE_STRERROR
1284 WCHAR *wcserror(int errnum);
1285 #endif
1286 #if !HAVE_WCSERROR_R && HAVE_STRERROR_R
1287 #if HAVE_POSIX_STRERROR_R
1288 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1289 #else
1290 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
1291 #endif
1292 #endif
1293
1294 #endif /* UNICODE && !_WIN32*/
1295
1296 #if !HAVE_STRTOLL
1297 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
1298 #endif
1299 #if !HAVE_STRTOULL
1300 UINT64 LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
1301 #endif
1302
1303 #if !HAVE_WCSTOLL
1304 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
1305 #endif
1306 #if !HAVE_WCSTOULL
1307 UINT64 LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
1308 #endif
1309
1310 #if !HAVE_WCSLWR && !defined(_WIN32)
1311 WCHAR LIBNETXMS_EXPORTABLE *wcslwr(WCHAR *str);
1312 #endif
1313
1314 #if !HAVE_WCSCASECMP && !defined(_WIN32)
1315 int LIBNETXMS_EXPORTABLE wcscasecmp(const wchar_t *s1, const wchar_t *s2);
1316 #endif
1317
1318 #if !HAVE_WCSNCASECMP && !defined(_WIN32)
1319 int LIBNETXMS_EXPORTABLE wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n);
1320 #endif
1321
1322 #ifdef _WIN32
1323 #ifdef UNICODE
1324 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1325 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1326 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1327
1328 #define _topendir wopendir
1329 #define _treaddir wreaddir
1330 #define _tclosedir wclosedir
1331 #else
1332 #define _topendir opendir
1333 #define _treaddir readdir
1334 #define _tclosedir closedir
1335 #endif
1336
1337 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
1338 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
1339 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
1340
1341 #else /* not _WIN32 */
1342
1343 DIRW LIBNETXMS_EXPORTABLE *wopendir(const WCHAR *filename);
1344 struct dirent_w LIBNETXMS_EXPORTABLE *wreaddir(DIRW *dirp);
1345 int LIBNETXMS_EXPORTABLE wclosedir(DIRW *dirp);
1346
1347 #endif
1348
1349 #if defined(_WIN32) || !(HAVE_SCANDIR)
1350 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
1351 int (*select)(const struct dirent *),
1352 int (*compar)(const struct dirent **, const struct dirent **));
1353 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
1354 #endif
1355
1356 TCHAR LIBNETXMS_EXPORTABLE *safe_fgetts(TCHAR *buffer, int len, FILE *f);
1357
1358 #ifdef UNDER_CE
1359 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
1360 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
1361 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
1362 #endif
1363
1364 BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, UINT32 flags, const TCHAR *msgModule,
1365 unsigned int msgCount, const TCHAR **messages);
1366 void LIBNETXMS_EXPORTABLE nxlog_close(void);
1367 void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
1368 BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int rotationMode, int maxLogSize, int historySize, const TCHAR *dailySuffix);
1369 BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
1370
1371 typedef void (*NxLogConsoleWriter)(const TCHAR *, ...);
1372 void LIBNETXMS_EXPORTABLE nxlog_set_console_writer(NxLogConsoleWriter writer);
1373
1374 void LIBNETXMS_EXPORTABLE WriteToTerminal(const TCHAR *text);
1375 void LIBNETXMS_EXPORTABLE WriteToTerminalEx(const TCHAR *format, ...)
1376 #if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
1377 __attribute__ ((format(printf, 1, 2)))
1378 #endif
1379 ;
1380
1381 #ifdef _WIN32
1382 int LIBNETXMS_EXPORTABLE mkstemp(char *tmpl);
1383 int LIBNETXMS_EXPORTABLE wmkstemp(WCHAR *tmpl);
1384 #ifdef UNICODE
1385 #define _tmkstemp wmkstemp
1386 #else
1387 #define _tmkstemp mkstemp
1388 #endif
1389 #endif
1390
1391 #ifndef _WIN32
1392 int strcat_s(char *dst, size_t dstSize, const char *src);
1393 int wcscat_s(WCHAR *dst, size_t dstSize, const WCHAR *src);
1394 #endif
1395
1396 #if !HAVE_STRPTIME
1397 char LIBNETXMS_EXPORTABLE *strptime(const char *buf, const char *fmt, struct tm *_tm);
1398 #endif
1399
1400 #if !HAVE_TIMEGM
1401 time_t LIBNETXMS_EXPORTABLE timegm(struct tm *_tm);
1402 #endif
1403
1404 #ifdef __cplusplus
1405 }
1406 #endif
1407
1408
1409 //
1410 // C++ only finctions
1411 //
1412
1413 #ifdef __cplusplus
1414
1415 TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *str, int length);
1416 String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *str, int length = -1);
1417 const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
1418 int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
1419 UINT32 LIBNETXMS_EXPORTABLE XMLGetAttrUINT32(const char **attrs, const char *name, UINT32 defVal);
1420 bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
1421
1422 #if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
1423 void LIBNETXMS_EXPORTABLE StartMainLoop(ThreadFunction pfSignalHandler, ThreadFunction pfMain);
1424 #endif
1425
1426 #endif
1427
1428 #endif /* _nms_util_h_ */