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

1.20      paf         1: /** @file
                      2:        Parser: table parser class.
                      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.21    ! paf         8:        $Id: table.C,v 1.20 2001/03/19 22:11:07 paf Exp $
1.1       paf         9: */
                     10: 
1.16      paf        11: #include "pa_common.h"
1.1       paf        12: #include "pa_request.h"
                     13: #include "_table.h"
                     14: #include "pa_vtable.h"
1.6       paf        15: #include "pa_vint.h"
1.1       paf        16: 
                     17: // global var
                     18: 
1.14      paf        19: VStateless_class *table_class;
1.1       paf        20: 
                     21: // methods
                     22: 
1.2       paf        23: static void set_or_load(
                     24:                                                Request& r, 
                     25:                                                const String& method_name, Array *params, 
                     26:                                                bool is_load) {
1.1       paf        27:        Pool& pool=r.pool();
                     28:        // data is last parameter
1.21    ! paf        29:        Value *vdata_or_filename=static_cast<Value *>(params->get(params->size()-1));
1.4       paf        30:        // forcing
1.21    ! paf        31:        // ^load[this file name type]
1.17      paf        32:        // ^set{this body type}
1.21    ! paf        33:        r.fail_if_junction_(is_load, *vdata_or_filename, 
        !            34:                method_name, is_load?"file name must not be junction":"body must be junction");
1.1       paf        35: 
1.2       paf        36:        // data or file_name
1.21    ! paf        37:        char *ldata_or_filename;
        !            38:        if(is_load) {
        !            39:                // forcing untaint language
        !            40:                String lfile_name(pool);
        !            41:                lfile_name.append(vdata_or_filename->as_string(),
        !            42:                        String::Untaint_lang::FILE_NAME, true);
        !            43:                ldata_or_filename=lfile_name.cstr();
        !            44:        } else {
        !            45:                // suggesting untaint language
        !            46:                Temp_lang temp_lang(r, String::Untaint_lang::TABLE);
        !            47:                ldata_or_filename=r.process(*vdata_or_filename).as_string().cstr();
1.17      paf        48:        }
1.2       paf        49:        // data
1.21    ! paf        50:        char *data=is_load?
        !            51:                file_read(pool, r.absolute(ldata_or_filename)/*\, false*/):ldata_or_filename;
1.1       paf        52: 
                     53:        // parse columns
                     54:        Array *columns;
                     55: #ifndef NO_STRING_ORIGIN
                     56:        const Origin& origin=method_name.origin();
1.2       paf        57:        const char *file=origin.file;
1.1       paf        58:        uint line=origin.line;
                     59: #endif
                     60:        if(params->size()==2) {
                     61:                columns=0;
                     62:        } else {
                     63:                columns=new(pool) Array(pool);
                     64: 
                     65:                if(char *row_chars=getrow(&data)) 
                     66:                        do {
                     67:                                String *name=new(pool) String(pool);
                     68:                                name->APPEND(lsplit(&row_chars, '\t'), 0, file, line++);
                     69:                                *columns+=name;
                     70:                        } while(row_chars);
                     71:        }
                     72: 
                     73:        // parse cells
                     74:        Table& table=*new(pool) Table(pool, method_name, columns);
                     75:        char *row_chars;
                     76:        while(row_chars=getrow(&data)) {
                     77:                Array *row=new(pool) Array(pool);
                     78:                while(char *cell_chars=lsplit(&row_chars, '\t')) {
                     79:                        String *cell=new(pool) String(pool);
                     80:                        cell->APPEND(cell_chars, 0, file, line);
                     81:                        *row+=cell;
                     82:                }
                     83:                line++;
                     84:                table+=row;
                     85:        };
                     86: 
                     87:        // replace any previous table value
1.18      paf        88:        static_cast<VTable *>(r.self)->set_table(table);
1.1       paf        89: }
                     90: 
1.2       paf        91: 
                     92: static void _set(Request& r, const String& method_name, Array *params) {
                     93:        set_or_load(r, method_name, params, false);
                     94: }
                     95: 
                     96: static void _load(Request& r, const String& method_name, Array *params) {
                     97:        set_or_load(r, method_name, params, true);
                     98: }
                     99: 
1.6       paf       100: static void _count(Request& r, const String&, Array *) {
                    101:        Pool& pool=r.pool();
1.18      paf       102:        Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.15      paf       103:        r.write_no_lang(value);
1.6       paf       104: }
                    105: 
                    106: static void _line(Request& r, const String&, Array *) {
                    107:        Pool& pool=r.pool();
1.18      paf       108:        Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().get_current());
1.15      paf       109:        r.write_no_lang(value);
1.6       paf       110: }
                    111: 
                    112: static void _offset(Request& r, const String&, Array *params) {
                    113:        Pool& pool=r.pool();
1.18      paf       114:        Table& table=static_cast<VTable *>(r.self)->table();
1.6       paf       115:        if(params->size()) {
                    116:                if(int size=table.size()) {
1.9       paf       117:                        int offset=
                    118:                                (int)r.process(*static_cast<Value *>(params->get(0))).get_double();
1.6       paf       119:                        table.set_current((table.get_current()+offset+size)%size);
                    120:                }
                    121:        } else {
                    122:                Value& value=*new(pool) VInt(pool, table.get_current());
1.15      paf       123:                r.write_no_lang(value);
1.6       paf       124:        }
                    125: }
                    126: 
1.7       paf       127: static void _menu(Request& r, const String& method_name, Array *params) {
                    128:        Value& body_code=*static_cast<Value *>(params->get(0));
                    129:        // forcing ^menu{this param type}
                    130:        r.fail_if_junction_(false, body_code, 
                    131:                method_name, "body must be junction");
                    132:        
                    133:        Value *delim_code=params->size()==2?static_cast<Value *>(params->get(1)):0;
                    134: 
1.18      paf       135:        Table& table=static_cast<VTable *>(r.self)->table();
1.7       paf       136:        bool need_delim=false;
                    137:        for(int i=0; i<table.size(); i++) {
                    138:                table.set_current(i);
                    139: 
1.12      paf       140:                Value& processed_body=r.process(body_code);
1.7       paf       141:                if(delim_code) { // delimiter set?
                    142:                        const String *string=processed_body.get_string();
                    143:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    144:                                r.write_pass_lang(r.process(*delim_code));
                    145:                        need_delim=true;
                    146:                }
                    147:                r.write_pass_lang(processed_body);
                    148:        }
                    149: }
                    150: 
1.8       paf       151: static void _empty(Request& r, const String&, Array *params) {
1.18      paf       152:        Table& table=static_cast<VTable *>(r.self)->table();
1.8       paf       153:        if(table.size()==0) {
                    154:                Value& value=r.process(*static_cast<Value *>(params->get(0)));
                    155:                r.write_pass_lang(value);
                    156:        } else if(params->size()==2) {
                    157:                Value& value=r.process(*static_cast<Value *>(params->get(1)));
                    158:                r.write_pass_lang(value);
                    159:        }
                    160: }
1.15      paf       161: 
                    162: // initialize
1.8       paf       163: 
1.14      paf       164: void initialize_table_class(Pool& pool, VStateless_class& vclass) {
1.10      paf       165:        // ^table.set[data]  
                    166:        // ^table.set[nameless;data]
1.1       paf       167:        vclass.add_native_method("set", _set, 1, 2);
1.2       paf       168: 
1.10      paf       169:        // ^table.load[file]  
                    170:        // ^table.load[nameless;file]
1.2       paf       171:        vclass.add_native_method("load", _load, 1, 2);
1.6       paf       172: 
                    173:        // ^table.count[]
                    174:        vclass.add_native_method("count", _count, 0, 0);
                    175: 
                    176:        // ^table.line[]
                    177:        vclass.add_native_method("line", _line, 0, 0);
                    178: 
1.10      paf       179:        // ^table.offset[]  
                    180:        // ^table.offset[offset]
1.6       paf       181:        vclass.add_native_method("offset", _offset, 0, 1);
1.7       paf       182: 
1.10      paf       183:        // ^table.menu{code}  
                    184:        // ^table.menu{code}[delim]
1.7       paf       185:        vclass.add_native_method("menu", _menu, 1, 2);
1.8       paf       186: 
1.10      paf       187:        // ^table.empty{code-when-empty}  
                    188:        // ^table.empty{code-when-empty}{code-when-not}
1.8       paf       189:        vclass.add_native_method("empty", _empty, 1, 2);
                    190: 
1.1       paf       191: }      

E-mail: