Annotation of kernel/driverkit/KernStringList.m, 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: 
                     25: /*
                     26:  * A List object for C-strings.
                     27:  *
                     28:  * Its sole purpose is to make whitespace-separated lists of words
                     29:  * easier to deal with, since we use them a lot in driverkit string tables.
                     30:  *
                     31:  *
                     32:  */
                     33: 
                     34: #import "KernStringList.h"
                     35: 
                     36: #define isspace(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n'))
                     37: 
                     38: static void *my_malloc(unsigned len)
                     39: {
                     40: #if KERNEL
                     41:     extern void *IOMalloc(int);
                     42:     return IOMalloc(len);
                     43: #else
                     44:     extern void *malloc(unsigned);
                     45:     void *ptr = malloc(len);
                     46:     char *p = (char *)ptr;
                     47:     
                     48:     while (len--)
                     49:        *p++ = 'X';
                     50:     return ptr;
                     51: #endif
                     52: }
                     53: 
                     54: static void my_free(void *ptr, unsigned len)
                     55: {
                     56: #if KERNEL
                     57:     extern void IOFree(void *, int);
                     58:     IOFree(ptr, len);
                     59: #else
                     60:     extern void free(void *);
                     61:     free(ptr);
                     62: #endif
                     63: }
                     64: 
                     65: @implementation KernStringList
                     66: 
                     67: - init
                     68: {
                     69:     return [self initWithWhitespaceDelimitedString:NULL];
                     70: }
                     71: 
                     72: 
                     73: - initWithWhitespaceDelimitedString:(const char *)str
                     74: {
                     75:     char *sp;
                     76:     int index;
                     77:     unsigned len;
                     78:     extern int strncpy(char *,const char *, int);
                     79:     
                     80:     [super init];
                     81: 
                     82:     while (*str && isspace(*str))
                     83:        str++;
                     84: 
                     85:     sp = (char *)str;
                     86:     while (*sp) {
                     87:        while (*sp && isspace(*sp))
                     88:            sp++;
                     89:        if (*sp)
                     90:            count++;
                     91:        while (*sp && !isspace(*sp))
                     92:            sp++;
                     93:     }
                     94:     
                     95:     strings = (char **)my_malloc(sizeof(char *) * count);
                     96: 
                     97:     for (index = 0; str && *str; index++) {
                     98:        char *s;
                     99:        
                    100:        for (sp = (char *)str; *sp && !isspace(*sp); sp++)
                    101:            continue;
                    102:        len = sp - str + 1;
                    103:        s = strings[index] = (char *)my_malloc(len);
                    104:        strncpy(s, str, len - 1);
                    105:        s[len - 1] = '\0';
                    106:        for (str = sp; *str && isspace(*str); str++)
                    107:            continue;
                    108:     }
                    109:     return self;
                    110: }
                    111: 
                    112: - free
                    113: {
                    114:     int i;
                    115:     
                    116:     for (i=0; i<count; i++) {
                    117:        my_free((char *)strings[i], strlen(strings[i])+1);
                    118:     }
                    119:     my_free(strings, sizeof(char *) * count);
                    120:     return [super free];
                    121: }
                    122: 
                    123: - (unsigned)count
                    124: {
                    125:     return count;
                    126: }
                    127: 
                    128: - (const char *)stringAt:(unsigned)index
                    129: {
                    130:     if (index >= 0 && index < count)
                    131:        return strings[index];
                    132:     return NULL;
                    133: }
                    134: 
                    135: - (const char *)lastString
                    136: {
                    137:     return strings[count - 1];
                    138: }
                    139: 
                    140: 
                    141: @end

unix.superglobalmegacorp.com

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