imapext-2007
diff src/ansilib/strtoul.c @ 0:ada5e610ab86
imap-2007e
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 15:17:45 +0900 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/ansilib/strtoul.c Mon Sep 14 15:17:45 2009 +0900 1.3 @@ -0,0 +1,73 @@ 1.4 +/* ======================================================================== 1.5 + * Copyright 1988-2006 University of Washington 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * 1.14 + * ======================================================================== 1.15 + */ 1.16 + 1.17 +/* 1.18 + * Program: String to unsigned long 1.19 + * 1.20 + * Author: Mark Crispin 1.21 + * Networks and Distributed Computing 1.22 + * Computing & Communications 1.23 + * University of Washington 1.24 + * Administration Building, AG-44 1.25 + * Seattle, WA 98195 1.26 + * 1.27 + * Date: 14 February 1995 1.28 + * Last Edited: 30 August 2006 1.29 + */ 1.30 + 1.31 +/* 1.32 + * Turn a string unsigned long into the real thing 1.33 + * Accepts: source string 1.34 + * pointer to place to return end pointer 1.35 + * base 1.36 + * Returns: parsed unsigned long integer, end pointer is updated 1.37 + */ 1.38 + 1.39 +unsigned long strtoul (char *t,char **endp,int base) 1.40 +{ 1.41 + unsigned long value = 0; /* the accumulated value */ 1.42 + int negative = 0; /* this a negative number? */ 1.43 + unsigned char c,*s = t; 1.44 + if (base && (base < 2 || base > 36)) { 1.45 + errno = EINVAL; /* insist upon valid base */ 1.46 + return value; 1.47 + } 1.48 + while (isspace (*s)) s++; /* skip leading whitespace */ 1.49 + switch (*s) { /* check for leading sign char */ 1.50 + case '-': 1.51 + negative = 1; /* yes, negative #. fall into '+' */ 1.52 + case '+': 1.53 + s++; /* skip the sign character */ 1.54 + } 1.55 + if (!base) { /* base not specified? */ 1.56 + if (*s != '0') base = 10; /* must be decimal if doesn't start with 0 */ 1.57 + /* starts with 0x? */ 1.58 + else if (tolower (*++s) == 'x') { 1.59 + base = 16; /* yes, is hex */ 1.60 + s++; /* skip the x */ 1.61 + } 1.62 + else base = 8; /* ...or octal */ 1.63 + } 1.64 + do { /* convert to numeric form if digit */ 1.65 + if (isdigit (*s)) c = *s - '0'; 1.66 + /* alphabetic conversion */ 1.67 + else if (isalpha (*s)) c = *s - (isupper (*s) ? 'A' : 'a') + 10; 1.68 + else break; /* else no way it's valid */ 1.69 + if (c >= base) break; /* digit out of range for base? */ 1.70 + value = value * base + c; /* accumulate the digit */ 1.71 + } while (*++s); /* loop until non-numeric character */ 1.72 + if (tolower (*s) == 'l') s++; /* ignore 'l' or 'L' marker */ 1.73 + if (endp) *endp = s; /* save users endp to after number */ 1.74 + /* negate number if needed */ 1.75 + return negative ? -value : value; 1.76 +}