📄 utilities.c
字号:
#include "typedef.h"
#include "utilities.h"
#include "true_false.h"
//u8_t net_buf[NETWORK_TX_BUFFER_SIZE]; /* Network transmit buffer */
/********************************************************************************
Function: strlen
Parameters: u8_t * str - start address of string buffer
u16_t len - buffer length
Return val: u16_t - (0) Not a string
(>=0) Length of string
Date: 12.8.2002
Desc: Calculates the length of given string
*********************************************************************************/
/**
CNOVER IP STRING TO 4BYTE IP ADDRESS
EXAMPLE: INTPUT STRING 192.168.2.13 RETUEN VALUE IS 0XC0A8020D
**/
u32_t iptoul(u8_t * s)
{
u8_t t=0,i=0 ;
u8_t count=4 ;
u32_t ret=0 ;
while(count)
{
while((*(s+i)<='9')&&(*(s+i)>='0'))
{
t=t*10+*(s+i)-'0';
i++;
}
ret=ret*256+t;
t=0;
i++;
count--;
}
return ret ;
}
/*
*input ip 0XC0A8020D
* s[]=192.168.2.13
*/
void iptostr(u8_t*s,u32_t ip)
{
u8_t*p=s,i ;
u8_t tip=0,flag=0 ;
for(i=24;;i-=8)
{
flag=0 ;
tip=(u8_t)(ip>>i&0x000000ff);
if(((tip/100)%10)!=0)
{
*p++=((tip/100)%10+'0');
flag=1 ;
}
if(((tip/10)%10)!=0)
{
*p++=((tip/10)%10+'0');
flag=1 ;
}
if(tip%10!=0)
{
*p++=(tip%10+'0');
flag=1 ;
}
if(flag==0)
{
*p++='0' ;
}
if(i==0)break ;
*p++='.' ;
}
*p='\0' ;
}
u16_t strlen (u8_t * buf, u16_t len)
{
u16_t i;
for(i=0; i<len; i++) {
if(*buf == '\0')
return( i );
buf++;
}
/* Not found */
return(0);
}
u16_t strncmp( u8_t *s1, u8_t *s2, u16_t maxlen)
{
u16_t i;
for(i = 0; i < maxlen; i++) {
if(s1[i] != s2[i])
return ((int) s1[i]) - ((int) s2[i]);
if(s1[i] == 0)
return 0;
}
return 0;
}
/********************************************************************************
Function: bufsearch
Parameters: u8_t * startadr - start address of given buffer
u16_t len - buffer length
u8_t * str - given searchstring
Return val: u16_t - (-1) Not found
(>=0) Start of matched string from startadr
Date: 12.7.2002
Desc: Seeks given string from given buffer
*********************************************************************************/
u16_t bufsearch (u8_t * startadr, u16_t len, u8_t * str)
{
u16_t i;
u16_t position;
u8_t matchesneeded;
u8_t matchesnow;
u8_t * target;
u8_t * key;
target = startadr;
position = -1;
key = str;
matchesnow = 0;
matchesneeded = 0;
/* How many matches we need? */
while( *key++ != '\0' ) {
/* Break possible deadlock */
matchesneeded++;
if(matchesneeded > 30)
return(-1);
}
/* Search for first mark and continue searching if found */
key = str;
for(i=0; i<len; i++) {
if( *target == *key) {
/* We found matching character */
matchesnow++;
/* Move to next character of key */
key++;
target++;
if(matchesnow == 1) {
/* First character match */
position = i;
}
if(matchesneeded == matchesnow) {
/* Whole string matched */
return(position);
}
} else {
if( matchesnow != 0) {
/* It wasn't a complete match... */
/* Initialize counters and start again */
matchesnow = 0;
key = str;
/* Move to next character of target after */
/* previous matching character */
target = startadr;
target += position;
target += 1;
i = position;
} else {
/* Just continue searching the first match */
target++;
}
}
}
/* No matches found... */
return(-1);
}
/********************************************************************************
Function: tolower
Parameters: u8_t ch - ASCII character to be converted lowercase
Return val: u8_t - converted character
Date: 21.8.2002
Desc: If ch is UPPERCASE letter it is converted to lowercase and
returned. Otherwise original character is returned
*********************************************************************************/
u8_t tolower (u8_t ch)
{
if( (ch < 91) && (ch > 64) )
return(ch + 32);
return(ch);
}
/********************************************************************************
Function: toupper
Parameters: u8_t ch - ASCII character to be converted UPPERCASE
Return val: u8_t - converted character
Date: 21.8.2002
Desc: If ch is lowercase letter it is converted to UPPERCASE and
returned. Otherwise original character is returned
*********************************************************************************/
u8_t toupper (u8_t ch)
{
if( (ch < 123) && (ch > 96) )
return(ch - 32);
return(ch);
}
/* Is the given ASCII code numerical */
/* e.g. '0','1','2' ... '9' */
u8_t isnumeric (u8_t ch)
{
if( (ch < 58) && (ch > 47) )
return(1);
return(0);
}
/* HexToAscii - Take one byte and return its two ASCII */
/* values for both nibbles */
u16_t hextoascii (u8_t c)
{
u8_t ch = c;
u8_t as1;
u8_t as2;
/* take the char and turn it to ASCII */
as1 = ch;
as1 >>= 4;
as1 &= 0x0F;
if ( as1<10 )
as1 += 48;
else
as1 += 55;
as2 = ch;
as2 &= 0x0F;
if ( as2<10 )
as2 += 48;
else
as2 += 55;
return( ((u16_t )(as1)<<8) + as2 );
}
/* Convert ASCII character to numerical */
/* e.g. '1' -> 0x01, 'A' ->0x0A */
u8_t asciitohex (u8_t ch)
{
if( (ch < 58) && (ch > 47) )
return(ch - 48);
if( (ch < 71 ) && (ch > 64) )
return(ch - 55);
}
void ltoa (u32_t nmbr, u8_t *ch )
{
/* Transforms value of long word to ASCII string */
/* Makes it iterative */
u16_t multiple;
u32_t decade,comp;
u8_t i,found;
/* Init String */
for( i=0; i<10;i++ )
*ch++ = '0';
ch -= 10;
/* See if Zero */
if(nmbr == 0) {
*ch++ = '0';
*ch = '\0';
}
decade = 1000000000;
found = FALSE;
for( i=0; i<10; i++) {
if(i != 0)
decade /= 10;
for( multiple=9; multiple>0; multiple--) {
if( (i==0) && (multiple > 2) )
continue;
comp = decade * multiple;
if(nmbr >= comp) {
*ch = hextoascii(multiple);
nmbr -= comp;
found = TRUE;
break; /* Still processing */
}
}
if( found == TRUE)
ch++;
}
*ch = '\0'; /* EOL */
}
void itoa (u16_t nmbr, u8_t * ch )
{
/* Transforms value of word to ASCII string */
/* Makes it iterative */
u16_t decade, multiple;
u32_t comp;
u8_t i,found;
/* Init String */
for( i=0; i<5;i++)
*ch++ = '0';
ch -= 5;
/* See if Zero */
if(nmbr == 0) {
*ch++ = '0';
*ch = '\0';
}
decade = 10000;
found = FALSE;
for( i=0; i<5; i++) {
if(i != 0)
decade /= 10;
for( multiple=9; multiple>0; multiple--) {
if( (i==0) && (multiple > 6) )
continue;
comp = decade * multiple;
if(nmbr >= comp) {
*ch = hextoascii(multiple);
nmbr -= comp;
found = TRUE;
break; /* Still processing */
}
}
if( found == TRUE)
ch++;
}
*ch = '\0'; /* EOL */
}
void memcpy(void *s1, void *s2,u32_t n)
{
u32_t i;
for (i = 0; i < n; i++)
((u8_t *)(s1))[i] = (( u8_t *)(s2))[i];
}
void memset(void *s, u8_t ch, u32_t n)
{
int i;
for (i = 0; i < n; i++)
((u8_t *)(s))[i] = ch;
}
DWORD ntohl(DWORD in)
{
DWORD out;
out = (in<<24) | ((in<<8)&(0x00FF0000)) | ((in>>8)&(0x0000FF00)) | (in>>24) ;
return out;
}
WORD ntohs(WORD in)
{
WORD out;
out = (in<<8) | (in>>8);
return out;
}
DWORD htonl(DWORD in)
{
DWORD out;
out = (in<<24) | ((in<<8)&(0x00FF0000)) | ((in>>8)&(0x0000FF00)) | (in>>24) ;
return out;
}
WORD htons(WORD in)
{
WORD out;
out = (in<<8) | (in>>8);
return out;
}
u32_t strtodec(u8_t *str, u16_t cnt)
{
u32_t i, data = 0;
for(i=0; i<cnt; i++)
{
data *= 10;
if(str[i]<'0'||str[i]>'9')
return -1;
data += str[i]-'0';
}
return data;
}
u32_t strtobcd(u8_t *s)
{
u32_t ret;
int i;
ret = 0;
while (*s != '\0') {
if (*s >= '0' && *s <= '9')
i = *s - '0';
else
return -1;
ret = (ret << 4) + i;
s++;
}
return ret;
}
void u32tostr(u8_t *s, u32_t data)
{
int i;
s[8] = 0;
for(i=7; i>=0; i--, data >>=4)
{
if((data&0xf)<=9)
s[i] = (data&0xf)+'0';
else
s[i] = (data&0xf)+'a'-0x0a;
}
}
u32_t strtou32(u8_t *s)
{
u32_t ret;
int i;
ret = 0;
while (*s != '\0') {
if (*s >= '0' && *s <= '9')
i = *s - '0';
else if (*s >= 'a' && *s <= 'f')
i = *s - 'a' + 0xa;
else if (*s >= 'A' && *s <= 'F')
i = *s - 'A' + 0xa;
else
return -1;
ret = (ret << 4) + i;
s++;
}
return ret;
}
void MemCopy(void *buf1,void *buf2,WORD size)
{
BYTE * EndBuf;
for(EndBuf = (BYTE *)buf1 + size; EndBuf != (BYTE *)buf1;)
{
*((BYTE *)buf1)++ = *((BYTE *)buf2)++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -