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

📄 xscanf.c

📁 VRTX操作系统
💻 C
📖 第 1 页 / 共 3 页
字号:
	#define	BASE_10_EXP(x)	(x)
#endif

#if	(defined (EXCLUDE_FORMAT_IO_INT_FMT)) && \
	(defined (EXCLUDE_FORMAT_IO_HEX_FMT)) && \
	(defined (EXCLUDE_FORMAT_IO_PNT_FMT))
	#define	BASE_16_EXP(x)	(0)
#else
	#define	BASE_16_EXP(x)	(x)
#endif

#if	(defined (EXCLUDE_FORMAT_IO_INT_FMT)) && \
	(defined (EXCLUDE_FORMAT_IO_OCT_FMT))
	#define	BASE_8_EXP(x)	(0)
#else
	#define	BASE_8_EXP(x)	(x)
#endif

/*	Stolen from stdio.h	*/
#define EOF             (-1)

#ifdef _sun
    #define	NULL	((void *)0)
#endif

#ifndef EXCLUDE_FORMAT_IO_FLOAT_FMT
#include	"xsfef.c"
#endif

#define EMPTY		-2
#define SETSIZE		32
#define MAXWIDTH	32767
#define NUL		'\0'

#define	isset(a, i)	(a[(i) >> 3] & (1 << ((i) & 7)))
#define	setbit(a, i)	a[(i) >> 3] |= 1 << ((i) & 7)
#define	clrbit(a, i)	a[(i) >> 3] &= ~(1 << ((i) & 7))

#define min(x,y)	(((x)<(y))?(x):(y))

#define getNext		if(EOF==(in = (--width > 0) ? (++inputCnt, (*getcfn)(param)) : EMPTY)) --inputCnt;	/* Correct if EOF returned */

#define computeCount	(inputCnt - (((in == EOF) || (in == EMPTY)) ? 0 : 1))

#define FMT_NONE	0
#define FMT_EOF		1
#define FMT_LITNOMATCH	2
#define FMT_WS		3
#define FMT_LITERAL	4
#define FMT_CHAR	5
#define FMT_STRING	6
#define FMT_INT		7
#define FMT_FLOAT	8
#define FMT_CHARSET	9
#define	FMT_INPUTCNT	10

#define FIF_CONTIN	010
#define FIF_SKIP_WS	004
#define FIF_NEED_CHAR	002
#define FIF_ASSIGNS	001

#define FORMAT_FLAGS	"\000\000\000\014\012\013\017\017\017\013\011"

#if defined(EXCLUDE_FORMAT_IO_INT_FMT) && \
    defined(EXCLUDE_FORMAT_IO_DEC_FMT) && \
    defined(EXCLUDE_FORMAT_IO_UNS_FMT) && \
    defined(EXCLUDE_FORMAT_IO_HEX_FMT) && \
    defined(EXCLUDE_FORMAT_IO_OCT_FMT) && \
    defined(EXCLUDE_FORMAT_IO_PNT_FMT)
	/* No code inserted here */
#else

static int chtol(int ch)	/* conver a character to its value int */
{
	if ((ch <='9') && (ch>='0'))
		return (ch - '0');
	if ((ch <='f') && (ch>='a'))
		return (ch - 'a' + 10);
	if ((ch <='F') && (ch>='A'))
		return (ch - 'A' + 10);
	return (0);
}

#endif	/* EXCLUDE_FORMAT_IO_INT_FMT && .... */

#if defined(EXCLUDE_FORMAT_IO_INT_FMT) && defined(EXCLUDE_FORMAT_IO_OCT_FMT)
	/* No code inserted here */
#else

static int isodigit(int c)
{
    return (c >= '0') && (c <= '7');
}

#endif	/* (EXCLUDE_FORMAT_IO_INT_FMT) || .... */

#if defined(EXCLUDE_FORMAT_IO_INT_FMT) && \
    defined(EXCLUDE_FORMAT_IO_DEC_FMT) && \
    defined(EXCLUDE_FORMAT_IO_UNS_FMT)
	/* No code inserted here */
#else

static int isdigit(int c)
{
    return (c >= '0') && (c <= '9');
}

#endif	/* EXCLUDE_FORMAT_IO_INT_FMT && .... */

#if defined(EXCLUDE_FORMAT_IO_INT_FMT) && \
    defined(EXCLUDE_FORMAT_IO_HEX_FMT) && \
    defined(EXCLUDE_FORMAT_IO_PNT_FMT)
	/* No code insterted here */
#else

static int isxdigit(int c)
{
    return ((c >= '0') && (c <= '9')) || 
	   ((c >= 'a') && (c <= 'f')) || 
	   ((c >= 'A') && (c <= 'F'));
}

#endif	/* EXCLUDE_FORMAT_IO_INT_FMT && .... */

static int isspace (int c)
{
    return (c == ' ' || (c >= '\t' && c <= '\r'));
}

/****************************************************************/
/* little helper function for getting next character from string */

static int ss_getc(ptr)
char **ptr;
{
    int ch = **ptr;
    if (ch == NUL)
	return EOF;
    ++*ptr;
    return ch;
}

/****************************************************************/


int _scanf(getcfn, ungetcfn, param, fmt, parglist)
register int (*getcfn)(char *);
         int (*ungetcfn)(int, char *);
register char *param;
char	*fmt;
va_list parglist;

{
    char *format_flag = FORMAT_FLAGS;	/* replaces table */
register int in = EMPTY;	/* current character from input stream */
register int inputCnt = 0;	/* # of input chars read so far */
    int fch;		/* current character from format string */
    int format = FMT_WS; /* field attribute: format code */
    int width;		/* field attribute: total width */
    int (*isdigitfn)();	/* field attribute: is-digit? function */
    int base;		/* field attribute: numeric base */
    char set[SETSIZE];	/* set of characters matched by %[, also %f buffer */
    int nmatch = 0;	/* number of matched and assigned fields */
    int ismatch;	/* did this field match? */
    char *pvar;		/* pointer to variable we are assigning to */
    char *inputstr;	/* input string */
    int	start_convrt=0;	/* flag indicated conversion has started */

    PNT_DEF		/* flag to indicated %p format */
    ASSGN_DEF		/* field attribute: assign value? */
    SIZE_DEF		/* field attribute: h=short, l=long, other=int */

    if (getcfn == (int (*)()) NULL) {
	getcfn = (int (*)()) ss_getc;
	inputstr = param;
	param = (char *) &inputstr;
    }
    while (format_flag[format] & FIF_CONTIN) {
	format = FMT_NONE;
	width = 0;
	ismatch = 0;
	ASSGN_EXP (isassigned = 1);
	PNT_EXP (ispointer = 0);
	switch (fch = *fmt++) {
	    case NUL:
	        break;
	    case ' ':
	    case '\t':
	    case '\n':
	        format = FMT_WS;
	        break;
	    case '%':
	        fch = *fmt++;
	        if (fch == '*') {
		    ASSGN_EXP (isassigned = 0);
		    fch = *fmt++;
	        }

	        for (; isdigit(fch); fch = *fmt++)	/* Get field width */
		    width = 10 * width + fch - '0';

	        SIZE_EXP (size = fch);
	        if (OPT_h_EXP (size == 'h') 
			|| OPT_l_EXP (size == 'l') 
			|| OPT_L_EXP (size == 'L'))
		    fch = *fmt++;

	        switch (fch) {
		    case NUL:
			format = FMT_EOF;
			break;
		    case '%':
			format = FMT_LITERAL;
			break;

#ifdef EXCLUDE_FORMAT_IO_CHAR_FMT
	/* No code inserted here */
#else
		    case 'c':
			start_convrt = 1;
			format = FMT_CHAR;
			break;
#endif	/* EXCLUDE_FORMAT_IO_CHAR_FMT */

#ifdef EXCLUDE_FORMAT_IO_STR_FMT
	/* No code inserted here */
#else
		    case 's':
			start_convrt = 1;
			format = FMT_STRING;
			break;
#endif	/* EXCLUDE_FORMAT_IO_STR_FMT */

#ifdef EXCLUDE_FORMAT_IO_OCT_FMT
	/* No code inserted here */
#else
		    case 'o':
			start_convrt = 1;
			format = FMT_INT;
			isdigitfn = isodigit;
			base = 8;
			break;
#endif	/* EXCLUDE_FORMAT_IO_OCT_FMT */

#if defined(EXCLUDE_FORMAT_IO_UNS_FMT) && defined(EXCLUDE_FORMAT_IO_DEC_FMT)
	/* No code inserted here */
#else

		    case 'u':
		    case 'd':
			start_convrt = 1;
			format = FMT_INT;
			isdigitfn = isdigit;
			base = 10;
			break;
#endif	/* (EXCLUDE_FORMAT_IO_UNS_FMT) || .... */

#ifdef EXCLUDE_FORMAT_IO_PNT_FMT
	/* No code inserted here */
#else
		    case 'p':
			start_convrt = 1;
			ispointer++;		/* Set flag then treat as %x */
#endif  /* EXCLUDE_FORMAT_IO_PNT_FMT */

#if defined(EXCLUDE_FORMAT_IO_PNT_FMT) && defined(EXCLUDE_FORMAT_IO_HEX_FMT)
	/* No code inserted here */
#else
		    case 'X':
		    case 'x':
			start_convrt = 1;
			format = FMT_INT;
			isdigitfn = isxdigit;
			base = 16;
			break;
#endif	/* EXCLUDE_FORMAT_IO_PNT_FMT && .... */

#ifdef EXCLUDE_FORMAT_IO_INT_FMT
	/* No code inserted here */
#else
		    case 'i':
			start_convrt = 1;
			format = FMT_INT;
			base = 0;		/* Pick up base from input */
			break;
#endif	/* EXCLUDE_FORMAT_IO_INT_FMT */

#ifdef EXCLUDE_FORMAT_IO_FLOAT_FMT
	/* No code inserted here */
#else
		    case 'e':
		    case 'f':
		    case 'g':
		    case 'E':
		    case 'G':
			start_convrt = 1;
			format = FMT_FLOAT;
			width = min(width, SETSIZE - 1);
			break;
#endif	/* EXCLUDE_FORMAT_IO_FLOAT_FMT */

#ifdef EXCLUDE_FORMAT_IO_NUMB_FMT
	/* No code inserted here */
#else
		    case 'n':
			format = FMT_INPUTCNT;
			break;
#endif  /* EXCLUDE_FORMAT_IO_NUMB_FMT */

#ifdef EXCLUDE_FORMAT_IO_BRAKT_FMT
	/* No code inserted here */
#else
		    case '[':
			start_convrt = 1;
			format = FMT_CHARSET;
			{
			    int iscomplement = 0;
			    int i;

			    fch = *fmt++;
			    if ((iscomplement = (fch == '^')) != 0)

⌨️ 快捷键说明

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