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

1.14      paf         1: /** @file
1.15      paf         2:        Parser: table class.
                      3: 
1.53      paf         4:        Copyright (c) 2001, 2003 ArtLebedev Group (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.55    ! paf         8: static const char* IDENT_TABLE_C="$Date: 2003/04/11 15:00:05 $";
1.1       paf         9: 
1.48      paf        10: //#include <stdlib.h>
1.2       paf        11: 
1.1       paf        12: #include "pa_table.h"
                     13: #include "pa_pool.h"
1.25      paf        14: #include "pa_exception.h"
1.1       paf        15: 
1.12      paf        16: Table::Table(Pool& apool, 
1.18      paf        17:                         const String *aorigin_string,
1.35      parser     18:                         const Array *acolumns, 
1.1       paf        19:                         int initial_rows) :
1.12      paf        20:        Array(apool, initial_rows),
1.13      paf        21: 
1.18      paf        22:        forigin_string(aorigin_string),
1.5       paf        23:        fcurrent(0),
                     24:        fcolumns(acolumns), 
1.47      paf        25:        name2number(*NEW Hash(pool())) {
1.1       paf        26: 
1.5       paf        27:        if(fcolumns)
                     28:                for(int i=0; i<fcolumns->size(); i++) {
1.12      paf        29:                        const String& name=*fcolumns->get_string(i);
1.2       paf        30:                        name2number.put(name, i+1);
1.1       paf        31:                }
1.42      paf        32: }
                     33: 
1.54      paf        34: Table::Table(Pool& apool, const Table& source, Action_options& options) :
                     35:        Array(apool, options.limit/*may be more than needed, no harm done*/),
1.42      paf        36: 
                     37:        forigin_string(source.forigin_string),
1.51      paf        38:        fcurrent(0),
1.42      paf        39:        fcolumns(source.fcolumns),
                     40:        name2number(source.name2number) {
                     41: 
1.54      paf        42:        append_array(source, options.offset, options.limit, options.reverse);
1.2       paf        43: }
                     44: 
1.29      paf        45: int Table::column_name2index(const String& column_name, bool bark) const {
                     46:        if(fcolumns) {// named
                     47:                int result=name2number.get_int(column_name)-1; // -1 = column not found
                     48:                if(bark && result<0)
1.45      paf        49:                        throw Exception("parser.runtime",
1.29      paf        50:                                &column_name,
                     51:                                "column not found");
                     52:                return result;
                     53:        } else { // nameless
1.36      parser     54:                char *error_pos;
                     55:                const char *cstr=column_name.cstr();
                     56:                int result=(int)strtol(cstr, &error_pos, 0);
1.37      parser     57:                if(*error_pos/*not EOS*/) {
                     58:                        result=-1;
1.30      paf        59:                        if(bark)
1.45      paf        60:                                throw Exception("parser.runtime",
1.30      paf        61:                                        &column_name,
                     62:                                        "invalid column number");
                     63:                }
1.24      paf        64:                return result;
                     65:        }
1.21      paf        66: }
                     67: 
1.22      paf        68: const String *Table::item(int column) const {
1.21      paf        69:        if(valid(fcurrent)) {
1.7       paf        70:                const Array& row=at(fcurrent);
1.22      paf        71:                if(column>=0 && column<row.size()) // proper index?
                     72:                        return row.get_string(column);
1.21      paf        73:        }
                     74:        return 0; // it's OK we don't have row|column, just return nothing
                     75: }
                     76: 
1.54      paf        77: bool Table::locate(Table::locate_func func, void *info, Table::Action_options& o) {
                     78:        int size=this->size();
1.55    ! paf        79:        int row=o.offset;
        !            80:        if(!size || !o.limit || row>=size)
1.54      paf        81:                return false;
1.55    ! paf        82:        // max(limit)
        !            83:        int m=o.reverse?
        !            84:                row
        !            85:                :size-row;
        !            86:        if(!m)
        !            87:                return false;
        !            88:        // fix o.limit
        !            89:        if(o.limit<0 || o.limit>m)
        !            90:                o.limit=m;
1.54      paf        91: 
                     92:        int saved_current=current();
                     93:        if(o.reverse) { // reverse
                     94:                int to=max(0, row-o.limit);
                     95:                for(; row>=to; --row) {
                     96:                        set_current(row);
                     97: 
                     98:                        if(func(*this, info))
                     99:                                return true;
                    100:                }
                    101:        } else { // forward
                    102:                int to=min(row+o.limit, size);
                    103:                for(; row<to; row++) {
                    104:                        set_current(row);
                    105: 
                    106:                        if(func(*this, info))
                    107:                                return true;
                    108:                }
1.2       paf       109:        }
1.54      paf       110:        set_current(saved_current);
1.2       paf       111: 
1.21      paf       112:        return false;
1.28      paf       113: }
                    114: 
1.54      paf       115: #ifndef DOXYGEN
                    116: struct Locate_int_string_info {
                    117:        int column;
                    118:        const String* value;
                    119: };
                    120: #endif
                    121: bool locate_int_string(Table& self, void* ainfo) {
                    122:        Locate_int_string_info& info=*static_cast<Locate_int_string_info*>(ainfo);
                    123: 
                    124:        const String *item_value=self.item(info.column);
                    125:        return item_value && *item_value==*info.value;
                    126: }
                    127: 
                    128: bool Table::locate(int column, const String& value,
                    129:                   Table::Action_options& options) {
                    130:        Locate_int_string_info info={column, &value};
                    131:        return locate(locate_int_string, &info, options);
                    132: }
                    133: 
                    134: bool Table::locate(const String& column, const String& value, 
                    135:                   Table::Action_options& options) {
                    136:        return locate(column_name2index(column, true), value, options);
1.22      paf       137: }
                    138: 
1.41      paf       139: void Table::offset(bool absolute, int offset) {
1.22      paf       140:        if(size())
1.41      paf       141:                fcurrent=((absolute?0:fcurrent)+offset+size())%size();
1.1       paf       142: }

E-mail: