2014-01-26 12:31:56 +01:00
|
|
|
/*!
|
|
|
|
* \copy
|
|
|
|
* Copyright (c) 2009-2013, Cisco Systems
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* \file crt_utils_safe_x.cpp
|
|
|
|
*
|
|
|
|
* \brief common tool/function utilization
|
|
|
|
*
|
|
|
|
* \date 03/10/2009 Created
|
|
|
|
*
|
|
|
|
*************************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif //!_MSC_VER
|
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#endif //_WIN32
|
|
|
|
|
|
|
|
#include "macros.h"
|
|
|
|
#include "crt_util_safe_x.h" // Safe CRT routines like utils for cross platforms
|
|
|
|
|
|
|
|
#if defined(_WIN32) && defined(_MSC_VER)
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER>=1500)
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
int32_t WelsSnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, ...) {
|
2014-01-26 12:31:56 +01:00
|
|
|
va_list pArgPtr;
|
|
|
|
int32_t iRc;
|
|
|
|
|
|
|
|
va_start (pArgPtr, kpFormat);
|
|
|
|
|
|
|
|
iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
|
|
|
|
|
|
|
|
va_end (pArgPtr);
|
|
|
|
|
|
|
|
return iRc;
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
char* WelsStrncpy (char* pDest, int32_t iSizeInBytes, const char* kpSrc) {
|
2014-01-28 11:08:35 +01:00
|
|
|
strncpy_s (pDest, iSizeInBytes, kpSrc, _TRUNCATE);
|
2014-01-26 12:31:56 +01:00
|
|
|
|
|
|
|
return pDest;
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
|
2014-01-26 13:33:08 +01:00
|
|
|
return vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
|
2014-01-26 12:31:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
WelsFileHandle* WelsFopen (const char* kpFilename, const char* kpMode) {
|
2014-01-26 12:31:56 +01:00
|
|
|
WelsFileHandle* pFp = NULL;
|
|
|
|
if (fopen_s (&pFp, kpFilename, kpMode) != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pFp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WelsFclose (WelsFileHandle* pFp) {
|
|
|
|
return fclose (pFp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
|
|
|
|
return _ftime_s (pTp);
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
int32_t WelsStrftime (char* pBuffer, int32_t iSize, const char* kpFormat, const SWelsTime* kpTp) {
|
2014-01-26 12:31:56 +01:00
|
|
|
struct tm sTimeNow;
|
2014-01-26 13:48:38 +01:00
|
|
|
int32_t iRc;
|
2014-01-26 12:31:56 +01:00
|
|
|
|
|
|
|
localtime_s (&sTimeNow, &kpTp->time);
|
|
|
|
|
2014-01-26 13:48:38 +01:00
|
|
|
iRc = strftime (pBuffer, iSize, kpFormat, &sTimeNow);
|
|
|
|
if (iRc == 0)
|
|
|
|
pBuffer[0] = '\0';
|
|
|
|
return iRc;
|
2014-01-26 12:31:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
int32_t WelsSnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, ...) {
|
2014-01-26 12:31:56 +01:00
|
|
|
va_list pArgPtr;
|
|
|
|
int32_t iRc;
|
|
|
|
|
|
|
|
va_start (pArgPtr, kpFormat);
|
|
|
|
|
2014-01-26 13:44:14 +01:00
|
|
|
iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
|
2014-01-26 13:50:30 +01:00
|
|
|
if (iRc < 0)
|
|
|
|
pBuffer[iSizeOfBuffer - 1] = '\0';
|
2014-01-26 12:31:56 +01:00
|
|
|
|
|
|
|
va_end (pArgPtr);
|
|
|
|
|
|
|
|
return iRc;
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
char* WelsStrncpy (char* pDest, int32_t iSizeInBytes, const char* kpSrc) {
|
2014-01-28 11:08:35 +01:00
|
|
|
strncpy (pDest, kpSrc, iSizeInBytes); //confirmed_safe_unsafe_usage
|
|
|
|
pDest[iSizeInBytes - 1] = '\0';
|
2014-01-26 12:31:56 +01:00
|
|
|
|
|
|
|
return pDest;
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
|
2014-01-26 13:50:30 +01:00
|
|
|
int32_t iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
|
|
|
|
if (iRc < 0)
|
|
|
|
pBuffer[iSizeOfBuffer - 1] = '\0';
|
|
|
|
return iRc;
|
2014-01-26 12:31:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
WelsFileHandle* WelsFopen (const char* kpFilename, const char* kpMode) {
|
2014-01-26 12:31:56 +01:00
|
|
|
return fopen (kpFilename, kpMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WelsFclose (WelsFileHandle* pFp) {
|
|
|
|
return fclose (pFp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
|
|
|
|
_ftime (pTp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
int32_t WelsStrftime (char* pBuffer, int32_t iSize, const char* kpFormat, const SWelsTime* kpTp) {
|
2014-01-26 12:31:56 +01:00
|
|
|
struct tm* pTnow;
|
2014-01-26 13:48:38 +01:00
|
|
|
int32_t iRc;
|
2014-01-26 12:31:56 +01:00
|
|
|
|
|
|
|
pTnow = localtime (&kpTp->time);
|
|
|
|
|
2014-01-26 13:48:38 +01:00
|
|
|
iRc = strftime (pBuffer, iSize, kpFormat, pTnow);
|
|
|
|
if (iRc == 0)
|
|
|
|
pBuffer[0] = '\0';
|
|
|
|
return iRc;
|
2014-01-26 12:31:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
|
|
|
#else //GCC
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
int32_t WelsSnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, ...) {
|
2014-01-26 12:31:56 +01:00
|
|
|
va_list pArgPtr;
|
|
|
|
int32_t iRc;
|
|
|
|
|
|
|
|
va_start (pArgPtr, kpFormat);
|
|
|
|
|
|
|
|
iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr);
|
|
|
|
|
|
|
|
va_end (pArgPtr);
|
|
|
|
|
|
|
|
return iRc;
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
char* WelsStrncpy (char* pDest, int32_t iSizeInBytes, const char* kpSrc) {
|
2014-01-28 11:08:35 +01:00
|
|
|
strncpy (pDest, kpSrc, iSizeInBytes); //confirmed_safe_unsafe_usage
|
|
|
|
pDest[iSizeInBytes - 1] = '\0';
|
|
|
|
return pDest;
|
2014-01-26 12:31:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
|
2014-01-26 13:33:08 +01:00
|
|
|
return vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
|
2014-01-26 12:31:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
WelsFileHandle* WelsFopen (const char* kpFilename, const char* kpMode) {
|
2014-01-26 12:31:56 +01:00
|
|
|
return fopen (kpFilename, kpMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WelsFclose (WelsFileHandle* pFp) {
|
|
|
|
return fclose (pFp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WelsGetTimeOfDay (SWelsTime* pTp) {
|
|
|
|
struct timeval sTv;
|
|
|
|
|
|
|
|
if (gettimeofday (&sTv, NULL)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pTp->time = sTv.tv_sec;
|
|
|
|
pTp->millitm = (uint16_t)sTv.tv_usec / 1000;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
int32_t WelsStrftime (char* pBuffer, int32_t iSize, const char* kpFormat, const SWelsTime* kpTp) {
|
2014-01-26 12:31:56 +01:00
|
|
|
struct tm* pTnow;
|
2014-01-26 13:48:38 +01:00
|
|
|
int32_t iRc;
|
2014-01-26 12:31:56 +01:00
|
|
|
|
|
|
|
pTnow = localtime (&kpTp->time);
|
|
|
|
|
2014-01-26 13:48:38 +01:00
|
|
|
iRc = strftime (pBuffer, iSize, kpFormat, pTnow);
|
|
|
|
if (iRc == 0)
|
|
|
|
pBuffer[0] = '\0';
|
|
|
|
return iRc;
|
2014-01-26 12:31:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-02-08 22:28:42 +01:00
|
|
|
char* WelsStrcat (char* pDest, int32_t iSizeInBytes, const char* kpSrc) {
|
2014-01-28 11:21:34 +01:00
|
|
|
int32_t iCurLen = strlen(pDest);
|
|
|
|
return WelsStrncpy(pDest + iCurLen, iSizeInBytes - iCurLen, kpSrc);
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:26:33 +01:00
|
|
|
int32_t WelsFwrite (const void* kpBuffer, int32_t iSize, int32_t iCount, WelsFileHandle* pFp) {
|
2014-01-26 12:31:56 +01:00
|
|
|
return fwrite (kpBuffer, iSize, iCount, pFp);
|
|
|
|
}
|
|
|
|
|
2014-01-26 14:38:49 +01:00
|
|
|
uint16_t WelsGetMillisecond (const SWelsTime* kpTp) {
|
2014-01-26 12:31:56 +01:00
|
|
|
return kpTp->millitm;
|
|
|
|
}
|
|
|
|
|
2014-01-26 15:04:52 +01:00
|
|
|
int32_t WelsFseek (WelsFileHandle* fp, int32_t offset, int32_t origin) {
|
|
|
|
return fseek(fp, offset, origin);
|
|
|
|
}
|
|
|
|
|
2014-01-26 12:31:56 +01:00
|
|
|
int32_t WelsFflush (WelsFileHandle* pFp) {
|
|
|
|
return fflush (pFp);
|
|
|
|
}
|