📄 sblogosutils.cpp
字号:
/****************License************************************************ * * Copyright 2000-2003. ScanSoft, Inc. * * Use of this software is subject to notices and obligations set forth * in the SpeechWorks Public License - Software Version 1.2 which is * included with this software. * * ScanSoft is a registered trademark of ScanSoft, Inc., and OpenSpeech, * SpeechWorks and the SpeechWorks logo are registered trademarks or * trademarks of SpeechWorks International, Inc. in the United States * and other countries. * ***********************************************************************/ // -----1=0-------2=0-------3=0-------4=0-------5=0-------6=0-------7=0-------8 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #include <windows.h> #else #include <time.h> // For CLK_TCK/CLOCKS_PER_SEC #include <sys/times.h> // For times( ) #ifndef CLK_TCK #define CLK_TCK CLOCKS_PER_SEC #endif #endif #include <sys/timeb.h> // for ftime( )/_ftime( ) #include <sys/stat.h> // for stat( ) #include "SBlogOSUtils.h" #define BUFSIZE (4096 + 1024) // typical maxlen is 4096, want room over that // Convert wide to narrow characters #define w2c(w) (((w) & 0xff00)?'\277':((unsigned char) ((w) & 0x00ff))) /***************************** // SBlogGetTime *****************************/ extern "C" int SBlogGetTime(time_t *timestamp, VXIunsigned *timestampMsec) { #ifdef WIN32 struct _timeb tbuf; _ftime(&tbuf); *timestamp = tbuf.time; *timestampMsec = (VXIunsigned) tbuf.millitm; #else struct timeb tbuf; ftime(&tbuf); *timestamp = tbuf.time; *timestampMsec = (VXIunsigned) tbuf.millitm; #endif return 0; } /***************************** // SBlogGetTimeStampStr *****************************/ extern "C" int SBlogGetTimeStampStr(time_t timestamp, VXIunsigned timestampMsec, char *timestampStr) { #ifdef WIN32 char *timeStr = ctime(×tamp); #else char timeStr_r[64] = ""; char *timeStr = ctime_r(×tamp, timeStr_r); #endif if (timeStr) { // Strip the weekday name from the front, year from the end, // append hundredths of a second (the thousandths position is // inaccurate, remains constant across entire runs of the process) strncpy(timestampStr, &timeStr[4], 15); sprintf(×tampStr[15], ".%02u", timestampMsec / 10); } else { timestampStr[0] = '\0'; return -1; } return 0; } /***************************** // SBlogGetFileStats *****************************/ extern "C" int SBlogGetFileStats(const char *path, SBlogFileStats *fileStats) { int rc; #ifdef WIN32 struct _stat stats; #else struct stat stats; #endif if ((! path) || (! fileStats)) return -1; #ifdef WIN32 rc = _stat(path, &stats); #else rc = stat(path, &stats); #endif if (rc != 0) { return -1; } fileStats->st_size = stats.st_size; fileStats->st_mode = stats.st_mode; fileStats->st_atim = stats.st_atime; fileStats->st_mtim = stats.st_mtime; fileStats->st_ctim = stats.st_ctime; return 0; } /***************************** // SBlogGetCPUTimes *****************************/ extern "C" int SBlogGetCPUTimes(long *userTime /* ms spent in user mode */, long *kernelTime /* ms spent in kernel mode*/ ) { #ifdef WIN32 FILETIME dummy; FILETIME k, u; LARGE_INTEGER lk, lu; if ((! userTime) || (! kernelTime)) return -1; if (GetThreadTimes(GetCurrentThread(), &dummy, &dummy, &k, &u) == FALSE) return -1; lk.LowPart = k.dwLowDateTime; lk.HighPart = k.dwHighDateTime; *kernelTime = (long) (lk.QuadPart / 10000); lu.LowPart = u.dwLowDateTime; lu.HighPart = u.dwHighDateTime; *userTime = (long) (lu.QuadPart / 10000); #else struct tms timeBuf; if ((! userTime) || (! kernelTime)) return -1; times(&timeBuf); *userTime = (long)timeBuf.tms_utime * 1000 / CLK_TCK; *kernelTime = (long)timeBuf.tms_stime * 1000 / CLK_TCK; #endif return 0; } /***************************** // format_wcs2str (internal-only) *****************************/ static int format_wcs2str(const wchar_t *wformat, char *nformat) { size_t len, i; bool replacement = false; len = wcslen(wformat); for (i = 0; i <= len; i++) { nformat[i] = w2c(wformat[i]); if (nformat[i] == '%') { if (replacement) replacement = false; // double %% else replacement = true; } else if ((replacement == true) && (((nformat[i] >= 'a') && (nformat[i] <= 'z')) || ((nformat[i] >= 'A') && (nformat[i] <= 'Z')))) { switch (nformat[i]) { case 's': // wide insert for wide format -> wide insert for narrow format nformat[i] = 'S'; break; case 'S': // narrow insert for wide format -> narrow insert for narrow format nformat[i] = 's'; break; default: break; } replacement = false; } } nformat[i] = '\0'; return len; } /***************************** // SBlogVswprintf *****************************/ #ifndef WIN32 #if ! (defined(__GNUC__) && (__GNUC__ <= 2)) static wchar_t* ConvertFormatForWidePrintf(const wchar_t* format) { int formatLen = wcslen(format); // The maximum length of the new format string is 1 and 2/3 that // of the original. wchar_t* newFormat = new wchar_t [2 * formatLen]; int nfIndex = 0; for (int fIndex = 0; fIndex < formatLen; ++fIndex) { // We found %s in format. if ((format[fIndex] == L'%') && (fIndex != (formatLen - 1))) { newFormat[nfIndex++] = L'%'; fIndex++; while ((fIndex < formatLen - 1) && ((format[fIndex] >= L'0') && (format[fIndex] <= L'9')) || (format[fIndex] == L'+') || (format[fIndex] == L'-') || (format[fIndex] == L'.') || (format[fIndex] == L'*') || (format[fIndex] == L'#')) { newFormat[nfIndex++] = format[fIndex++]; } switch(format[fIndex]) { case L's': { newFormat[nfIndex++] = L'l'; newFormat[nfIndex++] = L's'; break; } case L'S': { newFormat[nfIndex++] = L's'; break; } case L'c': { newFormat[nfIndex++] = L'l'; newFormat[nfIndex++] = L'c'; break; } case L'C': { newFormat[nfIndex++] = L'c'; break; } default: { newFormat[nfIndex++] = format[fIndex]; break; } } } else { newFormat[nfIndex++] = format[fIndex]; } } newFormat[nfIndex] = 0; return newFormat; } #endif /* ! defined(__GNUC__) && (__GNUC__ <= 2) */ #endif /* ! WIN32 */ extern "C" int SBlogVswprintf(wchar_t* wcs, size_t maxlen, const wchar_t* format, va_list args) { int rc; if (maxlen < 1) return -1; *wcs = 0; #ifdef WIN32 /* Straight-forward Win32 implementation */ rc = _vsnwprintf(wcs, maxlen, format, args); if ((size_t) rc >= maxlen - 1) /* overflow */ wcs[maxlen - 1] = L'\0'; #else /* ! WIN32 */ #if defined(__GNUC__) && (__GNUC__ <= 2)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -