📄 sblogosutils.cpp
字号:
/****************License************************************************ * * Copyright 2000-2001. SpeechWorks International, Inc. * * Use of this software is subject to notices and obligations set forth * in the SpeechWorks Public License - Software Version 1.1 which is * included with this software. * * SpeechWorks is a registered trademark, and SpeechWorks Here, * DialogModules and the SpeechWorks logo are trademarks of SpeechWorks * International, Inc. in the United States and other countries. * ************************************************************************ * * $Id: SBlogOSUtils.cpp,v 1.8.6.3.6.1 2002/02/19 19:31:34 jerry Exp $ * * OS specific utilities. Broken out here to provide uniform support * across operating systems * ************************************************************************ */static const char *rcsid = 0 ? (char *) &rcsid :"$Id: SBlogOSUtils.cpp,v 1.8.6.3.6.1 2002/02/19 19:31:34 jerry Exp $";// -----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 <sys/times.h> // For times( )#endif#include <sys/timeb.h> // for ftime( )/_ftime( )#include <sys/stat.h> // for stat( )#ifndef CLK_TCK#define CLK_TCK CLOCKS_PER_SEC#endif#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_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) && (isalpha(nformat[i]))) { 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*****************************/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] = '\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';#elif defined(__GNUC__) && (__GNUC__ <= 2 || (__GNUC__ == 3 && __GNUC_MINOR__ == 0)) /* 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 /* Use a temporary buffer for output to protect against relatively small overruns */ wchar_t *buf, tmpbuf[BUFSIZE]; if (BUFSIZE < maxlen + 1024) buf = new wchar_t[maxlen + 1024]; else buf = tmpbuf; if (!buf) return -1; rc = vswprintf(buf, format, args); if (rc >= 0) { size_t len = wcslen(buf); if (len < maxlen) { wcscpy(wcs, buf); } else { wcsncpy(wcs, buf, maxlen - 1); wcs[maxlen - 1] = L'\0'; } } if (buf != tmpbuf) delete [] buf;#endif return rc;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -