ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

wcstol.c
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:   See COPYING in the top level directory
00003  * PROJECT:     ReactOS system libraries
00004  * FILE:        lib/crt/??????
00005  * PURPOSE:     Unknown
00006  * PROGRAMER:   Unknown
00007  * UPDATE HISTORY:
00008  *              25/11/05: Added license header
00009  */
00010 
00011 #include <precomp.h>
00012 /*
00013  * @implemented
00014  */
00015 long
00016 wcstol(const wchar_t *nptr, wchar_t **endptr, int base)
00017 {
00018   const wchar_t *s = nptr;
00019   long acc;
00020   int c;
00021   unsigned long cutoff;
00022   int neg = 0, any, cutlim;
00023 
00024   /*
00025    * Skip white space and pick up leading +/- sign if any.
00026    * If base is 0, allow 0x for hex and 0 for octal, else
00027    * assume decimal; if base is already 16, allow 0x.
00028    */
00029   do {
00030     c = *s++;
00031   } while (iswctype(c, _SPACE));
00032   if (c == '-')
00033   {
00034     neg = 1;
00035     c = *s++;
00036   }
00037   else if (c == L'+')
00038     c = *s++;
00039   if ((base == 0 || base == 16) &&
00040       c == L'0' && (*s == L'x' || *s == L'X'))
00041   {
00042     c = s[1];
00043     s += 2;
00044     base = 16;
00045   }
00046   if (base == 0)
00047     base = c == L'0' ? 8 : 10;
00048 
00049   /*
00050    * Compute the cutoff value between legal numbers and illegal
00051    * numbers.  That is the largest legal value, divided by the
00052    * base.  An input number that is greater than this value, if
00053    * followed by a legal input character, is too big.  One that
00054    * is equal to this value may be valid or not; the limit
00055    * between valid and invalid numbers is then based on the last
00056    * digit.  For instance, if the range for longs is
00057    * [-2147483648..2147483647] and the input base is 10,
00058    * cutoff will be set to 214748364 and cutlim to either
00059    * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
00060    * a value > 214748364, or equal but the next digit is > 7 (or 8),
00061    * the number is too big, and we will return a range error.
00062    *
00063    * Set any if any `digits' consumed; make it negative to indicate
00064    * overflow.
00065    */
00066   cutoff = neg ? ((unsigned long)LONG_MAX+1) : LONG_MAX;
00067   cutlim = cutoff % (unsigned long)base;
00068   cutoff /= (unsigned long)base;
00069   for (acc = 0, any = 0;; c = *s++)
00070   {
00071     if (iswctype(c, _DIGIT))
00072       c -= L'0';
00073     else if (iswctype(c, _ALPHA))
00074       c -= iswctype(c, _UPPER) ? L'A' - 10 : L'a' - 10;
00075     else
00076       break;
00077     if (c >= base)
00078       break;
00079     if (any < 0 || (unsigned long)acc > cutoff || (acc == cutoff && c > cutlim))
00080       any = -1;
00081     else
00082     {
00083       any = 1;
00084       acc *= base;
00085       acc += c;
00086     }
00087   }
00088   if (any < 0)
00089   {
00090     acc = neg ? LONG_MIN : LONG_MAX;
00091   }
00092   else if (neg)
00093     acc = 0-acc;
00094   if (endptr != 0)
00095     *endptr = any ? (wchar_t *)((size_t)(s - 1)) : (wchar_t *)((size_t)nptr);
00096   return acc;
00097 }

Generated on Sat May 26 2012 04:35:36 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.