|
|
| version 1.27, 2001/05/07 08:29:45 | version 1.61, 2005/08/09 08:14:52 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: table class. | Parser: table class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| $Id$ | |
| */ | */ |
| #include <stdlib.h> | static const char * const IDENT_TABLE_C="$Date$"; |
| #include "pa_table.h" | #include "pa_table.h" |
| #include "pa_pool.h" | |
| #include "pa_exception.h" | #include "pa_exception.h" |
| Table::Table(Pool& apool, | Table::Table(columns_type acolumns, size_t initial_rows): |
| const String *aorigin_string, | Array<element_type>(initial_rows), |
| Array *acolumns, | |
| int initial_rows) : | |
| Array(apool, initial_rows), | |
| forigin_string(aorigin_string), | |
| fcurrent(0), | fcurrent(0), |
| fcolumns(acolumns), | fcolumns(acolumns), |
| name2number(pool()) { | name2number(new name2number_hash_class) { |
| if(fcolumns) | if(fcolumns) { |
| for(int i=0; i<fcolumns->size(); i++) { | size_t number=1; |
| const String& name=*fcolumns->get_string(i); | for(Array_iterator<const String*> i(*fcolumns); i.has_next(); ) { |
| name2number.put(name, i+1); | const String& name=*i.next(); |
| name2number->put(name, number++); | |
| } | } |
| } | |
| } | |
| Table::Table(const Table& src, Action_options& options) : | |
| 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); | |
| } | } |
| 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 |
| return name2number.get_int(column_name)-1; // -1 = column not found | int result=name2number->get(column_name)-1; // -1 = column not found |
| else { // nameless | if(bark && result<0) |
| char *error_pos=0; | throw Exception("parser.runtime", |
| int result=(int)strtol(column_name.cstr(), &error_pos, 0); | |
| if(error_pos && *error_pos) | |
| THROW(0, 0, | |
| &column_name, | &column_name, |
| "invalid column number"); | "column not found"); |
| return result; | return result; |
| } | } else // nameless |
| return column_name.as_int(); | |
| } | } |
| const String *Table::item(int column) const { | const String* Table::item(size_t column) { |
| if(valid(fcurrent)) { | if(valid(fcurrent)) { |
| const Array& row=at(fcurrent); | element_type row=get(fcurrent); |
| if(column>=0 && column<row.size()) // proper index? | if(column<row->count()) // proper index? |
| return row.get_string(column); | return row->get(column); |
| } | } |
| return 0; // it's OK we don't have row|column, just return nothing | return 0; // it's OK we don't have row|column, just return nothing |
| } | } |
| bool Table::locate(int column, const String& value) { | #ifndef DOXYGEN |
| for(fcurrent=0; fcurrent<size(); fcurrent++) { | struct Locate_int_string_info { |
| const String *item_value=item(column); | int column; |
| if(item_value && *item_value==value) | const String* value; |
| return true; | }; |
| } | #endif |
| bool locate_int_string(Table& self, Locate_int_string_info* info) { | |
| 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 table_first_that(locate_int_string, &info, options); | |
| } | |
| fcurrent=0; | bool Table::locate(const String& column, const String& value, |
| return false; | Table::Action_options& options) { |
| return locate(column_name2index(column, true), value, options); | |
| } | } |
| void Table::shift(int offset) { | void Table::offset(bool absolute, int offset) { |
| if(size()) | if(size_t lcount=count()) |
| fcurrent=(fcurrent+offset+size())%size(); | fcurrent=((absolute?0:fcurrent)+offset+lcount)%lcount; |
| } | } |