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

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.74    ! paf         8:        $Id: table.C,v 1.73 2001/05/08 06:42:54 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.74    ! paf       196:        if(params->size()) {
        !           197:                Value& offset_expr=params->get_junction(0, "offset must be expression");
        !           198:                table.shift((int)r.process(offset_expr).as_double());
        !           199:        } else {
1.37      paf       200:                Value& value=*new(pool) VInt(pool, table.current());
1.15      paf       201:                r.write_no_lang(value);
1.6       paf       202:        }
                    203: }
                    204: 
1.61      paf       205: static void _menu(Request& r, const String& method_name, MethodParams *params) {
                    206:        Value& body_code=params->get_junction(0, "body must be code");
1.7       paf       207:        
1.61      paf       208:        Value *delim_code=params->size()==2?&params->get(1):0;
1.7       paf       209: 
1.64      paf       210:        VTable& vtable=*static_cast<VTable *>(r.self);
                    211:        Table& table=vtable.table();
1.7       paf       212:        bool need_delim=false;
1.64      paf       213:        vtable.lock(); int saved_current=table.current();
1.22      paf       214:        for(int row=0; row<table.size(); row++) {
                    215:                table.set_current(row);
1.7       paf       216: 
1.12      paf       217:                Value& processed_body=r.process(body_code);
1.7       paf       218:                if(delim_code) { // delimiter set?
                    219:                        const String *string=processed_body.get_string();
                    220:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    221:                                r.write_pass_lang(r.process(*delim_code));
                    222:                        need_delim=true;
                    223:                }
                    224:                r.write_pass_lang(processed_body);
                    225:        }
1.64      paf       226:        table.set_current(saved_current); vtable.unlock();
1.7       paf       227: }
                    228: 
1.74    ! paf       229: /// @test make bool returning func
1.61      paf       230: static void _empty(Request& r, const String& method_name, MethodParams *params) {
1.18      paf       231:        Table& table=static_cast<VTable *>(r.self)->table();
1.61      paf       232:        if(table.size()==0)
1.62      paf       233:                r.write_pass_lang(r.process(params->get(0)));
                    234:        else if(params->size()>1)
                    235:                r.write_pass_lang(r.process(params->get(1)));
1.8       paf       236: }
1.15      paf       237: 
1.65      paf       238: /// used by table: _record / store_column_item_to_hash
1.29      paf       239: struct Record_info {
                    240:        Pool *pool;
                    241:        Table *table;
                    242:        Hash *hash;
                    243: };
                    244: static void store_column_item_to_hash(Array::Item *item, void *info) {
                    245:        Record_info& ri=*static_cast<Record_info *>(info);
                    246:        String& column_name=*static_cast<String *>(item);
                    247:        Value *value;
1.73      paf       248:        if(const String *column_item=ri.table->item(column_name))
1.29      paf       249:                value=new(*ri.pool) VString(*column_item);
                    250:        else
                    251:                value=new(*ri.pool) VUnknown(*ri.pool);
                    252:        ri.hash->put(column_name, value);
                    253: }
1.74    ! paf       254: static void _record(Request& r, const String& method_name, MethodParams *) {
1.29      paf       255:        Table& table=static_cast<VTable *>(r.self)->table();
                    256:        if(const Array *columns=table.columns()) {
                    257:                Pool& pool=r.pool();
1.73      paf       258:                Value& result=*new(pool) VHash(pool);
                    259:                Record_info record_info={&pool, &table, result.get_hash()};
1.29      paf       260:                columns->for_each(store_column_item_to_hash, &record_info);
1.73      paf       261:                result.set_name(method_name);
                    262:                r.write_no_lang(result);
1.29      paf       263:        }
                    264: }
                    265: 
1.74    ! paf       266: /// used by table: _hash / table_row_to_hash
        !           267: struct Row_info {
        !           268:        Table *table;
        !           269:        int key_field;
        !           270:        Array *value_fields;
        !           271:        Hash *hash;
        !           272: };
        !           273: static void table_row_to_hash(Array::Item *value, void *info) {
        !           274:        Array& row=*static_cast<Array *>(value);
        !           275:        Row_info& ri=*static_cast<Row_info *>(info);
        !           276:        Pool& pool=ri.table->pool();
        !           277: 
        !           278:        Value *result;
        !           279:        if(ri.value_fields->size()==1) { // key=value [not valueS]
        !           280:                int value_field=ri.value_fields->get_int(0);
        !           281:                if(value_field<row.size())
        !           282:                        result=new(pool) VString(*row.get_string(value_field));
        !           283:                else
        !           284:                        result=0;
        !           285:        } else {
        !           286:                VHash& vhash=*new(pool) VHash(pool);
        !           287:                Hash& hash=*vhash.get_hash();
        !           288:                for(int i=0; i<ri.value_fields->size(); i++) {
        !           289:                        int value_field=ri.value_fields->get_int(i);
        !           290:                        if(value_field<row.size())
        !           291:                                hash.put(
        !           292:                                        *ri.table->columns()->get_string(value_field),
        !           293:                                        new(pool) VString(*row.get_string(value_field)));
        !           294:                }
        !           295:                
        !           296:                result=&vhash;
        !           297:        }
        !           298:        
        !           299:        if(ri.key_field<row.size())
        !           300:                ri.hash->put(*row.get_string(ri.key_field), result);
        !           301: }
        !           302: static void _hash(Request& r, const String& method_name, MethodParams *params) {
        !           303:        Table& table=static_cast<VTable *>(r.self)->table();
        !           304:        if(const Array *columns=table.columns()) 
        !           305:                if(columns->size()>1) {
        !           306:                        Pool& pool=r.pool();
        !           307: 
        !           308:                        const String& key_field_name=params->get_no_junction(0, 
        !           309:                                "key field name must not be code").as_string();
        !           310:                        int key_field=table.column_name2index(key_field_name, true);
        !           311:                        int value_fields_count=params->size()-1;
        !           312:                        bool value_fields_by_params=value_fields_count!=0;
        !           313:                        if(!value_fields_by_params)
        !           314:                                value_fields_count=columns->size()-1; // all columns except key
        !           315:                        Array value_fields(pool, value_fields_count);
        !           316:                        if(value_fields_by_params) {
        !           317:                                for(int i=1; i<params->size(); i++) {
        !           318:                                        const String& value_field_name=params->get_no_junction(i,
        !           319:                                                "value field name must not be code").as_string();
        !           320:                                        value_fields+=table.column_name2index(value_field_name, true);
        !           321:                                }
        !           322:                        } else { // by all columns except key
        !           323:                                for(int i=0; i<columns->size(); i++)
        !           324:                                        if(i!=key_field)
        !           325:                                                value_fields+=i;
        !           326:                        }
        !           327: 
        !           328:                        // integers: key_field & value_fields
        !           329:                        Value& result=*new(pool) VHash(pool);
        !           330:                        Row_info row_info={&table, key_field, &value_fields, result.get_hash()};
        !           331:                        table.for_each(table_row_to_hash, &row_info);
        !           332:                        result.set_name(method_name);
        !           333:                        r.write_no_lang(result);
        !           334:                }
        !           335: }
        !           336: 
        !           337: /// used by table: _sort / sort_cmp_string|sort_cmp_double
1.34      paf       338: struct Seq_item {
                    339:        Array *row;
                    340:        union {
                    341:                char *c_str;
                    342:                double d;
                    343:        } value;
1.32      paf       344: };
1.34      paf       345: static int sort_cmp_string(const void *a, const void *b) {
                    346:        return strcmp(
                    347:                static_cast<const Seq_item *>(a)->value.c_str, 
                    348:                static_cast<const Seq_item *>(b)->value.c_str
                    349:        );
                    350: }
                    351: static int sort_cmp_double(const void *a, const void *b) {
                    352:        double va=static_cast<const Seq_item *>(a)->value.d;
                    353:        double vb=static_cast<const Seq_item *>(b)->value.d;
                    354:        if(va<vb)
                    355:                return -1;
                    356:        else if(va>vb)
                    357:                return +1;
                    358:        else 
                    359:                return 0;
                    360: }
1.61      paf       361: static void _sort(Request& r, const String& method_name, MethodParams *params) {
                    362:        Value& key_maker=params->get_junction(0, "key-maker must be code");
                    363: 
                    364:        bool reverse=params->size()==2/*..[asc|desc]*/?
                    365:                reverse=params->get_no_junction(1, "order must not be code").as_string()=="desc":
                    366:                false;
1.32      paf       367: 
                    368:        Table& table=static_cast<VTable *>(r.self)->table();
1.34      paf       369: 
                    370:        // anything to sort?
                    371:        if(!table.size())
                    372:                return;
                    373: 
                    374:        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*table.size());
                    375:        int i;
                    376: 
                    377:        // calculate key values
                    378:        bool key_values_are_strings=true;
                    379:        for(i=0; i<table.size(); i++) {
1.32      paf       380:                table.set_current(i);
                    381:                // calculate key value
1.61      paf       382:                seq[i].row=(MethodParams *)table.get(i);
1.34      paf       383:                Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
                    384:                if(i==0) // determining key values type by first one
                    385:                        key_values_are_strings=value.is_string();
                    386: 
                    387:                if(key_values_are_strings)
                    388:                        seq[i].value.c_str=value.as_string().cstr();
                    389:                else
                    390:                        seq[i].value.d=value.as_double();
1.32      paf       391:        }
                    392:        // sort keys
1.34      paf       393:        _qsort(seq, table.size(), sizeof(Seq_item), 
                    394:                key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf       395: 
1.34      paf       396:        // reorder table as they require in 'seq'
                    397:        for(i=0; i<table.size(); i++)
                    398:                table.put(i, seq[reverse?table.size()-1-i:i].row);
1.32      paf       399: 
1.34      paf       400:        // reset 'current'
1.32      paf       401:        table.set_current(0);
                    402: }
                    403: 
1.61      paf       404: static void _locate(Request& r, const String& method_name, MethodParams *params) {
1.72      paf       405:        Pool& pool=r.pool();
                    406: 
1.36      paf       407:        VTable& vtable=*static_cast<VTable *>(r.self);
                    408:        Table& table=vtable.table();
1.72      paf       409:        Value& result=*new(pool) VBool(pool, table.locate(
1.61      paf       410:                params->get(0).as_string(),
1.72      paf       411:                params->get(1).as_string()));
                    412:        result.set_name(method_name);
                    413:        r.write_no_lang(result);
1.37      paf       414: }
                    415: 
1.61      paf       416: static void _flip(Request& r, const String& method_name, MethodParams *params) {
1.39      paf       417:        Pool& pool=r.pool();
                    418:        VTable& vtable=*static_cast<VTable *>(r.self);
                    419: 
                    420:        Table& old_table=*vtable.get_table();
                    421:        Table& new_table=*new(pool) Table(pool, &method_name, 0/*nameless*/);
                    422:        if(old_table.size())
                    423:                if(int old_cols=old_table.at(0).size()) 
                    424:                        for(int column=0; column<old_cols; column++) {
                    425:                                Array& new_row=*new(pool) Array(pool, old_table.size());
                    426:                                for(int i=0; i<old_table.size(); i++) {
                    427:                                        const Array& old_row=old_table.at(i);
                    428:                                        new_row+=column<old_row.size()?old_row.get(column):empty_string;
                    429:                                }
                    430:                                new_table+=&new_row;
                    431:                        }
                    432: 
                    433:        vtable.set_table(new_table);
                    434: }
                    435: 
1.61      paf       436: static void _append(Request& r, const String& method_name, MethodParams *params) {
1.41      paf       437:        Pool& pool=r.pool();
                    438:        // data is last parameter
1.61      paf       439:        const String& string=
                    440:                r.process(params->get_junction(0, "body must be code")).as_string();
1.41      paf       441: 
                    442:        // parse cells
                    443:        Array& row=*new(pool) Array(pool);
1.46      paf       444:        string.split(row, 0, "\t", 1, String::UL_CLEAN);
1.41      paf       445: 
1.66      paf       446:        VTable& vtable=*static_cast<VTable *>(r.self);
                    447:        // disable ^a.menu{^a.append[]}
                    448:        vtable.lock();
                    449:        vtable.table()+=&row;
                    450:        vtable.unlock();
1.41      paf       451: }
                    452: 
1.61      paf       453: static void _join(Request& r, const String& method_name, MethodParams *params) {
1.42      paf       454:        Pool& pool=r.pool();
                    455: 
1.61      paf       456:        Table *maybe_src=params->get_no_junction(0, "table ref must not be code").get_table();
1.42      paf       457:        if(!maybe_src)
1.49      paf       458:                PTHROW(0, 0,
1.42      paf       459:                        &method_name,
                    460:                        "source is not a table");
                    461: 
                    462:        Table& src=*maybe_src;
                    463:        Table& dest=static_cast<VTable *>(r.self)->table();
                    464:        if(&src == &dest)
1.49      paf       465:                PTHROW(0, 0,
1.42      paf       466:                        &method_name,
                    467:                        "source and destination are same table");
                    468: 
                    469:        if(const Array *dest_columns=dest.columns()) { // dest is named
                    470:                int saved_src_current=src.current();
                    471:                for(int src_row=0; src_row<src.size(); src_row++) {
                    472:                        src.set_current(src_row);
                    473:                        Array& dest_row=*new(pool) Array(pool);
                    474:                        for(int dest_column=0; dest_column<dest_columns->size(); dest_column++) 
                    475:                                dest_row+=src.item(*dest_columns->get_string(dest_column));
                    476:                        dest+=&dest_row;
                    477:                }
                    478:                src.set_current(saved_src_current);
                    479:        } else { // dest is nameless
                    480:                for(int src_row=0; src_row<src.size(); src_row++)
                    481:                        dest+=&src.at(src_row);
                    482:        }
                    483: }
                    484: 
1.61      paf       485: static void _sql(Request& r, const String& method_name, MethodParams *params) {
1.49      paf       486:        Pool& pool=r.pool();
                    487: 
                    488:        if(!r.connection)
                    489:                PTHROW(0, 0,
                    490:                        &method_name,
                    491:                        "without connect");
                    492: 
1.61      paf       493:        Value& statement=params->get_junction(0, "statement must be code");
1.49      paf       494: 
1.53      paf       495:        ulong limit=0;
1.49      paf       496:        if(params->size()>1) {
1.61      paf       497:                Value& limit_code=params->get_junction(1, "limit must be expression");
1.53      paf       498:                limit=(uint)r.process(limit_code).as_double();
1.49      paf       499:        }
                    500: 
1.51      paf       501:        ulong offset=0;
1.49      paf       502:        if(params->size()>2) {
1.61      paf       503:                Value& offset_code=params->get_junction(2, "offset must be expression");
1.51      paf       504:                offset=(ulong)r.process(offset_code).as_double();
1.49      paf       505:        }
                    506: 
1.54      paf       507:        Temp_lang temp_lang(r, String::UL_SQL);
                    508:        const String& statement_string=r.process(statement).as_string();
1.55      paf       509:        const char *statement_cstr=
                    510:                statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
1.51      paf       511:        unsigned int sql_column_count; SQL_Driver::Cell *sql_columns;
                    512:        unsigned long sql_row_count; SQL_Driver::Cell **sql_rows;
1.52      paf       513:        bool need_rethrow=false; Exception rethrow_me;
                    514:        PTRY {
                    515:                r.connection->query(
1.53      paf       516:                        statement_cstr, offset, limit,
1.52      paf       517:                        &sql_column_count, &sql_columns,
                    518:                        &sql_row_count, &sql_rows);
                    519:        }
                    520:        PCATCH(e) { // connect/process problem
                    521:                rethrow_me=e;  need_rethrow=true;
                    522:        }
                    523:        PEND_CATCH
                    524:        if(need_rethrow)
                    525:                PTHROW(rethrow_me.type(), rethrow_me.code(),
1.53      paf       526:                        &statement_string, // setting more specific source [were url]
1.52      paf       527:                        rethrow_me.comment());
1.51      paf       528:        
                    529:        Array& table_columns=*new(pool) Array(pool);
                    530:        for(unsigned int i=0; i<sql_column_count; i++) {
                    531:                String& table_column=*new(pool) String(pool);
                    532:                table_column.APPEND_TAINTED(
                    533:                        (const char *)sql_columns[i].ptr, sql_columns[i].size,
                    534:                        statement_cstr, 0);
                    535:                table_columns+=&table_column;
                    536:        }
                    537: 
                    538:        Table& table=*new(pool) Table(pool, &method_name, &table_columns);
                    539: 
                    540:        {
                    541:                for(unsigned long r=0; r<sql_row_count; r++) {
                    542:                        SQL_Driver::Cell *sql_cells=sql_rows[r];
                    543:                        Array& table_row=*new(pool) Array(pool);
                    544:                        
                    545:                        for(unsigned int i=0; i<sql_column_count; i++) {
                    546:                                String& table_cell=*new(pool) String(pool);
                    547:                                table_cell.APPEND_TAINTED(
                    548:                                        (const char *)sql_cells[i].ptr, sql_cells[i].size,
                    549:                                        statement_cstr, r);
                    550:                                table_row+=&table_cell;
                    551:                        }
                    552:                        table+=&table_row;
                    553:                }
                    554:        }
1.49      paf       555: 
                    556:        // replace any previous table value
                    557:        static_cast<VTable *>(r.self)->set_table(table);
                    558: }
                    559: 
1.61      paf       560: static void _dir(Request& r, const String& method_name, MethodParams *params) {
1.58      paf       561:        Pool& pool=r.pool();
                    562: 
1.61      paf       563:        Value& relative_path=params->get_no_junction(0, "path must not be code");
1.58      paf       564: 
1.59      paf       565:        const String *regexp;
                    566:        pcre *regexp_code;
                    567:        int ovecsize;
                    568:        int *ovector;
                    569:        if(params->size()>1) {
1.61      paf       570:                regexp=&params->get_no_junction(1, "regexp must not be code").as_string();
1.59      paf       571: 
                    572:                const char *pattern=regexp->cstr(String::UL_AS_IS);
                    573:                const char *errptr;
                    574:                int erroffset;
                    575:                regexp_code=pcre_compile(pattern, PCRE_EXTRA | PCRE_DOTALL, 
                    576:                        &errptr, &erroffset,
1.63      paf       577:                        r.pcre_tables);
1.59      paf       578: 
                    579:                if(!regexp_code)
                    580:                        PTHROW(0, 0,
                    581:                                &regexp->mid(erroffset, regexp->size()),
                    582:                                "regular expression syntax error - %s", errptr);
                    583: 
                    584:                ovector=(int *)malloc(sizeof(int)*(ovecsize=(1/*match*/)*3));
                    585:        } else 
                    586:                regexp_code=0;
                    587: 
                    588: 
1.58      paf       589:        const char* absolute_path_cstr=r.absolute(relative_path.as_string())
                    590:                .cstr(String::UL_FILE_NAME);
                    591: 
                    592:        Array& columns=*new(pool) Array(pool);
                    593:        columns+=new(pool) String(pool, "name");        
                    594:        Table& table=*new(pool) Table(pool, &method_name, &columns);
                    595: 
                    596:        LOAD_DIR(absolute_path_cstr, 
                    597:                size_t file_name_size=strlen(ffblk.ff_name);
1.59      paf       598:                bool suits=true;
                    599:                if(regexp_code) {
                    600:                        int exec_result=pcre_exec(regexp_code, 0,
                    601:                                ffblk.ff_name, file_name_size, 0,
                    602:                                0, ovector, ovecsize);
                    603:                        
                    604:                        if(exec_result==PCRE_ERROR_NOMATCH)
                    605:                                suits=false;
                    606:                        else if(exec_result<0) {
                    607:                                (*pcre_free)(regexp_code);
                    608:                                PTHROW(0, 0,
                    609:                                        regexp,
1.60      paf       610:                                        "regular expression execute (%d)", 
1.59      paf       611:                                                exec_result);
                    612:                        }
                    613:                }
                    614: 
                    615:                if(suits) {
                    616:                        char *file_name_cstr=(char *)r.malloc(file_name_size);
                    617:                        memcpy(file_name_cstr, ffblk.ff_name, file_name_size);
                    618:                        String &file_name=*new(pool) String(pool);
1.69      paf       619:                        file_name.APPEND(file_name_cstr, file_name_size, String::UL_FILE_NAME,
1.59      paf       620:                                method_name.origin().file, method_name.origin().line);
                    621:                
                    622:                        Array& row=*new(pool) Array(pool);
                    623:                        row+=&file_name;
                    624:                        table+=&row;
                    625:                }
1.58      paf       626:        );
1.59      paf       627: 
                    628:        if(regexp_code)
                    629:                (*pcre_free)(regexp_code);
1.58      paf       630: 
                    631:        // replace any previous table value
                    632:        static_cast<VTable *>(r.self)->set_table(table);
                    633: }
                    634: 
1.66      paf       635: // constructor
                    636: 
                    637: MTable::MTable(Pool& apool) : Methoded(apool) {
                    638:        set_name(*NEW String(pool(), TABLE_CLASS_NAME));
                    639: 
1.49      paf       640:        // ^table:set{data}
                    641:        // ^table:set[nameless]{data}
1.66      paf       642:        add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2);
1.2       paf       643: 
1.49      paf       644:        // ^table:load[file]  
                    645:        // ^table:load[nameless;file]
1.66      paf       646:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.22      paf       647: 
                    648:        // ^table.save[file]  
                    649:        // ^table.save[nameless;file]
1.66      paf       650:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6       paf       651: 
                    652:        // ^table.count[]
1.66      paf       653:        add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6       paf       654: 
                    655:        // ^table.line[]
1.66      paf       656:        add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6       paf       657: 
1.10      paf       658:        // ^table.offset[]  
                    659:        // ^table.offset[offset]
1.66      paf       660:        add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1);
1.7       paf       661: 
1.10      paf       662:        // ^table.menu{code}  
                    663:        // ^table.menu{code}[delim]
1.66      paf       664:        add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.8       paf       665: 
1.10      paf       666:        // ^table.empty{code-when-empty}  
                    667:        // ^table.empty{code-when-empty}{code-when-not}
1.66      paf       668:        add_native_method("empty", Method::CT_DYNAMIC, _empty, 1, 2);
1.29      paf       669: 
                    670:        // ^table.record[]
1.66      paf       671:        add_native_method("record", Method::CT_DYNAMIC, _record, 0, 0);
1.74    ! paf       672: 
        !           673:        /** @fn _hash
        !           674:                ^table:hash[key field name]
        !           675:                ^table:hash[key field name][value field name;...]
        !           676:        */
        !           677:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 1000);
1.32      paf       678: 
                    679:        // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc]
                    680:        // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc]
1.66      paf       681:        add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8       paf       682: 
1.36      paf       683:        // ^table.locate[field;value]
1.66      paf       684:        add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2);
1.39      paf       685: 
                    686:        // ^table.flip[]
1.66      paf       687:        add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41      paf       688: 
                    689:        // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.66      paf       690:        add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42      paf       691: 
                    692:        // ^table.join[table]
1.66      paf       693:        add_native_method("join", Method::CT_DYNAMIC, _join, 1, 1);
1.49      paf       694: 
                    695: 
1.51      paf       696:        // ^table:sql[query][(count[;offset])]
1.66      paf       697:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3);
1.58      paf       698: 
                    699:        // ^table:dir[path]
                    700:        // ^table:dir[path][regexp]
1.66      paf       701:        add_native_method("dir", Method::CT_DYNAMIC, _dir, 1, 2);
                    702: }
                    703: 
                    704: // global variable
                    705: 
                    706: Methoded *table_class;
                    707: 
                    708: // creator
                    709: 
                    710: Methoded *MTable_create(Pool& pool) {
                    711:        return table_class=new(pool) MTable(pool);
1.40      paf       712: }

E-mail: