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

📄 sscanf.c

📁 MIPS下的boottloader yamon 的源代码
💻 C
字号:

/************************************************************************
 *
 *  sscanf.c
 *
 *  sscanf function
 *
 *
 * ######################################################################
 *
 * Copyright (c) 1999-2000 MIPS Technologies, Inc. All rights reserved. 
 * 
 * Unpublished rights reserved under the Copyright Laws of the United States of 
 * America. 
 * 
 * This document contains information that is proprietary to MIPS Technologies, 
 * Inc. ("MIPS Technologies"). Any copying, modifying or use of this information 
 * (in whole or in part) which is not expressly permitted in writing by MIPS 
 * Technologies or a contractually-authorized third party is strictly 
 * prohibited. At a minimum, this information is protected under unfair 
 * competition laws and the expression of the information contained herein is 
 * protected under federal copyright laws. Violations thereof may result in 
 * criminal penalties and fines. 
 * MIPS Technologies or any contractually-authorized third party reserves the 
 * right to change the information contained in this document to improve 
 * function, design or otherwise. MIPS Technologies does not assume any 
 * liability arising out of the application or use of this information. Any 
 * license under patent rights or any other intellectual property rights owned 
 * by MIPS Technologies or third parties shall be conveyed by MIPS Technologies 
 * or any contractually-authorized third party in a separate license agreement 
 * between the parties. 
 * The information contained in this document constitutes one or more of the 
 * following: commercial computer software, commercial computer software 
 * documentation or other commercial items. If the user of this information, or 
 * any related documentation of any kind, including related technical data or 
 * manuals, is an agency, department, or other entity of the United States 
 * government ("Government"), the use, duplication, reproduction, release, 
 * modification, disclosure, or transfer of this information, or any related 
 * documentation of any kind, is restricted in accordance with Federal 
 * Acquisition Regulation 12.212 for civilian agencies and Defense Federal 
 * Acquisition Regulation Supplement 227.7202 for military agencies. The use of 
 * this information by the Government is further restricted in accordance with 
 * the terms of the license agreement(s) and/or applicable contract terms and 
 * conditions covering this information from MIPS Technologies or any 
 * contractually-authorized third party. 
 *
 ************************************************************************/


/************************************************************************
 *  Include files
 ************************************************************************/

#include <string.h>
#include <ctype.h>
#include <stdio.h>

/************************************************************************
 *  Definitions
 ************************************************************************/

#define	FLOAT	0

#define	EOF	-1
#define	NUL	0

#ifndef _fstrcpy
#define _fstrcpy strcpy
#endif

#define MAXLINE 64

typedef int  BOOL;
typedef int  TBOOL;

#define NEXT            TRUE    /* Forward direction for getting chars. */
#define BACK            FALSE   /* Reverse direction for getting chars. */
#define LONGTYP         TRUE    /* Value to signal long or double.      */
#define NORMTYP         FALSE   /* Value to denote normal width number. */
#define SHORTTYP        -TRUE   /* Value to denote short rather than int*/

/************************************************************************
 *  Public variables
 ************************************************************************/

/************************************************************************
 *  Static variables
 ************************************************************************/

static char      *scanstr;
static BOOL      NOT_EOL;

static int       width;
static int       (*xgetc)();

/************************************************************************
 *  Static function prototypes
 ************************************************************************/

static int 
sgetc(
    int forward );

static BOOL 
eatblanks( void );

static long 
nxtnumber(
    int base );

#if FLOAT

static double
nxtfloat(
    char *buffer );

#endif

/************************************************************************
 *  Implementation : Static functions
 ************************************************************************/


/************************************************************************
 *
 *                          sgetc
 *  Description :
 *  -------------
 *
 *
 *  Return values :
 *  ---------------
 *
 *  Character from the static scanstr.
 *  If forward is TRUE, get next character and advance; else, back up 
 *  and get one unless the end has been reached. Return EOF if so.
 *
 ************************************************************************/
static int
sgetc(
    BOOL forward )
{
	if (forward)
	{       if (*scanstr)
			return *scanstr++ & 0xff;

		else
			NOT_EOL = FALSE;
	}
	else if (NOT_EOL)
		return *--scanstr & 0xff;

	return EOF;
}



/************************************************************************
 *
 *                          eatblanks
 *  Description :
 *  -------------
 *
 *  Get characters from input medium until no longer whitespace.
 *
 *  Return values :
 *  ---------------
 *
 *  FALSE normally, or TRUE at end of line.
 *
 ************************************************************************/
static BOOL
eatblanks( void )     
{
    char c;
	
    while (isspace(c = (*xgetc)(NEXT)))
	;
    c = c; /* dummy */
    if ((*xgetc)(BACK) == EOF)
		return TRUE;
    else
		return FALSE;
}


#if	FLOAT

/************************************************************************
 *
 *                          nxtfloat
 *  Description :
 *  -------------
 *
 *  Get the next ascii e-form or f-form from the input medium, and 
 *  return its value. Store the ascii form in buffer.
 *
 *  Return values :
 *  ---------------
 *
 *  See description above.
 *
 ************************************************************************/
static double
nxtfloat(
    char *buffer )
{
	int c;
	char *s;
	TBOOL decmlpt, sign, exp;
	double atof();

	s = buffer;
	sign = exp = decmlpt = FALSE;
	while (width--)
	{       c = (*xgetc)(NEXT);
		if (!sign && (c == '-' || c == '+'))
			sign = TRUE;
		else if (!decmlpt && c == '.')
			sign = decmlpt = TRUE;
		else if (!exp && (c == 'e' || c == 'E'))
		{       sign = FALSE;
			exp = decmlpt = TRUE;
		}
		else if (!isdigit(c))
		{       (*xgetc)(BACK);
			break;
		}
		*s++ = c;
	}
	*s = 0;
	/*return atof(buffer);*/    
	return 0L;
}

#endif  /* #if FLOAT */


/************************************************************************
 *
 *                          nxtnumber
 *  Description :
 *  -------------
 *
 *  Get the ascii string of a number with given base from input
 *  medium, return its value.
 *
 *  Return values :
 *  ---------------
 *
 *  See description above.
 *
 ************************************************************************/
static long
nxtnumber(
    int base )
{
	int c;
	long value;
	BOOL minus;
	static char digits[] = "0123456789abcdef";

	if (width <= 0)
		return 0L;

	value = 0;
	minus = FALSE;
	if ((c = (*xgetc)(NEXT)) == '-')
	{       minus = TRUE;
		--width;
	}
	else if (c == '+')
		--width;
	else
		(*xgetc)(BACK);
	while (width--)
	{       c = (int)(index((char *)digits, tolower((*xgetc)(NEXT))) - (char *)digits);
		if (c < 0 || c >= base)
		{       (*xgetc)(BACK);
			break;
		}
		value *= base;
		value += c;
	}
	return (minus ? -value : value);
}


/************************************************************************
 *  Implementation : Public functions
 ************************************************************************/

/************************************************************************
 *
 *                          sscanf
 *  Description :
 *  -------------
 *
 *  Read string s using format fmt, and deposit information into 
 *  arguments in arg list.
 *
 *  Return values :
 *  ---------------
 *
 *  Return count of items read.
 *
 ************************************************************************/
int 
sscanf(
    char *s, 
    char *fmt, 
    ...)
{
	int sgetc(), unformat();

	int **arg = (int **) (&fmt) + 1;

	scanstr = s;
	NOT_EOL = TRUE;
	return unformat(sgetc, fmt, (int **) arg);
}



/************************************************************************
 *
 *                          unformat
 *  Description :
 *  -------------
 *
 *  Perform scanf() on getch input medium, using format fmt and argument 
 *  list pointed to by arglist. 
 *
 *  Return values :
 *  ---------------
 *
 *  Number of matched and assigned input items, or EOF if input finished 
 *  before fmt.
 *
 ************************************************************************/
int
unformat(
    int    (*getch)(),
    char   *fmt, 
    int    **arglist )
{
	int c;
#if	FLOAT
	double nxtfloat(), d;
#endif
	long nxtnumber(), val;
	int count, base;
	char *s;
	BOOL suppress, numtyp;
	char wkbuf[MAXLINE];

	count = 0;
	xgetc = getch;

	while (c = *fmt++)
	{       if (c == '%')
		{       numtyp = NORMTYP;
			suppress = FALSE;
			width = MAXLINE;
			if (*fmt == '*')
			{       ++fmt;
				suppress = TRUE;
			}
			if (isdigit(*fmt))
			{       width = 0;
				do
				{       width *= 10;
					width += (*fmt - '0');
				} while (isdigit(*++fmt));
			}
			if (*fmt == 'l')
			{       numtyp = LONGTYP;
				++fmt;
			}
			if (*fmt == 'h')
			{       numtyp = SHORTTYP;
				++fmt;
			}
			if (index("DOXEF", c = *fmt++))
				numtyp = LONGTYP;
			switch (c = tolower(c))
			{   case ' ':
				fmt--;
				if (numtyp == SHORTTYP)
					c = 'd';
				else
					break;
			    case 'd':
			    case 'o':
			    case 'x':
				base = (c == 'd'? 10 : (c == 'o' ? 8 : 16));
				if (eatblanks())
					return (count ? count : EOF);

				val = nxtnumber(base);
				if (!suppress)
				{       if (numtyp == LONGTYP)
						*(long *)(*arglist++) = val;
					else if (numtyp == SHORTTYP)
						*(short *)(*arglist++) = val;
					else
						*(*arglist++) = (int)val;
					++count;
				}
				break;
#if	FLOAT
			    case 'e':
			    case 'f':
				if (eatblanks())
					return (count ? count : EOF);

				d = nxtfloat((char *)wkbuf);
				if (!suppress)
				{       if (numtyp == LONGTYP)
						*(double *)(*arglist++) = d;
					else
						*(float *)(*arglist++) = d;
					++count;
				}
				break;
#endif
			    case '[':
			    case 's':
				if (c == '[')
				{       if (*fmt == '^' || *fmt == '~')
					{       numtyp = NORMTYP;
						++fmt;
					}
					else
						numtyp = LONGTYP;
					for (s = (char *)wkbuf;(c = *fmt++) != ']';)
						*s++ = c;
					*s = (char)NUL;
				}
				else
				{       numtyp = NORMTYP;
					_fstrcpy((char *)wkbuf, " \t\n");
				}
				if (eatblanks())
					return (count ? count : EOF);

				if (!suppress)
					s = (char *) *arglist++;
				while (width--)
				{       if ((c = (*xgetc)(NEXT)) == EOF)
						break;
					if (numtyp == NORMTYP ?
					    (index((char *)wkbuf, c) != NUL) :
					    (index((char *)wkbuf, c)  ==  NUL))
					{       (*xgetc)(BACK);
						break;
					}
					if (!suppress)
						*s++ = c;
				}
				if (!suppress)
				{       *s = (char)NUL;
					++count;
				}
				break;

			    case 'c':
				if ((c = (*xgetc)(NEXT)) == EOF)
					return (count ? count : EOF);

				if (!suppress)
				{       *(char *)(*arglist++) = c;
					++count;
				}
				break;
			    case '%':
				goto match;
			}
		}
		else if (isspace(c))
		{       if (eatblanks())
				return (count ? count : EOF);
		}
		else
		{
match:                  if (eatblanks())
				return (count ? count : EOF);

			if ((*xgetc)(NEXT) != c)
				return count;
		}
	}
	return count;
}


/************************************************************************
 *
 *                          index
 *  Description :
 *  -------------
 *
 *  Return values :
 *  ---------------
 *
 *  Return pointer to char c in string s, if it occurs, NUL otherwise.
 *
 ************************************************************************/
char *
index(
    char *s, 
    int  c)
{
	int i;

	while ((i = *s) && c != i)
		s++;
	return (*s ? s : NUL);
}

⌨️ 快捷键说明

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