input.c

来自「不错的东西 请查看 WINCE OS」· C语言 代码 · 共 1,160 行 · 第 1/3 页

C
1,160
字号

            if (!suppress) {
                arglistsave = arglist;
                pointer = va_arg(arglist,void *);
            }

            done_flag = 0;

            if (!widechar) {    /* use case if not explicitly specified */
                if ((*format == CRT_T('S')) || (*format == CRT_T('C')))
#ifdef CRT_UNICODE
                    --widechar;
                else
                    ++widechar;
#else
                    ++widechar;
                else
                    --widechar;
#endif
            }

            /* switch to lowercase to allow %E,%G, and to
               keep the switch table small */

            comchr = *format | (CRT_T('a') - CRT_T('A'));

            if (CRT_T('n') != comchr)
                if (CRT_T('c') != comchr && LEFT_BRACKET != comchr)
                    ch = EAT_WHITE();
                else
                    ch = INC();

            if (!widthset || width) {

                switch(comchr) {

                    case CRT_T('c'):
                /*  case CRT_T('C'):  */
                        if (!widthset) {
                            ++widthset;
                            ++width;
                        }
                        if (widechar>0)
                            fl_wchar_arg++;
                        scanptr = (CRT_TCHAR*)cbrackset;
                        --reject; /* set reject to 255 */
                        goto scanit2;

                    case CRT_T('s'):
                /*  case CRT_T('S'):  */
                        if (widechar>0)
                            fl_wchar_arg++;
                        scanptr = (CRT_TCHAR*)sbrackset;
                        --reject; /* set reject to 255 */
                        goto scanit2;

                    case LEFT_BRACKET :   /* scanset */
                        if (widechar>0)
                            fl_wchar_arg++;
                        scanptr = (CRT__TCHAR *)(++format);

                        if (CRT_T('^') == *scanptr) {
                            ++scanptr;
                            --reject; /* set reject to 255 */
                        }


scanit2:
#ifdef CRT_UNICODE
                        memset(table, 0, ASCII*256);
#else
                        memset(table, 0, ASCII);
#endif

#ifdef ALLOW_RANGE

                        if (LEFT_BRACKET == comchr)
                            if (CRT_T(']') == *scanptr) {
                                prevchar = CRT_T(']');
                                ++scanptr;

                                table[ CRT_T(']') >> 3] = 1 << (CRT_T(']') & 7);

                            }

                        while (CRT_T(']') != *scanptr) {

                            rngch = *scanptr++;

                            if (CRT_T('-') != rngch ||
                                 !prevchar ||           /* first char */
                                 CRT_T(']') == *scanptr) /* last char */

                                table[(prevchar = rngch) >> 3] |= 1 << (rngch & 7);

                            else {  /* handle a-z type set */

                                rngch = *scanptr++; /* get end of range */

                                if (prevchar < rngch)  /* %[a-z] */
                                    last = rngch;
                                else {              /* %[z-a] */
                                    last = prevchar;
                                    prevchar = rngch;
                                }
                                for (rngch = prevchar; rngch <= last; ++rngch)
                                    table[rngch >> 3] |= 1 << (rngch & 7);

                                prevchar = 0;

                            }
                        }


#else
                        if (LEFT_BRACKET == comchr)
                            if (CRT_T(']') == *scanptr) {
                                ++scanptr;
                                table[(prevchar = CRT_T(']')) >> 3] |= 1 << (CRT_T(']') & 7);
                            }

                        while (CRT_T(']') != *scanptr) {
                            table[scanptr >> 3] |= 1 << (scanptr & 7);
                            ++scanptr;
                        }
                        /* code under !ALLOW_RANGE is probably never compiled */
                        /* and has probably never been tested */
#endif
                        if (!*scanptr)
                            goto error_return;      /* trunc'd format string */

                        /* scanset completed.  Now read string */

                        if (LEFT_BRACKET == comchr)
                            format = scanptr;

                        start = pointer;

                        /*
                         * execute the format directive. that is, scan input
                         * characters until the directive is fulfilled, eof
                         * is reached, or a non-matching character is
                         * encountered.
                         *
                         * it is important not to get the next character
                         * unless that character needs to be tested! other-
                         * wise, reads from line-buffered devices (e.g.,
                         * scanf()) would require an extra, spurious, newline
                         * if the first newline completes the current format
                         * directive.
                         */
                        UN_INC(ch);

                        while ( !widthset || width-- ) {

                            ch = INC();
                            if (
#ifndef CRT_UNICODE
#ifndef CPRFLAG
                              (EOF != ch) &&
#endif
                              ((table[ch >> 3] ^ reject) & (1 << (ch & 7)))
#else
                              (WEOF != ch) &&
                         /*     ((ch>>3 >= ASCII) ? reject : */
                              ((table[ch >> 3] ^ reject) & 
                                        (1 << (ch & 7)))         /* ) */
#endif /* CRT_UNICODE */
                            ) {
                                if (!suppress) {
#ifndef CRT_UNICODE
                                    if (fl_wchar_arg) {
                                        char temp[2];
                                        int mbsize = 1;
                                        temp[0] = (char) ch;
                                        if (isleadbyte((BYTE)ch))
                                        {
                                            temp[1] = (char) INC();
                                            mbsize++;
                                        }
                                        change_to_widechar(&wctemp, temp, mbsize);
                                        *(wchar_t UNALIGNED *)pointer = wctemp;
                                        /* do nothing if mbtowc fails */
                                        pointer = (wchar_t *)pointer + 1;
                                    } else
#else
                                    if (fl_wchar_arg) {
                                        *(wchar_t UNALIGNED *)pointer = ch;
                                        pointer = (wchar_t *)pointer + 1;
                                    } else
#endif
                                    {
#ifndef CRT_UNICODE
                                    *(char *)pointer = (char)ch;
                                    pointer = (char *)pointer + 1;
#else
                                    int temp;
                                    /* convert wide to multibyte */
                    temp = change_to_multibyte((char *)pointer, MB_LEN_MAX, ch);
                                    /* do nothing if wctomb fails */
                                    pointer = (char *)pointer + temp;
#endif
                                    }
                                } /* suppress */
                                else {
                                    /* just indicate a match */
                                    start = (CRT__TCHAR *)start + 1;
                                }
                            }
                            else  {
                                UN_INC(ch);
                                break;
                            }
                        }

                        /* make sure something has been matched and, if
                           assignment is not suppressed, null-terminate
                           output string if comchr != c */

                        if (start != pointer) {
                            if (!suppress) {
                                ++count;
                                if ('c' != comchr) /* null-terminate strings */
                                    if (fl_wchar_arg)
                                        *(wchar_t UNALIGNED *)pointer = L'\0';
                                    else
                                    *(char *)pointer = '\0';
                            } else /*NULL*/;
                        }
                        else
                            goto error_return;

                        break;

                    case CRT_T('i') :   /* could be d, o, or x */

                        comchr = CRT_T('d'); /* use as default */

                    case CRT_T('x'):

                        if (CRT_T('-') == ch) {
                            ++negative;

                            goto x_incwidth;

                        } else if (CRT_T('+') == ch) {
x_incwidth:
                            if (!--width && widthset)
                                ++done_flag;
                            else
                                ch = INC();
                        }

                        if (CRT_T('0') == ch) {

                            if (CRT_T('x') == (CRT__TCHAR)(ch = INC()) || CRT_T('X') == (CRT__TCHAR)ch) {
                                ch = INC();
                                comchr = CRT_T('x');
                            } else {
                                ++started;
                                if (CRT_T('x') != comchr)
                                    comchr = CRT_T('o');
                                else {
                                    /* scanning a hex number that starts */
                                    /* with a 0. push back the character */
                                    /* currently in ch and restore the 0 */
                                    UN_INC(ch);
                                    ch = CRT_T('0');
                                }
                            }
                        }
                        goto getnum;

                        /* NOTREACHED */

                    case CRT_T('p') :
                        /* force %hp to be treated as %p */
                        longone = 1;

                    case CRT_T('o') :
                    case CRT_T('u') :
                    case CRT_T('d') :

                        if (CRT_T('-') == ch) {
                            ++negative;

                            goto d_incwidth;

                        } else if (CRT_T('+') == ch) {
d_incwidth:
                            if (!--width && widthset)
                                ++done_flag;
                            else
                                ch = INC();
                        }

getnum:
#if _INTEGRAL_MAX_BITS >= 64
                        if ( integer64 ) {

                            while (!done_flag) {

                                if (CRT_T('x') == comchr)

                                    if (_ISXDIGIT(ch)) {
                                        num64 <<= 4;
                                        ch = HEXTODEC(ch);
                                    }
                                    else
                                        ++done_flag;

                                else if (_ISDIGIT(ch))

                                    if (CRT_T('o') == comchr)
                                        if (CRT_T('8') > ch)
                                                num64 <<= 3;
                                        else {
                                                ++done_flag;
                                        }
                                    else /* CRT_T('d') == comchr */
                                        num64 = MUL10(num64);

                                else
                                    ++done_flag;

                                if (!done_flag) {
                                    ++started;
                                    num64 += ch - CRT_T('0');

                                    if (widthset && !--width)
                                        ++done_flag;
                                    else
                                        ch = INC();
                                } else
                                    UN_INC(ch);

                            } /* end of WHILE loop */

                            if (negative)
                                num64 = (unsigned __int64 )(-(__int64)num64);
                        }
                        else {
#endif
                            while (!done_flag) {

                                if (CRT_T('x') == comchr || CRT_T('p') == comchr)

                                    if (_ISXDIGIT(ch)) {
                                        number = (number << 4);
                                        ch = HEXTODEC(ch);
                                    }
                                    else
                                        ++done_flag;

                                else if (_ISDIGIT(ch))

                                    if (CRT_T('o') == comchr)
                                        if (CRT_T('8') > ch)
                                            number = (number << 3);
                                        else {
                                            ++done_flag;
                                        }
                                    else /* CRT_T('d') == comchr */
                                        number = MUL10(number);

                                else
                                    ++done_flag;

                                if (!done_flag) {
                                    ++started;
                                    number += ch - CRT_T('0');

                                    if (widthset && !--width)
                                        ++done_flag;
                                    else
                                        ch = INC();
                                } else
                                    UN_INC(ch);

                            } /* end of WHILE loop */

                            if (negative)
                                number = (unsigned long)(-(long)number);
#if _INTEGRAL_MAX_BITS >= 64
                        }
#endif
                        if (CRT_T('F')==comchr) /* expected ':' in long pointer */

⌨️ 快捷键说明

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