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

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

E-mail: