|
|
| version 1.13.2.1, 2001/03/12 13:37:48 | version 1.46.2.1, 2002/05/07 07:23:11 |
|---|---|
| Line 1 | Line 1 |
| /* | /** @file |
| Parser | Parser: table class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| $Id$ | $Id$ |
| */ | */ |
| Line 10 | Line 11 |
| #include "pa_table.h" | #include "pa_table.h" |
| #include "pa_pool.h" | #include "pa_pool.h" |
| #include "pa_exception.h" | |
| Table::Table(Pool& apool, | Table::Table(Pool& apool, |
| Array *acolumns, | const String *aorigin_string, |
| const Array *acolumns, | |
| int initial_rows) : | int initial_rows) : |
| Array(apool, initial_rows), | Array(apool, initial_rows), |
| forigin_string(aorigin_string), | |
| fcurrent(0), | fcurrent(0), |
| fcolumns(acolumns), | fcolumns(acolumns), |
| name2number(pool(), false) { | name2number(*NEW Hash(pool())) { |
| if(fcolumns) | if(fcolumns) |
| for(int i=0; i<fcolumns->size(); i++) { | for(int i=0; i<fcolumns->size(); i++) { |
| Line 27 Table::Table(Pool& apool, | Line 31 Table::Table(Pool& apool, |
| } | } |
| } | } |
| const Array &Table::at(int index) { | Table::Table(const Table& source) : |
| return *static_cast<const Array *>(get(index)); | Array(source), |
| forigin_string(source.forigin_string), | |
| fcurrent(source.fcurrent), | |
| fcolumns(source.fcolumns), | |
| name2number(source.name2number) { | |
| } | } |
| const char *Table::item(const String& column_name) { | Table::Table(Pool& apool, const Table& source, int offset) : |
| int column_index; | Array(apool), |
| if(fcolumns) { // named | |
| int found_index=name2number.get_int(column_name); | forigin_string(source.forigin_string), |
| if(found_index) | fcurrent(source.fcurrent), |
| column_index=found_index-1; | fcolumns(source.fcolumns), |
| else | name2number(source.name2number) { |
| THROW(0, 0, | |
| &column_name, | append_array(source, offset); |
| } | |
| int Table::column_name2index(const String& column_name, bool bark) const { | |
| if(fcolumns) {// named | |
| int result=name2number.get_int(column_name)-1; // -1 = column not found | |
| if(bark && result<0) | |
| throw Exception("parser.runtime", | |
| &column_name, | |
| "column not found"); | "column not found"); |
| return result; | |
| } else { // nameless | } else { // nameless |
| column_index=atoi(column_name.cstr()); | char *error_pos; |
| if(!valid(fcurrent)) | const char *cstr=column_name.cstr(); |
| return 0; // it's OK we don't have row, just return nothing | int result=(int)strtol(cstr, &error_pos, 0); |
| if(*error_pos/*not EOS*/) { | |
| result=-1; | |
| if(bark) | |
| throw Exception("parser.runtime", | |
| &column_name, | |
| "invalid column number"); | |
| } | |
| return result; | |
| } | |
| } | |
| 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(int column, const String& value) { | |
| int scurrent=fcurrent; | |
| for(fcurrent=0; fcurrent<size(); fcurrent++) { | |
| const String *item_value=item(column); | |
| if(item_value && *item_value==value) | |
| return true; | |
| } | } |
| return item(column_index); | fcurrent=scurrent; |
| return false; | |
| } | |
| bool Table::locate(const String& column, const String& value) { | |
| return locate(column_name2index(column, true), value); | |
| } | |
| void Table::offset(bool absolute, int offset) { | |
| if(size()) | |
| fcurrent=((absolute?0:fcurrent)+offset+size())%size(); | |
| } | } |