string.c
上传用户:qddsws
上传日期:2022-06-22
资源大小:723k
文件大小:7k
- #include <lib/string.h>
- //unsigned char *VGA = (unsigned char *)0xA0000;
- int cursorx,cursory;
- #include <Bitmapfonts/font1.fnt>
- #include <Bitmapfonts/font2.fnt>
- #include <Bitmapfonts/font4.fnt>
- #include <Bitmapfonts/font5.fnt>
- int act;
- char *itoa (unsigned long number,char* ret, int base) {
- int count=0;
- int k=0;
- int res=0;
- if (number==0) {
- ret[0]='0';
- ret[1]=' ';
- }
- else {
- while (number!=0) {
- res=number%base;
- number=number/base;
- if (res<10) ret[k]=res+'0';
- else ret[k]=(res-10)+'a';
- k++;
- }
- count=k;
- k--;
- for(res=0;res < k;res++,k--) {
- ret[res]^=ret[k];
- ret[k]^=ret[res];
- ret[res]^=ret[k];
- }
- }
- //if (number==0) {
- // ret[0]='0';
- // count=1;
- //}
-
- ret[count]=0;
- return ret;
- }
- void snprintf (char* dest,const char *fmt, ...){
- const char *p;
- va_list argp;
- int i;
- char *s;
- char fmtbuf[256];
- int k=0;
- va_start(argp, fmt);
- for(p = fmt; *p != ' '; p++){
- if(*p != '%') {
- //putchar(*p);
- dest[k]=*p;
- k++;
- continue;
-
- }
-
- switch(*++p) {
- case 'c':
- i = va_arg(argp, int);
- dest[k]=i;
- k++;
- //putchar(i);
- break;
-
- case 'd':
- i = va_arg(argp, int);
- s = itoa(i,fmtbuf,10);
- strcpy(&dest[k],s);
- k=k+strlen(s);
- //fputs(s, stdout);
- break;
-
- case 's':
- s = va_arg(argp, char *);
- strcpy(&dest[k],s);
- k=k+strlen(s);
- //fputs(s, stdout);
- break;
-
- case 'x':
- i = va_arg(argp, int);
- s = itoa(i, fmtbuf, 16);
- strcpy(&dest[k],s);
- k=k+strlen(s);
- //fputs(s, stdout);
- break;
-
-
- case '%':
- //putchar('%');
- dest[k]='%';
- k++;
-
- break;
- }
- }
- dest[k]=' ';
- va_end(argp);
- }
- void jump_to_line (int l) {
- cursorx=0;
- cursory=l;
- }
- /*char* itoa(int number) {
- char *ret;
-
- char num[5];
- char mun[5];
- ret=(char*)#
- int sign=1;
- int x,y;
- for (x=0;x<5;x++) num[x]=0;
- for (x=0;x<5;x++) mun[x]=0;
- if (number<0) {
- number=number*-1;
- sign=-1;
- }
- num[0]='0';
- x=0;
- y=number;
- while (y>0) {
- mun[x]=y%10+'0';
- x++;
- y=y/10;
- }
- y=0;
- if (sign==-1) {
- num[0]='-';
- y++;
- }
- while (x>0) {
- num[y]=mun[x-1];
- y++;
- x--;
- }
- return ret;
- }
- */
- void hexkprint4b(unsigned long num) {
- char *aux;
- char hexnum[5];
- hexnum[4]=0;
- unsigned long auxtmp=num;/*
- if ((auxtmp&0xF)>9) hexnum[3]='A'+(auxtmp&0xF)-10;
- else {
- aux = itoa (auxtmp&0xF);
- hexnum[3]=aux[0];
- }
- if (((auxtmp&0xF0)>>4)>9) hexnum[2]='A'+((auxtmp&0xF0)>>4)-10;
- else {
- aux = itoa ((auxtmp&0xF0)>>4);
- hexnum[2]=aux[0];
- }
- if (((auxtmp&0xF00)>>8)>9) hexnum[1]='A'+((auxtmp&0xF00)>>8)-10;
- else {
- aux = itoa ((auxtmp&0xF00)>>8);
- hexnum[1]=aux[0];
- }
- if (((auxtmp&0xF000)>>12)>9) hexnum[0]='A'+((auxtmp&0xF000)>>12)-10;
- else {
- aux = itoa ((auxtmp&0xF000)>>12);
- hexnum[0]=aux[0];
- }*/
-
- kprint(hexnum);
- }
- void hexkprint8b(unsigned long num) {
- hexkprint4b(num>>16&0xFFFF);
- hexkprint4b(num&0xFFFF);
- }
- void clrscr() {
- cursorx=0;
- cursory=0;
- if (act==1) memset(VGA,BACKGROUND_COLOR,SWIDTH*SHEIGHT*BPP);
- }
- void test_printk( char * str) {
-
- int i=0;
- while (str[i]!=0 && i < SWIDTH/(FONT_W)) {
- switch (str[i]) {
- case 'n':
- str[i]=' ';
- xyprint(str,cursorx*FONT_W,cursory*FONT_H,DEF_COLOR,DEF_FONT);
- cursorx=0;
- cursory=cursory+1;
- str=str+i+1 ;
- i=-1;
-
- break;
- }
- i++;
-
- }
-
- xyprint(str,cursorx*FONT_W,cursory*FONT_H,DEF_COLOR,DEF_FONT);
- cursorx=cursorx+strlen(str);
-
- }
- #include <devices/sys_console.h>
- void kprint( char * str ) {
-
-
- if (act==2) {
- // sys_console_print_text(test_ret_print(),"*");
- sys_console_print_text(test_ret_print(),str);
- }
- else {
- int i=0;
- while (str[i]!=' ' && (i+cursorx) < SWIDTH/(FONT_W)) {
- switch (str[i]) {
- case 'n':
- str[i]=' ';
- xyprint(str,cursorx*FONT_W,cursory*FONT_H,DEF_COLOR,DEF_FONT);
- cursorx=0;
- cursory=cursory+1;
- str=str+i+1 ;
- i=-1;
-
- break;
- }
- i++;
- if (i+cursorx == SWIDTH/(FONT_W) ) {
- str[i]=' ';
- xyprint(str,cursorx*FONT_W,cursory*FONT_H,DEF_COLOR,DEF_FONT);
- cursorx=0;
- cursory=cursory+1;
- str=str+i+1 ;
- i=0;
-
-
- }
-
- }
-
- xyprint(str,cursorx*FONT_W,cursory*FONT_H,DEF_COLOR,DEF_FONT);
- cursorx=cursorx+strlen(str);
- }
- }
- void deact_print () {
- act=0;
- }
- void act_print (int i) {
- act=i;
- }
- void xyprint_area( char * str, int x, int y, int color, int font, word* temp_area) {
- int i=0;
- int j=0;
- int k=0;
- word lin[FONT_W-1];
- word lin_mask[FONT_W-1];
- word lin_notmask[FONT_W-1];
- char faux1;
-
- for (k=0;k<strlen(str);k++) {
- int lletra=str[k];
- for (j=0;j<FONT_H;j++) {
- for (i=0;i<FONT_W;i++) {
- #ifdef FONT1
- if (font==1) faux1=font1[j+(lletra*(FONT_H))];
- #endif
- #ifdef FONT2
- if (font==2) faux1=font2[j+(lletra*(FONT_H))];
- #endif
- #ifdef FONT4
- if (font==4) faux1=font4[j+(lletra*(FONT_H))];
- #endif
- #ifdef FONT5
- if (font==5) faux1=font5[j+(lletra*(FONT_H))];
- #endif
- faux1 = faux1 >> i;
- faux1=faux1 & 0x01;
- if (faux1==0x01) {
- lin[FONT_W-1-i]=color;
- }
- else{
- lin[FONT_W-1-i]=*(word*)(temp_area+(SWIDTH*(y+j))+(x+(FONT_W-1-i)+((FONT_W)*k)));
- }
- }
-
- memcpy (temp_area+(SWIDTH*(y+j))+(x+((FONT_W)*k)),&lin,FONT_W*2);
- }
- }
- }
- void xyprint( char * str, int x, int y, int color, int font) {
- int i=0;
- int j=0;
- int k=0;
- word lin[FONT_W-1];
- word lin_mask[FONT_W-1];
- word lin_notmask[FONT_W-1];
- char faux1;
-
- for (k=0;k<strlen(str);k++) {
- int lletra=str[k];
- for (j=0;j<FONT_H;j++) {
- for (i=0;i<FONT_W;i++) {
- #ifdef FONT1
- if (font==1) faux1=font1[j+(lletra*(FONT_H))];
- #endif
- #ifdef FONT2
- if (font==2) faux1=font2[j+(lletra*(FONT_H))];
- #endif
- #ifdef FONT4
- if (font==4) faux1=font4[j+(lletra*(FONT_H))];
- #endif
- #ifdef FONT5
- if (font==5) faux1=font5[j+(lletra*(FONT_H))];
- #endif
- faux1 = faux1 >> i;
- faux1=faux1 & 0x01;
- if (faux1==0x01) {
- lin[FONT_W-1-i]=color;
- }
- else{
- lin[FONT_W-1-i]=*(word*)(VGA+(SWIDTH*(y+j))+(x+(FONT_W-1-i)+((FONT_W)*k)));
- }
- }
-
- memcpy (VGA+(SWIDTH*(y+j))+(x+((FONT_W)*k)),&lin,FONT_W*2);
- }
- }
- }
- int abs(int num ) {
- if (num<0) num=-num;
- return num;
- }
- void* memwordset(void* dest, word c, dword count) {
- word *ds=dest;
- dword i;
- for (i=0;i<count;i++)
- ds[i]=c;
- return dest;
-
- }
- void* memset(void* dest, int c, unsigned int count)
- {
- unsigned char* ds = dest;
- unsigned int i;
- for (i = 0; i < count; i++)
- ds[i] = c;
- return dest;
- }
- void* memcpy(void* dest, const void* src, unsigned int count)
- {
- unsigned char* ds = (unsigned char*)dest;
- unsigned char* ss = (unsigned char*)src;
- unsigned int i;
- for (i = 0; i < count; i++)
- ds[i] = ss[i];
- return dest;
- }
- unsigned int strlen(const char* str)
- {
- const char *s;
- for (s = str; *s; ++s) ;
- return (s - str);
- }
- char * strcpy(char * dest,const char *src)
- {
- char *tmp = dest;
- while ((*dest++ = *src++) != ' ');
- return tmp;
- }
- int strcmp(const char * cs,const char * ct)
- {
- register signed char __res;
- while (1) {
- if ((__res = *cs - *ct++) != 0 || !*cs++) break;
- }
-
- return __res;
- }
- int strncmp(const char * cs,const char * ct, int n)
- {
- register signed char __res;
- while (n>0) {
- if ((__res = *cs - *ct++) != 0 || !*cs++) break;
- n--;
- }
-
- return __res;
- }