Пример #1
0
int
NXVScanf(NXStream *stream, register const char *fmt, va_list arg)
{
	register int ch;
	int nmatch, len, ch1;
	void *ptr;
	int fileended, size;
	char _sctab[SCTAB_SIZE];

	_NXVerifyStream(stream);
	bzero( _sctab, SCTAB_SIZE );
	_sctab[9] = _sctab[10] = _sctab[32] = SPC;
	nmatch = 0;
	fileended = 0;
	for (;;) switch (ch = *fmt++) {
	case '\0': 
		return (nmatch);
	case '%': 
		if ((ch = *fmt++) == '%')
			goto def;
		ptr = 0;
		if (ch != '*')
			ptr = va_arg(arg, void *);
		else
			ch = *fmt++;
		len = 0;
		size = REGULAR;
		while (isdigit(ch)) {
			len = len*10 + ch - '0';
			ch = *fmt++;
		}
		if (len == 0)
			len = 30000;
		if (ch=='l') {
			size = LONG;
			ch = *fmt++;
		} else if (ch=='h') {
			size = SHORT;
			ch = *fmt++;
		} else if (ch=='[')
			fmt = _getccl(fmt, _sctab);
		if (isupper(ch)) {
			ch = tolower(ch);
			size = LONG;
		}
		if (ch == '\0')
			return(-1);
		if (_innum(ptr, ch, len, size, stream, &fileended, _sctab) && ptr)
			nmatch++;
		if (fileended)
			return(nmatch? nmatch: -1);
		break;

	case ' ':
	case '\n':
	case '\t': 
		while ((ch1 = NXGetc(stream))==' ' || ch1=='\t' || ch1=='\n')
			;
		if (ch1 != EOF)
			NXUngetc(stream);
		break;

	default: 
	def:
		ch1 = NXGetc(stream);
		if (ch1 != ch) {
			if (ch1==EOF)
				return(-1);
			NXUngetc(stream);
			return(nmatch);
		}
	}
Пример #2
0
/**
 * Scan and recognize input according to a format
 * @param *fmt format string for the scanf
 * @param **argp arguments to scanf
 * @param getch function to unget a character
 * @param ungetch function to unget a character
 * @param arg1 1st argument to getch/ungetch
 * @param arg2 2nd argument to getch/ungetch
 */
int _doscan(register char *fmt, register int **argp, int (*getch)(), int (*ungetch)(), int arg1, int arg2)
{
	register int ch;
	int nmatch, len, ch1;
	int **ptr, fileended, size;

	nmatch = 0;
	fileended = 0;
	for (;;) 
	{
		switch (ch = *fmt++) 
		{
			case '\0': 
				return (nmatch);
			case '%': 
				if ((ch = *fmt++) == '%')
				{ goto def; }
				ptr = 0;
				if (ch != '*')
			    { ptr = argp++; }
				else
				{ ch = *fmt++; }
				len = 0;
				size = REGULAR;
				while (isdigit(ch)) 
				{
					len = len*10 + ch - '0';
					ch = *fmt++;
				}
				if (len == 0)
				{ len = 30000; }
				if (ch=='l') 
				{
					ch = *fmt++;
					size = LONG;
				} 
				else if (ch=='h') 
				{
					size = SHORT;
					ch = *fmt++;
				} 
				else if (ch=='[')
			    { fmt = _getccl(fmt); }
				if (isupper(ch)) 
				{
					ch = tolower(ch);
	   				size = LONG;
				}
				if (ch == '\0')
				{ return (-1); }
			    if (_innum(ptr, ch, len, size, getch, ungetch, arg1, arg2, &fileended) && ptr)
				{ nmatch++; }
				if (fileended)
				{ return (nmatch? nmatch: -1); }
				break;

			case ' ':
			case '\n':
			case '\t': 
		        while ((ch1 = (*getch)(arg1, arg2))==' ' || ch1=='\t' || ch1=='\n')
                { ; }
				if (ch1 != EOF)
				{ (*ungetch)(arg1, arg2); }
				break;

			default: 
 def:
		        ch1 = (*getch)(arg1, arg2);
  				if (ch1 != ch) 
				{
					if (ch1==EOF)
					{ return (-1); }
		            (*ungetch)(arg1, arg2);
					return nmatch;
				}
		}
	}
}