⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printf.c

📁 一个dos操作系统DRDOS的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
		    	    break;

		        case 'X':		/* unsigned hexadecimal, upper case */
		            sp = to_hex(value, s+14, UPPER_CASE);
		            break;

		        case 's':		/* string -- could be far */
		            cp = farptr((BYTE *)uvalue);
		            for (i=0; cp[i] != '\0'; i++)
		                ;
		            goto havelen;

		        case 'c':
		            c = uvalue;
					/* drop into "default:" */
		        default:			/* normal characters */
		            sp = s+13;
		            *sp = c;
		            break;
		    }

		    i = (s+14) - sp;	/* work out length of string */
		    cp = (BYTE FAR *) sp;	/* convert to FAR pointer */

havelen:
		    if (i > maxwidth)
		        i = maxwidth;
			
		    if (rjust) {
		        while (width-- > i)
			    *buff++ = filler;
		    }

		    for (k = 0; *cp && k < maxwidth; ++k) {
		        if (*cp == '\n')
			        *buff++ = '\r';
		        *buff++ = *cp++;
		    }

		    if (!rjust)	{	/* if right justified */
		        while (width-- > i)
			        *buff++ = ' ';
		    }
	    }
	    else if (c == '\n')	{	/* expand newline to CR,LF then flush the buffer */
		    *buff++ = '\r';
		    *buff++ = '\n';
                if (type!=SPRINTF) {
                    /* Flush the buffer */
	            *buff = '\0';		/* terminate the string	*/
                    switch (type) {
                        case PRINTF:
	                    far_write(err_flag ? STDERR:STDOUT,str,
			    		(UWORD) buff - (UWORD) str);
                            break;
                        case EPRINTF:
	                    far_write(STDERR, str, buff-str);
                            break;
                        default:
	                    far_write(type, str, buff-str);	
                            break;
                    } /*ENDSWITCH*/
                    totlen+=buff-str;
                    buff=str;
                }
	    }
	    else 
		    *buff++ = c;
	}
	*buff = '\0';		/* terminate the string	*/
        return totlen+=buff-str;
}



/* Initialise the table of pointers to all arguments (ptr_list).
   The ptr_list table will not be filled in unless the format
   string (fmt) contains parameter order indexes. */

MLOCAL VOID arg_ptrs(fmt, args, ptr_list)
REG BYTE FAR	*fmt;			/* ASCIIZ format string */
UWORD		*args;			/* pointer to first argument */
UWORD	   	*ptr_list[];		/* list of pointers to all arguments */
{
UWORD		arg_cnt;		/* running count of parameters found */
UWORD		num;
UBYTE		size_list[MAX_ARG_NUM];	/* number bytes in each argument */
UWORD		i;


    /* Read through format string to determine the size of each argument. */
    for (arg_cnt = 0; ; )
    {
    	while (*fmt && *fmt != '%')	/* find the next parameter */
	    fmt++;

	if (!*fmt)			/* patameter found? */
	    break;			/*  no - exit loop */
	fmt++;				/* skip '%' */

	if (*fmt == '%')		/* "%%" escape for percent? */
	{
	    fmt++;			/*  yes - skip second '%' */
	    continue;			/*  and look for next parameter */
	}

	fmt = arg_num(fmt, &num);	/* get this argument's index */
	if (num >= MAX_ARG_NUM)		/* arg index present and valid? */
	    break;			/*  no - go no further */

					/* record this argument's size */
	fmt = arg_siz(fmt, &size_list[num]);
	arg_cnt++;			/* one more argument found */
    }


    /* Loop once for each argument found in format string, filling in
       the offset for that argument in the size_list table. */
    for (i = 0; i < arg_cnt; i++)
    {
    	ptr_list[i] = args;			/* record the address of arg */
	args += (UWORD)(size_list[i]);		/* update ptr by size of arg */
    }
}



/* Determine the size in bytes (siz) of the argument that corresponds to the 
   given format string (fmt). Return a pointer to the character following
   the last in the format string. For example...

	fmt	       siz
   	%d		2
	%ld		4
	%*d		4
*/

MLOCAL BYTE FAR * arg_siz(fmt, siz)
REG BYTE FAR	*fmt;			/* printf argument format string */
UBYTE		*siz;			/* returned argument size, in bytes */
{

    *siz = 0;				/* argument size not yet known */

    if (*fmt != '%')
    	return fmt;			/* format string must begin with '%' */
    fmt++;				/* skip '%' */

    if (*fmt == '#')			/* ignore various formatting ctrls */
	fmt++;
    if (*fmt == '-')
	fmt++;
    if (*fmt == '0')
	fmt++;

    if (*fmt == '*')
    {					/* width argument also on stack */
	*siz += sizeof(WORD);		/* width argument is a WORD */
	fmt++;
    }
    else				/* ignore any static width control */
	while (*fmt >= '0' && *fmt <= '9')
	    fmt++;

    if (*fmt == '.')
    {
	fmt++;
	if (*fmt == '*')
	{				/* 2nd width argument on stack */
	    *siz += sizeof(WORD);
	    fmt++;
	}
	else				/* ignore any static width control */
	    while (*fmt >= '0' && *fmt <= '9')
	    	fmt++;
    }

    if (*fmt == 'l')
    {
    	*siz += sizeof(LONG);
	fmt++;
    }
    else
	*siz += sizeof(WORD);		/* assume sizeof(WORD)==sizeof(BYTE *) */

    return ++fmt;			/* skip final char: 'd', 's', etc. */
}



/* Determine the index number (num) of the argument given by the format
   string (fmt). If the format string does not have a valid index number
   num is set to MAX_ARG_NUM. Return a pointer to the character following
   the last in the index number. For example...

	fmt		num		returned pointer to...
	3%4d		3		'%'
	4d		MAX_ARG_NUM	'4'
	%		MAX_ARG_NUM	'%'
*/

MLOCAL BYTE FAR * arg_num(fmt, num)
BYTE FAR	*fmt;
UWORD	   	*num;
{
REG BYTE FAR	*cp;
REG UWORD	n;


    *num = MAX_ARG_NUM;			/* argument index not yet known */

    cp = fmt;
    if (!isdigit(*cp))			/* at least one digit expected */
	return fmt;
					/* extract index number */
    for (n = 0; *cp >= '0' && *cp <= '9'; cp++)
	n = n * 10 + *cp - '0';

    if (*cp != '%')
    	return fmt;			/* number must be terminated by '%' */

    *num = n;				/* record the index number */
    return cp;				/* return pointer to last '%' */
}



MLOCAL BYTE * to_hex(n, s, hex_base)
ULONG  		n;
BYTE   		*s;
BYTE		hex_base;	/* 'a'-10 for lowercase, 'A'-10 uppercase */
{
REG UBYTE	i;

    do
    {
	i = (UBYTE)n & 0x0F;
	*--s = (i > 9) ? (hex_base + i) : ('0' + i);
	n >>= 4;
    }
    while (n != 0);

    return s;
}



MLOCAL BYTE * to_dec(n, s)
ULONG  		n;
BYTE   		*s;
{
    if ((LONG)n < 0)
    {
	s = to_udec(0 - (LONG)n, s);
	*--s = '-';			/* preceed number with sign */
	return s;
    }
    else
	return to_udec(n, s);
}



MLOCAL BYTE * to_udec(n, s)
ULONG  		n;
BYTE   		*s;
{
    do
    {
	*--s = '0' + (n % 10);
	n /= 10;
    } 
    while (n != 0);

    return s;
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -