Annotation of parser3/src/main/pa_table.C, revision 1.79
1.14 paf 1: /** @file
1.15 paf 2: Parser: table class.
3:
1.79 ! moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.73 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.48 paf 6: */
1.15 paf 7:
1.63 moko 8: #include "pa_table.h"
1.1 paf 9:
1.79 ! moko 10: volatile const char * IDENT_PA_TABLE_C="$Id: pa_table.C,v 1.78 2024/11/16 02:57:05 moko Exp $" IDENT_PA_TABLE_H;
1.2 paf 11:
1.25 paf 12: #include "pa_exception.h"
1.1 paf 13:
1.72 moko 14: Table::Table(columns_type acolumns, size_t initial_rows): Array<element_type>(initial_rows), fcurrent(0), fcolumns(acolumns), name2number(NULL){
15: column_names_init();
16: }
1.1 paf 17:
1.72 moko 18: void Table::column_names_init(){
19: if(fcolumns){
20: name2number = new name2number_hash_class;
1.56 paf 21: size_t number=1;
1.75 moko 22: for(ArrayString::Iterator i(*fcolumns); i; ) {
1.56 paf 23: const String& name=*i.next();
24: name2number->put(name, number++);
1.1 paf 25: }
1.56 paf 26: }
1.42 paf 27: }
28:
1.65 moko 29: static void append_row(Table& src, Table* dest) {
30: Table::element_type src_row(src[src.current()]);
31: Table::element_type row(new ArrayString(src_row->count()));
32: row->append(*src_row);
33: *dest+=row;
34: }
35:
1.56 paf 36: Table::Table(const Table& src, Action_options& options) :
1.76 moko 37: Array<element_type>(options.limit > src.count() ? src.count() : options.limit),
1.42 paf 38:
1.51 paf 39: fcurrent(0),
1.56 paf 40: fcolumns(src.fcolumns),
41: name2number(src.name2number) {
1.42 paf 42:
1.65 moko 43: ((Table &)src).table_for_each(append_row, this, options);
1.2 paf 44: }
45:
1.68 moko 46: size_t Table::max_cells() const {
47: size_t result=0;
48: for(size_t i=0; i<count(); i++){
1.67 moko 49: element_type row=get(i);
50: if(row->count()>result)
51: result=row->count();
52: }
53: return result;
54: }
55:
1.29 paf 56: int Table::column_name2index(const String& column_name, bool bark) const {
57: if(fcolumns) {// named
1.56 paf 58: int result=name2number->get(column_name)-1; // -1 = column not found
1.29 paf 59: if(bark && result<0)
1.72 moko 60: throw Exception(PARSER_RUNTIME, &column_name, "column not found");
1.29 paf 61: return result;
1.56 paf 62: } else // nameless
63: return column_name.as_int();
1.21 paf 64: }
65:
1.56 paf 66: const String* Table::item(size_t column) {
1.21 paf 67: if(valid(fcurrent)) {
1.56 paf 68: element_type row=get(fcurrent);
1.59 paf 69: if(column<row->count()) // proper index?
1.56 paf 70: return row->get(column);
1.21 paf 71: }
72: return 0; // it's OK we don't have row|column, just return nothing
73: }
74:
1.65 moko 75: void Table::put_item(size_t column, const String* value) {
76: if(!valid(fcurrent)) {
77: throw Exception(PARSER_RUNTIME, 0, "invalid current row");
78: }
79: element_type row=get(fcurrent);
80: while(row->count()<=column){
81: *row+=&String::Empty;
82: }
83: row->put(column, value);
84: }
85:
1.66 moko 86: void Table::remove_current(){
87: if(!valid(fcurrent)) {
88: throw Exception(PARSER_RUNTIME, 0, "invalid current row");
89: }
90: remove(fcurrent);
1.78 moko 91: if(fcurrent>=count() && count()>0){
1.66 moko 92: fcurrent--;
93: }
94: }
95:
1.54 paf 96: #ifndef DOXYGEN
97: struct Locate_int_string_info {
98: int column;
99: const String* value;
100: };
101: #endif
1.57 paf 102: bool locate_int_string(Table& self, Locate_int_string_info* info) {
103: const String *item_value=self.item(info->column);
104: return item_value && *item_value==*info->value;
1.54 paf 105: }
106:
1.72 moko 107: bool Table::locate(int column, const String& value, Table::Action_options& options) {
1.54 paf 108: Locate_int_string_info info={column, &value};
1.56 paf 109: return table_first_that(locate_int_string, &info, options);
1.54 paf 110: }
111:
1.72 moko 112: bool Table::locate(const String& column, const String& value, Table::Action_options& options) {
1.54 paf 113: return locate(column_name2index(column, true), value, options);
1.22 paf 114: }
115:
1.41 paf 116: void Table::offset(bool absolute, int offset) {
1.65 moko 117: // not +lcount, but "if either operand is unsigned, the other shall be converted to unsigned" C++ rule works here
1.56 paf 118: if(size_t lcount=count())
119: fcurrent=((absolute?0:fcurrent)+offset+lcount)%lcount;
1.1 paf 120: }
E-mail: