|
|
| version 1.24, 2001/03/29 20:53:04 | version 1.39, 2001/10/19 12:43:30 |
|---|---|
| Line 2 | Line 2 |
| Parser: table class. | 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 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, |
| const String *aorigin_string, | const String *aorigin_string, |
| Array *acolumns, | const Array *acolumns, |
| int initial_rows) : | int initial_rows) : |
| Array(apool, initial_rows), | Array(apool, initial_rows), |
| forigin_string(aorigin_string), | 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, |
| } | } |
| } | } |
| int Table::column_name2index(const String& column_name) const { | int Table::column_name2index(const String& column_name, bool bark) const { |
| if(fcolumns) { // named | if(fcolumns) {// named |
| int column_number=name2number.get_int(column_name); | int result=name2number.get_int(column_name)-1; // -1 = column not found |
| if(column_number) | if(bark && result<0) |
| return column_number-1; | throw Exception(0, 0, |
| else { | &column_name, |
| THROW(0, 0, | |
| &column_name, | |
| "column not found"); | "column not found"); |
| return 0; // unreached | return result; |
| } | |
| } else { // nameless | } else { // nameless |
| char *error_pos=0; | char *error_pos; |
| int result=(int)strtol(column_name.cstr(), &error_pos, 0); | const char *cstr=column_name.cstr(); |
| if(error_pos && *error_pos) | int result=(int)strtol(cstr, &error_pos, 0); |
| THROW(0, 0, | if(*error_pos/*not EOS*/) { |
| &column_name, | result=-1; |
| "invalid column number"); | if(bark) |
| throw Exception(0, 0, | |
| &column_name, | |
| "invalid column number"); | |
| } | |
| return result; | return result; |
| } | } |
| } | } |
| Line 63 const String *Table::item(int column) co | Line 64 const String *Table::item(int column) co |
| } | } |
| bool Table::locate(int column, const String& value) { | bool Table::locate(int column, const String& value) { |
| int scurrent=fcurrent; | |
| for(fcurrent=0; fcurrent<size(); fcurrent++) { | for(fcurrent=0; fcurrent<size(); fcurrent++) { |
| const String *item_value=item(column); | const String *item_value=item(column); |
| if(item_value && *item_value==value) | if(item_value && *item_value==value) |
| return true; | return true; |
| } | } |
| fcurrent=0; | fcurrent=scurrent; |
| return false; | return false; |
| } | } |
| bool Table::locate(const String& column, const String& value) { | |
| return locate(column_name2index(column, true), value); | |
| } | |
| void Table::shift(int offset) { | void Table::shift(int offset) { |
| if(size()) | if(size()) |
| fcurrent=(fcurrent+offset+size())%size(); | fcurrent=(fcurrent+offset+size())%size(); |