首页 > 其他分享 >test

test

时间:2022-10-08 20:22:31浏览次数:51  
标签:int fmt unsigned char ++ str test

校园网外访问:
https://course1.istratus.cn/projects/pa/wiki
https://course1.istratus.cn/pa/doc2019/
git clone https://course1.istratus.cn/pa/ics2019_1.git

  1. 这是通过阿里云跳转,速度可能不太稳定。
  2. 文档中提到的网址,要将域名从course.cunok.cn替换成course1.istratus.cn来访问。
  3. 如果前期已经在校园网内clone,后期要在校园网外访问,则需要手动修改代码根目录.git/config,以及Makefile,将其中的course.cunok.cn都替换成course1.istratus.cn、scripts都替换成scripts1。反之亦然。
#include "klib.h"
#include <stdarg.h>

#if !defined(__ISA_NATIVE__) || defined(__NATIVE_USE_KLIB__)

#define bool int
#define true 1
#define false 0
#define ZEROPAD 1		//Pad with zero
#define SIGN		2		//Unsigned/signed long
#define PLUS		4		//Show plus
#define SPACE		8		//Space if plus
#define LEFT		16	//Left justified
#define SPECIAL 32	//0x
#define LARGE		64	//Use 'ABCD' instead of 'abcd'
static char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
static char *upper_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static inline int is_space(int ch){
	return (unsigned long)(ch - 9) < 5u || ' '==ch;
}
#define is_digit(c)    ((c) >= '0' && (c) <= '9')


static int skip_atoi(const char **s){
	int i = 0;
	while(is_digit(**s)){
		i = i*10 + *((*s)++) - '0';
	}
	return i;
}

static char * number(char *str, long num, int base, int size, int precision, int type)
{
	char c, sign, tmp[36];
	char *dig = digits;
	int i;

	if(type & LARGE) dig = upper_digits;
	if(type & LEFT) type &= ~ZEROPAD;
	if(base < 2 || base > 36)	return 0;

	c = (type & ZEROPAD) ? '0' : ' ';
	
	
    if (type&SIGN && num<0) {
        sign='-';
        num = -num;
    } else
        sign=(type&PLUS) ? '+' : ((type&SPACE) ? ' ' : 0);
    if (sign) size--;

	if(type & SPECIAL){
		if(base==16)
			size -= 2;
		else if(base==8)
			size--;
	}

	i = 0;

	if(num == 0)
		tmp[i++] = '0';
	
	else{
		while(num != 0){
			tmp[i++] = dig[((unsigned long) num) % (unsigned) base];
            if(base == 16)
                num = (unsigned)num >> 4;
            else
			    num = ((unsigned long) num) / (unsigned) base;
		}
	}

	if(i > precision)	precision = i;
	size -= precision;
	if(!(type & (ZEROPAD | LEFT))){
		while(size-- > 0)	*str++ = ' ';
	}
	if(sign) *str++ = sign;

	if(type & SPECIAL){
		if(8 == base){
			*str++ = '0';
		}
		else if(16 == base){
			*str++ = '0';
			*str++ = digits[33];
		}
	}

	if(!(type & LEFT)){
		while(size-- > 0)	*str++ = c;
	}
	while(i < precision--)	*str++ = '0';
	while(i-- > 0)	*str++ = tmp[i];
	while(size-- > 0)	*str++ = ' ';

	return str;
}

int printf(const char *fmt, ...) {
  char buff[1024]={'\0'};
  va_list args;
  int n;
  va_start(args,fmt);
  n = vsprintf(buff,fmt,args);
  int i = 0;
  while(buff[i] !='\0') {
    _putc(buff[i]);
    i++;
  }
  va_end(args);
  return n;
  
}

int vsprintf(char *buf, const char *fmt, va_list args) {

	char *str;
	int field_width;	/*width of output field*/

	for(str=buf;*fmt;fmt++){
		unsigned long num;
		int base = 10;
		int flags = 0;
		int qualifier = -1;
		int precision = -1;
		bool bFmt = true;
		if(*fmt != '%'){
			*str++ = *fmt;
			continue;
		}

		bFmt = true;
		while(bFmt){
			fmt++;/*This also skips first '%'*/
			switch(*fmt){
				case '0':	flags |= ZEROPAD;break;
				case '-':
				case '+':
				case ' ':
				case '#':
									_putc('$');break;
				default:	bFmt = false;break;
			}
		}

		/*Get field width*/
		field_width = -1;
		if(is_digit(*fmt)){
			field_width = skip_atoi(&fmt);
		}else if(*fmt=='*'){
			fmt++;
			field_width = va_arg(args, int);
			if(field_width<0){
				field_width = -field_width;
				flags |= LEFT;
			}
		}

		/*Get the precision*/
		precision = -1;
		if(*fmt=='.'){
			_putc('$');
		}
		/*Get the conversion qualifier*/
		qualifier = -1;
		if('h' == *fmt || 'l' == *fmt || 'L' == *fmt){
			qualifier = *fmt;
			fmt++;
		}

		/*Default base*/
		base = 10;
		switch(*fmt){
			case 'c':
				{
					if(!(flags & LEFT))	while(--field_width > 0) *str++ = ' ';
					*str++ = (unsigned char) va_arg(args, int);
					while(--field_width > 0) *str++ = ' ';
					continue;
				}
			case 's':
				{
					int len;
					char *s = va_arg(args, char *);
					if(!s) s = "<NULL>";
					len = strlen(s);
					if(!(flags & LEFT))	while(len < field_width--) *str++ = ' ';
					for(int i = 0; i < len; ++i)	*str++ = *s++;
					while(len < field_width--) *str++ = ' ';
					continue;
				}
			case 'p':
			case 'n':
			case 'A':
			case 'a':
			case 'o':
			case 'X':
				_putc('$');break;
			case 'x':
				{
					base = 16;
					break;
				}
			case 'd':
			case 'i':
				{
					flags |= SIGN;/*no break*/
				}
			case 'u':
				{
					break;
				}
			default:
				{
					if(*fmt != '%')	*str++ = '%';

					if(*fmt){
						*str++ = *fmt;
					}else{
						--fmt;
					}
					continue;
				}
		}/*end of switch(*fmt)*/

		if(qualifier == 'l'){
			num = va_arg(args, unsigned long);
		}
		else if(qualifier == 'h'){
			if(flags & SIGN)
				num = va_arg(args, int);
			else
				num = va_arg(args, unsigned);
		}
		else if(flags & SIGN){
			num = va_arg(args, int);
		}
		else{
			num = va_arg(args, unsigned long);
		}

		str = number(str, num, base, field_width, precision, flags);
	}/* end of for (str = buf; *fmt; fmt++) */
	*str = '\0';
	return str - buf;
}

int sprintf(char *buf, const char *fmt, ...) {
	va_list args;
	int n;

	va_start(args, fmt);
	n = vsprintf(buf, fmt, args);
	va_end(args);

	return n;
}

int snprintf(char *out, size_t n, const char *fmt, ...) {
  return 0;
}

#endif

#include "klib.h"

#if !defined(__ISA_NATIVE__) || defined(__NATIVE_USE_KLIB__)

size_t strlen(const char *s) {
    size_t length = 0;
    while (*s++ )
        ++length;
    return length;
}

char *strcpy(char* dst,const char* src) {
    char *ret = dst; 
    while ((*dst++=*src++)!='\0'); 
    return ret;
}

char* strncpy(char* dst, const char* src, size_t n) {
  	char* p = dst;
	while (n && (*dst++=*src++))
		n--;
	if(n)
		while (--n)
			*dst++='\0';
	return(p);
}

char* strcat(char* dst, const char* src) {
        char *p = dst;
        while (*dst)
                dst++; 
        while ((*dst++ = *src++) != '\0'); 
        return p;
}

int strcmp(const char* s1, const char* s2) {
    int ret = 0 ;
    while( !(ret = *(unsigned char *)s1 - *(unsigned char *)s2) && *s2)
        ++s1, ++s2;
    if ( ret < 0 )
        ret = -1 ;
    else if ( ret > 0 )
        ret = 1 ;
    return( ret );
}

int strncmp(const char* s1, const char* s2, size_t n) {
    if(!n)
        return 0;
    while(--n && *s1 && *s1 == *s2)
    {
        s1++;
        s2++;
    }
    return (*s1 - *s2);
}

void* memset(void* v,int c,size_t n) {
	const unsigned char uc = c;
	unsigned char *su;
	for(su = v;0 < n;++su,--n)
	*su = uc;
	return v;
}

void* memcpy(void* out, const void* in, size_t n) {
    unsigned char *p = (unsigned char *) out;
    unsigned char *q = (unsigned char *) in;

    while(n--)
    {
        *p++ = *q++;
    }

    return out;
}

int memcmp(const void* s1, const void* s2, size_t n){
	unsigned char* src = (unsigned char*)s2;
	unsigned char* dst = (unsigned char*)s1;
	for(int i = 0; i < n; i++){
		if(src[i] < dst[i]) 
			return -1;
		if(src[i] > dst[i])
			return 1;
	}
	return 0;
}

#endif

标签:int,fmt,unsigned,char,++,str,test
From: https://www.cnblogs.com/cherish-/p/16735694.html

相关文章