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

1.14      paf         1: /** @file
1.15      paf         2:        Parser: table class.
                      3: 
1.53      paf         4:        Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.44      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.48      paf         6: */
1.15      paf         7: 
1.53.2.7! paf         8: static const char* IDENT_TABLE_C="$Date: 2003/01/30 11:00:41 $";
1.2       paf         9: 
1.1       paf        10: #include "pa_table.h"
                     11: #include "pa_pool.h"
1.25      paf        12: #include "pa_exception.h"
1.1       paf        13: 
1.53.2.1  paf        14: Table::Table(
1.53.2.7! paf        15:                         StringPtr aorigin_string,
1.53.2.4  paf        16:                         columns_type acolumns,
1.53.2.1  paf        17:                         int initial_rows):
                     18:        Array<element_type>(initial_rows),
1.13      paf        19: 
1.18      paf        20:        forigin_string(aorigin_string),
1.5       paf        21:        fcurrent(0),
                     22:        fcolumns(acolumns), 
1.53.2.1  paf        23:        name2number(object_ptr<name2number_hash_class>(new name2number_hash_class)) {
1.1       paf        24: 
1.53.2.1  paf        25:        if(fcolumns.get())
                     26:                for(int i=0; i<fcolumns->count(); i++) {
1.53.2.2  paf        27:                        const String& name=*fcolumns->get(i).get();
1.53.2.1  paf        28:                        name2number->put(name, i+1);
1.1       paf        29:                }
1.42      paf        30: }
                     31: 
1.53.2.1  paf        32: Table::Table(const Table& src, int offset, int limit) :
                     33:        Array<element_type>(limit/*may be more than needed, no harm done*/),
1.42      paf        34: 
1.53.2.1  paf        35:        forigin_string(src.forigin_string),
1.51      paf        36:        fcurrent(0),
1.53.2.1  paf        37:        fcolumns(src.fcolumns),
                     38:        name2number(src.name2number) {
1.42      paf        39: 
1.53.2.1  paf        40:        append(src, offset, limit);
1.2       paf        41: }
                     42: 
1.53.2.7! paf        43: int Table::column_name2index(StringPtr column_name, bool bark) const {
1.53.2.1  paf        44:        if(fcolumns.get()) {// named
                     45:                int result=name2number->get(*column_name.get())-1; // -1 = column not found
1.29      paf        46:                if(bark && result<0)
1.45      paf        47:                        throw Exception("parser.runtime",
1.53.2.1  paf        48:                                column_name,
1.29      paf        49:                                "column not found");
                     50:                return result;
1.53.2.2  paf        51:        } else // nameless
                     52:                return column_name->as_int();
1.21      paf        53: }
                     54: 
1.53.2.7! paf        55: StringPtr Table::item(int column) {
1.21      paf        56:        if(valid(fcurrent)) {
1.53.2.6  paf        57:                element_type row=get(fcurrent);
1.53.2.2  paf        58:                if(column>=0 && column<row->count()) // proper index?
                     59:                        return row->get(column);
1.21      paf        60:        }
                     61:        return 0; // it's OK we don't have row|column, just return nothing
                     62: }
                     63: 
1.53.2.7! paf        64: bool Table::locate(int column, StringPtr value) {
1.31      parser     65:        int scurrent=fcurrent;
1.53.2.2  paf        66:        for(fcurrent=0; fcurrent<count(); fcurrent++) {
1.53.2.7! paf        67:                StringPtr item_value=item(column);
1.53.2.2  paf        68:                if(item_value.get() == value.get())
1.21      paf        69:                        return true;
1.2       paf        70:        }
                     71: 
1.31      parser     72:        fcurrent=scurrent;
1.21      paf        73:        return false;
1.28      paf        74: }
                     75: 
1.53.2.7! paf        76: bool Table::locate(StringPtr column, StringPtr value) {
1.29      paf        77:        return locate(column_name2index(column, true), value);
1.22      paf        78: }
                     79: 
1.41      paf        80: void Table::offset(bool absolute, int offset) {
1.53.2.2  paf        81:        if(count())
                     82:                fcurrent=((absolute?0:fcurrent)+offset+count())%count();
1.1       paf        83: }

E-mail: