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

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

E-mail: