Annotation of parser3/src/main/pa_table.C, revision 1.7

1.1       paf         1: /*
1.7     ! paf         2:   $Id: pa_table.C,v 1.6 2001/01/30 13:07:31 paf Exp $
1.1       paf         3: */
                      4: 
1.2       paf         5: #include <stdlib.h>
                      6: 
1.1       paf         7: #include "pa_table.h"
                      8: #include "pa_pool.h"
                      9: 
1.2       paf        10: Table::Table(Request& arequest, 
1.1       paf        11:                         char *afile, uint aline, 
                     12:                         Array *acolumns, 
                     13:                         int initial_rows) :
1.6       paf        14:        Array(arequest.pool(), initial_rows),
1.2       paf        15:        request(arequest),
1.5       paf        16:        fcurrent(0),
                     17:        fcolumns(acolumns), 
1.6       paf        18:        name2number(arequest.pool(), false) {
1.1       paf        19: #ifndef NO_STRING_ORIGIN
1.5       paf        20:        forigin.file=afile;
                     21:        forigin.line=aline;
1.1       paf        22: #endif
                     23: 
1.5       paf        24:        if(fcolumns)
                     25:                for(int i=0; i<fcolumns->size(); i++) {
1.1       paf        26:                        String name(pool);
1.5       paf        27:                        name.APPEND(fcolumns->get_cstr(i), 0, 0);
1.2       paf        28:                        name2number.put(name, i+1);
1.1       paf        29:                }
1.2       paf        30: }
                     31: 
1.7     ! paf        32: const Array &Table::at(int index) {
1.2       paf        33:        if(index<0 || index>=size())
1.6       paf        34:                request.exception().raise(0, 0, 
                     35:                        0,
1.7     ! paf        36:                        "table row index %d is out of range [0..%d]", 
1.2       paf        37:                        index, size()-1);
                     38:        
1.7     ! paf        39:        return *static_cast<const Array *>(get(index));
1.2       paf        40: }
                     41: 
1.3       paf        42: const char *Table::item(int index) {
1.7     ! paf        43:        const Array& row=at(fcurrent);
        !            44:        return row.get_cstr(index);
1.2       paf        45: }
                     46: 
1.5       paf        47: const char *Table::item(const String& column_name) {
1.2       paf        48:        int column_index;
1.5       paf        49:        if(fcolumns) {
1.2       paf        50:                int found_index=name2number.get_int(column_name);
                     51:                if(found_index)
                     52:                        column_index=found_index-1;
                     53:                else
1.6       paf        54:                        request.exception().raise(0, 0,
                     55:                                &column_name, 
                     56:                                "column not found");
1.2       paf        57:        } else {
                     58:                column_index=atoi(column_name.cstr());
1.7     ! paf        59:                const Array& row=at(fcurrent);
        !            60:                if(column_index<0 || column_index>=row.size())
        !            61:                        return 0;
        !            62:                /*
1.6       paf        63:                        request.exception().raise(0, 0,
                     64:                                &column_name, 
1.2       paf        65:                                "table column index %d is out of range [0..%d]", 
                     66:                                column_index, row->size()-1);
1.7     ! paf        67:                                */
1.2       paf        68:        }
                     69: 
                     70:        return item(column_index);
1.1       paf        71: }

E-mail: