|
|
| version 1.15.2.1, 2001/03/20 18:24:44 | version 1.27, 2001/05/07 08:29:45 |
|---|---|
| Line 12 | Line 12 |
| #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, | 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 32 Table::Table(Pool& apool, |
| } | } |
| } | } |
| const Array &Table::at(int index) { | int Table::column_name2index(const String& column_name) const { |
| return *const_cast<const Array *>(static_cast<Array *>(get(index))); | if(fcolumns) // named |
| return name2number.get_int(column_name)-1; // -1 = column not found | |
| else { // nameless | |
| char *error_pos=0; | |
| int result=(int)strtol(column_name.cstr(), &error_pos, 0); | |
| if(error_pos && *error_pos) | |
| THROW(0, 0, | |
| &column_name, | |
| "invalid column number"); | |
| return result; | |
| } | |
| } | } |
| const String *Table::item(const String& column_name) { | const String *Table::item(int column) const { |
| int column_index; | if(valid(fcurrent)) { |
| if(fcolumns) { // named | |
| int found_index=name2number.get_int(column_name); | |
| if(found_index) | |
| column_index=found_index-1; | |
| else | |
| THROW(0, 0, | |
| &column_name, | |
| "column not found"); | |
| } else { // nameless | |
| column_index=atoi(column_name.cstr()); | |
| if(!valid(fcurrent)) | |
| return 0; // it's OK we don't have row, just return nothing | |
| 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) { | |
| 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=0; |
| return false; | |
| } | |
| void Table::shift(int offset) { | |
| if(size()) | |
| fcurrent=(fcurrent+offset+size())%size(); | |
| } | } |