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

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.28    ! paf         8:        $Id: pa_table.C,v 1.27 2001/05/07 08:29:45 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"
1.25      paf        15: #include "pa_exception.h"
1.1       paf        16: 
1.12      paf        17: Table::Table(Pool& apool, 
1.18      paf        18:                         const String *aorigin_string,
1.1       paf        19:                         Array *acolumns, 
                     20:                         int initial_rows) :
1.12      paf        21:        Array(apool, initial_rows),
1.13      paf        22: 
1.18      paf        23:        forigin_string(aorigin_string),
1.5       paf        24:        fcurrent(0),
                     25:        fcolumns(acolumns), 
1.26      paf        26:        name2number(pool()) {
1.1       paf        27: 
1.5       paf        28:        if(fcolumns)
                     29:                for(int i=0; i<fcolumns->size(); i++) {
1.12      paf        30:                        const String& name=*fcolumns->get_string(i);
1.2       paf        31:                        name2number.put(name, i+1);
1.1       paf        32:                }
1.2       paf        33: }
                     34: 
1.21      paf        35: int Table::column_name2index(const String& column_name) const {
1.27      paf        36:        if(fcolumns) // named
                     37:                return name2number.get_int(column_name)-1; // -1 = column not found
                     38:        else { // nameless
1.24      paf        39:                char *error_pos=0;
                     40:                int result=(int)strtol(column_name.cstr(), &error_pos, 0);
                     41:                if(error_pos && *error_pos)
                     42:                        THROW(0, 0,
                     43:                                &column_name,
                     44:                                "invalid column number");
                     45:                return result;
                     46:        }
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.23      paf        58: bool Table::locate(int column, const String& value) {
1.22      paf        59:        for(fcurrent=0; fcurrent<size(); fcurrent++) {
1.23      paf        60:                const String *item_value=item(column);
1.21      paf        61:                if(item_value && *item_value==value)
                     62:                        return true;
1.2       paf        63:        }
                     64: 
1.21      paf        65:        fcurrent=0;
                     66:        return false;
1.28    ! paf        67: }
        !            68: 
        !            69: bool Table::locate(const String& column, const String& value) {
        !            70:        int index=column_name2index(column);
        !            71:        if(index<0)
        !            72:                THROW(0, 0,
        !            73:                        &column,
        !            74:                        "column not found (locate)");
        !            75:        return locate(index, value);
1.22      paf        76: }
                     77: 
                     78: void Table::shift(int offset) {
                     79:        if(size())
                     80:                fcurrent=(fcurrent+offset+size())%size();
1.1       paf        81: }

E-mail: