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

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

E-mail: