Annotation of driverkit/libDriver/atob.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
                      3:  *
                      4:  * @APPLE_LICENSE_HEADER_START@
                      5:  * 
                      6:  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
                      7:  * Reserved.  This file contains Original Code and/or Modifications of
                      8:  * Original Code as defined in and that are subject to the Apple Public
                      9:  * Source License Version 1.1 (the "License").  You may not use this file
                     10:  * except in compliance with the License.  Please obtain a copy of the
                     11:  * License at http://www.apple.com/publicsource and read it before using
                     12:  * this file.
                     13:  * 
                     14:  * The Original Code and all software distributed under the License are
                     15:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
                     16:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
                     17:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT.  Please see the
                     19:  * License for the specific language governing rights and limitations
                     20:  * under the License.
                     21:  * 
                     22:  * @APPLE_LICENSE_HEADER_END@
                     23:  */
                     24: /* Copyright (c) 1991 NeXT Computer, Inc.  All rights reserved.
                     25:  *
                     26:  *     File:   libsa/atob.c
                     27:  *
                     28:  *     Ascii to Binary converter.  Accepts all C numeric formats.
                     29:  *
                     30:  * HISTORY
                     31:  * 13-Sep-91  Mike DeMoney ([email protected]) and Peter King ([email protected])
                     32:  *     Created from libmike.
                     33:  */
                     34: 
                     35: 
                     36: /*
                     37:  * Header files.
                     38:  */
                     39: #import "ctype.h"
                     40: 
                     41: static unsigned int    digit(char c);
                     42: 
                     43: /*
                     44:  * atob -- convert ascii to binary.  Accepts all C numeric formats.
                     45:  *        Returns pointer to next character after last character used.
                     46:  */
                     47: char *
                     48: atob(
                     49:        const char              *cp,
                     50:        int                     *iptr
                     51: ) {
                     52:        int             minus = 0;
                     53:        int             value = 0;
                     54:        unsigned int    base = 10;
                     55:        unsigned int    d;
                     56: 
                     57:        *iptr = 0;
                     58:        if (!cp) {
                     59:                return(0);
                     60:        }
                     61:  
                     62:        while (isspace(*cp)) {
                     63:                cp++;
                     64:        }
                     65: 
                     66:        while (*cp == '-') {
                     67:                cp++;
                     68:                minus = !minus;
                     69:        }
                     70: 
                     71:        /*
                     72:         * Determine base by looking at first 2 characters
                     73:         */
                     74:        if (*cp == '0') {
                     75:                switch (*++cp) {
                     76:                case 'X':
                     77:                case 'x':
                     78:                        base = 16;
                     79:                        cp++;
                     80:                        break;
                     81: 
                     82:                case 'B':       /* a frill: allow binary base */
                     83:                case 'b':
                     84:                        base = 2;
                     85:                        cp++;
                     86:                        break;
                     87:                
                     88:                default:
                     89:                        base = 8;
                     90:                        break;
                     91:                }
                     92:        }
                     93: 
                     94:        while ((d = digit(*cp)) < base) {
                     95:                value *= base;
                     96:                value += d;
                     97:                cp++;
                     98:        }
                     99: 
                    100:        if (minus) {
                    101:                value = -value;
                    102:        }
                    103: 
                    104:        *iptr = value;
                    105:        return((char *)cp);
                    106: }
                    107: 
                    108: /*
                    109:  * digit -- convert the ascii representation of a digit to its
                    110:  * binary representation
                    111:  */
                    112: static unsigned int
                    113: digit(
                    114:        char            c
                    115: ) {
                    116:        unsigned int    d;
                    117: 
                    118:        if (isdigit(c)) {
                    119:                d = c - '0';
                    120:        } else if (isalpha(c)) {
                    121:                if (isupper(c)) {
                    122:                        c = tolower(c);
                    123:                }
                    124:                d = c - 'a' + 10;
                    125:        } else {
                    126:                d = 999999; /* larger than any base to break callers loop */
                    127:        }
                    128: 
                    129:        return d;
                    130: }
                    131: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.