Annotation of researchv10no/cmd/mk/export.proto/libc/getfields.c, revision 1.1

1.1     ! root        1: #include       <libc.h>
        !             2: 
        !             3: static char is_sep[256] = {
        !             4:        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        !             5:        1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        !             6:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        !             7:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        !             8:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        !             9:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        !            10:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        !            11:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        !            12: };
        !            13: static char is_field[256] = {
        !            14:        0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        !            15:        0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        !            16:        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        !            17:        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        !            18:        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        !            19:        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        !            20:        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        !            21:        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        !            22: };
        !            23: static char last_sep[256];
        !            24: 
        !            25: char *
        !            26: setfields(arg)
        !            27:        char *arg;
        !            28: {
        !            29:        register unsigned char *s;
        !            30:        register i;
        !            31: 
        !            32:        for(i = 1, s = (unsigned char *)last_sep; i < 256; i++)
        !            33:                if(is_sep[i])
        !            34:                        *s++ = i;
        !            35:        *s = 0;
        !            36:        memset(is_sep, 0, sizeof is_sep);
        !            37:        memset(is_field, 1, sizeof is_field);
        !            38:        for(s = (unsigned char *)arg; *s;){
        !            39:                is_sep[*s] = 1;
        !            40:                is_field[*s++] = 0;
        !            41:        }
        !            42:        is_field[0] = 0;
        !            43:        return(last_sep);
        !            44: }
        !            45: 
        !            46: getfields(ss, sp, nptrs)
        !            47:        char *ss;
        !            48:        char **sp;
        !            49: {
        !            50:        register unsigned char *s = (unsigned char *)ss;
        !            51:        register unsigned char **p = (unsigned char **)sp;
        !            52:        register unsigned c;
        !            53: 
        !            54:        for(;;){
        !            55:                if(--nptrs < 0) break;
        !            56:                *p++ = s;
        !            57:                while(is_field[c = *s++])
        !            58:                        ;
        !            59:                if(c == 0) break;
        !            60:                s[-1] = 0;
        !            61:        }
        !            62:        if(nptrs > 0)
        !            63:                *p = 0;
        !            64:        else if(--s >= (unsigned char *)ss)
        !            65:                *s = c;
        !            66:        return(p - (unsigned char **)sp);
        !            67: }
        !            68: 
        !            69: getmfields(ss, sp, nptrs)
        !            70:        char *ss;
        !            71:        char **sp;
        !            72: {
        !            73:        register unsigned char *s = (unsigned char *)ss;
        !            74:        register unsigned char **p = (unsigned char **)sp;
        !            75:        register unsigned c;
        !            76:        register unsigned d;
        !            77: 
        !            78:        if(nptrs <= 0)
        !            79:                return(0);
        !            80:        goto flushdelim;
        !            81:        for(;;){
        !            82:                *p++ = s;
        !            83:                if(--nptrs == 0) break;
        !            84:                while(is_field[c = *s++])
        !            85:                        ;
        !            86:                /*
        !            87:                 *  s is now pointing 1 past the delimiter of the last field
        !            88:                 *  c is the delimiter
        !            89:                 */
        !            90:                if(c == 0) break;
        !            91:                s[-1] = 0;
        !            92:        flushdelim:
        !            93:                while(is_sep[c = *s++])
        !            94:                        ;
        !            95:                /*
        !            96:                 *  s is now pointing 1 past the beginning of the next field
        !            97:                 *  c is the first letter of the field
        !            98:                 */
        !            99:                if(c == 0) break;
        !           100:                s--;
        !           101:                /*
        !           102:                 *  s is now pointing to the beginning of the next field
        !           103:                 *  c is the first letter of the field
        !           104:                 */
        !           105:        }
        !           106:        if(nptrs > 0)
        !           107:                *p = 0;
        !           108:        return(p - (unsigned char **)sp);
        !           109: }
        !           110: 
        !           111: #ifdef MAIN
        !           112: #include       <fio.h>
        !           113: 
        !           114: main()
        !           115: {
        !           116:        char *fields[256];
        !           117:        char *s;
        !           118:        int n, i;
        !           119:        char buf[1024];
        !           120: 
        !           121:        print("go:\n");
        !           122:        while(s = Frdline(0)){
        !           123:                strcpy(buf, s);
        !           124:                Fprint(1, "getf:");
        !           125:                n = getfields(s, fields, 4);
        !           126:                for(i = 0; i < n; i++)
        !           127:                        Fprint(1, " >%s<", fields[i]);
        !           128:                Fputc(1, '\n');
        !           129:                Fprint(1, "getmf:");
        !           130:                n = getmfields(buf, fields, 4);
        !           131:                for(i = 0; i < n; i++)
        !           132:                        Fprint(1, " >%s<", fields[i]);
        !           133:                Fputc(1, '\n');
        !           134:                Fflush(1);
        !           135:        }
        !           136:        exit(0);
        !           137: }
        !           138: #endif

unix.superglobalmegacorp.com

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