Merge pull request #724 from mstorsjo/string-cleanup

Unify return values from Wels*Snprintf and simplify calling code
This commit is contained in:
Licai Guo 2014-04-22 16:41:44 +08:00
commit 75c941a836
4 changed files with 46 additions and 69 deletions

View File

@ -68,6 +68,8 @@ int32_t WelsSnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpForma
va_start (pArgPtr, kpFormat); va_start (pArgPtr, kpFormat);
iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr); iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
if (iRc < 0)
iRc = iSizeOfBuffer;
va_end (pArgPtr); va_end (pArgPtr);
@ -81,7 +83,10 @@ char* WelsStrncpy (char* pDest, int32_t iSizeInBytes, const char* kpSrc) {
} }
int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) { int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
return vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr); int32_t iRc = vsnprintf_s (pBuffer, iSizeOfBuffer, _TRUNCATE, kpFormat, pArgPtr);
if (iRc < 0)
iRc = iSizeOfBuffer;
return iRc;
} }
WelsFileHandle* WelsFopen (const char* kpFilename, const char* kpMode) { WelsFileHandle* WelsFopen (const char* kpFilename, const char* kpMode) {
@ -122,8 +127,10 @@ int32_t WelsSnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpForma
va_start (pArgPtr, kpFormat); va_start (pArgPtr, kpFormat);
iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
if (iRc < 0) if (iRc < 0) {
pBuffer[iSizeOfBuffer - 1] = '\0'; pBuffer[iSizeOfBuffer - 1] = '\0';
iRc = iSizeOfBuffer;
}
va_end (pArgPtr); va_end (pArgPtr);
@ -139,8 +146,10 @@ char* WelsStrncpy (char* pDest, int32_t iSizeInBytes, const char* kpSrc) {
int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) { int32_t WelsVsnprintf (char* pBuffer, int32_t iSizeOfBuffer, const char* kpFormat, va_list pArgPtr) {
int32_t iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage int32_t iRc = vsnprintf (pBuffer, iSizeOfBuffer, kpFormat, pArgPtr); //confirmed_safe_unsafe_usage
if (iRc < 0) if (iRc < 0) {
pBuffer[iSizeOfBuffer - 1] = '\0'; pBuffer[iSizeOfBuffer - 1] = '\0';
iRc = iSizeOfBuffer;
}
return iRc; return iRc;
} }

View File

@ -110,20 +110,16 @@ CWelsDecoder::CWelsDecoder (void)
iCurUsed = WelsSnprintf (chFileName, iBufLeft, "bs_0x%p_", (void*)this); iCurUsed = WelsSnprintf (chFileName, iBufLeft, "bs_0x%p_", (void*)this);
iCurUsedSize = WelsSnprintf (chFileNameSize, iBufLeftSize, "size_0x%p_", (void*)this); iCurUsedSize = WelsSnprintf (chFileNameSize, iBufLeftSize, "size_0x%p_", (void*)this);
if (iCurUsed > 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
if (iBufLeft > 0) { if (iBufLeft > 0) {
iCurUsed = WelsStrftime (&chFileName[iBufUsed], iBufLeft, "%y%m%d%H%M%S", &sCurTime); iCurUsed = WelsStrftime (&chFileName[iBufUsed], iBufLeft, "%y%m%d%H%M%S", &sCurTime);
iBufUsed += iCurUsed; iBufUsed += iCurUsed;
iBufLeft -= iCurUsed; iBufLeft -= iCurUsed;
} }
if (iCurUsedSize > 0) { iBufUsedSize += iCurUsedSize;
iBufUsedSize += iCurUsedSize; iBufLeftSize -= iCurUsedSize;
iBufLeftSize -= iCurUsedSize;
}
if (iBufLeftSize > 0) { if (iBufLeftSize > 0) {
iCurUsedSize = WelsStrftime (&chFileNameSize[iBufUsedSize], iBufLeftSize, "%y%m%d%H%M%S", &sCurTime); iCurUsedSize = WelsStrftime (&chFileNameSize[iBufUsedSize], iBufLeftSize, "%y%m%d%H%M%S", &sCurTime);
iBufUsedSize += iCurUsedSize; iBufUsedSize += iCurUsedSize;
@ -132,19 +128,15 @@ CWelsDecoder::CWelsDecoder (void)
if (iBufLeft > 0) { if (iBufLeft > 0) {
iCurUsed = WelsSnprintf (&chFileName[iBufUsed], iBufLeft, ".%03.3u.264", WelsGetMillisecond (&sCurTime)); iCurUsed = WelsSnprintf (&chFileName[iBufUsed], iBufLeft, ".%03.3u.264", WelsGetMillisecond (&sCurTime));
if (iCurUsed > 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
} }
if (iBufLeftSize > 0) { if (iBufLeftSize > 0) {
iCurUsedSize = WelsSnprintf (&chFileNameSize[iBufUsedSize], iBufLeftSize, ".%03.3u.len", iCurUsedSize = WelsSnprintf (&chFileNameSize[iBufUsedSize], iBufLeftSize, ".%03.3u.len",
WelsGetMillisecond (&sCurTime)); WelsGetMillisecond (&sCurTime));
if (iCurUsedSize > 0) { iBufUsedSize += iCurUsedSize;
iBufUsedSize += iCurUsedSize; iBufLeftSize -= iCurUsedSize;
iBufLeftSize -= iCurUsedSize;
}
} }

View File

@ -144,26 +144,20 @@ void WelsLogDefault (void* pCtx, const int32_t kiLevel, const char* kpFmtStr, va
WelsGetTimeOfDay(&tTime); WelsGetTimeOfDay(&tTime);
iCurUsed = WelsSnprintf (&pBuf[iBufUsed], iBufLeft, "[0x%p @ ", pEncCtx); // confirmed_safe_unsafe_usage iCurUsed = WelsSnprintf (&pBuf[iBufUsed], iBufLeft, "[0x%p @ ", pEncCtx); // confirmed_safe_unsafe_usage
if (iCurUsed >= 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
if (iBufLeft > 0) { if (iBufLeft > 0) {
iCurUsed = GetCodeName (&pBuf[iBufUsed], iBufLeft); iCurUsed = GetCodeName (&pBuf[iBufUsed], iBufLeft);
if (iCurUsed > 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
pBuf[iBufUsed] = ' '; pBuf[iBufUsed] = ' ';
++ iBufUsed; ++ iBufUsed;
-- iBufLeft; -- iBufLeft;
iCurUsed = GetLibName (&pBuf[iBufUsed], iBufLeft); iCurUsed = GetLibName (&pBuf[iBufUsed], iBufLeft);
if (iCurUsed > 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
pBuf[iBufUsed] = ' '; pBuf[iBufUsed] = ' ';
++ iBufUsed; ++ iBufUsed;
-- iBufLeft; -- iBufLeft;
@ -172,10 +166,8 @@ void WelsLogDefault (void* pCtx, const int32_t kiLevel, const char* kpFmtStr, va
++ iBufUsed; ++ iBufUsed;
-- iBufLeft; -- iBufLeft;
iCurUsed = GetVerNum (&pBuf[iBufUsed], iBufLeft); iCurUsed = GetVerNum (&pBuf[iBufUsed], iBufLeft);
if (iCurUsed > 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
pBuf[iBufUsed] = ' '; pBuf[iBufUsed] = ' ';
++ iBufUsed; ++ iBufUsed;
-- iBufLeft; -- iBufLeft;
@ -183,20 +175,16 @@ void WelsLogDefault (void* pCtx, const int32_t kiLevel, const char* kpFmtStr, va
if (iBufLeft > 0) { if (iBufLeft > 0) {
iCurUsed = WelsStrftime (&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", &tTime); iCurUsed = WelsStrftime (&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", &tTime);
if (iCurUsed > 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
} else { } else {
return; return;
} }
if (iBufLeft > 0) { if (iBufLeft > 0) {
iCurUsed = WelsSnprintf (&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tTime.millitm); // confirmed_safe_unsafe_usage iCurUsed = WelsSnprintf (&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tTime.millitm); // confirmed_safe_unsafe_usage
if (iCurUsed >= 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
} else { } else {
return; return;
} }
@ -209,18 +197,14 @@ void WelsLogDefault (void* pCtx, const int32_t kiLevel, const char* kpFmtStr, va
pStr = GetLogTag (kiLevel, &i_shift); pStr = GetLogTag (kiLevel, &i_shift);
if (NULL != pStr) { if (NULL != pStr) {
iCurUsed = WelsSnprintf (&pBuf[iBufUsed], iBufLeft, "%s ", pStr); iCurUsed = WelsSnprintf (&pBuf[iBufUsed], iBufLeft, "%s ", pStr);
if (iCurUsed >= 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
} }
} }
if (iBufLeft > 0) { if (iBufLeft > 0) {
iCurUsed = WelsVsnprintf (&pBuf[iBufUsed], iBufLeft, kpFmtStr, argv); // confirmed_safe_unsafe_usage iCurUsed = WelsVsnprintf (&pBuf[iBufUsed], iBufLeft, kpFmtStr, argv); // confirmed_safe_unsafe_usage
if (iCurUsed > 0) { iBufUsed += iCurUsed;
iBufUsed += iCurUsed; iBufLeft -= iCurUsed;
iBufLeft -= iCurUsed;
}
} }
#ifdef ENABLE_TRACE_FILE #ifdef ENABLE_TRACE_FILE
if (NULL != pEncCtx && NULL != pEncCtx->pFileLog) { if (NULL != pEncCtx && NULL != pEncCtx->pFileLog) {

View File

@ -90,20 +90,16 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder()
iCurUsedSize = WelsSnprintf (strLenFileName, iBufferLeftSize, "enc_size_0x%p_", (void*)this); iCurUsedSize = WelsSnprintf (strLenFileName, iBufferLeftSize, "enc_size_0x%p_", (void*)this);
if (iCurUsed > 0) { iBufferUsed += iCurUsed;
iBufferUsed += iCurUsed; iBufferLeft -= iCurUsed;
iBufferLeft -= iCurUsed;
}
if (iBufferLeft > 0) { if (iBufferLeft > 0) {
iCurUsed = WelsStrftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S", &tTime); iCurUsed = WelsStrftime (&strStreamFileName[iBufferUsed], iBufferLeft, "%y%m%d%H%M%S", &tTime);
iBufferUsed += iCurUsed; iBufferUsed += iCurUsed;
iBufferLeft -= iCurUsed; iBufferLeft -= iCurUsed;
} }
if (iCurUsedSize > 0) { iBufferUsedSize += iCurUsedSize;
iBufferUsedSize += iCurUsedSize; iBufferLeftSize -= iCurUsedSize;
iBufferLeftSize -= iCurUsedSize;
}
if (iBufferLeftSize > 0) { if (iBufferLeftSize > 0) {
iCurUsedSize = WelsStrftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S", &tTime); iCurUsedSize = WelsStrftime (&strLenFileName[iBufferUsedSize], iBufferLeftSize, "%y%m%d%H%M%S", &tTime);
iBufferUsedSize += iCurUsedSize; iBufferUsedSize += iCurUsedSize;
@ -113,19 +109,15 @@ CWelsH264SVCEncoder::CWelsH264SVCEncoder()
if (iBufferLeft > 0) { if (iBufferLeft > 0) {
iCurUsed = WelsSnprintf (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264", iCurUsed = WelsSnprintf (&strStreamFileName[iBufferUsed], iBufferLeft, ".%03.3u.264",
WelsGetMillisecond(&tTime)); WelsGetMillisecond(&tTime));
if (iCurUsed > 0) { iBufferUsed += iCurUsed;
iBufferUsed += iCurUsed; iBufferLeft -= iCurUsed;
iBufferLeft -= iCurUsed;
}
} }
if (iBufferLeftSize > 0) { if (iBufferLeftSize > 0) {
iCurUsedSize = WelsSnprintf (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len", iCurUsedSize = WelsSnprintf (&strLenFileName[iBufferUsedSize], iBufferLeftSize, ".%03.3u.len",
WelsGetMillisecond(&tTime)); WelsGetMillisecond(&tTime));
if (iCurUsedSize > 0) { iBufferUsedSize += iCurUsedSize;
iBufferUsedSize += iCurUsedSize; iBufferLeftSize -= iCurUsedSize;
iBufferLeftSize -= iCurUsedSize;
}
} }
m_pFileBs = WelsFopen (strStreamFileName, "wb"); m_pFileBs = WelsFopen (strStreamFileName, "wb");
@ -471,8 +463,8 @@ int CWelsH264SVCEncoder::EncodeFrameInternal(const SSourcePicture* pSrcPic, SFr
m_pFileBsSize = NULL; m_pFileBsSize = NULL;
} }
char strStreamFileName[128] = {0}; char strStreamFileName[128] = {0};
int32_t iLen = WelsSnprintf (strStreamFileName, 128, "adj%d_w%d.264", m_iSwitchTimes, WelsSnprintf (strStreamFileName, 128, "adj%d_w%d.264", m_iSwitchTimes,
m_pEncContext->pSvcParam->iPicWidth); m_pEncContext->pSvcParam->iPicWidth);
m_pFileBs = WelsFopen (strStreamFileName, "wb"); m_pFileBs = WelsFopen (strStreamFileName, "wb");
WelsSnprintf (strStreamFileName, 128, "adj%d_w%d_size.iLen", m_iSwitchTimes, WelsSnprintf (strStreamFileName, 128, "adj%d_w%d_size.iLen", m_iSwitchTimes,
m_pEncContext->pSvcParam->iPicWidth); m_pEncContext->pSvcParam->iPicWidth);