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

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.32    ! parser      8:        $Id: pa_table.C,v 1.31 2001/05/17 15:57:35 parser Exp $
1.1       paf         9: */
1.32    ! parser     10: static char *RCSId="$Id$"; 
1.1       paf        11: 
1.2       paf        12: #include <stdlib.h>
                     13: 
1.1       paf        14: #include "pa_table.h"
                     15: #include "pa_pool.h"
1.25      paf        16: #include "pa_exception.h"
1.1       paf        17: 
1.12      paf        18: Table::Table(Pool& apool, 
1.18      paf        19:                         const String *aorigin_string,
1.1       paf        20:                         Array *acolumns, 
                     21:                         int initial_rows) :
1.12      paf        22:        Array(apool, initial_rows),
1.13      paf        23: 
1.18      paf        24:        forigin_string(aorigin_string),
1.5       paf        25:        fcurrent(0),
                     26:        fcolumns(acolumns), 
1.26      paf        27:        name2number(pool()) {
1.1       paf        28: 
1.5       paf        29:        if(fcolumns)
                     30:                for(int i=0; i<fcolumns->size(); i++) {
1.12      paf        31:                        const String& name=*fcolumns->get_string(i);
1.2       paf        32:                        name2number.put(name, i+1);
1.1       paf        33:                }
1.2       paf        34: }
                     35: 
1.29      paf        36: int Table::column_name2index(const String& column_name, bool bark) const {
                     37:        if(fcolumns) {// named
                     38:                int result=name2number.get_int(column_name)-1; // -1 = column not found
                     39:                if(bark && result<0)
                     40:                        THROW(0, 0,
                     41:                                &column_name,
                     42:                                "column not found");
                     43:                return result;
                     44:        } else { // nameless
1.24      paf        45:                char *error_pos=0;
                     46:                int result=(int)strtol(column_name.cstr(), &error_pos, 0);
1.30      paf        47:                if(error_pos && *error_pos) {
                     48:                        result=-1;
                     49:                        if(bark)
                     50:                                THROW(0, 0,
                     51:                                        &column_name,
                     52:                                        "invalid column number");
                     53:                }
1.24      paf        54:                return result;
                     55:        }
1.21      paf        56: }
                     57: 
1.22      paf        58: const String *Table::item(int column) const {
1.21      paf        59:        if(valid(fcurrent)) {
1.7       paf        60:                const Array& row=at(fcurrent);
1.22      paf        61:                if(column>=0 && column<row.size()) // proper index?
                     62:                        return row.get_string(column);
1.21      paf        63:        }
                     64:        return 0; // it's OK we don't have row|column, just return nothing
                     65: }
                     66: 
1.23      paf        67: bool Table::locate(int column, const String& value) {
1.31      parser     68:        int scurrent=fcurrent;
1.22      paf        69:        for(fcurrent=0; fcurrent<size(); fcurrent++) {
1.23      paf        70:                const String *item_value=item(column);
1.21      paf        71:                if(item_value && *item_value==value)
                     72:                        return true;
1.2       paf        73:        }
                     74: 
1.31      parser     75:        fcurrent=scurrent;
1.21      paf        76:        return false;
1.28      paf        77: }
                     78: 
                     79: bool Table::locate(const String& column, const String& value) {
1.29      paf        80:        return locate(column_name2index(column, true), value);
1.22      paf        81: }
                     82: 
                     83: void Table::shift(int offset) {
                     84:        if(size())
                     85:                fcurrent=(fcurrent+offset+size())%size();
1.1       paf        86: }

E-mail: