Annotation of parser3/src/classes/table.C, revision 1.35

1.20      paf         1: /** @file
                      2:        Parser: table parser class.
                      3: 
1.1       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.20      paf         5: 
1.1       paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: 
1.35    ! paf         8:        $Id: table.C,v 1.34 2001/03/27 16:35:52 paf Exp $
1.1       paf         9: */
                     10: 
1.24      paf        11: #include "pa_config_includes.h"
1.16      paf        12: #include "pa_common.h"
1.1       paf        13: #include "pa_request.h"
                     14: #include "_table.h"
                     15: #include "pa_vtable.h"
1.6       paf        16: #include "pa_vint.h"
1.1       paf        17: 
                     18: // global var
                     19: 
1.14      paf        20: VStateless_class *table_class;
1.1       paf        21: 
                     22: // methods
                     23: 
1.2       paf        24: static void set_or_load(
                     25:                                                Request& r, 
                     26:                                                const String& method_name, Array *params, 
                     27:                                                bool is_load) {
1.1       paf        28:        Pool& pool=r.pool();
                     29:        // data is last parameter
1.21      paf        30:        Value *vdata_or_filename=static_cast<Value *>(params->get(params->size()-1));
1.4       paf        31:        // forcing
1.21      paf        32:        // ^load[this file name type]
1.17      paf        33:        // ^set{this body type}
1.21      paf        34:        r.fail_if_junction_(is_load, *vdata_or_filename, 
                     35:                method_name, is_load?"file name must not be junction":"body must be junction");
1.1       paf        36: 
1.2       paf        37:        // data or file_name
1.25      paf        38:        char *data;
1.21      paf        39:        if(is_load) {
                     40:                // forcing untaint language
                     41:                String lfile_name(pool);
1.25      paf        42:                lfile_name.append(vdata_or_filename->as_string(), String::UL_FILE_NAME, true);
                     43:                // loading text
                     44:                data=file_read_text(pool, r.absolute(lfile_name));
1.21      paf        45:        } else {
                     46:                // suggesting untaint language
1.23      paf        47:                Temp_lang temp_lang(r, String::UL_TABLE);
1.25      paf        48:                data=r.process(*vdata_or_filename).as_string().cstr();
1.17      paf        49:        }
1.1       paf        50: 
                     51:        // parse columns
                     52:        Array *columns;
                     53: #ifndef NO_STRING_ORIGIN
                     54:        const Origin& origin=method_name.origin();
1.2       paf        55:        const char *file=origin.file;
1.1       paf        56:        uint line=origin.line;
                     57: #endif
                     58:        if(params->size()==2) {
                     59:                columns=0;
                     60:        } else {
                     61:                columns=new(pool) Array(pool);
                     62: 
                     63:                if(char *row_chars=getrow(&data)) 
                     64:                        do {
                     65:                                String *name=new(pool) String(pool);
                     66:                                name->APPEND(lsplit(&row_chars, '\t'), 0, file, line++);
                     67:                                *columns+=name;
                     68:                        } while(row_chars);
                     69:        }
                     70: 
                     71:        // parse cells
1.27      paf        72:        Table& table=*new(pool) Table(pool, &method_name, columns);
1.1       paf        73:        char *row_chars;
                     74:        while(row_chars=getrow(&data)) {
1.28      paf        75:                if(!*row_chars) // remove empty lines
                     76:                        continue;
1.1       paf        77:                Array *row=new(pool) Array(pool);
                     78:                while(char *cell_chars=lsplit(&row_chars, '\t')) {
                     79:                        String *cell=new(pool) String(pool);
                     80:                        cell->APPEND(cell_chars, 0, file, line);
                     81:                        *row+=cell;
                     82:                }
                     83:                line++;
                     84:                table+=row;
                     85:        };
                     86: 
                     87:        // replace any previous table value
1.18      paf        88:        static_cast<VTable *>(r.self)->set_table(table);
1.1       paf        89: }
                     90: 
1.2       paf        91: 
                     92: static void _set(Request& r, const String& method_name, Array *params) {
                     93:        set_or_load(r, method_name, params, false);
                     94: }
                     95: 
                     96: static void _load(Request& r, const String& method_name, Array *params) {
                     97:        set_or_load(r, method_name, params, true);
                     98: }
                     99: 
1.22      paf       100: static void _save(Request& r, const String& method_name, Array *params) {
                    101:        Pool& pool=r.pool();
                    102:        Value *vfile_name=static_cast<Value *>(params->get(params->size()-1));
                    103:        // forcing
                    104:        // ^save[this body type]
                    105:        r.fail_if_junction_(true, *vfile_name, 
                    106:                method_name, "file name must not be junction");
                    107: 
                    108:        // forcing untaint language
                    109:        String lfile_name(pool);
                    110:        lfile_name.append(vfile_name->as_string(),
1.23      paf       111:                String::UL_FILE_NAME, true);
1.22      paf       112: 
1.31      paf       113:        Table& table=static_cast<VTable *>(r.self)->table();
                    114: 
                    115:        String sdata(pool);
                    116:        if(params->size()==1) { // not nameless=named output
                    117:                // write out names line
                    118:                if(table.columns()) { // named table
                    119:                        for(int column=0; column<table.columns()->size(); column++) {
                    120:                                if(column)
                    121:                                        sdata.APPEND_CONST("\t");
                    122:                                sdata.append(*static_cast<String *>(table.columns()->quick_get(column)), 
                    123:                                        String::UL_TABLE);
                    124:                        }
                    125:                } else { // nameless table
                    126:                        int lsize=table.size()?static_cast<Array *>(table.get(0))->size():0;
                    127:                        if(lsize)
                    128:                                for(int column=0; column<lsize; column++) {
                    129:                                        char *cindex_tab=(char *)malloc(MAX_NUMBER);
                    130:                                        snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
                    131:                                        sdata.APPEND_CONST(cindex_tab);
                    132:                                }
                    133:                        else
                    134:                                sdata.APPEND_CONST("empty nameless table");
                    135:                }
                    136:                sdata.APPEND_CONST("\n");
                    137:        }
                    138:        // data lines
                    139:        for(int index=0; index<table.size(); index++) {
                    140:                Array *row=static_cast<Array *>(table.quick_get(index));
                    141:                for(int column=0; column<row->size(); column++) {
                    142:                        if(column)
                    143:                                sdata.APPEND_CONST("\t");
                    144:                        sdata.append(*static_cast<String *>(row->quick_get(column)), 
                    145:                                String::UL_TABLE);
                    146:                }
                    147:                sdata.APPEND_CONST("\n");
                    148:        }
                    149: 
                    150:        // write
                    151:        file_write(pool, r.absolute(lfile_name), sdata.cstr(), sdata.size(), true);
1.22      paf       152: }
                    153: 
1.6       paf       154: static void _count(Request& r, const String&, Array *) {
                    155:        Pool& pool=r.pool();
1.18      paf       156:        Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.15      paf       157:        r.write_no_lang(value);
1.6       paf       158: }
                    159: 
                    160: static void _line(Request& r, const String&, Array *) {
                    161:        Pool& pool=r.pool();
1.18      paf       162:        Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().get_current());
1.15      paf       163:        r.write_no_lang(value);
1.6       paf       164: }
                    165: 
                    166: static void _offset(Request& r, const String&, Array *params) {
                    167:        Pool& pool=r.pool();
1.18      paf       168:        Table& table=static_cast<VTable *>(r.self)->table();
1.6       paf       169:        if(params->size()) {
                    170:                if(int size=table.size()) {
1.9       paf       171:                        int offset=
1.34      paf       172:                                (int)r.process(*static_cast<Value *>(params->get(0))).as_double();
1.6       paf       173:                        table.set_current((table.get_current()+offset+size)%size);
                    174:                }
                    175:        } else {
                    176:                Value& value=*new(pool) VInt(pool, table.get_current());
1.15      paf       177:                r.write_no_lang(value);
1.6       paf       178:        }
                    179: }
                    180: 
1.7       paf       181: static void _menu(Request& r, const String& method_name, Array *params) {
                    182:        Value& body_code=*static_cast<Value *>(params->get(0));
                    183:        // forcing ^menu{this param type}
                    184:        r.fail_if_junction_(false, body_code, 
                    185:                method_name, "body must be junction");
                    186:        
                    187:        Value *delim_code=params->size()==2?static_cast<Value *>(params->get(1)):0;
                    188: 
1.18      paf       189:        Table& table=static_cast<VTable *>(r.self)->table();
1.7       paf       190:        bool need_delim=false;
1.22      paf       191:        for(int row=0; row<table.size(); row++) {
                    192:                table.set_current(row);
1.7       paf       193: 
1.12      paf       194:                Value& processed_body=r.process(body_code);
1.7       paf       195:                if(delim_code) { // delimiter set?
                    196:                        const String *string=processed_body.get_string();
                    197:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    198:                                r.write_pass_lang(r.process(*delim_code));
                    199:                        need_delim=true;
                    200:                }
                    201:                r.write_pass_lang(processed_body);
                    202:        }
                    203: }
                    204: 
1.8       paf       205: static void _empty(Request& r, const String&, Array *params) {
1.18      paf       206:        Table& table=static_cast<VTable *>(r.self)->table();
1.8       paf       207:        if(table.size()==0) {
                    208:                Value& value=r.process(*static_cast<Value *>(params->get(0)));
                    209:                r.write_pass_lang(value);
                    210:        } else if(params->size()==2) {
                    211:                Value& value=r.process(*static_cast<Value *>(params->get(1)));
                    212:                r.write_pass_lang(value);
                    213:        }
                    214: }
1.15      paf       215: 
1.29      paf       216: struct Record_info {
                    217:        Pool *pool;
                    218:        Table *table;
                    219:        Hash *hash;
                    220: };
                    221: static void store_column_item_to_hash(Array::Item *item, void *info) {
                    222:        Record_info& ri=*static_cast<Record_info *>(info);
                    223:        String& column_name=*static_cast<String *>(item);
                    224:        const String *column_item=ri.table->item(column_name);
                    225:        Value *value;
                    226:        if(column_item)
                    227:                value=new(*ri.pool) VString(*column_item);
                    228:        else
                    229:                value=new(*ri.pool) VUnknown(*ri.pool);
                    230:        ri.hash->put(column_name, value);
                    231: }
                    232: static void _record(Request& r, const String&, Array *params) {
                    233:        Table& table=static_cast<VTable *>(r.self)->table();
                    234:        if(const Array *columns=table.columns()) {
                    235:                Pool& pool=r.pool();
                    236:                Value& value=*new(pool) VHash(pool);
                    237:                Record_info record_info={&pool, &table, value.get_hash()};
                    238:                columns->for_each(store_column_item_to_hash, &record_info);
                    239:                
                    240:                r.write_no_lang(value);
                    241:        }
                    242: }
                    243: 
1.34      paf       244: struct Seq_item {
                    245:        Array *row;
                    246:        union {
                    247:                char *c_str;
                    248:                double d;
                    249:        } value;
1.32      paf       250: };
1.34      paf       251: static int sort_cmp_string(const void *a, const void *b) {
                    252:        return strcmp(
                    253:                static_cast<const Seq_item *>(a)->value.c_str, 
                    254:                static_cast<const Seq_item *>(b)->value.c_str
                    255:        );
                    256: }
                    257: static int sort_cmp_double(const void *a, const void *b) {
                    258:        double va=static_cast<const Seq_item *>(a)->value.d;
                    259:        double vb=static_cast<const Seq_item *>(b)->value.d;
                    260:        if(va<vb)
                    261:                return -1;
                    262:        else if(va>vb)
                    263:                return +1;
                    264:        else 
                    265:                return 0;
                    266: }
1.32      paf       267: static void _sort(Request& r, const String& method_name, Array *params) {
                    268:        Value& key_maker=*(Value *)params->get(0);
                    269:        // forcing ^sort{this} ^sort(or this) param type
                    270:        r.fail_if_junction_(false, key_maker, method_name, "key-maker must be junction");
                    271: 
                    272:        bool reverse;
                    273:        if(params->size()==2) { // ..[asc|desc]
1.35    ! paf       274:                Value& order=*(Value *)params->get(1);
1.32      paf       275:                // forcing ..[this param-type]
1.35    ! paf       276:                r.fail_if_junction_(true, order, method_name, "order must not be junction");
        !           277:                reverse=order.as_string()=="desc";
1.32      paf       278:        } else
                    279:                reverse=false;
                    280: 
                    281:        Table& table=static_cast<VTable *>(r.self)->table();
1.34      paf       282: 
                    283:        // anything to sort?
                    284:        if(!table.size())
                    285:                return;
                    286: 
                    287:        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*table.size());
                    288:        int i;
                    289: 
                    290:        // calculate key values
                    291:        bool key_values_are_strings=true;
                    292:        for(i=0; i<table.size(); i++) {
1.32      paf       293:                table.set_current(i);
                    294:                // calculate key value
1.34      paf       295:                seq[i].row=(Array *)table.get(i);
                    296:                Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
                    297:                if(i==0) // determining key values type by first one
                    298:                        key_values_are_strings=value.is_string();
                    299: 
                    300:                if(key_values_are_strings)
                    301:                        seq[i].value.c_str=value.as_string().cstr();
                    302:                else
                    303:                        seq[i].value.d=value.as_double();
1.32      paf       304:        }
                    305:        // sort keys
1.34      paf       306:        _qsort(seq, table.size(), sizeof(Seq_item), 
                    307:                key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf       308: 
1.34      paf       309:        // reorder table as they require in 'seq'
                    310:        for(i=0; i<table.size(); i++)
                    311:                table.put(i, seq[reverse?table.size()-1-i:i].row);
1.32      paf       312: 
1.34      paf       313:        // reset 'current'
1.32      paf       314:        table.set_current(0);
                    315: }
                    316: 
1.15      paf       317: // initialize
1.8       paf       318: 
1.14      paf       319: void initialize_table_class(Pool& pool, VStateless_class& vclass) {
1.22      paf       320:        // ^table.set{data}
                    321:        // ^table.set[nameless]{data}
1.1       paf       322:        vclass.add_native_method("set", _set, 1, 2);
1.2       paf       323: 
1.10      paf       324:        // ^table.load[file]  
                    325:        // ^table.load[nameless;file]
1.2       paf       326:        vclass.add_native_method("load", _load, 1, 2);
1.22      paf       327: 
                    328:        // ^table.save[file]  
                    329:        // ^table.save[nameless;file]
                    330:        vclass.add_native_method("save", _save, 1, 2);
1.6       paf       331: 
                    332:        // ^table.count[]
                    333:        vclass.add_native_method("count", _count, 0, 0);
                    334: 
                    335:        // ^table.line[]
                    336:        vclass.add_native_method("line", _line, 0, 0);
                    337: 
1.10      paf       338:        // ^table.offset[]  
                    339:        // ^table.offset[offset]
1.6       paf       340:        vclass.add_native_method("offset", _offset, 0, 1);
1.7       paf       341: 
1.10      paf       342:        // ^table.menu{code}  
                    343:        // ^table.menu{code}[delim]
1.7       paf       344:        vclass.add_native_method("menu", _menu, 1, 2);
1.8       paf       345: 
1.10      paf       346:        // ^table.empty{code-when-empty}  
                    347:        // ^table.empty{code-when-empty}{code-when-not}
1.8       paf       348:        vclass.add_native_method("empty", _empty, 1, 2);
1.29      paf       349: 
                    350:        // ^table.record[]
                    351:        vclass.add_native_method("record", _record, 0, 0);
1.32      paf       352: 
                    353:        // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc]
                    354:        // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc]
                    355:        vclass.add_native_method("sort", _sort, 1, 2);
1.8       paf       356: 
1.1       paf       357: }      

E-mail: