📄 sblogosutils.cpp
字号:
/* Some versions of the GNU C library do not provide the required vswprintf( ) function, so we emulate it by converting the format string to narrow characters, do a narrow sprintf( ), then convert back */ /* Use a temporary buffer for output to protect against relatively small overruns */ char *buf, tmpbuf[BUFSIZE]; if (BUFSIZE < maxlen + 1024) buf = new char[maxlen + 1024]; else buf = tmpbuf; if (!buf) return -1; /* convert format to narrow, a bit generous compared to the ANSI/ISO C specifications for the printf( ) family format strings but we're not trying to do full validation here anyway */ char *fmt, tmpfmt[BUFSIZE]; size_t fmtlen = wcslen(format); if (BUFSIZE < fmtlen + 1) fmt = new char[fmtlen + 1]; else fmt = tmpfmt; if (!fmt) return -1; format_wcs2str(format, fmt); /* generate the final string based on the narrow format and arguments */ rc = vsprintf(buf, fmt, args); /* copy back to wide characters */ size_t finallen = strlen(buf); if (finallen >= maxlen) finallen = maxlen - 1; for (size_t i = 0; i < finallen; i++) wcs[i] = buf[i]; wcs[finallen] = L'\0'; /* clean up */ if (buf != tmpbuf) delete [] buf; if (fmt != tmpfmt) delete [] fmt; #else /* ! defined(__GNUC__) || (__GNUC__ > 2) */ wchar_t* newFormat = ConvertFormatForWidePrintf(format); rc = vswprintf(wcs, maxlen, newFormat, args); delete [] newFormat; if ((size_t) rc >= maxlen - 1) /* overflow */ wcs[maxlen - 1] = L'\0'; #endif /* defined(__GNUC__) && (__GNUC__ <= 2) */ #endif /* WIN32 */ return rc; } /***************************** // SBlogMkDir *****************************/ int SBlogMkDir(const char *path) { #ifdef WIN32 return (CreateDirectory (path, NULL) ? 1 : 0); #else return (mkdir (path, (mode_t)0755) == 0 ? 1 : 0); #endif } /***************************** // SBlogIsDir *****************************/ int SBlogIsDir(const SBlogFileStats *statInfo) { #ifdef WIN32 #ifdef _S_ISDIR return (_S_ISDIR(statInfo->st_mode) ? 1 : 0); #else return ((statInfo->st_mode & _S_IFDIR) ? 1 : 0); #endif #else // ! WIN32 #ifdef S_ISDIR return (S_ISDIR(statInfo->st_mode) ? 1 : 0); #else return ((statInfo->st_mode & S_IFDIR) ? 1 : 0); #endif #endif // WIN32 } /***************************** // SBlogWchar2Latin1 *****************************/ extern "C" VXIbool SBlogWchar2Latin1(const wchar_t * input, char * output, VXIunsigned maxlen) { const wchar_t * i; char * j; unsigned int idx = 0; if( output == NULL ) return FALSE; if(input == NULL) { *output = '\0'; } else { for (i = input, j = output; *i && idx < maxlen; ++i, ++j, ++idx) { char t = w2c(*i); *j = t; } *j = '\0'; } return TRUE; } /***************************** // SBlogWchar2UTF8 *****************************/ extern "C" VXIbool SBlogWchar2UTF8(const wchar_t * input, char * output, VXIunsigned maxOutputBytes, VXIunsigned * outputBytes) { // firstByteMark // A list of values to mask onto the first byte of an encoded sequence, // indexed by the number of bytes used to create the sequence. static const char firstByteMark[7] = { char(0x00), char(0x00), char(0xC0), char(0xE0), char(0xF0), char(0xF8), char(0xFC) }; // Get pointers to our start and end points of the input buffer. const wchar_t* srcPtr = input; const wchar_t* srcEnd = srcPtr + wcslen(input); *outputBytes = 0; while (srcPtr < srcEnd) { wchar_t curVal = *srcPtr++; // Watchout for surrogates, if found truncate if ((curVal >= 0xD800) && (curVal <= 0xDBFF)) { break; } // Figure out how many bytes we need unsigned int encodedBytes; if (curVal < 0x80) encodedBytes = 1; else if (curVal < 0x800) encodedBytes = 2; else if (curVal < 0x10000) encodedBytes = 3; else if (curVal < 0x200000) encodedBytes = 4; else if (curVal < 0x4000000) encodedBytes = 5; else if (curVal <= 0x7FFFFFFF) encodedBytes = 6; else { // THIS SHOULD NOT HAPPEN! output[*outputBytes] = '\0'; return FALSE; } // If we don't have enough room in the buffer, truncate if ( *outputBytes + encodedBytes >= maxOutputBytes ) { break; } // And spit out the bytes. We spit them out in reverse order // here, so bump up the output pointer and work down as we go. char buffer[7] = { 0, 0, 0, 0, 0, 0, 0 }; char * outPtr = buffer + encodedBytes; switch(encodedBytes) { case 6 : *--outPtr = char((curVal | 0x80) & 0xBF); curVal >>= 6; case 5 : *--outPtr = char((curVal | 0x80) & 0xBF); curVal >>= 6; case 4 : *--outPtr = char((curVal | 0x80) & 0xBF); curVal >>= 6; case 3 : *--outPtr = char((curVal | 0x80) & 0xBF); curVal >>= 6; case 2 : *--outPtr = char((curVal | 0x80) & 0xBF); curVal >>= 6; case 1 : *--outPtr = char(curVal | firstByteMark[encodedBytes]); } for (int i = 0; buffer[i] != 0; i++) { output[*outputBytes] = buffer[i]; (*outputBytes)++; } } // NULL terminate output[*outputBytes] = '\0'; return TRUE; } /***************************** // Log errors to the console, only used for errors that occur prior // to initializing the log subsystem *****************************/ VXIlogResult SBlogLogErrorToConsole(const VXIchar *moduleName, VXIunsigned errorID, const VXIchar *errorIDText) { VXIlogResult rc = VXIlog_RESULT_SUCCESS; const VXIchar *severity; if (errorID < 200) severity = L"CRITICAL"; else if (errorID < 300) severity = L"SEVERE"; else severity = L"WARNING"; #ifdef WIN32 fwprintf(stderr, L"%ls: %ls|%u|%ls\n", severity, moduleName, errorID, errorIDText); #else fprintf(stderr, "%ls: %ls|%u|%ls\n", severity, moduleName, errorID, errorIDText); #endif return rc; } VXIlogResult SBlogVLogErrorToConsole(const VXIchar *moduleName, VXIunsigned errorID, const VXIchar *errorIDText, const VXIchar *format, va_list arguments) { int argCount; VXIlogResult rc = VXIlog_RESULT_SUCCESS; const VXIchar *in; VXIchar *out, tempFormat[256]; /* Get the severity */ const VXIchar *severity; if (errorID < 200) severity = L"CRITICAL"; else if (errorID < 300) severity = L"SEVERE"; else severity = L"WARNING"; /* Properly delimit the format arguments for human consumption */ argCount = 0; if (format) { for (in = format, out = tempFormat; *in != L'\0'; in++, out++) { if (*in == L'%') { argCount++; *out = (argCount % 2 == 0 ? L'=' : L'|'); out++; } *out = *in; } *out = L'\n'; out++; *out = L'\0'; #ifdef WIN32 fwprintf(stderr, L"%ls: %ls|%u|%ls", severity, moduleName, errorID, errorIDText); vfwprintf(stderr, tempFormat, arguments); #else /* SBlogVswprintf( ) handles the format conversion if necessary from Microsoft Visual C++ run-time library/GNU gcc 2.x C library notation to GNU gcc 3.x C library (and most other UNIX C library) notation: %s -> %ls, %S -> %s. Unfortunately there is no single format string representation that can be used universally. */ VXIchar tempBuf[4096]; SBlogVswprintf(tempBuf, 4096, tempFormat, arguments); fprintf(stderr, "%ls: %ls|%u|%ls%ls\n", severity, moduleName, errorID, errorIDText, tempBuf); #endif } else { #ifdef WIN32 fwprintf(stderr, L"%ls: %ls|%u|%ls\n", severity, moduleName, errorID, errorIDText); #else fprintf(stderr, "%ls: %ls|%u|%ls\n", severity, moduleName, errorID, errorIDText); #endif } return rc; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -