Path: blob/master/waterbox/libc/functions/stdio/_PDCLIB_scan.c
2 views
/* _PDCLIB_scan( const char *, struct _PDCLIB_status_t * )12This file is part of the Public Domain C Library (PDCLib).3Permission is granted to use, modify, and / or redistribute at will.4*/56#include <stdio.h>7#include <stdbool.h>8#include <stdlib.h>9#include <stdarg.h>10#include <stdint.h>11#include <ctype.h>12#include <string.h>13#include <stddef.h>14#include <limits.h>1516#ifndef REGTEST1718#include "_PDCLIB_io.h"1920/* Using an integer's bits as flags for both the conversion flags and length21modifiers.22*/23#define E_suppressed 1<<024#define E_char 1<<625#define E_short 1<<726#define E_long 1<<827#define E_llong 1<<928#define E_intmax 1<<1029#define E_size 1<<1130#define E_ptrdiff 1<<1231#define E_intptr 1<<1332#define E_ldouble 1<<1433#define E_unsigned 1<<16343536/* Helper function to get a character from the string or stream, whatever is37used for input. When reading from a string, returns EOF on end-of-string38so that handling of the return value can be uniform for both streams and39strings.40*/41static int GET( struct _PDCLIB_status_t * status )42{43int rc = EOF;44if ( status->stream != NULL )45{46rc = getc( status->stream );47}48else49{50rc = ( *status->s == '\0' ) ? EOF : (unsigned char)*((status->s)++);51}52if ( rc != EOF )53{54++(status->i);55++(status->current);56}57return rc;58}596061/* Helper function to put a read character back into the string or stream,62whatever is used for input.63*/64static void UNGET( int c, struct _PDCLIB_status_t * status )65{66if ( status->stream != NULL )67{68ungetc( c, status->stream ); /* TODO: Error? */69}70else71{72--(status->s);73}74--(status->i);75--(status->current);76}777879/* Helper function to check if a character is part of a given scanset */80static bool IN_SCANSET( const char * scanlist, const char * end_scanlist, int rc )81{82// SOLAR83int previous = -1;84while ( scanlist != end_scanlist )85{86if ( ( *scanlist == '-' ) && ( previous != -1 ) )87{88/* possible scangroup ("a-z") */89if ( ++scanlist == end_scanlist )90{91/* '-' at end of scanlist does not describe a scangroup */92return rc == '-';93}94while ( ++previous <= (unsigned char)*scanlist )95{96if ( previous == rc )97{98return true;99}100}101previous = -1;102}103else104{105/* not a scangroup, check verbatim */106if ( rc == (unsigned char)*scanlist )107{108return true;109}110previous = (unsigned char)(*scanlist++);111}112}113return false;114}115116117const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status )118{119/* generic input character */120int rc = EOF;121const char * orig_spec = spec;122if ( *(++spec) == '%' )123{124/* %% -> match single '%' */125rc = GET( status );126switch ( rc )127{128case EOF:129/* input error */130if ( status->n == 0 )131{132status->n = -1;133}134return NULL;135case '%':136return ++spec;137default:138UNGET( rc, status );139break;140}141}142/* Initializing status structure */143status->flags = 0;144status->base = -1;145status->current = 0;146status->width = 0;147status->prec = 0;148149/* '*' suppresses assigning parsed value to variable */150if ( *spec == '*' )151{152status->flags |= E_suppressed;153++spec;154}155156/* If a width is given, strtol() will return its value. If not given,157strtol() will return zero. In both cases, endptr will point to the158rest of the conversion specifier - just what we need.159*/160char const * prev_spec = spec;161status->width = (int)strtol( spec, (char**)&spec, 10 );162if ( spec == prev_spec )163{164status->width = UINT_MAX;165}166167/* Optional length modifier168We step one character ahead in any case, and step back only if we find169there has been no length modifier (or step ahead another character if it170has been "hh" or "ll").171*/172switch ( *(spec++) )173{174case 'h':175if ( *spec == 'h' )176{177/* hh -> char */178status->flags |= E_char;179++spec;180}181else182{183/* h -> short */184status->flags |= E_short;185}186break;187case 'l':188if ( *spec == 'l' )189{190/* ll -> long long */191status->flags |= E_llong;192++spec;193}194else195{196/* l -> long */197status->flags |= E_long;198}199break;200case 'j':201/* j -> intmax_t, which might or might not be long long */202status->flags |= E_intmax;203break;204case 'z':205/* z -> size_t, which might or might not be unsigned int */206status->flags |= E_size;207break;208case 't':209/* t -> ptrdiff_t, which might or might not be long */210status->flags |= E_ptrdiff;211break;212case 'L':213/* L -> long double */214status->flags |= E_ldouble;215break;216default:217--spec;218break;219}220221/* Conversion specifier */222223/* whether valid input had been parsed */224bool value_parsed = false;225226switch ( *spec )227{228case 'd':229status->base = 10;230break;231case 'i':232status->base = 0;233break;234case 'o':235status->base = 8;236status->flags |= E_unsigned;237break;238case 'u':239status->base = 10;240status->flags |= E_unsigned;241break;242case 'x':243status->base = 16;244status->flags |= E_unsigned;245break;246case 'f':247case 'F':248case 'e':249case 'E':250case 'g':251case 'G':252case 'a':253case 'A':254break;255case 'c':256{257char * c = va_arg( status->arg, char * );258/* for %c, default width is one */259if ( status->width == UINT_MAX )260{261status->width = 1;262}263/* reading until width reached or input exhausted */264while ( ( status->current < status->width ) &&265( ( rc = GET( status ) ) != EOF ) )266{267*(c++) = rc;268value_parsed = true;269}270/* width or input exhausted */271if ( value_parsed )272{273++status->n;274return ++spec;275}276else277{278/* input error, no character read */279if ( status->n == 0 )280{281status->n = -1;282}283return NULL;284}285}286case 's':287{288char * c = va_arg( status->arg, char * );289while ( ( status->current < status->width ) &&290( ( rc = GET( status ) ) != EOF ) )291{292if ( isspace( rc ) )293{294UNGET( rc, status );295if ( value_parsed )296{297/* matching sequence terminated by whitespace */298*c = '\0';299++status->n;300return ++spec;301}302else303{304/* matching error */305return NULL;306}307}308else309{310/* match */311value_parsed = true;312*(c++) = rc;313}314}315/* width or input exhausted */316if ( value_parsed )317{318*c = '\0';319++status->n;320return ++spec;321}322else323{324/* input error, no character read */325if ( status->n == 0 )326{327status->n = -1;328}329return NULL;330}331}332case '[':333{334const char * endspec = spec;335bool negative_scanlist = false;336if ( *(++endspec) == '^' )337{338negative_scanlist = true;339++endspec;340}341spec = endspec;342do343{344// TODO: This can run beyond a malformed format string345++endspec;346} while ( *endspec != ']' );347// read according to scanlist, equiv. to %s above348char * c = va_arg( status->arg, char * );349while ( ( status->current < status->width ) &&350( ( rc = GET( status ) ) != EOF ) )351{352if ( negative_scanlist )353{354if ( IN_SCANSET( spec, endspec, rc ) )355{356UNGET( rc, status );357break;358}359}360else361{362if ( ! IN_SCANSET( spec, endspec, rc ) )363{364UNGET( rc, status );365break;366}367}368value_parsed = true;369*(c++) = rc;370}371if ( value_parsed )372{373*c = '\0';374++status->n;375return ++endspec;376}377else378{379if ( rc == EOF )380{381status->n = -1;382}383return NULL;384}385}386case 'p':387status->base = 16;388// TODO: Like _PDCLIB_print, E_pointer(?)389status->flags |= E_unsigned | E_long;390break;391case 'n':392{393int * val = va_arg( status->arg, int * );394*val = status->i;395return ++spec;396}397default:398/* No conversion specifier. Bad conversion. */399return orig_spec;400}401402if ( status->base != -1 )403{404/* integer conversion */405uintmax_t value = 0; /* absolute value read */406bool prefix_parsed = false;407int sign = 0;408while ( ( status->current < status->width ) &&409( ( rc = GET( status ) ) != EOF ) )410{411if ( isspace( rc ) )412{413if ( sign )414{415/* matching sequence terminated by whitespace */416UNGET( rc, status );417break;418}419else420{421/* leading whitespace not counted against width */422status->current--;423}424}425else if ( ! sign )426{427/* no sign parsed yet */428switch ( rc )429{430case '-':431sign = -1;432break;433case '+':434sign = 1;435break;436default:437/* not a sign; put back character */438sign = 1;439UNGET( rc, status );440break;441}442}443else if ( ! prefix_parsed )444{445/* no prefix (0x... for hex, 0... for octal) parsed yet */446prefix_parsed = true;447if ( rc != '0' )448{449/* not a prefix; if base not yet set, set to decimal */450if ( status->base == 0 )451{452status->base = 10;453}454UNGET( rc, status );455}456else457{458/* starts with zero, so it might be a prefix. */459/* check what follows next (might be 0x...) */460if ( ( status->current < status->width ) &&461( ( rc = GET( status ) ) != EOF ) )462{463if ( tolower( rc ) == 'x' )464{465/* 0x... would be prefix for hex base... */466if ( ( status->base == 0 ) ||467( status->base == 16 ) )468{469status->base = 16;470}471else472{473/* ...unless already set to other value */474UNGET( rc, status );475value_parsed = true;476}477}478else479{480/* 0... but not 0x.... would be octal prefix */481UNGET( rc, status );482if ( status->base == 0 )483{484status->base = 8;485}486/* in any case we have read a zero */487value_parsed = true;488}489}490else491{492/* failed to read beyond the initial zero */493value_parsed = true;494break;495}496}497}498else499{500char * digitptr = memchr( _PDCLIB_digits, tolower( rc ), status->base );501if ( digitptr == NULL )502{503/* end of input item */504UNGET( rc, status );505break;506}507value *= status->base;508value += digitptr - _PDCLIB_digits;509value_parsed = true;510}511}512/* width or input exhausted, or non-matching character */513if ( ! value_parsed )514{515/* out of input before anything could be parsed - input error */516/* FIXME: if first character does not match, value_parsed is not set - but it is NOT an input error */517if ( ( status->n == 0 ) && ( rc == EOF ) )518{519status->n = -1;520}521return NULL;522}523/* convert value to target type and assign to parameter */524if ( ! ( status->flags & E_suppressed ) )525{526switch ( status->flags & ( E_char | E_short | E_long | E_llong |527E_intmax | E_size | E_ptrdiff |528E_unsigned ) )529{530case E_char:531*( va_arg( status->arg, char * ) ) = (char)( value * sign );532break;533case E_char | E_unsigned:534*( va_arg( status->arg, unsigned char * ) ) = (unsigned char)( value * sign );535break;536537case E_short:538*( va_arg( status->arg, short * ) ) = (short)( value * sign );539break;540case E_short | E_unsigned:541*( va_arg( status->arg, unsigned short * ) ) = (unsigned short)( value * sign );542break;543544case 0:545*( va_arg( status->arg, int * ) ) = (int)( value * sign );546break;547case E_unsigned:548*( va_arg( status->arg, unsigned int * ) ) = (unsigned int)( value * sign );549break;550551case E_long:552*( va_arg( status->arg, long * ) ) = (long)( value * sign );553break;554case E_long | E_unsigned:555*( va_arg( status->arg, unsigned long * ) ) = (unsigned long)( value * sign );556break;557558case E_llong:559*( va_arg( status->arg, long long * ) ) = (long long)( value * sign );560break;561case E_llong | E_unsigned:562*( va_arg( status->arg, unsigned long long * ) ) = (unsigned long long)( value * sign );563break;564565case E_intmax:566*( va_arg( status->arg, intmax_t * ) ) = (intmax_t)( value * sign );567break;568case E_intmax | E_unsigned:569*( va_arg( status->arg, uintmax_t * ) ) = (uintmax_t)( value * sign );570break;571572case E_size:573/* E_size always implies unsigned */574*( va_arg( status->arg, size_t * ) ) = (size_t)( value * sign );575break;576577case E_ptrdiff:578/* E_ptrdiff always implies signed */579*( va_arg( status->arg, ptrdiff_t * ) ) = (ptrdiff_t)( value * sign );580break;581582default:583puts( "UNSUPPORTED SCANF FLAG COMBINATION" );584return NULL; /* behaviour unspecified */585}586++(status->n);587}588return ++spec;589}590/* TODO: Floats. */591return NULL;592}593#endif594595#ifdef TEST596#define _PDCLIB_FILEID "_PDCLIB/scan.c"597#define _PDCLIB_STRINGIO598599#include "_PDCLIB_test.h"600601#ifndef REGTEST602static int testscanf( char const * s, char const * format, ... )603{604struct _PDCLIB_status_t status;605status.n = 0;606status.i = 0;607status.s = (char *)s;608status.stream = NULL;609va_start( status.arg, format );610if ( *(_PDCLIB_scan( format, &status )) != '\0' )611{612printf( "_PDCLIB_scan() did not return end-of-specifier on '%s'.\n", format );613++TEST_RESULTS;614}615va_end( status.arg );616return status.n;617}618#endif619620#define TEST_CONVERSION_ONLY621622int main( void )623{624#ifndef REGTEST625char source[100];626#include "scanf_testcases.h"627#endif628return TEST_RESULTS;629}630631#endif632633634