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

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

E-mail: