📄 utils.cpp
字号:
} else {
// This string is already damaged
return res;
}
maxlen -= len;
dest = str_copywhile(dest, src1, &maxlen);
*dest=0;
return res;
} // End of function()
//*****************************************************************************
// Purpose :
// string concatenation, boundary check and terminate with '\0' in all cases
// Parameters:
// destination, size of destination, 2 strings to concatenate
// Returns:
// char * (destination)
//*****************************************************************************
char * str_safecat( char *dest, size_t maxlen, const char *src1,
const char *src2){
char *res = dest;
if (maxlen == 0){
return dest;
}
size_t len = strlen(dest);
if (len < maxlen-1) {
dest = &(dest[len]);
}else {
// This string is already damaged
return res;
}
maxlen -= len;
dest = str_copywhile(dest, src1, &maxlen);
dest = str_copywhile(dest, src2, &maxlen);
*dest=0;
return res;
} // End of function()
//*****************************************************************************
// Purpose :
// string concatenation, boundary check and terminate with '\0' in all cases
// Parameters:
// destination, size of destination, 3 strings to concatenate
// Returns:
// char * (destination)
//*****************************************************************************
char * str_safecat( char *dest, size_t maxlen, const char *src1,
const char *src2,
const char *src3){
char *res = dest;
if (maxlen == 0){
return dest;
}
size_t len = strlen(dest);
if (len < maxlen-1) {
dest = &(dest[len]);
}else {
// This string is already damaged
return res;
}
maxlen -= len;
dest = str_copywhile(dest, src1, &maxlen);
dest = str_copywhile(dest, src2, &maxlen);
dest = str_copywhile(dest, src3, &maxlen);
*dest=0;
return res;
} // End of function()
//*****************************************************************************
// Purpose :
// compare strings, not case sensitive, accepts NULL pointers (less than any other )
// Parameters:
// strings to compare
// Returns:
// int >0 (first is greater) =0 (equal) <0 (second is greater)
//*****************************************************************************
int str_compare(const char *s1, const char *s2){
int rc;
if (s1 == 0) {
return (s2 == 0)?COMPARE_EQUAL:COMPARE_LESS;
}
if (s2 == 0) {
return COMPARE_GREAT;
}
while (*s1 && *s2) {
if ( ( rc = ((unsigned)tolower(*s1) - (unsigned)tolower(*s2)) ) != 0 ) {
return rc;
}
++s1;++s2;
}
return (*s1-*s2);
} // End of function()
//*****************************************************************************
// Purpose :
// check if this string represent numeric value (can be 0xHEX and/or negative)
// Parameters:
// none
// Returns:
// true if it is a number
//*****************************************************************************
bool str_isnumber(const char *string){
return str_getnumber(string,(int *)0);
} // End of function()
//*****************************************************************************
// Purpose :
// check if this string represent unsigned numeric value (can be 0xHEX )
// if yes, load the value in *result
// may overflow w/o warning if number is bigger than MAX UINT value
// Parameters:
// string
// Returns:
// true if it is a number + result ( if non zero ptr ) loaded
//*****************************************************************************
static bool string2num(const char *string, unsigned int *result){
int base = 10;
unsigned int tmp = 0;
// Hex base?
if ( (*string == '0')
&& ( tolower(*(string+1)) == 'x' ) ){
base = 16;
string+=2;
}
// Process
while (*string){
if ( (*string >= '0')
&& (*string <= '9' ) ){
// good for decimal and hex
tmp = tmp * base + (*string-'0');
}else if (base == 16) {
int dig = tolower(*string)-'a'+10;
if ( (dig >= 10) && (dig <= 15) ) {
tmp = tmp*16+ dig;
}else {
//non hex digit
return false;
}
}else {
return false;
}
++string;
} // while *string
if (result) {
*result = tmp;
}
return true;
} // End of function()
//*****************************************************************************
// Purpose :
// skip beginning spaces
// check if this string represent unsigned numeric value (can be 0xHEX )
// if yes, load the value in *result
// may overflow w/o warning if number is bigger than MAX UINT value
// Parameters:
// string
// Returns:
// true if it is a number + result ( if non zero ptr ) loaded
//*****************************************************************************
bool str_getunsignednumber(const char *string, unsigned int *result){
// skip spaces;
while( isspace(*string) && *string ) {
++string;
}
return string2num(string, result);
} // End of function()
//*****************************************************************************
// Purpose :
// check if this string represent numeric value (can be 0xHEX and/or negative)
// if yes, load the value in *result
// may overflow w/o warning if number is bigger than MAX INT value
// Parameters:
// string
// Returns:
// true if it is a number + result ( if non zero ptr ) loaded
//*****************************************************************************
bool str_getnumber(const char *string, int *result){
bool neg = false;
unsigned int u_result;
// skip spaces;
while( isspace(*string) && *string ) {
++string;
}
// negative?
if (*string == '-') {
++string;
neg = true;
}
bool brc = string2num(string, &u_result);
// store value
if (result) {
if (brc ) {
if (neg) {
*result = -(signed)u_result;
}else {
*result = (signed)u_result;
}
}else {
*result = 0;
}
}
return brc;
} // End of function()
//*****************************************************************************
// Purpose :
// find name for given code in given table
// Parameters:
// code, holder for name if found, structure to search in
// Returns:
// true = found
// false = not found
// name addr in *name
//*****************************************************************************
bool str_findname(int code, // event code
const char **name, // holder for name
const NAME_TABLE *table ){ // where to search
while (table->name){
if (table->code == code) {
*name = table->name;
return true;
}
++table;
}
*name = "<unknown name>";
return false;
}
//*****************************************************************************
// Purpose :
// find name for given code in given table
// Parameters:
// code, holder for name if found, structure to search in
// Returns:
// true = found
// false = not found
// name addr in *name
//*****************************************************************************
bool str_findcode(const char *name, // given name
int *code, // holder for code
const NAME_TABLE *table ){ // where to search
while (table->name){
if ( COMPARE_EQUAL == str_compare(table->name , name) ) {
*code = table->code;
return true;
}
++table;
}
*code = -1;
return false;
}
//*****************************************************************************
// Purpose :
// ret_code <> 0, Display message and wait key Enter
// Parameters:
// [in] ret_code
// [in] message
// Returns:
// none
//*****************************************************************************
void con_wait_a_key(int ret_code, const char *txt){
if ( ret_code ){
printf("\n%sPress <Enter>...",txt);
char buffer[10];
fgets(buffer,2, stdin);
}
return;
}
# ifndef WIN32
//*****************************************************************************
// Purpose :
// Block calling thread for specified number of milliseconds
// Parameters:
// number of milliseconds
// Returns:
// none
//*****************************************************************************
void Sleep(unsigned long milliseconds){
struct timeval tval;
tval.tv_sec = milliseconds / 1000;
tval.tv_usec = (milliseconds % 1000L)*1000;
select(0,NULL,NULL,NULL,&tval);
return ;
} // End of function()
# endif
//////////////////////////////////////////////////////////////////////
// End of Utils.cpp
//////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -