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

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

E-mail: