c604453df58e33e7ef662a4fc3a02202d8d111a3
[public/netxms.git] / include / nms_util.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2010 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 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 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 //
71 // Return codes for IcmpPing()
72 //
73
74 #define ICMP_SUCCESS 0
75 #define ICMP_UNREACHEABLE 1
76 #define ICMP_TIMEOUT 2
77 #define ICMP_RAW_SOCK_FAILED 3
78
79
80 //
81 // Token types for configuration loader
82 //
83
84 #define CT_LONG 0
85 #define CT_STRING 1
86 #define CT_STRING_LIST 2
87 #define CT_END_OF_LIST 3
88 #define CT_BOOLEAN 4
89 #define CT_WORD 5
90 #define CT_IGNORE 6
91
92
93 //
94 // Return codes for NxLoadConfig()
95 //
96
97 #define NXCFG_ERR_OK 0
98 #define NXCFG_ERR_NOFILE 1
99 #define NXCFG_ERR_SYNTAX 2
100
101
102 //
103 // nxlog_open() flags
104 //
105
106 #define NXLOG_USE_SYSLOG ((DWORD)0x00000001)
107 #define NXLOG_PRINT_TO_STDOUT ((DWORD)0x00000002)
108 #define NXLOG_IS_OPEN ((DWORD)0x80000000)
109
110
111 //
112 // Class for serial communications
113 //
114
115 #ifdef __cplusplus
116
117 #ifndef _WIN32
118 enum
119 {
120 NOPARITY,
121 ODDPARITY,
122 EVENPARITY,
123 ONESTOPBIT,
124 TWOSTOPBITS
125 };
126
127 enum
128 {
129 FLOW_NONE,
130 FLOW_HARDWARE,
131 FLOW_SOFTWARE
132 };
133
134 #ifndef INVALID_HANDLE_VALUE
135 #define INVALID_HANDLE_VALUE (-1)
136 #endif
137 #endif /* _WIN32 */
138
139 class LIBNETXMS_EXPORTABLE Serial
140 {
141 public:
142 Serial(void);
143 ~Serial(void);
144
145 bool Open(const TCHAR *pszPort);
146 void Close(void);
147 void SetTimeout(int nTimeout);
148 int Read(char *pBuff, int nSize); /* waits up to timeout and do single read */
149 int ReadAll(char *pBuff, int nSize); /* read until timeout or out of space */
150 bool Write(const char *pBuff, int nSize);
151 void Flush(void);
152 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits);
153 bool Set(int nSpeed, int nDataBits, int nParity, int nStopBits, int nFlowControl);
154 bool Restart(void);
155
156 private:
157 TCHAR *m_pszPort;
158 int m_nTimeout;
159 int m_nSpeed;
160 int m_nDataBits;
161 int m_nStopBits;
162 int m_nParity;
163 int m_nFlowControl;
164
165 #ifndef _WIN32
166 int m_hPort;
167 struct termios m_originalSettings;
168 #else
169 HANDLE m_hPort;
170 #endif
171 };
172
173
174 //
175 // Class for table data storage
176 //
177
178 class CSCPMessage;
179
180 class LIBNETXMS_EXPORTABLE Table
181 {
182 private:
183 int m_nNumRows;
184 int m_nNumCols;
185 TCHAR **m_ppData;
186 TCHAR **m_ppColNames;
187 LONG *m_colFormats;
188 TCHAR *m_title;
189
190 public:
191 Table();
192 Table(CSCPMessage *msg);
193 ~Table();
194
195 int fillMessage(CSCPMessage &msg, int offset, int rowLimit);
196
197 int getNumRows() { return m_nNumRows; }
198 int getNumColumns() { return m_nNumCols; }
199 const TCHAR *getTitle() { return CHECK_NULL_EX(m_title); }
200
201 const TCHAR *getColumnName(int col) { return ((col >= 0) && (col < m_nNumCols)) ? m_ppColNames[col] : NULL; }
202 LONG getColumnFormat(int col) { return ((col >= 0) && (col < m_nNumCols)) ? m_colFormats[col] : 0; }
203
204 void setTitle(const TCHAR *title) { safe_free(m_title); m_title = (title != NULL) ? _tcsdup(title) : NULL; }
205 int addColumn(const TCHAR *name, LONG format = 0);
206 int addRow();
207
208 void setAt(int nRow, int nCol, LONG nData);
209 void setAt(int nRow, int nCol, DWORD dwData);
210 void setAt(int nRow, int nCol, double dData);
211 void setAt(int nRow, int nCol, INT64 nData);
212 void setAt(int nRow, int nCol, QWORD qwData);
213 void setAt(int nRow, int nCol, const TCHAR *pszData);
214 void setPreallocatedAt(int nRow, int nCol, TCHAR *pszData);
215
216 void set(int nCol, LONG nData) { setAt(m_nNumRows - 1, nCol, nData); }
217 void set(int nCol, DWORD dwData) { setAt(m_nNumRows - 1, nCol, dwData); }
218 void set(int nCol, double dData) { setAt(m_nNumRows - 1, nCol, dData); }
219 void set(int nCol, INT64 nData) { setAt(m_nNumRows - 1, nCol, nData); }
220 void set(int nCol, QWORD qwData) { setAt(m_nNumRows - 1, nCol, qwData); }
221 void set(int nCol, const TCHAR *pszData) { setAt(m_nNumRows - 1, nCol, pszData); }
222 void setPreallocated(int nCol, TCHAR *pszData) { setPreallocatedAt(m_nNumRows - 1, nCol, pszData); }
223
224 const TCHAR *getAsString(int nRow, int nCol);
225 LONG getAsInt(int nRow, int nCol);
226 DWORD getAsUInt(int nRow, int nCol);
227 INT64 getAsInt64(int nRow, int nCol);
228 QWORD getAsUInt64(int nRow, int nCol);
229 double getAsDouble(int nRow, int nCol);
230 };
231
232
233 //
234 // Dynamic string class
235 //
236
237 class LIBNETXMS_EXPORTABLE String
238 {
239 protected:
240 TCHAR *m_pszBuffer;
241 DWORD m_dwBufSize;
242
243 public:
244 static const int npos;
245
246 String();
247 String(const TCHAR *init);
248 String(const String &src);
249 ~String();
250
251 void setBuffer(TCHAR *pszBuffer);
252
253 const String& operator =(const TCHAR *pszStr);
254 const String& operator =(const String &src);
255 const String& operator +=(const TCHAR *pszStr);
256 operator const TCHAR*() { return CHECK_NULL_EX(m_pszBuffer); }
257
258 void addString(const TCHAR *pStr, DWORD dwLen);
259 void addDynamicString(TCHAR *pszStr) { if (pszStr != NULL) { *this += pszStr; free(pszStr); } }
260
261 void addMultiByteString(const char *pStr, DWORD dwSize, int nCodePage);
262 void addWideCharString(const WCHAR *pStr, DWORD dwSize);
263
264 void addFormattedString(const TCHAR *format, ...);
265 void addFormattedStringV(const TCHAR *format, va_list args);
266 void escapeCharacter(int ch, int esc);
267 void translate(const TCHAR *pszSrc, const TCHAR *pszDst);
268
269 DWORD getSize() { return m_dwBufSize > 0 ? m_dwBufSize - 1 : 0; }
270 BOOL isEmpty() { return m_dwBufSize <= 1; }
271
272 TCHAR *subStr(int nStart, int nLen, TCHAR *pszBuffer);
273 TCHAR *subStr(int nStart, int nLen) { return subStr(nStart, nLen, NULL); }
274 int find(const TCHAR *pszStr, int nStart = 0);
275
276 void trim();
277 void shrink(int chars = 1);
278 };
279
280
281 //
282 // String map class
283 //
284
285 class LIBNETXMS_EXPORTABLE StringMap
286 {
287 protected:
288 DWORD m_size;
289 TCHAR **m_keys;
290 TCHAR **m_values;
291
292 DWORD find(const TCHAR *key);
293
294 public:
295 StringMap();
296 StringMap(const StringMap &src);
297 ~StringMap();
298
299 StringMap& operator =(const StringMap &src);
300
301 void set(const TCHAR *key, const TCHAR *value);
302 void setPreallocated(TCHAR *key, TCHAR *value);
303 const TCHAR *get(const TCHAR *key);
304 void remove(const TCHAR *key);
305 void clear();
306
307 DWORD getSize() { return m_size; }
308 const TCHAR *getKeyByIndex(DWORD idx) { return (idx < m_size) ? CHECK_NULL_EX(m_keys[idx]) : NULL; }
309 const TCHAR *getValueByIndex(DWORD idx) { return (idx < m_size) ? CHECK_NULL_EX(m_values[idx]) : NULL; }
310 };
311
312
313 //
314 // String list class
315 //
316
317 class LIBNETXMS_EXPORTABLE StringList
318 {
319 private:
320 int m_count;
321 int m_allocated;
322 TCHAR **m_values;
323
324 public:
325 StringList();
326 ~StringList();
327
328 void add(const TCHAR *value);
329 void addPreallocated(TCHAR *value);
330 void add(LONG value);
331 void add(DWORD value);
332 void add(INT64 value);
333 void add(QWORD value);
334 void add(double value);
335 void clear();
336 int getSize() { return m_count; }
337 const TCHAR *getValue(int index) { return ((index >=0) && (index < m_count)) ? m_values[index] : NULL; }
338 };
339
340 #endif /* __cplusplus */
341
342
343 //
344 // Configuration item template for configuration loader
345 //
346
347 typedef struct
348 {
349 TCHAR szToken[64];
350 BYTE iType;
351 BYTE cSeparator; // Separator character for lists
352 WORD wListElements; // Number of list elements, should be set to 0 before calling NxLoadConfig()
353 DWORD dwBufferSize; // Buffer size for strings or flag to be set for CT_BOOLEAN
354 DWORD dwBufferPos; // Should be set to 0
355 void *pBuffer;
356 } NX_CFG_TEMPLATE;
357
358
359 //
360 // Code translation structure
361 //
362
363 typedef struct __CODE_TO_TEXT
364 {
365 int code;
366 const TCHAR *text;
367 } CODE_TO_TEXT;
368
369
370 //
371 // getopt() prototype if needed
372 //
373
374 #ifdef _WIN32
375 #include <netxms_getopt.h>
376 #endif
377
378
379 //
380 // Win32 API functions missing under WinCE
381 //
382
383 #if defined(UNDER_CE) && defined(__cplusplus)
384
385 inline void GetSystemTimeAsFileTime(LPFILETIME pFt)
386 {
387 SYSTEMTIME sysTime;
388
389 GetSystemTime(&sysTime);
390 SystemTimeToFileTime(&sysTime, pFt);
391 }
392
393 #endif // UNDER_CE
394
395
396 //
397 // Structures for opendir() / readdir() / closedir()
398 //
399
400 #ifdef _WIN32
401
402 #ifndef SWIGPERL
403
404 typedef struct dirent
405 {
406 long d_ino; /* inode number (not used by MS-DOS) */
407 int d_namlen; /* Name length */
408 char d_name[257]; /* file name */
409 } _DIRECT;
410
411 typedef struct _dir_struc
412 {
413 char *start; /* Starting position */
414 char *curr; /* Current position */
415 long size; /* Size of string table */
416 long nfiles; /* number if filenames in table */
417 struct dirent dirstr; /* Directory structure to return */
418 } DIR;
419
420 #endif
421
422 #endif /* _WIN32 */
423
424
425 //
426 // Functions
427 //
428
429
430 #if WORDS_BIGENDIAN
431 #define htonq(x) (x)
432 #define ntohq(x) (x)
433 #define htond(x) (x)
434 #define ntohd(x) (x)
435 #define SwapWideString(x)
436 #else
437 #ifdef HAVE_HTONLL
438 #define htonq(x) htonll(x)
439 #else
440 #define htonq(x) __bswap_64(x)
441 #endif
442 #ifdef HAVE_NTOHLL
443 #define ntohq(x) ntohll(x)
444 #else
445 #define ntohq(x) __bswap_64(x)
446 #endif
447 #define htond(x) __bswap_double(x)
448 #define ntohd(x) __bswap_double(x)
449 #define SwapWideString(x) __bswap_wstr(x)
450 #endif
451
452 #ifdef UNDER_CE
453 #define close(x) CloseHandle((HANDLE)(x))
454 #endif
455
456 #ifdef __cplusplus
457 #ifndef LIBNETXMS_INLINE
458 inline TCHAR *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen)
459 {
460 #if defined(_WIN32) && (_MSC_VER >= 1400)
461 _tcsncpy_s(pszDest, nLen, pszSrc, _TRUNCATE);
462 #else
463 _tcsncpy(pszDest, pszSrc, nLen - 1);
464 pszDest[nLen - 1] = 0;
465 #endif
466 return pszDest;
467 }
468 #endif
469 #else
470 TCHAR LIBNETXMS_EXPORTABLE *nx_strncpy(TCHAR *pszDest, const TCHAR *pszSrc, size_t nLen);
471 #endif
472
473 #ifdef __cplusplus
474 extern "C"
475 {
476 #endif
477 int LIBNETXMS_EXPORTABLE ConnectEx(SOCKET s, struct sockaddr *addr, int len, DWORD timeout);
478 int LIBNETXMS_EXPORTABLE SendEx(SOCKET, const void *, size_t, int);
479 int LIBNETXMS_EXPORTABLE RecvEx(SOCKET nSocket, const void *pBuff,
480 size_t nSize, int nFlags, DWORD dwTimeout);
481
482 #if defined(_WIN32) || !(HAVE_DECL___BSWAP_32)
483 DWORD LIBNETXMS_EXPORTABLE __bswap_32(DWORD dwVal);
484 #endif
485 #if defined(_WIN32) || !(HAVE_DECL___BSWAP_64)
486 QWORD LIBNETXMS_EXPORTABLE __bswap_64(QWORD qwVal);
487 #endif
488 double LIBNETXMS_EXPORTABLE __bswap_double(double dVal);
489 void LIBNETXMS_EXPORTABLE __bswap_wstr(UCS2CHAR *pStr);
490
491 #if !defined(_WIN32) && !defined(_NETWARE)
492 #if defined(UNICODE_UCS2) || defined(UNICODE_UCS4)
493 void LIBNETXMS_EXPORTABLE wcsupr(WCHAR *in);
494 #endif
495 void LIBNETXMS_EXPORTABLE strupr(char *in);
496 #endif
497
498 void LIBNETXMS_EXPORTABLE QSortEx(void *base, size_t nmemb, size_t size, void *arg,
499 int (*compare)(const void *, const void *, void *));
500
501 INT64 LIBNETXMS_EXPORTABLE GetCurrentTimeMs(void);
502 QWORD LIBNETXMS_EXPORTABLE FileSize(const TCHAR *pszFileName);
503
504 int LIBNETXMS_EXPORTABLE BitsInMask(DWORD dwMask);
505 TCHAR LIBNETXMS_EXPORTABLE *IpToStr(DWORD dwAddr, TCHAR *szBuffer);
506 DWORD LIBNETXMS_EXPORTABLE ResolveHostName(const TCHAR *pszName);
507
508 void LIBNETXMS_EXPORTABLE *nx_memdup(const void *pData, DWORD dwSize);
509 void LIBNETXMS_EXPORTABLE nx_memswap(void *pBlock1, void *pBlock2, DWORD dwSize);
510
511 TCHAR LIBNETXMS_EXPORTABLE *BinToStr(BYTE *pData, DWORD dwSize, TCHAR *pStr);
512 DWORD LIBNETXMS_EXPORTABLE StrToBin(const TCHAR *pStr, BYTE *pData, DWORD dwSize);
513 void LIBNETXMS_EXPORTABLE MACToStr(BYTE *pData, TCHAR *pStr);
514
515 void LIBNETXMS_EXPORTABLE StrStrip(TCHAR *pszStr);
516 void LIBNETXMS_EXPORTABLE Trim(TCHAR *str);
517 BOOL LIBNETXMS_EXPORTABLE MatchString(const TCHAR *pattern, const TCHAR *string, BOOL matchCase);
518 BOOL LIBNETXMS_EXPORTABLE RegexpMatch(const TCHAR *pszStr, const TCHAR *pszExpr, BOOL bMatchCase);
519 TCHAR LIBNETXMS_EXPORTABLE *ExtractWord(TCHAR *line, TCHAR *buffer);
520 TCHAR LIBNETXMS_EXPORTABLE **SplitString(const TCHAR *source, TCHAR sep, int *numStrings);
521 int LIBNETXMS_EXPORTABLE NumChars(const TCHAR *pszStr, int ch);
522 void LIBNETXMS_EXPORTABLE RemoveTrailingCRLF(TCHAR *str);
523 #ifdef __cplusplus
524 BOOL LIBNETXMS_EXPORTABLE IsValidObjectName(const TCHAR *pszName, BOOL bExtendedChars = FALSE);
525 #endif
526 BOOL LIBNETXMS_EXPORTABLE IsValidScriptName(const TCHAR *pszName);
527 void LIBNETXMS_EXPORTABLE TranslateStr(TCHAR *pszString, const TCHAR *pszSubStr, const TCHAR *pszReplace);
528 TCHAR LIBNETXMS_EXPORTABLE *GetCleanFileName(TCHAR *pszFileName);
529 void LIBNETXMS_EXPORTABLE GetOSVersionString(TCHAR *pszBuffer, int nBufSize);
530 BYTE LIBNETXMS_EXPORTABLE *LoadFile(const TCHAR *pszFileName, DWORD *pdwFileSize);
531
532 DWORD LIBNETXMS_EXPORTABLE CalculateCRC32(const unsigned char *pData, DWORD dwSize, DWORD dwCRC);
533 void LIBNETXMS_EXPORTABLE CalculateMD5Hash(const unsigned char *data, size_t nbytes, unsigned char *hash);
534 void LIBNETXMS_EXPORTABLE MD5HashForPattern(const unsigned char *data, size_t patternSize, size_t fullSize, BYTE *hash);
535 void LIBNETXMS_EXPORTABLE CalculateSHA1Hash(unsigned char *data, size_t nbytes, unsigned char *hash);
536 void LIBNETXMS_EXPORTABLE SHA1HashForPattern(unsigned char *data, size_t patternSize, size_t fullSize, unsigned char *hash);
537 BOOL LIBNETXMS_EXPORTABLE CalculateFileMD5Hash(const TCHAR *pszFileName, BYTE *pHash);
538 BOOL LIBNETXMS_EXPORTABLE CalculateFileSHA1Hash(const TCHAR *pszFileName, BYTE *pHash);
539 BOOL LIBNETXMS_EXPORTABLE CalculateFileCRC32(const TCHAR *pszFileName, DWORD *pResult);
540
541 void LIBNETXMS_EXPORTABLE ICEEncryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
542 void LIBNETXMS_EXPORTABLE ICEDecryptData(const BYTE *in, int inLen, BYTE *out, const BYTE *key);
543
544 BOOL LIBNETXMS_EXPORTABLE DecryptPassword(const TCHAR *login, const TCHAR *encryptedPasswd, TCHAR *decryptedPasswd);
545
546 DWORD LIBNETXMS_EXPORTABLE IcmpPing(DWORD dwAddr, int iNumRetries, DWORD dwTimeout,
547 DWORD *pdwRTT, DWORD dwPacketSize);
548
549 /* deprecated:
550 DWORD LIBNETXMS_EXPORTABLE NxLoadConfig(const TCHAR *pszFileName, const TCHAR *pszSection,
551 NX_CFG_TEMPLATE *pTemplateList, BOOL bPrint);*/
552 int LIBNETXMS_EXPORTABLE NxDCIDataTypeFromText(const TCHAR *pszText);
553
554 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText);
555 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule);
556 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule, const TCHAR *pszSymbol, TCHAR *pszErrorText);
557
558 void LIBNETXMS_EXPORTABLE InitSubAgentAPI(void (* writeLog)(int, int, const TCHAR *),
559 void (* sendTrap2)(DWORD, const char *, va_list),
560 void (* sendTrap1)(DWORD, int, TCHAR **),
561 BOOL (* sendFile)(void *, DWORD, const TCHAR *, long),
562 BOOL (* pushData)(const TCHAR *, const TCHAR *));
563
564 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValue(const TCHAR *optString, const TCHAR *option, TCHAR *buffer, int bufSize);
565 BOOL LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsBool(const TCHAR *optString, const TCHAR *option, BOOL defVal);
566 long LIBNETXMS_EXPORTABLE ExtractNamedOptionValueAsInt(const TCHAR *optString, const TCHAR *option, long defVal);
567
568 TCHAR LIBNETXMS_EXPORTABLE *EscapeStringForXML(const TCHAR *string, int length);
569 const char LIBNETXMS_EXPORTABLE *XMLGetAttr(const char **attrs, const char *name);
570 int LIBNETXMS_EXPORTABLE XMLGetAttrInt(const char **attrs, const char *name, int defVal);
571 DWORD LIBNETXMS_EXPORTABLE XMLGetAttrDWORD(const char **attrs, const char *name, DWORD defVal);
572 bool LIBNETXMS_EXPORTABLE XMLGetAttrBoolean(const char **attrs, const char *name, bool defVal);
573
574 #ifdef __cplusplus
575 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText = _T("Unknown"));
576 #else
577 const TCHAR LIBNETXMS_EXPORTABLE *CodeToText(int iCode, CODE_TO_TEXT *pTranslator, const TCHAR *pszDefaultText);
578 #endif
579
580 #ifdef _WIN32
581 TCHAR LIBNETXMS_EXPORTABLE *GetSystemErrorText(DWORD dwError, TCHAR *pszBuffer, size_t iBufSize);
582 BOOL LIBNETXMS_EXPORTABLE GetWindowsVersionString(TCHAR *versionString, int strSize);
583 INT64 LIBNETXMS_EXPORTABLE GetProcessRSS();
584 #endif
585
586 #if !(HAVE_DAEMON)
587 int LIBNETXMS_EXPORTABLE daemon(int nochdir, int noclose);
588 #endif
589
590 DWORD LIBNETXMS_EXPORTABLE inet_addr_w(const WCHAR *pszAddr);
591
592 #ifndef _WIN32
593 BOOL LIBNETXMS_EXPORTABLE SetDefaultCodepage(const char *cp);
594 int LIBNETXMS_EXPORTABLE WideCharToMultiByte(int iCodePage, DWORD dwFlags, const WCHAR *pWideCharStr,
595 int cchWideChar, char *pByteStr, int cchByteChar,
596 char *pDefaultChar, BOOL *pbUsedDefChar);
597 int LIBNETXMS_EXPORTABLE MultiByteToWideChar(int iCodePage, DWORD dwFlags, const char *pByteStr,
598 int cchByteChar, WCHAR *pWideCharStr,
599 int cchWideChar);
600
601 #if !defined(UNICODE_UCS2) || !HAVE_WCSLEN
602 int LIBNETXMS_EXPORTABLE ucs2_strlen(const UCS2CHAR *pStr);
603 #endif
604 #if !defined(UNICODE_UCS2) || !HAVE_WCSNCPY
605 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strncpy(UCS2CHAR *pDst, const UCS2CHAR *pSrc, int nDstLen);
606 #endif
607 #if !defined(UNICODE_UCS2) || !HAVE_WCSDUP
608 UCS2CHAR LIBNETXMS_EXPORTABLE *ucs2_strdup(const UCS2CHAR *pStr);
609 #endif
610
611 #ifndef UNICODE
612 size_t LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, size_t srcLen, char *dst, size_t dstLen);
613 size_t LIBNETXMS_EXPORTABLE mb_to_ucs2(const char *src, size_t srcLen, UCS2CHAR *dst, size_t dstLen);
614 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromMBString(const char *pszString);
615 char LIBNETXMS_EXPORTABLE *MBStringFromUCS2String(const UCS2CHAR *pszString);
616 #endif
617
618 #ifdef UNICODE
619 int LIBNETXMS_EXPORTABLE nx_wprintf(const WCHAR *format, ...);
620 int LIBNETXMS_EXPORTABLE nx_fwprintf(FILE *fp, const WCHAR *format, ...);
621 int LIBNETXMS_EXPORTABLE nx_swprintf(WCHAR *buffer, size_t size, const WCHAR *format, ...);
622 int LIBNETXMS_EXPORTABLE nx_vwprintf(const WCHAR *format, va_list args);
623 int LIBNETXMS_EXPORTABLE nx_vfwprintf(FILE *fp, const WCHAR *format, va_list args);
624 int LIBNETXMS_EXPORTABLE nx_vswprintf(WCHAR *buffer, size_t size, const WCHAR *format, va_list args);
625 #endif
626
627 #endif /* _WIN32 */
628
629 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromMBString(const char *pszString);
630 WCHAR LIBNETXMS_EXPORTABLE *WideStringFromUTF8String(const char *pszString);
631 char LIBNETXMS_EXPORTABLE *MBStringFromWideString(const WCHAR *pwszString);
632 char LIBNETXMS_EXPORTABLE *UTF8StringFromWideString(const WCHAR *pwszString);
633
634 #ifdef _WITH_ENCRYPTION
635 WCHAR LIBNETXMS_EXPORTABLE *ERR_error_string_W(int nError, WCHAR *pwszBuffer);
636 #endif
637
638 #ifdef UNICODE_UCS4
639 size_t LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, size_t srcLen, WCHAR *dst, size_t dstLen);
640 size_t LIBNETXMS_EXPORTABLE ucs4_to_ucs2(const WCHAR *src, size_t srcLen, UCS2CHAR *dst, size_t dstLen);
641 size_t LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, size_t srcLen, char *dst, size_t dstLen);
642 UCS2CHAR LIBNETXMS_EXPORTABLE *UCS2StringFromUCS4String(const WCHAR *pwszString);
643 WCHAR LIBNETXMS_EXPORTABLE *UCS4StringFromUCS2String(const UCS2CHAR *pszString);
644 #endif
645
646 #ifdef UNICODE
647
648 #if !HAVE_WFOPEN
649 FILE LIBNETXMS_EXPORTABLE *wfopen(const WCHAR *_name, const WCHAR *_type);
650 #endif
651 #if !HAVE_WOPEN
652 int LIBNETXMS_EXPORTABLE wopen(const WCHAR *, int, ...);
653 #endif
654 #if !HAVE_WSTAT
655 int wstat(const WCHAR *_path, struct stat *_sbuf);
656 #endif
657 #if !HAVE_WRENAME
658 int wrename(const WCHAR *_oldpath, const WCHAR *_newpath);
659 #endif
660 #if !HAVE_WUNLINK
661 int wunlink(const WCHAR *_path);
662 #endif
663 #if !HAVE_WGETENV
664 WCHAR *wgetenv(const WCHAR *_string);
665 #endif
666 #if !HAVE_WCSERROR && HAVE_STRERROR
667 WCHAR *wcserror(int errnum);
668 #endif
669 #if !HAVE_WCSERROR_R && HAVE_STRERROR_R
670 #if HAVE_POSIX_STRERROR_R
671 int wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
672 #else
673 WCHAR *wcserror_r(int errnum, WCHAR *strerrbuf, size_t buflen);
674 #endif
675 #endif
676
677 #endif /* UNICODE */
678
679 #if !HAVE_STRTOLL
680 INT64 LIBNETXMS_EXPORTABLE strtoll(const char *nptr, char **endptr, int base);
681 #endif
682 #if !HAVE_STRTOULL
683 QWORD LIBNETXMS_EXPORTABLE strtoull(const char *nptr, char **endptr, int base);
684 #endif
685
686 #if !HAVE_WCSTOLL
687 INT64 LIBNETXMS_EXPORTABLE wcstoll(const WCHAR *nptr, WCHAR **endptr, int base);
688 #endif
689 #if !HAVE_WCSTOULL
690 QWORD LIBNETXMS_EXPORTABLE wcstoull(const WCHAR *nptr, WCHAR **endptr, int base);
691 #endif
692
693 #ifdef _WIN32
694 #ifndef SWIGPERL
695 DIR LIBNETXMS_EXPORTABLE *opendir(const char *filename);
696 struct dirent LIBNETXMS_EXPORTABLE *readdir(DIR *dirp);
697 int LIBNETXMS_EXPORTABLE closedir(DIR *dirp);
698 #endif
699 #endif
700
701 #if defined(_WIN32) || !(HAVE_SCANDIR)
702 int LIBNETXMS_EXPORTABLE scandir(const char *dir, struct dirent ***namelist,
703 int (*select)(const struct dirent *),
704 int (*compar)(const struct dirent **, const struct dirent **));
705 int LIBNETXMS_EXPORTABLE alphasort(const struct dirent **a, const struct dirent **b);
706 #endif
707
708 #ifdef UNDER_CE
709 int LIBNETXMS_EXPORTABLE _topen(TCHAR *pszName, int nFlags, ...);
710 int LIBNETXMS_EXPORTABLE read(int hFile, void *pBuffer, size_t nBytes);
711 int LIBNETXMS_EXPORTABLE write(int hFile, void *pBuffer, size_t nBytes);
712 #endif
713
714 #if !defined(_WIN32) && !defined(_NETWARE) && defined(NMS_THREADS_H_INCLUDED)
715 void LIBNETXMS_EXPORTABLE StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
716 THREAD_RESULT (THREAD_CALL * pfMain)(void *));
717 #endif
718
719 BOOL LIBNETXMS_EXPORTABLE nxlog_open(const TCHAR *logName, DWORD flags, const TCHAR *msgModule,
720 unsigned int msgCount, const TCHAR **messages);
721 void LIBNETXMS_EXPORTABLE nxlog_close(void);
722 void LIBNETXMS_EXPORTABLE nxlog_write(DWORD msg, WORD wType, const char *format, ...);
723 BOOL LIBNETXMS_EXPORTABLE nxlog_set_rotation_policy(int maxLogSize, int historySize);
724 BOOL LIBNETXMS_EXPORTABLE nxlog_rotate();
725
726 #ifdef __cplusplus
727 }
728 #endif
729
730
731 //
732 // C++ only finctions
733 //
734
735 #ifdef __cplusplus
736
737 String LIBNETXMS_EXPORTABLE EscapeStringForXML2(const TCHAR *string, int length = -1);
738
739 #endif
740
741 #endif /* _nms_util_h_ */