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

📄 scanf.h

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 H
📖 第 1 页 / 共 2 页
字号:
                        nch = _GETC_(file);
         if (width>0) width--;
                        while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
                            dec /= 10;
                            cur += dec * (nch - '0');
                            nch = _GETC_(file);
             if (width>0) width--;
                        }
                    }
          /* handle exponent */
          if (width!=0 && (nch == 'e' || nch == 'E')) {
         int exponent = 0, negexp = 0;
         float expcnt;
                        nch = _GETC_(file);
         if (width>0) width--;
         /* possible sign on the exponent */
         if (width!=0 && (nch=='+' || nch=='-')) {
             negexp = (nch=='-');
                            nch = _GETC_(file);
             if (width>0) width--;
         }
         /* exponent digits */
         while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
             exponent *= 10;
             exponent += (nch - '0');
                            nch = _GETC_(file);
             if (width>0) width--;
                        }
         /* update 'cur' with this exponent. */
         expcnt =  negexp ? .1 : 10;
         while (exponent!=0) {
             if (exponent&1)
            cur*=expcnt;
             exponent/=2;
             expcnt=expcnt*expcnt;
         }
          }
                    st = 1;
                    if (!suppress) {
         if (L_prefix) _SET_NUMBER_(long double);
         else if (l_prefix) _SET_NUMBER_(double);
         else _SET_NUMBER_(float);
          }
                }
                break;
      /* According to
       * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
       * 's' reads a character string in a call to fscanf
       * and 'S' a wide character string and vice versa in a
       * call to fwscanf. The 'h', 'w' and 'l' prefixes override
       * this behaviour. 'h' forces reading char * but 'l' and 'w'
       * force reading WCHAR. */
       case 's':
          if (w_prefix || l_prefix) goto widecharstring;
          else if (h_prefix) goto charstring;
#ifdef WIDE_SCANF
          else goto widecharstring;
#else /* WIDE_SCANF */
          else goto charstring;
#endif /* WIDE_SCANF */
       case 'S':
          if (w_prefix || l_prefix) goto widecharstring;
          else if (h_prefix) goto charstring;
#ifdef WIDE_SCANF
          else goto charstring;
#else /* WIDE_SCANF */
          else goto widecharstring;
#endif /* WIDE_SCANF */
       charstring: { /* read a word into a char */
          char*str = suppress ? NULL : va_arg(ap, char*);
                    char*sptr = str;
                    /* skip initial whitespace */
                    while ((nch!=_EOF_) && _ISSPACE_(nch))
                        nch = _GETC_(file);
                    /* read until whitespace */
                    while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
                        if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
         st++;
                        nch = _GETC_(file);
         if (width>0) width--;
                    }
                    /* terminate */
                    if (!suppress) *sptr = 0;
                }
                break;
       widecharstring: { /* read a word into a wchar_t* */
		    wchar_t*str =
			suppress ? NULL : va_arg(ap, wchar_t*);
                    wchar_t*sptr = str;
                    /* skip initial whitespace */
                    while ((nch!=_EOF_) && _ISSPACE_(nch))
                        nch = _GETC_(file);
                    /* read until whitespace */
                    while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
                        if (!suppress) *sptr++ = _WIDE2SUPPORTED_(nch);
         st++;
                        nch = _GETC_(file);
         if (width>0) width--;
                    }
                    /* terminate */
                    if (!suppress) *sptr = 0;
                }
                break;
            /* 'c' and 'C work analogously to 's' and 'S' as described
        * above */
       case 'c':
          if (w_prefix || l_prefix) goto widecharacter;
          else if (h_prefix) goto character;
#ifdef WIDE_SCANF
          else goto widecharacter;
#else /* WIDE_SCANF */
          else goto character;
#endif /* WIDE_SCANF */
       case 'C':
          if (w_prefix || l_prefix) goto widecharacter;
          else if (h_prefix) goto character;
#ifdef WIDE_SCANF
          else goto character;
#else /* WIDE_SCANF */
          else goto widecharacter;
#endif /* WIDE_SCANF */
     character: { /* read single character into char */
                    if (nch!=_EOF_) {
                        if (!suppress) {
                            char*c = va_arg(ap, char*);
                            *c = _CHAR2SUPPORTED_(nch);
                        }
                        st = 1;
                        nch = _GETC_(file);
                    }
                }
      break;
     widecharacter: { /* read single character into a wchar_t */
                    if (nch!=_EOF_) {
                        if (!suppress) {
                            wchar_t*c = va_arg(ap, wchar_t*);
                            *c = _WIDE2SUPPORTED_(nch);
                        }
                        nch = _GETC_(file);
                        st = 1;
                    }
           }
      break;
       case 'n': {
          if (!suppress) {
         int*n = va_arg(ap, int*);

         /* 
         *n = consumed - (nch!=_EOF_);
         
         FIXME: The above is the Wine version and it doesnt work in ros
         when %n is at end of input string (return one too many).
         But does it fail in Wine too?? If so wine also needs fixin.
         -Gunnar
         */

         *n = consumed - 1;
          }
          /* This is an odd one: according to the standard,
           * "Execution of a %n directive does not increment the
           * assignment count returned at the completion of
           * execution" even if it wasn't suppressed with the
           * '*' flag.  The Corrigendum to the standard seems
           * to contradict this (comment out the assignment to
           * suppress below if you want to implement these
           * alternate semantics) but the windows program I'm
           * looking at expects the behavior I've coded here
           * (which happens to be what glibc does as well).
           */
          suppress = 1;
          st = 1;
           }
      break;
       case '[': {
                    _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
                    _CHAR_ *sptr = str;
          RTL_BITMAP bitMask;
                    ULONG *Mask;
          int invert = 0; /* Set if we are NOT to find the chars */

          /* Init our bitmap */
          Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
          RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);

          /* Read the format */
          format++;
          if(*format == '^') {
         invert = 1;
         format++;
          }
          if(*format == ']') {
         RtlSetBits(&bitMask, ']', 1);
         format++;
          }
                    while(*format && (*format != ']')) {
         /* According to:
          * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_scanf_width_specification.asp
          * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
         if((*format == '-') && (*(format + 1) != ']')) {
             if ((*(format - 1)) < *(format + 1))
            RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
             else
            RtlSetBits(&bitMask, *(format + 1)    , *(format - 1) - *(format + 1));             
             format++;
         } else
             RtlSetBits(&bitMask, *format, 1);
         format++;
          }
                    /* read until char is not suitable */
                    while ((width != 0) && (nch != _EOF_)) {
         if(!invert) {
             if(RtlAreBitsSet(&bitMask, nch, 1)) {
            if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
             } else
            break;
         } else {
             if(RtlAreBitsClear(&bitMask, nch, 1)) {
            if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
             } else
            break;
         }
                        st++;
                        nch = _GETC_(file);
                        if (width>0) width--;
                    }
                    /* terminate */
                    if (!suppress) *sptr = 0;
          HeapFree(GetProcessHeap(), 0, Mask);
                }
                break;
            default:
      /* From spec: "if a percent sign is followed by a character
       * that has no meaning as a format-control character, that
       * character and the following characters are treated as
       * an ordinary sequence of characters, that is, a sequence
       * of characters that must match the input.  For example,
       * to specify that a percent-sign character is to be input,
       * use %%." */
                while ((nch!=_EOF_) && _ISSPACE_(nch))
                    nch = _GETC_(file);
                if (nch==*format) {
                    suppress = 1; /* whoops no field to be read */
                    st = 1; /* but we got what we expected */
                    nch = _GETC_(file);
                }
                break;
            }
            if (st && !suppress) rd++;
            else if (!st) break;
        }
   /* a non-white-space character causes scanf to read, but not store,
    * a matching non-white-space character. */
        else {
            /* check for character match */
            if (nch == *format) {
      nch = _GETC_(file);
            } else break;
        }
        format++;
    }
    if (nch!=_EOF_) {
   _UNGETC_(nch, file);
    }
    TRACE("returning %d\n", rd);
    return rd;
}

#undef _CHAR_
#undef _EOF_
#undef _EOF_RET
#undef _ISSPACE_
#undef _ISDIGIT_
#undef _CHAR2SUPPORTED_
#undef _WIDE2SUPPORTED_
#undef _CHAR2DIGIT_
#undef _GETC_
#undef _UNGETC_
#undef _FUNCTION_
#undef _BITMAPSIZE_

⌨️ 快捷键说明

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