|
|
| version 1.14, 2001/03/19 16:44:02 | version 1.22, 2001/03/28 09:01:22 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser | Parser: table class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) |
| $Id$ | $Id$ |
| Line 12 | Line 14 |
| #include "pa_pool.h" | #include "pa_pool.h" |
| Table::Table(Pool& apool, | Table::Table(Pool& apool, |
| const String& asource, | const String *aorigin_string, |
| Array *acolumns, | Array *acolumns, |
| int initial_rows) : | int initial_rows) : |
| Array(apool, initial_rows), | Array(apool, initial_rows), |
| fsource(asource), | forigin_string(aorigin_string), |
| fcurrent(0), | fcurrent(0), |
| fcolumns(acolumns), | fcolumns(acolumns), |
| name2number(pool(), false) { | name2number(pool(), false) { |
| Line 29 Table::Table(Pool& apool, | Line 31 Table::Table(Pool& apool, |
| } | } |
| } | } |
| const Array &Table::at(int index) { | int Table::column_name2index(const String& column_name) const { |
| return *static_cast<const Array *>(get(index)); | |
| } | |
| const String *Table::item(const String& column_name) { | |
| int column_index; | |
| if(fcolumns) { // named | if(fcolumns) { // named |
| int found_index=name2number.get_int(column_name); | int column_number=name2number.get_int(column_name); |
| if(found_index) | if(column_number) |
| column_index=found_index-1; | return column_number-1; |
| else | else { |
| THROW(0, 0, | THROW(0, 0, |
| &column_name, | &column_name, |
| "column not found"); | "column not found"); |
| } else { // nameless | return 0; // unreached |
| column_index=atoi(column_name.cstr()); | } |
| if(!valid(fcurrent)) | } else // nameless |
| return 0; // it's OK we don't have row, just return nothing | return atoi(column_name.cstr()); |
| } | |
| const String *Table::item(int column) const { | |
| if(valid(fcurrent)) { | |
| const Array& row=at(fcurrent); | const Array& row=at(fcurrent); |
| if(column_index<0 || column_index>=row.size()) // read past proper index? | if(column>=0 && column<row.size()) // proper index? |
| return 0; // it's OK we don't have column, just return nothing | return row.get_string(column); |
| } | |
| return 0; // it's OK we don't have row|column, just return nothing | |
| } | |
| bool Table::locate(const String& column, const String& value) { | |
| int key_index=column_name2index(column); | |
| for(fcurrent=0; fcurrent<size(); fcurrent++) { | |
| const String *item_value=item(key_index); | |
| if(item_value && *item_value==value) | |
| return true; | |
| } | } |
| return item(column_index); | fcurrent=0; |
| return false; | |
| } | |
| void Table::shift(int offset) { | |
| if(size()) | |
| fcurrent=(fcurrent+offset+size())%size(); | |
| } | } |