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

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.65.2.1! paf         8:        $Id: table.C,v 1.65 2001/04/26 14:55:13 paf Exp $
1.1       paf         9: */
                     10: 
1.24      paf        11: #include "pa_config_includes.h"
1.59      paf        12: 
                     13: #include "pcre.h"
                     14: 
1.65.2.1! paf        15: #include "classes.h"
1.16      paf        16: #include "pa_common.h"
1.1       paf        17: #include "pa_request.h"
                     18: #include "pa_vtable.h"
1.6       paf        19: #include "pa_vint.h"
1.51      paf        20: #include "pa_sql_connection.h"
1.58      paf        21: #include "pa_dir.h"
1.1       paf        22: 
1.65.2.1! paf        23: // defines
1.1       paf        24: 
1.65.2.1! paf        25: #define TABLE_CLASS_NAME "table"
        !            26: 
        !            27: // class
        !            28: 
        !            29: class MTable : public Methoded {
        !            30: public:
        !            31:        MTable(Pool& pool);
        !            32:        bool used_directly() { return true; }
        !            33: };
1.1       paf        34: 
                     35: // methods
1.65.2.1! paf        36: 
        !            37: void _set(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.1       paf        38:        Pool& pool=r.pool();
                     39:        // data is last parameter
1.61      paf        40:        Value& vdata=params->get_junction(params->size()-1, "body must be code");
1.44      paf        41: 
1.45      paf        42:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.61      paf        43:        const String& data=r.process(vdata).as_string();
1.44      paf        44: 
                     45:        size_t pos_after=0;
                     46:        // parse columns
                     47:        Array *columns;
                     48:        if(params->size()==2) {
                     49:                columns=0;
1.21      paf        50:        } else {
1.44      paf        51:                columns=new(pool) Array(pool);
                     52: 
                     53:                Array head(pool);
                     54:                data.split(head, &pos_after, "\n", 1, String::UL_CLEAN, 1);
                     55:                if(head.size())
                     56:                        head.get_string(0)->split(*columns, 0, "\t", 1, String::UL_CLEAN);
                     57:        }
                     58: 
                     59:        Table& table=*new(pool) Table(pool, &method_name, columns);
                     60:        // parse cells
                     61:        Array rows(pool);
                     62:        data.split(rows, &pos_after, "\n", 1, String::UL_CLEAN);
                     63:        int size=rows.quick_size();
                     64:        for(int i=0; i<size; i++) {
                     65:                Array& row=*new(pool) Array(pool);
                     66:                const String& string=*rows.quick_get_string(i);
                     67:                // remove empty lines
                     68:                if(!string.size())
                     69:                        continue;
                     70: 
                     71:                string.split(row, 0, "\t", 1, String::UL_CLEAN);
                     72:                table+=&row;
1.17      paf        73:        }
1.1       paf        74: 
1.44      paf        75:        // replace any previous table value
                     76:        static_cast<VTable *>(r.self)->set_table(table);
                     77: }
                     78: 
1.65.2.1! paf        79: void _load(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.44      paf        80:        Pool& pool=r.pool();
                     81:        // filename is last parameter
1.61      paf        82:        Value& vfilename=params->get_no_junction(params->size()-1, 
                     83:                "file name must not be code");
1.44      paf        84: 
                     85:        // loading text
1.61      paf        86:        char *data=file_read_text(pool, r.absolute(vfilename.as_string()));
1.44      paf        87: 
1.1       paf        88:        // parse columns
                     89:        Array *columns;
                     90: #ifndef NO_STRING_ORIGIN
                     91:        const Origin& origin=method_name.origin();
1.2       paf        92:        const char *file=origin.file;
1.1       paf        93:        uint line=origin.line;
                     94: #endif
                     95:        if(params->size()==2) {
                     96:                columns=0;
                     97:        } else {
                     98:                columns=new(pool) Array(pool);
                     99: 
                    100:                if(char *row_chars=getrow(&data)) 
                    101:                        do {
                    102:                                String *name=new(pool) String(pool);
1.45      paf       103:                                name->APPEND_TAINTED(lsplit(&row_chars, '\t'), 0, file, line++);
1.1       paf       104:                                *columns+=name;
                    105:                        } while(row_chars);
                    106:        }
                    107: 
                    108:        // parse cells
1.27      paf       109:        Table& table=*new(pool) Table(pool, &method_name, columns);
1.1       paf       110:        char *row_chars;
                    111:        while(row_chars=getrow(&data)) {
1.28      paf       112:                if(!*row_chars) // remove empty lines
                    113:                        continue;
1.1       paf       114:                Array *row=new(pool) Array(pool);
                    115:                while(char *cell_chars=lsplit(&row_chars, '\t')) {
                    116:                        String *cell=new(pool) String(pool);
1.44      paf       117:                        cell->APPEND_TAINTED(cell_chars, 0, file, line);
1.1       paf       118:                        *row+=cell;
                    119:                }
                    120:                line++;
                    121:                table+=row;
                    122:        };
                    123: 
                    124:        // replace any previous table value
1.18      paf       125:        static_cast<VTable *>(r.self)->set_table(table);
1.1       paf       126: }
1.2       paf       127: 
1.65.2.1! paf       128: void _save(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.22      paf       129:        Pool& pool=r.pool();
1.61      paf       130:        Value& vtable_name=params->get_no_junction(params->size()-1, 
                    131:                "file name must not be code");
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.61      paf       171:        file_write(pool, r.absolute(vtable_name.as_string()), 
1.48      paf       172:                sdata.cstr(), sdata.size(), true);
1.22      paf       173: }
                    174: 
1.65.2.1! paf       175: void _count(Methoded& m, Request& r, const String&, MethodParams *) {
1.6       paf       176:        Pool& pool=r.pool();
1.18      paf       177:        Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.15      paf       178:        r.write_no_lang(value);
1.6       paf       179: }
                    180: 
1.65.2.1! paf       181: void _line(Methoded& m, Request& r, const String& method_name, MethodParams *) {
1.6       paf       182:        Pool& pool=r.pool();
1.37      paf       183:        Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().current());
1.15      paf       184:        r.write_no_lang(value);
1.6       paf       185: }
                    186: 
1.65.2.1! paf       187: void _offset(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.6       paf       188:        Pool& pool=r.pool();
1.18      paf       189:        Table& table=static_cast<VTable *>(r.self)->table();
1.37      paf       190:        if(params->size())
1.61      paf       191:                table.shift((int)r.process(params->get(0)).as_double());
1.37      paf       192:        else {
                    193:                Value& value=*new(pool) VInt(pool, table.current());
1.15      paf       194:                r.write_no_lang(value);
1.6       paf       195:        }
                    196: }
                    197: 
1.65.2.1! paf       198: void _menu(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.61      paf       199:        Value& body_code=params->get_junction(0, "body must be code");
1.7       paf       200:        
1.61      paf       201:        Value *delim_code=params->size()==2?&params->get(1):0;
1.7       paf       202: 
1.64      paf       203:        VTable& vtable=*static_cast<VTable *>(r.self);
                    204:        Table& table=vtable.table();
1.7       paf       205:        bool need_delim=false;
1.64      paf       206:        vtable.lock(); int saved_current=table.current();
1.22      paf       207:        for(int row=0; row<table.size(); row++) {
                    208:                table.set_current(row);
1.7       paf       209: 
1.12      paf       210:                Value& processed_body=r.process(body_code);
1.7       paf       211:                if(delim_code) { // delimiter set?
                    212:                        const String *string=processed_body.get_string();
                    213:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    214:                                r.write_pass_lang(r.process(*delim_code));
                    215:                        need_delim=true;
                    216:                }
                    217:                r.write_pass_lang(processed_body);
                    218:        }
1.64      paf       219:        table.set_current(saved_current); vtable.unlock();
1.7       paf       220: }
                    221: 
1.65.2.1! paf       222: void _empty(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.18      paf       223:        Table& table=static_cast<VTable *>(r.self)->table();
1.61      paf       224:        if(table.size()==0)
1.62      paf       225:                r.write_pass_lang(r.process(params->get(0)));
                    226:        else if(params->size()>1)
                    227:                r.write_pass_lang(r.process(params->get(1)));
1.8       paf       228: }
1.15      paf       229: 
1.65      paf       230: /// used by table: _record / store_column_item_to_hash
1.29      paf       231: struct Record_info {
                    232:        Pool *pool;
                    233:        Table *table;
                    234:        Hash *hash;
                    235: };
                    236: static void store_column_item_to_hash(Array::Item *item, void *info) {
                    237:        Record_info& ri=*static_cast<Record_info *>(info);
                    238:        String& column_name=*static_cast<String *>(item);
                    239:        const String *column_item=ri.table->item(column_name);
                    240:        Value *value;
                    241:        if(column_item)
                    242:                value=new(*ri.pool) VString(*column_item);
                    243:        else
                    244:                value=new(*ri.pool) VUnknown(*ri.pool);
                    245:        ri.hash->put(column_name, value);
                    246: }
1.65.2.1! paf       247: void _record(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.29      paf       248:        Table& table=static_cast<VTable *>(r.self)->table();
                    249:        if(const Array *columns=table.columns()) {
                    250:                Pool& pool=r.pool();
                    251:                Value& value=*new(pool) VHash(pool);
                    252:                Record_info record_info={&pool, &table, value.get_hash()};
                    253:                columns->for_each(store_column_item_to_hash, &record_info);
                    254:                
                    255:                r.write_no_lang(value);
                    256:        }
                    257: }
                    258: 
1.34      paf       259: struct Seq_item {
                    260:        Array *row;
                    261:        union {
                    262:                char *c_str;
                    263:                double d;
                    264:        } value;
1.32      paf       265: };
1.34      paf       266: static int sort_cmp_string(const void *a, const void *b) {
                    267:        return strcmp(
                    268:                static_cast<const Seq_item *>(a)->value.c_str, 
                    269:                static_cast<const Seq_item *>(b)->value.c_str
                    270:        );
                    271: }
                    272: static int sort_cmp_double(const void *a, const void *b) {
                    273:        double va=static_cast<const Seq_item *>(a)->value.d;
                    274:        double vb=static_cast<const Seq_item *>(b)->value.d;
                    275:        if(va<vb)
                    276:                return -1;
                    277:        else if(va>vb)
                    278:                return +1;
                    279:        else 
                    280:                return 0;
                    281: }
1.65.2.1! paf       282: void _sort(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.61      paf       283:        Value& key_maker=params->get_junction(0, "key-maker must be code");
                    284: 
                    285:        bool reverse=params->size()==2/*..[asc|desc]*/?
                    286:                reverse=params->get_no_junction(1, "order must not be code").as_string()=="desc":
                    287:                false;
1.32      paf       288: 
                    289:        Table& table=static_cast<VTable *>(r.self)->table();
1.34      paf       290: 
                    291:        // anything to sort?
                    292:        if(!table.size())
                    293:                return;
                    294: 
                    295:        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*table.size());
                    296:        int i;
                    297: 
                    298:        // calculate key values
                    299:        bool key_values_are_strings=true;
                    300:        for(i=0; i<table.size(); i++) {
1.32      paf       301:                table.set_current(i);
                    302:                // calculate key value
1.61      paf       303:                seq[i].row=(MethodParams *)table.get(i);
1.34      paf       304:                Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
                    305:                if(i==0) // determining key values type by first one
                    306:                        key_values_are_strings=value.is_string();
                    307: 
                    308:                if(key_values_are_strings)
                    309:                        seq[i].value.c_str=value.as_string().cstr();
                    310:                else
                    311:                        seq[i].value.d=value.as_double();
1.32      paf       312:        }
                    313:        // sort keys
1.34      paf       314:        _qsort(seq, table.size(), sizeof(Seq_item), 
                    315:                key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf       316: 
1.34      paf       317:        // reorder table as they require in 'seq'
                    318:        for(i=0; i<table.size(); i++)
                    319:                table.put(i, seq[reverse?table.size()-1-i:i].row);
1.32      paf       320: 
1.34      paf       321:        // reset 'current'
1.32      paf       322:        table.set_current(0);
                    323: }
                    324: 
1.65.2.1! paf       325: void _locate(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.36      paf       326:        VTable& vtable=*static_cast<VTable *>(r.self);
                    327:        Table& table=vtable.table();
                    328:        vtable.last_locate_was_successful=table.locate(
1.61      paf       329:                params->get(0).as_string(),
                    330:                params->get(1).as_string());
1.36      paf       331: }
                    332: 
1.65.2.1! paf       333: void _found(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.61      paf       334:        if(static_cast<VTable *>(r.self)->last_locate_was_successful)
                    335:                r.write_pass_lang(r.process(params->get_junction(0, "found-parameter must be code")));
                    336:        else if(params->size()==2)
                    337:                r.write_pass_lang(r.process(params->get_junction(0, "else-parameter must be code")));
1.37      paf       338: }
                    339: 
1.65.2.1! paf       340: void _flip(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.39      paf       341:        Pool& pool=r.pool();
                    342:        VTable& vtable=*static_cast<VTable *>(r.self);
                    343: 
                    344:        Table& old_table=*vtable.get_table();
                    345:        Table& new_table=*new(pool) Table(pool, &method_name, 0/*nameless*/);
                    346:        if(old_table.size())
                    347:                if(int old_cols=old_table.at(0).size()) 
                    348:                        for(int column=0; column<old_cols; column++) {
                    349:                                Array& new_row=*new(pool) Array(pool, old_table.size());
                    350:                                for(int i=0; i<old_table.size(); i++) {
                    351:                                        const Array& old_row=old_table.at(i);
                    352:                                        new_row+=column<old_row.size()?old_row.get(column):empty_string;
                    353:                                }
                    354:                                new_table+=&new_row;
                    355:                        }
                    356: 
                    357:        vtable.set_table(new_table);
                    358: }
                    359: 
1.65.2.1! paf       360: void _append(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.41      paf       361:        Pool& pool=r.pool();
                    362:        // data is last parameter
1.61      paf       363:        const String& string=
                    364:                r.process(params->get_junction(0, "body must be code")).as_string();
1.41      paf       365: 
                    366:        // parse cells
                    367:        Array& row=*new(pool) Array(pool);
1.46      paf       368:        string.split(row, 0, "\t", 1, String::UL_CLEAN);
1.41      paf       369: 
                    370:        static_cast<VTable *>(r.self)->table()+=&row;
                    371: }
                    372: 
1.65.2.1! paf       373: void _join(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.42      paf       374:        Pool& pool=r.pool();
                    375: 
1.61      paf       376:        Table *maybe_src=params->get_no_junction(0, "table ref must not be code").get_table();
1.42      paf       377:        if(!maybe_src)
1.49      paf       378:                PTHROW(0, 0,
1.42      paf       379:                        &method_name,
                    380:                        "source is not a table");
                    381: 
                    382:        Table& src=*maybe_src;
                    383:        Table& dest=static_cast<VTable *>(r.self)->table();
                    384:        if(&src == &dest)
1.49      paf       385:                PTHROW(0, 0,
1.42      paf       386:                        &method_name,
                    387:                        "source and destination are same table");
                    388: 
                    389:        if(const Array *dest_columns=dest.columns()) { // dest is named
                    390:                int saved_src_current=src.current();
                    391:                for(int src_row=0; src_row<src.size(); src_row++) {
                    392:                        src.set_current(src_row);
                    393:                        Array& dest_row=*new(pool) Array(pool);
                    394:                        for(int dest_column=0; dest_column<dest_columns->size(); dest_column++) 
                    395:                                dest_row+=src.item(*dest_columns->get_string(dest_column));
                    396:                        dest+=&dest_row;
                    397:                }
                    398:                src.set_current(saved_src_current);
                    399:        } else { // dest is nameless
                    400:                for(int src_row=0; src_row<src.size(); src_row++)
                    401:                        dest+=&src.at(src_row);
                    402:        }
                    403: }
                    404: 
1.57      paf       405: /// ^table:sql{query}[(count[;offset])]
1.65.2.1! paf       406: void _sql(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.49      paf       407:        Pool& pool=r.pool();
                    408: 
                    409:        if(!r.connection)
                    410:                PTHROW(0, 0,
                    411:                        &method_name,
                    412:                        "without connect");
                    413: 
1.61      paf       414:        Value& statement=params->get_junction(0, "statement must be code");
1.49      paf       415: 
1.53      paf       416:        ulong limit=0;
1.49      paf       417:        if(params->size()>1) {
1.61      paf       418:                Value& limit_code=params->get_junction(1, "limit must be expression");
1.53      paf       419:                limit=(uint)r.process(limit_code).as_double();
1.49      paf       420:        }
                    421: 
1.51      paf       422:        ulong offset=0;
1.49      paf       423:        if(params->size()>2) {
1.61      paf       424:                Value& offset_code=params->get_junction(2, "offset must be expression");
1.51      paf       425:                offset=(ulong)r.process(offset_code).as_double();
1.49      paf       426:        }
                    427: 
1.54      paf       428:        Temp_lang temp_lang(r, String::UL_SQL);
                    429:        const String& statement_string=r.process(statement).as_string();
1.55      paf       430:        const char *statement_cstr=
                    431:                statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
1.51      paf       432:        unsigned int sql_column_count; SQL_Driver::Cell *sql_columns;
                    433:        unsigned long sql_row_count; SQL_Driver::Cell **sql_rows;
1.52      paf       434:        bool need_rethrow=false; Exception rethrow_me;
                    435:        PTRY {
                    436:                r.connection->query(
1.53      paf       437:                        statement_cstr, offset, limit,
1.52      paf       438:                        &sql_column_count, &sql_columns,
                    439:                        &sql_row_count, &sql_rows);
                    440:        }
                    441:        PCATCH(e) { // connect/process problem
                    442:                rethrow_me=e;  need_rethrow=true;
                    443:        }
                    444:        PEND_CATCH
                    445:        if(need_rethrow)
                    446:                PTHROW(rethrow_me.type(), rethrow_me.code(),
1.53      paf       447:                        &statement_string, // setting more specific source [were url]
1.52      paf       448:                        rethrow_me.comment());
1.51      paf       449:        
                    450:        Array& table_columns=*new(pool) Array(pool);
                    451:        for(unsigned int i=0; i<sql_column_count; i++) {
                    452:                String& table_column=*new(pool) String(pool);
                    453:                table_column.APPEND_TAINTED(
                    454:                        (const char *)sql_columns[i].ptr, sql_columns[i].size,
                    455:                        statement_cstr, 0);
                    456:                table_columns+=&table_column;
                    457:        }
                    458: 
                    459:        Table& table=*new(pool) Table(pool, &method_name, &table_columns);
                    460: 
                    461:        {
                    462:                for(unsigned long r=0; r<sql_row_count; r++) {
                    463:                        SQL_Driver::Cell *sql_cells=sql_rows[r];
                    464:                        Array& table_row=*new(pool) Array(pool);
                    465:                        
                    466:                        for(unsigned int i=0; i<sql_column_count; i++) {
                    467:                                String& table_cell=*new(pool) String(pool);
                    468:                                table_cell.APPEND_TAINTED(
                    469:                                        (const char *)sql_cells[i].ptr, sql_cells[i].size,
                    470:                                        statement_cstr, r);
                    471:                                table_row+=&table_cell;
                    472:                        }
                    473:                        table+=&table_row;
                    474:                }
                    475:        }
1.49      paf       476: 
                    477:        // replace any previous table value
                    478:        static_cast<VTable *>(r.self)->set_table(table);
                    479: }
                    480: 
1.58      paf       481: /// ^table:dir[path]
                    482: /// ^table:dir[path][regexp]
1.65.2.1! paf       483: void _dir(Methoded& m, Request& r, const String& method_name, MethodParams *params) {
1.58      paf       484:        Pool& pool=r.pool();
                    485: 
1.61      paf       486:        Value& relative_path=params->get_no_junction(0, "path must not be code");
1.58      paf       487: 
1.59      paf       488:        const String *regexp;
                    489:        pcre *regexp_code;
                    490:        int ovecsize;
                    491:        int *ovector;
                    492:        if(params->size()>1) {
1.61      paf       493:                regexp=&params->get_no_junction(1, "regexp must not be code").as_string();
1.59      paf       494: 
                    495:                const char *pattern=regexp->cstr(String::UL_AS_IS);
                    496:                const char *errptr;
                    497:                int erroffset;
                    498:                regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL, 
                    499:                        &errptr, &erroffset,
1.63      paf       500:                        r.pcre_tables);
1.59      paf       501: 
                    502:                if(!regexp_code)
                    503:                        PTHROW(0, 0,
                    504:                                &regexp->mid(erroffset, regexp->size()),
                    505:                                "regular expression syntax error - %s", errptr);
                    506: 
                    507:                ovector=(int *)malloc(sizeof(int)*(ovecsize=(1/*match*/)*3));
                    508:        } else 
                    509:                regexp_code=0;
                    510: 
                    511: 
1.58      paf       512:        const char* absolute_path_cstr=r.absolute(relative_path.as_string())
                    513:                .cstr(String::UL_FILE_NAME);
                    514: 
                    515:        Array& columns=*new(pool) Array(pool);
                    516:        columns+=new(pool) String(pool, "name");        
                    517:        Table& table=*new(pool) Table(pool, &method_name, &columns);
                    518: 
                    519:        LOAD_DIR(absolute_path_cstr, 
                    520:                size_t file_name_size=strlen(ffblk.ff_name);
1.59      paf       521:                bool suits=true;
                    522:                if(regexp_code) {
                    523:                        int exec_result=pcre_exec(regexp_code, 0,
                    524:                                ffblk.ff_name, file_name_size, 0,
                    525:                                0, ovector, ovecsize);
                    526:                        
                    527:                        if(exec_result==PCRE_ERROR_NOMATCH)
                    528:                                suits=false;
                    529:                        else if(exec_result<0) {
                    530:                                (*pcre_free)(regexp_code);
                    531:                                PTHROW(0, 0,
                    532:                                        regexp,
1.60      paf       533:                                        "regular expression execute (%d)", 
1.59      paf       534:                                                exec_result);
                    535:                        }
                    536:                }
                    537: 
                    538:                if(suits) {
                    539:                        char *file_name_cstr=(char *)r.malloc(file_name_size);
                    540:                        memcpy(file_name_cstr, ffblk.ff_name, file_name_size);
                    541:                        String &file_name=*new(pool) String(pool);
                    542:                        file_name.APPEND_TAINTED(file_name_cstr, file_name_size, 
                    543:                                method_name.origin().file, method_name.origin().line);
                    544:                
                    545:                        Array& row=*new(pool) Array(pool);
                    546:                        row+=&file_name;
                    547:                        table+=&row;
                    548:                }
1.58      paf       549:        );
1.59      paf       550: 
                    551:        if(regexp_code)
                    552:                (*pcre_free)(regexp_code);
1.58      paf       553: 
                    554:        // replace any previous table value
                    555:        static_cast<VTable *>(r.self)->set_table(table);
                    556: }
                    557: 
1.65.2.1! paf       558: // constructor
        !           559: 
        !           560: MTable::MTable(Pool& pool) : Methoded(pool) {
        !           561:        set_name(NEW String(pool, TABLE_CLASS_NAME));
        !           562: 
1.49      paf       563:        // ^table:set{data}
                    564:        // ^table:set[nameless]{data}
1.65.2.1! paf       565:        add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2);
1.2       paf       566: 
1.49      paf       567:        // ^table:load[file]  
                    568:        // ^table:load[nameless;file]
1.65.2.1! paf       569:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.22      paf       570: 
                    571:        // ^table.save[file]  
                    572:        // ^table.save[nameless;file]
1.65.2.1! paf       573:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6       paf       574: 
                    575:        // ^table.count[]
1.65.2.1! paf       576:        add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6       paf       577: 
                    578:        // ^table.line[]
1.65.2.1! paf       579:        add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6       paf       580: 
1.10      paf       581:        // ^table.offset[]  
                    582:        // ^table.offset[offset]
1.65.2.1! paf       583:        add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1);
1.7       paf       584: 
1.10      paf       585:        // ^table.menu{code}  
                    586:        // ^table.menu{code}[delim]
1.65.2.1! paf       587:        add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.8       paf       588: 
1.10      paf       589:        // ^table.empty{code-when-empty}  
                    590:        // ^table.empty{code-when-empty}{code-when-not}
1.65.2.1! paf       591:        add_native_method("empty", Method::CT_DYNAMIC, _empty, 1, 2);
1.29      paf       592: 
                    593:        // ^table.record[]
1.65.2.1! paf       594:        add_native_method("record", Method::CT_DYNAMIC, _record, 0, 0);
1.32      paf       595: 
                    596:        // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc]
                    597:        // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc]
1.65.2.1! paf       598:        add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8       paf       599: 
1.36      paf       600:        // ^table.locate[field;value]
1.65.2.1! paf       601:        add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2);
1.37      paf       602:        // ^table.found{when-found}
                    603:        // ^table.found{when-found}{when-not-found}
1.65.2.1! paf       604:        add_native_method("found", Method::CT_DYNAMIC, _found, 1, 2);
1.39      paf       605: 
                    606:        // ^table.flip[]
1.65.2.1! paf       607:        add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41      paf       608: 
                    609:        // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.65.2.1! paf       610:        add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42      paf       611: 
                    612:        // ^table.join[table]
1.65.2.1! paf       613:        add_native_method("join", Method::CT_DYNAMIC, _join, 1, 1);
1.49      paf       614: 
                    615: 
1.51      paf       616:        // ^table:sql[query][(count[;offset])]
1.65.2.1! paf       617:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3);
1.58      paf       618: 
                    619:        // ^table:dir[path]
                    620:        // ^table:dir[path][regexp]
1.65.2.1! paf       621:        add_native_method("dir", Method::CT_DYNAMIC, _dir, 1, 2);
        !           622: }
        !           623: 
        !           624: // global variable
        !           625: 
        !           626: Methoded *table_class;
        !           627: 
        !           628: // creator
        !           629: 
        !           630: Methoded *MTable_create(Pool& pool) {
        !           631:        return table_class=new(pool) MTable(pool);
1.40      paf       632: }

E-mail: