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

1.14      paf         1: /** @file
1.15      paf         2:        Parser: table class.
                      3: 
1.10      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.15      paf         5: 
1.11      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.10      paf         7: 
1.22    ! paf         8:        $Id: pa_table.C,v 1.21 2001/03/28 08:01:42 paf Exp $
1.1       paf         9: */
                     10: 
1.2       paf        11: #include <stdlib.h>
                     12: 
1.1       paf        13: #include "pa_table.h"
                     14: #include "pa_pool.h"
                     15: 
1.12      paf        16: Table::Table(Pool& apool, 
1.18      paf        17:                         const String *aorigin_string,
1.1       paf        18:                         Array *acolumns, 
                     19:                         int initial_rows) :
1.12      paf        20:        Array(apool, initial_rows),
1.13      paf        21: 
1.18      paf        22:        forigin_string(aorigin_string),
1.5       paf        23:        fcurrent(0),
                     24:        fcolumns(acolumns), 
1.12      paf        25:        name2number(pool(), false) {
1.1       paf        26: 
1.5       paf        27:        if(fcolumns)
                     28:                for(int i=0; i<fcolumns->size(); i++) {
1.12      paf        29:                        const String& name=*fcolumns->get_string(i);
1.2       paf        30:                        name2number.put(name, i+1);
1.1       paf        31:                }
1.2       paf        32: }
                     33: 
1.21      paf        34: int Table::column_name2index(const String& column_name) const {
1.12      paf        35:        if(fcolumns) { // named
1.22    ! paf        36:                int column_number=name2number.get_int(column_name);
        !            37:                if(column_number)
        !            38:                        return column_number-1;
        !            39:                else {
1.9       paf        40:                        THROW(0, 0,
1.6       paf        41:                                &column_name, 
                     42:                                "column not found");
1.22    ! paf        43:                        return 0; // unreached
        !            44:                }
        !            45:        } else // nameless
        !            46:                return atoi(column_name.cstr());
1.21      paf        47: }
                     48: 
1.22    ! paf        49: const String *Table::item(int column) const {
1.21      paf        50:        if(valid(fcurrent)) {
1.7       paf        51:                const Array& row=at(fcurrent);
1.22    ! paf        52:                if(column>=0 && column<row.size()) // proper index?
        !            53:                        return row.get_string(column);
1.21      paf        54:        }
                     55:        return 0; // it's OK we don't have row|column, just return nothing
                     56: }
                     57: 
1.22    ! paf        58: bool Table::locate(const String& column, const String& value) {
1.21      paf        59:        int key_index=column_name2index(column);
1.22    ! paf        60:        for(fcurrent=0; fcurrent<size(); fcurrent++) {
1.21      paf        61:                const String *item_value=item(key_index);
                     62:                if(item_value && *item_value==value)
                     63:                        return true;
1.2       paf        64:        }
                     65: 
1.21      paf        66:        fcurrent=0;
                     67:        return false;
1.22    ! paf        68: }
        !            69: 
        !            70: void Table::shift(int offset) {
        !            71:        if(size())
        !            72:                fcurrent=(fcurrent+offset+size())%size();
1.1       paf        73: }

E-mail: