|
|
| version 1.19, 2001/03/26 10:36:56 | version 1.53, 2003/01/21 15:51:15 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: table class. | Parser: table class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001, 2003 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* IDENT_TABLE_C="$Date$"; |
| //#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, |
| 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(*NEW Hash(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, |
| } | } |
| } | } |
| const Array &Table::at(int index) { | Table::Table(Pool& apool, const Table& source, int offset, int limit) : |
| return *const_cast<const Array *>(static_cast<Array *>(get(index))); | Array(apool, limit/*may be more than needed, no harm done*/), |
| forigin_string(source.forigin_string), | |
| fcurrent(0), | |
| fcolumns(source.fcolumns), | |
| name2number(source.name2number) { | |
| append_array(source, offset, limit); | |
| } | } |
| const String *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_int(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"); |
| return result; | |
| } else { // nameless | } else { // nameless |
| column_index=atoi(column_name.cstr()); | char *error_pos; |
| if(!valid(fcurrent)) | const char *cstr=column_name.cstr(); |
| return 0; // it's OK we don't have row, just return nothing | int result=(int)strtol(cstr, &error_pos, 0); |
| const Array& row=at(fcurrent); | if(*error_pos/*not EOS*/) { |
| if(column_index<0 || column_index>=row.size()) // read past proper index? | result=-1; |
| return 0; // it's OK we don't have column, just return nothing | if(bark) |
| throw Exception("parser.runtime", | |
| &column_name, | |
| "invalid column number"); | |
| } | |
| return result; | |
| } | } |
| return item(column_index); | |
| } | } |
| void Table::save(bool nameless_save, const String& file_spec) { | const String *Table::item(int column) const { |
| String sdata(pool()); | if(valid(fcurrent)) { |
| if(!nameless_save) { // not nameless=named output | const Array& row=at(fcurrent); |
| // write out names line | if(column>=0 && column<row.size()) // proper index? |
| if(fcolumns) { // named table | return row.get_string(column); |
| for(int column=0; column<fcolumns->size(); column++) { | |
| if(column) | |
| sdata.APPEND_CONST("\t"); | |
| sdata.append(*static_cast<String *>(fcolumns->quick_get(column)), | |
| String::UL_TABLE); | |
| } | |
| } else { // nameless table | |
| int lsize=size()?static_cast<Array *>(get(0))->size():0; | |
| if(lsize) | |
| for(int column=0; column<lsize; column++) { | |
| char *cindex_tab=(char *)malloc(MAX_NUMBER); | |
| snprintf(cindex_tab, MAX_NUMBER, "%d\t", column); | |
| sdata.APPEND_CONST(cindex_tab); | |
| } | |
| else | |
| sdata.APPEND_CONST("empty nameless table"); | |
| } | |
| sdata.APPEND_CONST("\n"); | |
| } | } |
| // data lines | return 0; // it's OK we don't have row|column, just return nothing |
| for(int index=0; index<size(); index++) { | } |
| Array *row=static_cast<Array *>(quick_get(index)); | |
| for(int column=0; column<row->size(); column++) { | bool Table::locate(int column, const String& value) { |
| if(column) | int scurrent=fcurrent; |
| sdata.APPEND_CONST("\t"); | for(fcurrent=0; fcurrent<size(); fcurrent++) { |
| sdata.append(*static_cast<String *>(row->quick_get(column)), | const String *item_value=item(column); |
| String::UL_TABLE); | if(item_value && *item_value==value) |
| } | return true; |
| sdata.APPEND_CONST("\n"); | |
| } | } |
| // write | |
| char *cdata=sdata.cstr(); | |
| file_write(pool(), file_spec, cdata, strlen(cdata), true); | |
| } | |
| fcurrent=scurrent; | |
| return false; | |
| } | |
| bool Table::locate(const String& column, const String& value) { | |
| return locate(column_name2index(column, true), value); | |
| } | |
| void Table::offset(bool absolute, int offset) { | |
| if(size()) | |
| fcurrent=((absolute?0:fcurrent)+offset+size())%size(); | |
| } |