📄 utils.cpp
字号:
/**********@@@SOFT@@@WARE@@@COPY@@@RIGHT@@@**********************************
* DIALOGIC CONFIDENTIAL
*
* Copyright (C) 2006-2007 Dialogic Corporation. All Rights Reserved.
* The source code contained or described herein and all documents related
* to the source code ("Material") are owned by Dialogic Corporation or its
* suppliers or licensors. Title to the Material remains with Dialogic Corporation
* or its suppliers and licensors. The Material contains trade secrets and
* proprietary and confidential information of Dialogic or its suppliers and
* licensors. The Material is protected by worldwide copyright and trade secret
* laws and treaty provisions. No part of the Material may be used, copied,
* reproduced, modified, published, uploaded, posted, transmitted, distributed,
* or disclosed in any way without Dialogic's prior express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by Dialogic in writing.
*
***********************************@@@SOFT@@@WARE@@@COPY@@@RIGHT@@@**********/
//***********************************************************************
//***********************************************************************
// Util.cpp: Implementation of test-processing utilities and other.
//
//////////////////////////////////////////////////////////////////////
#define _CRT_SECURE_NO_WARNINGS
// ignore security warning about ctime
#include <time.h>
#undef _CRT_SECURE_NO_WARNINGS
#include <sys/timeb.h>
#include "utils.h"
//*****************************************************************************
// Purpose :
// Signal TMO if timer is started before more than specific number of ms
// Parameters:
// none
// Returns:
// true if timer has expired
// false if timer is off or if it is not expired yet
//*****************************************************************************
bool CTimer::CheckTimer() {
if (m_timer.time) {
struct timeb now;
struct timeb diff;
ftime(&now);
timeb_diff(&diff, &m_timer, &now);
if ( !time_isgreater(time_expire, &diff) ){
StopTimer();
return true;
} // if timeout
} // if timer is started
return false;
}
//*****************************************************************************
// Purpose :
// obtain the time set by the system and convert to string "hh:mm:ss.mmm"
// Parameters:
// none
// Returns:
// const char *
//*****************************************************************************
const char * time_asstring() {
static char sysTime[64];
struct timeb tstruct;
char timeBuf[32];
ftime(&tstruct);
str_safecopy(timeBuf, sizeof(timeBuf), ctime( &tstruct.time) );
timeBuf[19]=0;
snprintf(sysTime, sizeof(sysTime), "%8s.%03d ",&(timeBuf[11]), tstruct.millitm);
return sysTime;
} // End of function()
//*****************************************************************************
// Purpose :
// calculate difference between 2 time()'s
// Parameters:
// none
// Returns:
// none ( returns result in *result)
//*****************************************************************************
//--------------------------------------------------------------------
void timeb_diff(struct timeb * result,
struct timeb * bgntime,
struct timeb * endtime){
int diff;
result->time = (endtime->time - bgntime->time);
diff = (endtime->millitm - bgntime->millitm);
while ( diff < 0 ) {
result->time--;
diff+=1000;
}
result->millitm = (unsigned short)diff;
result->time += result->millitm/1000;
result->millitm %= 1000;
return;
}
//*****************************************************************************
// Purpose :
// determine if given timeb is greater than given number of milliseconds
// Parameters:
// int milliseconds and timeb* time
// Returns:
// bool - true if milliseconds is greater
//*****************************************************************************
bool time_isgreater(int milliseconds, struct timeb * time){
int sec = milliseconds / 1000;
if (sec > time->time) {
return true;
}
if (sec < time->time) {
return false;
}
return (milliseconds % 1000) > time->millitm;
}
//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
// Allocate & delete new string
// The following routines are used to allocate and release string holders
//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
//*****************************************************************************
// Purpose :
// delete previously allocated string holder and clean Holder.
// Parameters:
// address to the holder (char **)
// Returns:
// bool (true)
//*****************************************************************************
bool str_deletestoredstring(char **Holder){
if (*Holder) {
delete [] *Holder;
*Holder = 0;
}
return true;
} // End of function()
//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
// string copy and movement
// The following routines are used to move strings
// Boundary check is guaranteed. Destination may be truncated.
// Destination is Guaranteed to be terminated with 0
//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
//
// Internal routine used to copy strings
//-----------------------------------------------------------------------------
static char * str_copywhile(char *dest, const char *src, size_t *maxlen){
if (0 == src){
return dest;
}
while( *maxlen > 1) {
*dest = *src;
if (0 == *src) {
break;
}
dest++;
src++;
--(*maxlen);
}
return dest;
} // End of function()
//*****************************************************************************
// Purpose :
// allocate memory to hold a string + trailing '\0'
// assign empty string
// Parameters:
// holder addr (char **)
// requested max length
// Returns:
// bool (true = success)
//*****************************************************************************
bool str_storestring(char **Holder, size_t length){
if (*Holder) {
delete [] *Holder;
*Holder = 0;
}
if (length) {
*Holder = new char[length+1];
**Holder = 0;
} else {
return true;
}
return (*Holder != 0);
} // End of function()
//*****************************************************************************
// Purpose :
// allocate memory to hold a string + trailing '\0'
// store given value
// Parameters:
// holder addr (char **) and value(string) to store
// Returns:
// bool (true = success)
//*****************************************************************************
bool str_storestring(char **Holder, const char *value){
if ( value ){
size_t len = strlen(value)+1;
if ( !str_storestring(Holder,len)){
return false;
}
str_safecopy(*Holder, len, value);
}else {
str_storestring(Holder,(size_t)0);
}
return true;
} // End of function()
//*****************************************************************************
// Purpose :
// a safe strcpy.
// Destination is guarantees to be 0 terminated and not to overflow
// Truncate the string if destination is shorter than source
// Parameters:
// destination, size of the destination buffer, source
// Returns:
// char * (destination)
//*****************************************************************************
//------------------------------------------------------------------
char * str_safecopy( char *dest,
size_t maxlen,
const char *src){
char *res = dest;
if (maxlen == 0){
return dest;
}
dest = str_copywhile(dest, src, &maxlen);
*dest=0;
return res;
} // End of function()
//*****************************************************************************
// Purpose :
// trim trailing space (spaces, '\n', '\r', '\t' and '\f' )
// Parameters:
// destination
// Returns:
// char * (destination)
//*****************************************************************************
char * str_rtrim(char *destination){
size_t len = strlen(destination);
char *ch = destination+len-1;
while ( len > 0 ){
--len;
if ( isspace(*ch) ){
// skip beginning spaces
--ch;
}
}
while (!isspace(*ch) ){
// skip
++ch;
}
*ch = 0;
return destination;
} // End of function()
//*****************************************************************************
// Purpose :
// trim beginning space (shift left destination) (spaces, '\n', '\r', '\t' and '\f' )
// Parameters:
// destination
// Returns:
// char * (destination)
//*****************************************************************************
char * str_ltrim(char *destination){
size_t len = strlen(destination);
size_t bgn = 0;
while (destination[bgn]) {
if ( isspace(destination[bgn]) ){
++bgn;
continue;
}
// move starting with dest
if ( bgn && ( bgn < len ) ) {
memmove(destination, destination+bgn, len-bgn+1);
}
return destination;
}
// empty string
*destination = 0;
return destination;
} // End of function()
//*****************************************************************************
// Purpose :
// string concatenation, boundary check and terminate with '\0' in all cases
// Parameters:
// destination, size of destination, string to concatenate
// Returns:
// char * (destination)
//*****************************************************************************
char * str_safecat( char *dest, size_t maxlen, const char *src1){
char *res = dest;
if (maxlen == 0){
return dest;
}
size_t len = strlen(dest);
if (len < maxlen-1) {
dest = &(dest[len]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -