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

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

E-mail: