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

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

E-mail: