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

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

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.