|
|
| version 1.13.2.1, 2001/03/12 13:37:48 | version 1.53.2.10.2.7, 2003/04/14 11:23:41 |
|---|---|
| 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) | |
| $Id$ | Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| */ | */ |
| #include <stdlib.h> | static const char* IDENT_TABLE_C="$Date$"; |
| #include "pa_table.h" | #include "pa_table.h" |
| #include "pa_pool.h" | |
| Table::Table(Pool& apool, | #include "pa_exception.h" |
| Array *acolumns, | |
| int initial_rows) : | Table::Table(columns_type acolumns, size_t initial_rows): |
| Array(apool, initial_rows), | Array<element_type>(initial_rows), |
| fcurrent(0), | fcurrent(0), |
| fcolumns(acolumns), | fcolumns(acolumns), |
| name2number(pool(), false) { | name2number(new name2number_hash_class) { |
| if(fcolumns) | if(fcolumns) { |
| for(int i=0; i<fcolumns->size(); i++) { | size_t fcolumns_count=fcolumns->count(); |
| const String& name=*fcolumns->get_string(i); | size_t number=1; |
| name2number.put(name, i+1); | for(Array_iterator<const String*> i(*fcolumns); i.has_next(); ) { |
| const String& name=*i.next(); | |
| name2number->put(name, number++); | |
| } | } |
| } | |
| } | } |
| const Array &Table::at(int index) { | Table::Table(const Table& src, Action_options& options) : |
| return *static_cast<const Array *>(get(index)); | Array<element_type>(options.limit==ARRAY_OPTION_LIMIT_ALL?0:options.limit/*may be more than needed, no harm done*/), |
| fcurrent(0), | |
| fcolumns(src.fcolumns), | |
| name2number(src.name2number) { | |
| append(src, options.offset, options.limit, options.reverse); | |
| } | } |
| const char *Table::item(const String& column_name) { | int Table::column_name2index(const String& column_name, bool bark) const { |
| int column_index; | if(fcolumns) {// named |
| if(fcolumns) { // named | int result=name2number->get(column_name)-1; // -1 = column not found |
| int found_index=name2number.get_int(column_name); | if(bark && result<0) |
| if(found_index) | throw Exception("parser.runtime", |
| column_index=found_index-1; | &column_name, |
| else | |
| THROW(0, 0, | |
| &column_name, | |
| "column not found"); | "column not found"); |
| } else { // nameless | return result; |
| column_index=atoi(column_name.cstr()); | } else // nameless |
| if(!valid(fcurrent)) | return column_name.as_int(); |
| return 0; // it's OK we don't have row, just return nothing | } |
| const Array& row=at(fcurrent); | |
| if(column_index<0 || column_index>=row.size()) // read past proper index? | const String* Table::item(size_t column) { |
| return 0; // it's OK we don't have column, just return nothing | if(valid(fcurrent)) { |
| element_type row=get(fcurrent); | |
| if(column>=0 && column<row->count()) // proper index? | |
| return row->get(column); | |
| } | } |
| return 0; // it's OK we don't have row|column, just return nothing | |
| } | |
| #ifndef DOXYGEN | |
| struct Locate_int_string_info { | |
| int column; | |
| const String* value; | |
| }; | |
| #endif | |
| bool locate_int_string(Table& self, void* ainfo) { | |
| Locate_int_string_info& info=*static_cast<Locate_int_string_info*>(ainfo); | |
| const String *item_value=self.item(info.column); | |
| return item_value && *item_value==*info.value; | |
| } | |
| bool Table::locate(int column, const String& value, | |
| Table::Action_options& options) { | |
| Locate_int_string_info info={column, &value}; | |
| return locate(locate_int_string, &info, options); | |
| } | |
| bool Table::locate(const String& column, const String& value, | |
| Table::Action_options& options) { | |
| return locate(column_name2index(column, true), value, options); | |
| } | |
| return item(column_index); | void Table::offset(bool absolute, int offset) { |
| if(size_t lcount=count()) | |
| fcurrent=((absolute?0:fcurrent)+offset+lcount)%lcount; | |
| } | } |