Annotation of parser3/src/main/pa_table.C, revision 1.53.2.10.2.5
1.14 paf 1: /** @file
1.15 paf 2: Parser: table class.
3:
1.53.2.8 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.10.2.5! (paf 8:: static const char* IDENT_TABLE_C="$Date: 2003/04/02 08:35:43 $";
1.2 paf 9:
1.1 paf 10: #include "pa_table.h"
1.53.2.10.2.1 (paf 11::
1.25 paf 12: #include "pa_exception.h"
1.1 paf 13:
1.53.2.10.2.2 (paf 14:: Table::Table(columns_type acolumns, int initial_rows):
1.53.2.1 paf 15: Array<element_type>(initial_rows),
1.13 paf 16:
1.5 paf 17: fcurrent(0),
18: fcolumns(acolumns),
1.53.2.10.2.2 (paf 19:: name2number(new name2number_hash_class) {
1.1 paf 20:
1.53.2.10.2.3 (paf 21:: if(fcolumns) {
22:: size_t fcolumns_count=fcolumns->count();
1.53.2.10.2.5! (paf 23:: size_t number=1;
! 24:: for(Array_iterator<const String*> i(*fcolumns); i.has_next(); ) {
! 25:: const String& name=*i.next();
! 26:: name2number->put(name, number++);
1.1 paf 27: }
1.53.2.10.2.3 (paf 28:: }
1.42 paf 29: }
30:
1.53.2.1 paf 31: Table::Table(const Table& src, int offset, int limit) :
32: Array<element_type>(limit/*may be more than needed, no harm done*/),
1.42 paf 33:
1.51 paf 34: fcurrent(0),
1.53.2.1 paf 35: fcolumns(src.fcolumns),
36: name2number(src.name2number) {
1.42 paf 37:
1.53.2.1 paf 38: append(src, offset, limit);
1.2 paf 39: }
40:
1.53.2.10.2.1 (paf 41:: int Table::column_name2index(const String& column_name, bool bark) const {
1.53.2.10.2.2 (paf 42:: if(fcolumns) {// named
43:: int result=name2number->get(column_name)-1; // -1 = column not found
1.29 paf 44: if(bark && result<0)
1.45 paf 45: throw Exception("parser.runtime",
1.53.2.10.2.2 (paf 46:: &column_name,
1.29 paf 47: "column not found");
48: return result;
1.53.2.2 paf 49: } else // nameless
1.53.2.10.2.2 (paf 50:: return column_name.as_int();
1.21 paf 51: }
52:
1.53.2.10.2.3 (paf 53:: const String* Table::item(size_t column) {
1.21 paf 54: if(valid(fcurrent)) {
1.53.2.6 paf 55: element_type row=get(fcurrent);
1.53.2.2 paf 56: if(column>=0 && column<row->count()) // proper index?
1.53.2.10.2.4 (paf 57:: return row->get(column);
1.21 paf 58: }
1.53.2.10.2.1 (paf 59:: return 0; // it's OK we don't have row|column, just return nothing
1.21 paf 60: }
61:
1.53.2.10.2.3 (paf 62:: bool Table::locate(size_t column, const String& value) {
63:: size_t scurrent=fcurrent;
64:: size_t lcount=count();
65:: for(fcurrent=0; fcurrent<lcount; fcurrent++) {
1.53.2.10.2.2 (paf 66:: const String* item_value=item(column);
67:: if(*item_value == value)
1.21 paf 68: return true;
1.2 paf 69: }
70:
1.31 parser 71: fcurrent=scurrent;
1.21 paf 72: return false;
1.28 paf 73: }
74:
1.53.2.10.2.1 (paf 75:: bool Table::locate(const String& column, const String& value) {
1.29 paf 76: return locate(column_name2index(column, true), value);
1.22 paf 77: }
78:
1.41 paf 79: void Table::offset(bool absolute, int offset) {
1.53.2.10.2.3 (paf 80:: if(size_t lcount=count())
81:: fcurrent=((absolute?0:fcurrent)+offset+lcount)%lcount;
1.1 paf 82: }
E-mail: