1
0
mirror of https://github.com/adtools/clib2.git synced 2025-12-08 14:59:05 +00:00
Files
amiga-clib2/library/stdio_vfscanf.c
Olaf Barthel 7f0f7929ba - Updated copyright text and contact information
- Replaced ISO Latin 1 code #160 with a plain blank space (ASCII code #32) where necessary


git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@15305 87f5fb63-7c3d-0410-a384-fd976d0f7a62
2015-06-10 13:13:40 +00:00

1987 lines
41 KiB
C

/*
* $Id: stdio_vfscanf.c,v 1.22 2015-04-24 13:00:12 obarthel Exp $
*
* :ts=4
*
* Portable ISO 'C' (1994) runtime library for the Amiga computer
* Copyright (c) 2002-2015 by Olaf Barthel <obarthel (at) gmx.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of Olaf Barthel nor the names of contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*#define DEBUG*/
#ifndef _STDLIB_NULL_POINTER_CHECK_H
#include "stdlib_null_pointer_check.h"
#endif /* _STDLIB_NULL_POINTER_CHECK_H */
/****************************************************************************/
#ifndef _STDIO_HEADERS_H
#include "stdio_headers.h"
#endif /* _STDIO_HEADERS_H */
#ifndef _LOCALE_HEADERS_H
#include "locale_headers.h"
#endif /* _LOCALE_HEADERS_H */
/****************************************************************************/
#if defined(FLOATING_POINT_SUPPORT) && !defined(_MATH_HEADERS_H)
#include "math_headers.h"
#endif /* FLOATING_POINT_SUPPORT && !_MATH_HEADERS_H */
/****************************************************************************/
/*
* Uncomment this to activate '%lld' support and the like. Better still,
* define this is in the Makefile!
*/
/*#define USE_64_BIT_INTS*/
/****************************************************************************/
int
vfscanf(FILE *stream, const char *format, va_list arg)
{
enum parameter_size_t
{
parameter_size_byte,
parameter_size_long,
parameter_size_short,
parameter_size_size_t,
parameter_size_ptrdiff_t,
parameter_size_long_long,
parameter_size_long_double,
parameter_size_intmax_t,
parameter_size_default
};
enum parameter_size_t parameter_size;
int total_num_chars_read = 0;
int num_chars_processed;
BOOL assignment_suppressed;
int maximum_field_width;
int result = EOF;
int num_conversions = 0;
int num_assignments = 0;
int conversion_type;
int c;
ENTER();
assert( stream != NULL && format != NULL );
if(__check_abort_enabled)
__check_abort();
flockfile(stream);
#if defined(CHECK_FOR_NULL_POINTERS)
{
if(stream == NULL || format == NULL)
{
__set_errno(EFAULT);
goto out;
}
}
#endif /* CHECK_FOR_NULL_POINTERS */
if(__fgetc_check(stream) < 0)
goto out;
/* Just so we can detect errors and tell them apart from
an 'end of file' condition. */
clearerr(stream);
while((c = (*(unsigned char *)format)) != '\0')
{
D(("next format character is '%lc'",c));
if(isspace(c))
{
/* Skip all blank spaces in the stream. */
format++;
while((c = __getc(stream)) != EOF)
{
if(isspace(c))
{
total_num_chars_read++;
}
else
{
/* End of the white space; we push the last
* character read back into the stream.
*/
if(ungetc(c,stream) == EOF)
goto out;
/* Resume scanning. */
break;
}
}
if(c == EOF)
{
SHOWMSG("end of file");
/* Hit the end of the stream? */
if(num_conversions == 0)
goto out;
/* Finished... */
break;
}
/* Resume scanning. */
continue;
}
else if (c != '%')
{
int d;
SHOWMSG("next character must match exactly");
/* Match the next character in the stream. */
format++;
d = __getc(stream);
if(d == EOF)
{
SHOWMSG("end of file");
/* Hit the end of the stream. */
if(num_conversions == 0)
goto out;
break;
}
else if (c == d)
{
SHOWMSG("it matches");
total_num_chars_read++;
}
else
{
SHOWMSG("it does not match");
if(ungetc(d,stream) == EOF)
goto out;
break;
}
/* Resume scanning. */
continue;
}
format++;
c = (*format);
if(c == '*')
{
/* If the first letter of the format specification
* is an asterisk, no output will be produced for
* this parameter.
*/
assignment_suppressed = TRUE;
format++;
c = (*format);
}
else
{
assignment_suppressed = FALSE;
}
maximum_field_width = -1;
if('0' <= c && c <= '9')
{
int next_sum;
int sum = 0;
/* This could be the field width specification. */
while(TRUE)
{
c = (*format);
if('0' <= c && c <= '9')
{
next_sum = (10 * sum) + c - '0';
if(next_sum < sum) /* overflow? */
break;
sum = next_sum;
format++;
}
else
{
break;
}
}
maximum_field_width = sum;
}
else if (c == '\0')
{
/* And that's the end of the string. */
break;
}
c = (*format);
if(c == 'h')
{
/* Parameter is a short integer. */
parameter_size = parameter_size_short;
SHOWMSG("short integer");
format++;
}
else if (c == 'j')
{
parameter_size = parameter_size_intmax_t;
SHOWMSG("largest integer");
format++;
}
else if (c == 'l')
{
/* Parameter is a long integer or a double precision floating point value. */
parameter_size = parameter_size_long;
SHOWMSG("long integer");
format++;
}
else if (c == 'L')
{
/* Parameter is a long double floating point value. */
parameter_size = parameter_size_long_double;
SHOWMSG("long double");
format++;
}
else if (c == 't')
{
parameter_size = parameter_size_ptrdiff_t;
SHOWMSG("pointer difference");
format++;
}
else if (c == 'z')
{
parameter_size = parameter_size_size_t;
SHOWMSG("size");
format++;
}
else if (c == '\0')
{
/* And that's the end of the string. */
break;
}
else
{
/* Parameter is a long integer or a single precision floating point value. */
parameter_size = parameter_size_default;
SHOWMSG("default");
}
/* Now for the conversion type. */
c = (*format);
if(c == '\0')
break;
#if defined(USE_64_BIT_INTS) && defined(__GNUC__)
{
D(("c = '%lc'",c));
/* Check for long long parameters. */
if(parameter_size == parameter_size_long && c == 'l')
{
SHOWMSG("this is a long long parameter");
parameter_size = parameter_size_long_long;
format++;
/* The conversion type follows. */
c = (*format);
if(c == '\0')
break;
}
}
#endif /* __GNUC__ */
/* Check for byte parameters. */
if(parameter_size == parameter_size_short && c == 'h')
{
parameter_size = parameter_size_byte;
SHOWMSG("byte-sized integer");
format++;
/* The conversion type follows. */
c = (*format);
if(c == '\0')
break;
}
switch(c)
{
/* It's a pointer. */
case 'p':
conversion_type = 'x';