|
|
1.1 ! root 1: /****************************************************************************** ! 2: * Copyright (c) 2004, 2008 IBM Corporation ! 3: * All rights reserved. ! 4: * This program and the accompanying materials ! 5: * are made available under the terms of the BSD License ! 6: * which accompanies this distribution, and is available at ! 7: * http://www.opensource.org/licenses/bsd-license.php ! 8: * ! 9: * Contributors: ! 10: * IBM Corporation - initial implementation ! 11: *****************************************************************************/ ! 12: ! 13: long int strtol(const char *S, char **PTR,int BASE) ! 14: { ! 15: long rval = 0; ! 16: short int negative = 0; ! 17: short int digit; ! 18: // *PTR is S, unless PTR is NULL, in which case i override it with my own ptr ! 19: char* ptr; ! 20: if (PTR == 0) ! 21: { ! 22: //override ! 23: PTR = &ptr; ! 24: } ! 25: // i use PTR to advance through the string ! 26: *PTR = (char *) S; ! 27: //check if BASE is ok ! 28: if ((BASE < 0) || BASE > 36) ! 29: { ! 30: return 0; ! 31: } ! 32: // ignore white space at beginning of S ! 33: while ((**PTR == ' ') ! 34: || (**PTR == '\t') ! 35: || (**PTR == '\n') ! 36: || (**PTR == '\r') ! 37: ) ! 38: { ! 39: (*PTR)++; ! 40: } ! 41: // check if S starts with "-" in which case the return value is negative ! 42: if (**PTR == '-') ! 43: { ! 44: negative = 1; ! 45: (*PTR)++; ! 46: } ! 47: // if BASE is 0... determine the base from the first chars... ! 48: if (BASE == 0) ! 49: { ! 50: // if S starts with "0x", BASE = 16, else 10 ! 51: if ((**PTR == '0') && (*((*PTR)+1) == 'x')) ! 52: { ! 53: BASE = 16; ! 54: (*PTR)++; ! 55: (*PTR)++; ! 56: } ! 57: else ! 58: { ! 59: BASE = 10; ! 60: } ! 61: } ! 62: if (BASE == 16) ! 63: { ! 64: // S may start with "0x" ! 65: if ((**PTR == '0') && (*((*PTR)+1) == 'x')) ! 66: { ! 67: (*PTR)++; ! 68: (*PTR)++; ! 69: } ! 70: } ! 71: //until end of string ! 72: while (**PTR) ! 73: { ! 74: if (((**PTR) >= '0') && ((**PTR) <= '9')) ! 75: { ! 76: //digit (0..9) ! 77: digit = **PTR - '0'; ! 78: } ! 79: else if (((**PTR) >= 'a') && ((**PTR) <='z')) ! 80: { ! 81: //alphanumeric digit lowercase(a (10) .. z (35) ) ! 82: digit = (**PTR - 'a') + 10; ! 83: } ! 84: else if (((**PTR) >= 'A') && ((**PTR) <='Z')) ! 85: { ! 86: //alphanumeric digit uppercase(a (10) .. z (35) ) ! 87: digit = (**PTR - 'A') + 10; ! 88: } ! 89: else ! 90: { ! 91: //end of parseable number reached... ! 92: break; ! 93: } ! 94: if (digit < BASE) ! 95: { ! 96: rval = (rval * BASE) + digit; ! 97: } ! 98: else ! 99: { ! 100: //digit found, but its too big for current base ! 101: //end of parseable number reached... ! 102: break; ! 103: } ! 104: //next... ! 105: (*PTR)++; ! 106: } ! 107: if (negative) ! 108: { ! 109: return rval * -1; ! 110: } ! 111: //else ! 112: return rval; ! 113: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.