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

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.39    ! paf         8:        $Id: table.C,v 1.38 2001/03/29 15:00:19 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.39    ! paf        28:        if(r.self==table_class)
        !            29:                RTHROW(0, 0,
        !            30:                        &method_name,
        !            31:                        "method of 'table' is not static");
        !            32: 
1.1       paf        33:        Pool& pool=r.pool();
                     34:        // data is last parameter
1.21      paf        35:        Value *vdata_or_filename=static_cast<Value *>(params->get(params->size()-1));
1.4       paf        36:        // forcing
1.21      paf        37:        // ^load[this file name type]
1.17      paf        38:        // ^set{this body type}
1.21      paf        39:        r.fail_if_junction_(is_load, *vdata_or_filename, 
                     40:                method_name, is_load?"file name must not be junction":"body must be junction");
1.1       paf        41: 
1.39    ! paf        42:        // data or table_name
1.25      paf        43:        char *data;
1.21      paf        44:        if(is_load) {
                     45:                // forcing untaint language
1.39    ! paf        46:                String ltable_name(pool);
        !            47:                ltable_name.append(vdata_or_filename->as_string(), String::UL_FILE_NAME, true);
1.25      paf        48:                // loading text
1.39    ! paf        49:                data=file_read_text(pool, r.absolute(ltable_name));
1.21      paf        50:        } else {
                     51:                // suggesting untaint language
1.23      paf        52:                Temp_lang temp_lang(r, String::UL_TABLE);
1.25      paf        53:                data=r.process(*vdata_or_filename).as_string().cstr();
1.17      paf        54:        }
1.1       paf        55: 
                     56:        // parse columns
                     57:        Array *columns;
                     58: #ifndef NO_STRING_ORIGIN
                     59:        const Origin& origin=method_name.origin();
1.2       paf        60:        const char *file=origin.file;
1.1       paf        61:        uint line=origin.line;
                     62: #endif
                     63:        if(params->size()==2) {
                     64:                columns=0;
                     65:        } else {
                     66:                columns=new(pool) Array(pool);
                     67: 
                     68:                if(char *row_chars=getrow(&data)) 
                     69:                        do {
                     70:                                String *name=new(pool) String(pool);
1.38      paf        71:                                name->APPEND_CLEAN(lsplit(&row_chars, '\t'), 0, file, line++);
1.1       paf        72:                                *columns+=name;
                     73:                        } while(row_chars);
                     74:        }
                     75: 
                     76:        // parse cells
1.27      paf        77:        Table& table=*new(pool) Table(pool, &method_name, columns);
1.1       paf        78:        char *row_chars;
                     79:        while(row_chars=getrow(&data)) {
1.28      paf        80:                if(!*row_chars) // remove empty lines
                     81:                        continue;
1.1       paf        82:                Array *row=new(pool) Array(pool);
                     83:                while(char *cell_chars=lsplit(&row_chars, '\t')) {
                     84:                        String *cell=new(pool) String(pool);
1.38      paf        85:                        cell->APPEND_CLEAN(cell_chars, 0, file, line);
1.1       paf        86:                        *row+=cell;
                     87:                }
                     88:                line++;
                     89:                table+=row;
                     90:        };
                     91: 
                     92:        // replace any previous table value
1.18      paf        93:        static_cast<VTable *>(r.self)->set_table(table);
1.1       paf        94: }
                     95: 
1.2       paf        96: 
                     97: static void _set(Request& r, const String& method_name, Array *params) {
                     98:        set_or_load(r, method_name, params, false);
                     99: }
                    100: 
                    101: static void _load(Request& r, const String& method_name, Array *params) {
                    102:        set_or_load(r, method_name, params, true);
                    103: }
                    104: 
1.22      paf       105: static void _save(Request& r, const String& method_name, Array *params) {
1.39    ! paf       106:        if(r.self==table_class)
        !           107:                RTHROW(0, 0,
        !           108:                        &method_name,
        !           109:                        "method of 'table' is not static");
        !           110: 
1.22      paf       111:        Pool& pool=r.pool();
1.39    ! paf       112:        Value *vtable_name=static_cast<Value *>(params->get(params->size()-1));
1.22      paf       113:        // forcing
                    114:        // ^save[this body type]
1.39    ! paf       115:        r.fail_if_junction_(true, *vtable_name, 
1.22      paf       116:                method_name, "file name must not be junction");
                    117: 
                    118:        // forcing untaint language
1.39    ! paf       119:        String ltable_name(pool);
        !           120:        ltable_name.append(vtable_name->as_string(),
1.23      paf       121:                String::UL_FILE_NAME, true);
1.22      paf       122: 
1.31      paf       123:        Table& table=static_cast<VTable *>(r.self)->table();
                    124: 
                    125:        String sdata(pool);
                    126:        if(params->size()==1) { // not nameless=named output
                    127:                // write out names line
                    128:                if(table.columns()) { // named table
                    129:                        for(int column=0; column<table.columns()->size(); column++) {
                    130:                                if(column)
                    131:                                        sdata.APPEND_CONST("\t");
                    132:                                sdata.append(*static_cast<String *>(table.columns()->quick_get(column)), 
                    133:                                        String::UL_TABLE);
                    134:                        }
                    135:                } else { // nameless table
                    136:                        int lsize=table.size()?static_cast<Array *>(table.get(0))->size():0;
                    137:                        if(lsize)
                    138:                                for(int column=0; column<lsize; column++) {
                    139:                                        char *cindex_tab=(char *)malloc(MAX_NUMBER);
                    140:                                        snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
                    141:                                        sdata.APPEND_CONST(cindex_tab);
                    142:                                }
                    143:                        else
                    144:                                sdata.APPEND_CONST("empty nameless table");
                    145:                }
                    146:                sdata.APPEND_CONST("\n");
                    147:        }
                    148:        // data lines
                    149:        for(int index=0; index<table.size(); index++) {
                    150:                Array *row=static_cast<Array *>(table.quick_get(index));
                    151:                for(int column=0; column<row->size(); column++) {
                    152:                        if(column)
                    153:                                sdata.APPEND_CONST("\t");
                    154:                        sdata.append(*static_cast<String *>(row->quick_get(column)), 
                    155:                                String::UL_TABLE);
                    156:                }
                    157:                sdata.APPEND_CONST("\n");
                    158:        }
                    159: 
                    160:        // write
1.39    ! paf       161:        file_write(pool, r.absolute(ltable_name), sdata.cstr(), sdata.size(), true);
1.22      paf       162: }
                    163: 
1.39    ! paf       164: static void _count(Request& r, const String&method_name, Array *) {
        !           165:        if(r.self==table_class)
        !           166:                RTHROW(0, 0,
        !           167:                        &method_name,
        !           168:                        "method of 'table' is not static");
        !           169: 
1.6       paf       170:        Pool& pool=r.pool();
1.18      paf       171:        Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.15      paf       172:        r.write_no_lang(value);
1.6       paf       173: }
                    174: 
1.39    ! paf       175: static void _line(Request& r, const String& method_name, Array *) {
        !           176:        if(r.self==table_class)
        !           177:                RTHROW(0, 0,
        !           178:                        &method_name,
        !           179:                        "method of 'table' is not static");
        !           180: 
1.6       paf       181:        Pool& pool=r.pool();
1.37      paf       182:        Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().current());
1.15      paf       183:        r.write_no_lang(value);
1.6       paf       184: }
                    185: 
1.39    ! paf       186: static void _offset(Request& r, const String& method_name, Array *params) {
        !           187:        if(r.self==table_class)
        !           188:                RTHROW(0, 0,
        !           189:                        &method_name,
        !           190:                        "method of 'table' is not static");
        !           191: 
1.6       paf       192:        Pool& pool=r.pool();
1.18      paf       193:        Table& table=static_cast<VTable *>(r.self)->table();
1.37      paf       194:        if(params->size())
                    195:                table.shift((int)r.process(*static_cast<Value *>(params->get(0))).as_double());
                    196:        else {
                    197:                Value& value=*new(pool) VInt(pool, table.current());
1.15      paf       198:                r.write_no_lang(value);
1.6       paf       199:        }
                    200: }
                    201: 
1.7       paf       202: static void _menu(Request& r, const String& method_name, Array *params) {
1.39    ! paf       203:        if(r.self==table_class)
        !           204:                RTHROW(0, 0,
        !           205:                        &method_name,
        !           206:                        "method of 'table' is not static");
        !           207: 
1.7       paf       208:        Value& body_code=*static_cast<Value *>(params->get(0));
                    209:        // forcing ^menu{this param type}
                    210:        r.fail_if_junction_(false, body_code, 
                    211:                method_name, "body must be junction");
                    212:        
                    213:        Value *delim_code=params->size()==2?static_cast<Value *>(params->get(1)):0;
                    214: 
1.18      paf       215:        Table& table=static_cast<VTable *>(r.self)->table();
1.7       paf       216:        bool need_delim=false;
1.37      paf       217:        int saved_current=table.current();
1.22      paf       218:        for(int row=0; row<table.size(); row++) {
                    219:                table.set_current(row);
1.7       paf       220: 
1.12      paf       221:                Value& processed_body=r.process(body_code);
1.7       paf       222:                if(delim_code) { // delimiter set?
                    223:                        const String *string=processed_body.get_string();
                    224:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    225:                                r.write_pass_lang(r.process(*delim_code));
                    226:                        need_delim=true;
                    227:                }
                    228:                r.write_pass_lang(processed_body);
                    229:        }
1.37      paf       230:        table.set_current(saved_current);
1.7       paf       231: }
                    232: 
1.39    ! paf       233: static void _empty(Request& r, const String& method_name, Array *params) {
        !           234:        if(r.self==table_class)
        !           235:                RTHROW(0, 0,
        !           236:                        &method_name,
        !           237:                        "method of 'table' is not static");
        !           238: 
1.18      paf       239:        Table& table=static_cast<VTable *>(r.self)->table();
1.8       paf       240:        if(table.size()==0) {
                    241:                Value& value=r.process(*static_cast<Value *>(params->get(0)));
                    242:                r.write_pass_lang(value);
                    243:        } else if(params->size()==2) {
                    244:                Value& value=r.process(*static_cast<Value *>(params->get(1)));
                    245:                r.write_pass_lang(value);
                    246:        }
                    247: }
1.15      paf       248: 
1.29      paf       249: struct Record_info {
                    250:        Pool *pool;
                    251:        Table *table;
                    252:        Hash *hash;
                    253: };
                    254: static void store_column_item_to_hash(Array::Item *item, void *info) {
                    255:        Record_info& ri=*static_cast<Record_info *>(info);
                    256:        String& column_name=*static_cast<String *>(item);
                    257:        const String *column_item=ri.table->item(column_name);
                    258:        Value *value;
                    259:        if(column_item)
                    260:                value=new(*ri.pool) VString(*column_item);
                    261:        else
                    262:                value=new(*ri.pool) VUnknown(*ri.pool);
                    263:        ri.hash->put(column_name, value);
                    264: }
1.39    ! paf       265: static void _record(Request& r, const String& method_name, Array *params) {
        !           266:        if(r.self==table_class)
        !           267:                RTHROW(0, 0,
        !           268:                        &method_name,
        !           269:                        "method of 'table' is not static");
        !           270: 
1.29      paf       271:        Table& table=static_cast<VTable *>(r.self)->table();
                    272:        if(const Array *columns=table.columns()) {
                    273:                Pool& pool=r.pool();
                    274:                Value& value=*new(pool) VHash(pool);
                    275:                Record_info record_info={&pool, &table, value.get_hash()};
                    276:                columns->for_each(store_column_item_to_hash, &record_info);
                    277:                
                    278:                r.write_no_lang(value);
                    279:        }
                    280: }
                    281: 
1.34      paf       282: struct Seq_item {
                    283:        Array *row;
                    284:        union {
                    285:                char *c_str;
                    286:                double d;
                    287:        } value;
1.32      paf       288: };
1.34      paf       289: static int sort_cmp_string(const void *a, const void *b) {
                    290:        return strcmp(
                    291:                static_cast<const Seq_item *>(a)->value.c_str, 
                    292:                static_cast<const Seq_item *>(b)->value.c_str
                    293:        );
                    294: }
                    295: static int sort_cmp_double(const void *a, const void *b) {
                    296:        double va=static_cast<const Seq_item *>(a)->value.d;
                    297:        double vb=static_cast<const Seq_item *>(b)->value.d;
                    298:        if(va<vb)
                    299:                return -1;
                    300:        else if(va>vb)
                    301:                return +1;
                    302:        else 
                    303:                return 0;
                    304: }
1.32      paf       305: static void _sort(Request& r, const String& method_name, Array *params) {
1.39    ! paf       306:        if(r.self==table_class)
        !           307:                RTHROW(0, 0,
        !           308:                        &method_name,
        !           309:                        "method of 'table' is not static");
        !           310: 
1.32      paf       311:        Value& key_maker=*(Value *)params->get(0);
                    312:        // forcing ^sort{this} ^sort(or this) param type
                    313:        r.fail_if_junction_(false, key_maker, method_name, "key-maker must be junction");
                    314: 
                    315:        bool reverse;
                    316:        if(params->size()==2) { // ..[asc|desc]
1.35      paf       317:                Value& order=*(Value *)params->get(1);
1.32      paf       318:                // forcing ..[this param-type]
1.35      paf       319:                r.fail_if_junction_(true, order, method_name, "order must not be junction");
                    320:                reverse=order.as_string()=="desc";
1.32      paf       321:        } else
                    322:                reverse=false;
                    323: 
                    324:        Table& table=static_cast<VTable *>(r.self)->table();
1.34      paf       325: 
                    326:        // anything to sort?
                    327:        if(!table.size())
                    328:                return;
                    329: 
                    330:        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*table.size());
                    331:        int i;
                    332: 
                    333:        // calculate key values
                    334:        bool key_values_are_strings=true;
                    335:        for(i=0; i<table.size(); i++) {
1.32      paf       336:                table.set_current(i);
                    337:                // calculate key value
1.34      paf       338:                seq[i].row=(Array *)table.get(i);
                    339:                Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
                    340:                if(i==0) // determining key values type by first one
                    341:                        key_values_are_strings=value.is_string();
                    342: 
                    343:                if(key_values_are_strings)
                    344:                        seq[i].value.c_str=value.as_string().cstr();
                    345:                else
                    346:                        seq[i].value.d=value.as_double();
1.32      paf       347:        }
                    348:        // sort keys
1.34      paf       349:        _qsort(seq, table.size(), sizeof(Seq_item), 
                    350:                key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf       351: 
1.34      paf       352:        // reorder table as they require in 'seq'
                    353:        for(i=0; i<table.size(); i++)
                    354:                table.put(i, seq[reverse?table.size()-1-i:i].row);
1.32      paf       355: 
1.34      paf       356:        // reset 'current'
1.32      paf       357:        table.set_current(0);
                    358: }
                    359: 
1.39    ! paf       360: static void _locate(Request& r, const String& method_name, Array *params) {
        !           361:        if(r.self==table_class)
        !           362:                RTHROW(0, 0,
        !           363:                        &method_name,
        !           364:                        "method of 'table' is not static");
        !           365: 
1.36      paf       366:        VTable& vtable=*static_cast<VTable *>(r.self);
                    367:        Table& table=vtable.table();
                    368:        vtable.last_locate_was_successful=table.locate(
                    369:                static_cast<Value *>(params->get(0))->as_string(),
                    370:                static_cast<Value *>(params->get(1))->as_string());
                    371: }
                    372: 
1.37      paf       373: static void _found(Request& r, const String& method_name, Array *params) {
1.39    ! paf       374:        if(r.self==table_class)
        !           375:                RTHROW(0, 0,
        !           376:                        &method_name,
        !           377:                        "method of 'table' is not static");
        !           378: 
1.37      paf       379:        if(static_cast<VTable *>(r.self)->last_locate_was_successful) {
                    380:                Value& then_code=*static_cast<Value *>(params->get(0));
                    381:                // forcing ^found{this param type}
                    382:                r.fail_if_junction_(false, then_code, 
                    383:                        method_name, "found-parameter must be junction");
                    384:                r.write_pass_lang(r.process(then_code));
                    385:        } else if(params->size()==2) {
                    386:                Value& else_code=*static_cast<Value *>(params->get(1));
                    387:                // forcing ^found{this param type}
                    388:                r.fail_if_junction_(false, else_code, 
                    389:                        method_name, "not found-parameter must be junction");
                    390:                r.write_pass_lang(r.process(else_code));
                    391:        }
                    392: }
                    393: 
1.39    ! paf       394: static void _flip(Request& r, const String& method_name, Array *params) {
        !           395:        Pool& pool=r.pool();
        !           396: 
        !           397:        if(r.self==table_class)
        !           398:                RTHROW(0, 0,
        !           399:                        &method_name,
        !           400:                        "method of 'table' is not static");
        !           401: 
        !           402:        VTable& vtable=*static_cast<VTable *>(r.self);
        !           403: 
        !           404:        Table& old_table=*vtable.get_table();
        !           405:        Table& new_table=*new(pool) Table(pool, &method_name, 0/*nameless*/);
        !           406:        if(old_table.size())
        !           407:                if(int old_cols=old_table.at(0).size()) 
        !           408:                        for(int column=0; column<old_cols; column++) {
        !           409:                                Array& new_row=*new(pool) Array(pool, old_table.size());
        !           410:                                for(int i=0; i<old_table.size(); i++) {
        !           411:                                        const Array& old_row=old_table.at(i);
        !           412:                                        new_row+=column<old_row.size()?old_row.get(column):empty_string;
        !           413:                                }
        !           414:                                new_table+=&new_row;
        !           415:                        }
        !           416: 
        !           417:        vtable.set_table(new_table);
        !           418: }
        !           419: 
1.15      paf       420: // initialize
1.8       paf       421: 
1.14      paf       422: void initialize_table_class(Pool& pool, VStateless_class& vclass) {
1.22      paf       423:        // ^table.set{data}
                    424:        // ^table.set[nameless]{data}
1.1       paf       425:        vclass.add_native_method("set", _set, 1, 2);
1.2       paf       426: 
1.10      paf       427:        // ^table.load[file]  
                    428:        // ^table.load[nameless;file]
1.2       paf       429:        vclass.add_native_method("load", _load, 1, 2);
1.22      paf       430: 
                    431:        // ^table.save[file]  
                    432:        // ^table.save[nameless;file]
                    433:        vclass.add_native_method("save", _save, 1, 2);
1.6       paf       434: 
                    435:        // ^table.count[]
                    436:        vclass.add_native_method("count", _count, 0, 0);
                    437: 
                    438:        // ^table.line[]
                    439:        vclass.add_native_method("line", _line, 0, 0);
                    440: 
1.10      paf       441:        // ^table.offset[]  
                    442:        // ^table.offset[offset]
1.6       paf       443:        vclass.add_native_method("offset", _offset, 0, 1);
1.7       paf       444: 
1.10      paf       445:        // ^table.menu{code}  
                    446:        // ^table.menu{code}[delim]
1.7       paf       447:        vclass.add_native_method("menu", _menu, 1, 2);
1.8       paf       448: 
1.10      paf       449:        // ^table.empty{code-when-empty}  
                    450:        // ^table.empty{code-when-empty}{code-when-not}
1.8       paf       451:        vclass.add_native_method("empty", _empty, 1, 2);
1.29      paf       452: 
                    453:        // ^table.record[]
                    454:        vclass.add_native_method("record", _record, 0, 0);
1.32      paf       455: 
                    456:        // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc]
                    457:        // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc]
                    458:        vclass.add_native_method("sort", _sort, 1, 2);
1.8       paf       459: 
1.36      paf       460:        // ^table.locate[field;value]
                    461:        vclass.add_native_method("locate", _locate, 2, 2);
1.37      paf       462:        // ^table.found{when-found}
                    463:        // ^table.found{when-found}{when-not-found}
                    464:        vclass.add_native_method("found", _found, 1, 2);
1.39    ! paf       465: 
        !           466:        // ^table.flip[]
        !           467:        vclass.add_native_method("flip", _flip, 0, 0);
1.1       paf       468: }      

E-mail: