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

1.14      paf         1: /** @file
1.15      paf         2:        Parser: table class.
                      3: 
1.43      paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.44    ! paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.15      paf         6: 
1.44    ! paf         7:        $Id: pa_table.C,v 1.43 2002/02/08 07:27:49 paf Exp $
1.1       paf         8: */
                      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.42      paf        32: }
                     33: 
                     34: Table::Table(const Table& source)  :
                     35:        Array(source),
                     36: 
                     37:        forigin_string(source.forigin_string),
                     38:        fcurrent(source.fcurrent),
                     39:        fcolumns(source.fcolumns),
                     40:        name2number(source.name2number) {
                     41: 
1.2       paf        42: }
                     43: 
1.29      paf        44: int Table::column_name2index(const String& column_name, bool bark) const {
                     45:        if(fcolumns) {// named
                     46:                int result=name2number.get_int(column_name)-1; // -1 = column not found
                     47:                if(bark && result<0)
1.39      parser     48:                        throw Exception(0, 0,
1.29      paf        49:                                &column_name,
                     50:                                "column not found");
                     51:                return result;
                     52:        } else { // nameless
1.36      parser     53:                char *error_pos;
                     54:                const char *cstr=column_name.cstr();
                     55:                int result=(int)strtol(cstr, &error_pos, 0);
1.37      parser     56:                if(*error_pos/*not EOS*/) {
                     57:                        result=-1;
1.30      paf        58:                        if(bark)
1.39      parser     59:                                throw Exception(0, 0,
1.30      paf        60:                                        &column_name,
                     61:                                        "invalid column number");
                     62:                }
1.24      paf        63:                return result;
                     64:        }
1.21      paf        65: }
                     66: 
1.22      paf        67: const String *Table::item(int column) const {
1.21      paf        68:        if(valid(fcurrent)) {
1.7       paf        69:                const Array& row=at(fcurrent);
1.22      paf        70:                if(column>=0 && column<row.size()) // proper index?
                     71:                        return row.get_string(column);
1.21      paf        72:        }
                     73:        return 0; // it's OK we don't have row|column, just return nothing
                     74: }
                     75: 
1.23      paf        76: bool Table::locate(int column, const String& value) {
1.31      parser     77:        int scurrent=fcurrent;
1.22      paf        78:        for(fcurrent=0; fcurrent<size(); fcurrent++) {
1.23      paf        79:                const String *item_value=item(column);
1.21      paf        80:                if(item_value && *item_value==value)
                     81:                        return true;
1.2       paf        82:        }
                     83: 
1.31      parser     84:        fcurrent=scurrent;
1.21      paf        85:        return false;
1.28      paf        86: }
                     87: 
                     88: bool Table::locate(const String& column, const String& value) {
1.29      paf        89:        return locate(column_name2index(column, true), value);
1.22      paf        90: }
                     91: 
1.41      paf        92: void Table::offset(bool absolute, int offset) {
1.22      paf        93:        if(size())
1.41      paf        94:                fcurrent=((absolute?0:fcurrent)+offset+size())%size();
1.1       paf        95: }

E-mail: