1.常用宏或函数的实现
_T,_L宏:
#define unsigned short L
#define _T(x) __T(x)
#define _TEXT(x) __T(x)
#ifdef _UNICODE
#define __T(x) L##x
#else
#define __T(x) x
#endif
#define _L(x) L##x
assert宏实现:
#define assert(expr)
do{
if (!(expr))
{
outputx("Assertion failed! %s, %s, %s, line %d\n",
#expr, __FILE__, __func__, __LINE__);
}
}while(0)
assert函数实现:
inline bool Assert(bool result, const char* function, const char* file,
int line, const char* expression)
{
if (!result)
{
Log_Assert(function, file, line, expression);
Break();
return false;
}
return true;
}
得到指定地址上的一个字节或字:
#define MEM_B(x) (*((byte *)(x)))
#define MEM_W(x) (*((word *)(x))
得到一个变量的地址(word宽度):
#define B_PTR(var) ((byte *) (void *) &(var))
#define W_PTR(var) ((word *) (void *) &(var))
得到一个字的高位和低位字节:
#define WORD_LO(xxx) ((byte) ((word)(xxx) & 255))
#define WORD_HI(xxx) ((byte) ((word)(xxx) >> 8))
将一个字母转换为大写:
#define UPCASE(c) (((c)>='a' && (c) <= 'z') ? ((c) - 0x20) : (c))
防止溢出的一个方法:
#define INC_SAT(val) (val=((val)+1>(val)) ? (val)+1 : (val))
DEBUG宏:
#ifdef _DEBUG
#define DEBUGMSG(msg,date) printf(msg);printf(“%d%d%d”,date,_LINE_,_FILE_)
#else
#define DEBUGMSG(msg,date)
#endif
2.字符串相关函数
int _strlen(const char* src)
{
assert(src != NULL);
const char *p = src;
while(*p++!=NULL);
return p - str - 1;
}
char* _strstr(const char* str, const char* sub)
{
for(int i=0; str[i]!='\0'; i++)
{
int pos = i,j=0;
while(str[i]==sub[j] && sub[j]!='\0')
{
++i;
++j;
}
if(sub[j] == '\0')
return &str[pos];
}
return NULL;
}
char* _strncpy(char* dest, const char* src, int count)
{
assert(dest!=NULL && src!=NULL && count>0);
char* tmp = dest;
while(count-->0 && (*dest++=*src++)!='\0');
return tmp;
}
char* _strcpy(char* dest, const char* src)
{
assert(dest!=NULL && src!=NULL);
char* tmp = dest;
while((*dest++=*src++) != '\0');
return tmp;
}
int _strcmp(const char* first, const char* second)
{
assert(first!=NULL && src!=second);
int ret = 0;
while(!(ret=(*(unsigned char*)first) - (*(unsigned char*)second))
&& (*first != '\0'))
{
++first;
++second;
}
if(ret < 0)
ret = -1;
else if(ret > 0)
ret = 1;
return ret;
}
char* _strcat(char* dest,char* src)
{
assert(dest!=NULL && src!=NULL);
char* tmp = dest;
while(*dest != '\0')
++dest;
while(((*dest++)=(*src++)) != '\0');
return tmp;
}
void _loopmove(char* str, int steps)
{
int n = strlen(str)-steps;
char tmp[256];
memcpy(tmp,str+n,steps);
memcpy(str+steps,str,n);
memcpy(str,tmp,steps);
}
char* _reversestring(char* src)
{
assert(src!=NULL);
int len=strlen(src);
char tmp = '\0';
for(int i=0; i<len/2; i++)
{
tmp = src[i];
src[i] = src[len-i-1];
src[len-i-1] = tmp;
}
return src;
}
void _makepath(char* path, const char* drive, const char* dir, const char*filename, const char* ext)
{
assert(path!=NULL && drive!=NULL && filename!=NULL && ext!=NULL);
register const char* p;
if(*drive)
{
*path++ = *drive;
*path++ = ':';
*path++ = '\\';
}
else
return;
if((p=dir) && *p)
{
do
{
*path++ = *p++;
}while(*p);
if(*(p-1) != '\\')
*path++ = '\\';
}
if(p = filename)
{
while(*p)
*path++ = *p++;
}
if(p = ext)
{
if(*p && *p != '.')
*path++ = '.';
while(*path++ = *p++);
}
*path++ = '\0';
}
3.内存相关函数
void* _memset(void* dest, int c,int count)
{
assert(dest != NULL);
char* tmp = (char*)dest;
int i = 0;
while(count--)
dest[i++] = c;
return tmp;
}
void _memcpy(void* dest, const void* src, int count)
{
assert(dest!=NULL && src!=NULL);
unsigned char* pdst = (unsigned char*)dest;
unsigned char* psrc = (unsigned char*)src;
assert(!(psrc<=pdst && pdst<psrc+count));
assert(!(pdst<=psrc && psrc<pdst+count));
while(count--)
*pdst++ = *psrc++;
}
4.类型转换函数
void _itoa(long num,char str[],int radix)
{
if( str == NULL)
return;
long sign = num,i = 0,j = 0;
char temp[100];
if(sign < 0)
num = -num;
do
{
temp[i] = num % radix+'0';
num /= radix;
i++;
}while(num > 0);
if(sign < 0)
temp[i++] = '-';
temp[i] = '\0';
i--;
while(i >= 0)
{
str[j] = temp[i];
j++;
i--;
}
str[j] = '\0';
}
void _ftoa(float number,int ndigit,char buf[])
{
assert(buf!=NULL);
long int_part;
double float_part;
char str_int[512];
char str_float[256];
memset(str_int,0,512);
memset(str_float,0,256);
int_part = (long)number;
float_part = number - int_part;
_itoa(int_part,str_int,10);
if(ndigit>0)
{
float_part =fabs(pow(10,ndigit)*float_part);
_itoa((long)float_part,str_float,10);
}
int i = strlen(str_int);
str_int[i] = '.';
strcat(str_int,str_float);
strcpy(buf,str_int);
}
int _atoi(const char str[])
{
int ret_Integer = 0;
int integer_Sign = 1;
if(str == NULL)
return 0;
while(*str == ' ')
str++;
if(*str == '-')
integer_Sign = -1;
if(*str == '-' || *str == '+')
str++;
while(*str >= '0' && *str <= '9')
{
ret_Integer = ret_Integer * 10 + *str - '0';
str++;
}
ret_Integer = integer_Sign * ret_Integer;
return ret_Integer;
}
float _atof(const char str[])
{
if(str == NULL)
return 0.0;
int len = strlen(str),i = 0;
if(str[0] == '-')
i++;
float s = 0.0;
while(i < len && (str[i]>='0' && str[i]<='9'))
{
s = s * 10 + (float)(str[i]-'0');
i++;
}
if(str[i] == '.')
i++;
else
return s;
float order = 1.0;
while(i < len && (str[i]>='0' && str[i]<='9'))
{
order = order / 10;
s = s + (float)(str[i]-'0')*order;
i++;
}
if (str[i] == '-')
s = -1*s;
return s;
}
?
详细解决方案
根本字符串相关函数,基本宏,内存相关函数,类型转换函数实现合集
热度:174 发布时间:2016-04-29 11:23:32.0
相关解决方案