|
|
| version 1.15.2.1, 2001/03/20 18:24:44 | version 1.36.2.1, 2001/09/18 08:52:49 |
|---|---|
| Line 4 | Line 4 |
| 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$ | |
| */ | */ |
| static const char *RCSId="$Id$"; | |
| #include <stdlib.h> | #include <stdlib.h> |
| #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, |
| const String& asource, | const String *aorigin_string, |
| Array *acolumns, | const 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()) { |
| if(fcolumns) | if(fcolumns) |
| for(int i=0; i<fcolumns->size(); i++) { | for(int i=0; i<fcolumns->size(); i++) { |
| Line 31 Table::Table(Pool& apool, | Line 31 Table::Table(Pool& apool, |
| } | } |
| } | } |
| const Array &Table::at(int index) { | int Table::column_name2index(const String& column_name, bool bark) const { |
| return *const_cast<const Array *>(static_cast<Array *>(get(index))); | if(fcolumns) {// named |
| } | int result=name2number.get_int(column_name)-1; // -1 = column not found |
| if(bark && result<0) | |
| const String *Table::item(const String& column_name) { | |
| int column_index; | |
| if(fcolumns) { // named | |
| int found_index=name2number.get_int(column_name); | |
| if(found_index) | |
| column_index=found_index-1; | |
| else | |
| THROW(0, 0, | THROW(0, 0, |
| &column_name, | &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(0, 0, | |
| &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; | |
| } | |
| fcurrent=scurrent; | |
| return false; | |
| } | |
| bool Table::locate(const String& column, const String& value) { | |
| return locate(column_name2index(column, true), value); | |
| } | |
| return item(column_index); | void Table::shift(int offset) { |
| if(size()) | |
| fcurrent=(fcurrent+offset+size())%size(); | |
| } | } |