📄 utils.c
字号:
/*****************************************************************************
* utils.c: BIOSBOX 公共函数
*
* Copyright(C) 2007, uCdragon
* All rights reserved.
*
* History
* 2007.03.16 lqm@ucdragon.net
*
******************************************************************************/
#include "def.h"
#include "board.h"
#define INVALID_RESULT 0xFFFFFFFF
void memcpy(void *s1, const void *s2, int n)
{
int i;
for (i = 0; i < n; i++)
((char *)(s1))[i] = ((const char *)(s2))[i];
}
void memset(void *s, const char ch, int n)
{
int i;
for (i = 0; i < n; i++)
((char *)(s))[i] = ch;
}
unsigned short ntohs(unsigned short s)
{
return (s >> 8) | (s << 8);
}
unsigned long ntohl(unsigned long l)
{
return ((l >> 24) & 0x000000ff) |
((l >> 8) & 0x0000ff00) |
((l << 8) & 0x00ff0000) |
((l << 24) & 0xff000000);
}
unsigned short htons(unsigned short s)
{
return (s >> 8) | (s << 8);
}
unsigned long htonl(unsigned long l)
{
return ntohl(l);
}
unsigned long strtonum(char *s,int radix)
{
unsigned long ret;
int i;
ret = 0;
while (*s != '\0') {
if (*s >= '0' && *s <= '9')
i = *s - '0';
else
return INVALID_RESULT;
ret = (ret << 4) + i;
s++;
}
return ret;
}
int strtoul(char *s)
{
int ret;
int radix = 10;
int negative = 0;
int i;
ret = 0;
if(*s == '-')
{
negative = 1;
s++;
}
else if(*s == '0')
{
s++;
if(*s == 'x')
{
s++;
radix = 0x10;
}
}
while (*s) {
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
break;
if(i >= radix) break;
ret = (ret * radix) + i;
s++;
}
return negative?(-ret):ret;
}
void ultostr(char *s, unsigned long 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;
}
}
int strlen(const char *s)
{
int i = 0;
for(; *s; s++)
i++;
return i;
}
int strncmp(const char *s1, const char *s2, int maxlen)
{
int 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;
}
char getch(void)
{
char c;
while(!PollUart(&c));
return c;
}
void puts(char * s)
{
while(*s) putch(*s++);
}
/*---------------------printf and support routines ---------------------*/
/* print c count times */
void RepeatChar(char c, int count)
{
while (count--) putch(c);
}
/* put string reverse */
void PutStringReverse(char *s, int index)
{
while ((index--) > 0) putch(s[index]);
}
/*-------------------------------------------------------------------------*/
/*
prints value in radix, in a field width width, with fill
character fill
if radix is negative, print as signed quantity
if width is negative, left justify
if width is 0, use whatever is needed
if fill is 0, use ' '
*/
static void PutNumber(int value, int radix, int width, char fill)
{
char buffer[40];
int bi = 0;
int unsigned uvalue;
short int digit;
short int left = FALSE;
short int negative = FALSE;
if (fill == 0) fill = ' ';
if (width < 0) {
width = -width;
left = TRUE;
}
if (width < 0 || width > 80) width = 0;
if (radix < 0) {
radix = -radix;
if (value < 0) {
negative = TRUE;
value = -value;
}
}
uvalue = value;
do {
if (radix != 16) {
digit = uvalue % radix ;
uvalue = uvalue / radix ;
}
else {
digit = uvalue & 0xf;
uvalue = uvalue >> 4;
}
buffer[bi] = digit + ((digit <= 9) ? '0' : ('A' - 10));
bi++;
if (uvalue != 0) {
if ((radix==10)&&((bi==3)||(bi==7)||(bi==11)|(bi==15))) {
buffer[bi++]=',';
}
}
} while (uvalue != 0);
if (negative) {
buffer[bi] = '-';
bi += 1;
}
if (width <= bi) PutStringReverse(buffer, bi);
else {
width -= bi;
if (!left) RepeatChar(fill, width);
PutStringReverse(buffer, bi);
if (left) RepeatChar(fill, width);
}
}
/*-------------------------------------------------------------------------*/
static char *FormatItem(char *f, int a)
{
char c;
int fieldwidth = 0;
int leftjust = FALSE;
int radix = 0;
char fill = ' ';
if (*f == '0') fill = '0';
while ((c = *f++)!=0) {
if (c >= '0' && c <= '9') {
fieldwidth = (fieldwidth * 10) + (c - '0');
}
else switch (c) {
case '\000': return(--f);
case '%': putch('%');
return(f);
case '-': leftjust = TRUE;
break;
case 'c': {
if (leftjust) putch(a & 0x7f);
if (fieldwidth > 0) RepeatChar(fill, fieldwidth - 1);
if (!leftjust) putch(a & 0x7f);
return(f);
}
case 's': {
if (leftjust) puts((char *) a);
if (fieldwidth > strlen((char *) a))
RepeatChar(fill, fieldwidth - strlen((char *)a));
if (!leftjust) puts((char *) a);
return(f);
}
case 'd':
case 'i': radix = -10;break;
case 'u': radix = 10;break;
case 'x': radix = 16;break;
case 'X': radix = 16;break;
case 'o': radix = 8;break;
default : radix = 3;break;/* unknown switch! */
}
if (radix) break;
}
if (leftjust) fieldwidth = -fieldwidth;
PutNumber(a, radix, fieldwidth, fill);
return(f);
}
#define vaStart(list, param) list = (char*)((int)¶m + sizeof(param))
#define vaArg(list, type) ((type *)(list += sizeof(type)))[-1]
#define vaEnd(list)
void printf(char *f, ...) /* variable arguments */
{
// U32 mode ;
char *argP;
vaStart(argP,f); /* point at the end of the format string */
while (*f) { /* this works because args are all ints */
if (*f == '%')
f = FormatItem(f + 1, vaArg(argP, int));
else
putch(*f++);
}
vaEnd(argP);
}
int getyorn(void)
{
char c;
puts(" [y/n] ");
while(1) {
c = getch();
if((c=='y')||(c=='Y')||(c=='n')||(c=='N'))
break;
}
putch(c);
putch('\n');
return c&1; //y&Y are odd, n&N are even
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -