Annotation of parser3/src/main/pa_table.C, revision 1.68

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

E-mail: