|
|
| version 1.1, 2001/01/29 15:56:04 | version 1.29, 2001/05/08 08:14:56 |
|---|---|
| Line 1 | Line 1 |
| /* | /** @file |
| $Id$ | Parser: table class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| $Id$ | |
| */ | */ |
| #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, |
| char *afile, uint aline, | const String *aorigin_string, |
| Array *acolumns, | Array *acolumns, |
| int initial_rows) : | int initial_rows) : |
| Array(apool, initial_rows), | Array(apool, initial_rows), |
| columns_order(acolumns), | |
| name2number(apool, false) { | forigin_string(aorigin_string), |
| #ifndef NO_STRING_ORIGIN | fcurrent(0), |
| origin.file=afile; | fcolumns(acolumns), |
| origin.line=aline; | name2number(pool()) { |
| #endif | |
| if(fcolumns) | |
| if(columns_order) | for(int i=0; i<fcolumns->size(); i++) { |
| for(int i=0; i<columns_order->size(); i++) { | const String& name=*fcolumns->get_string(i); |
| String name(pool); | name2number.put(name, i+1); |
| name.APPEND(static_cast<char *>((*columns_order)[i]), 0, 0); | |
| name2number.put(name, reinterpret_cast<Item>(i)); | |
| } | } |
| } | } |
| 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(0, 0, | |
| &column_name, | |
| "column not found"); | |
| return result; | |
| } 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(int column) const { | |
| if(valid(fcurrent)) { | |
| const Array& row=at(fcurrent); | |
| if(column>=0 && column<row.size()) // proper index? | |
| 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; | |
| } | |
| fcurrent=0; | |
| return false; | |
| } | |
| bool Table::locate(const String& column, const String& value) { | |
| return locate(column_name2index(column, true), value); | |
| } | |
| void Table::shift(int offset) { | |
| if(size()) | |
| fcurrent=(fcurrent+offset+size())%size(); | |
| } |