Annotation of parser3/src/main/pa_table.C, revision 1.66
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.66 ! moko 10: volatile const char * IDENT_PA_TABLE_C="$Id: pa_table.C,v 1.65 2015/07/28 14:42:44 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.29 paf 47: int Table::column_name2index(const String& column_name, bool bark) const {
48: if(fcolumns) {// named
1.56 paf 49: int result=name2number->get(column_name)-1; // -1 = column not found
1.29 paf 50: if(bark && result<0)
1.62 misha 51: throw Exception(PARSER_RUNTIME,
1.29 paf 52: &column_name,
53: "column not found");
54: return result;
1.56 paf 55: } else // nameless
56: return column_name.as_int();
1.21 paf 57: }
58:
1.56 paf 59: const String* Table::item(size_t column) {
1.21 paf 60: if(valid(fcurrent)) {
1.56 paf 61: element_type row=get(fcurrent);
1.59 paf 62: if(column<row->count()) // proper index?
1.56 paf 63: return row->get(column);
1.21 paf 64: }
65: return 0; // it's OK we don't have row|column, just return nothing
66: }
67:
1.65 moko 68: void Table::put_item(size_t column, const String* value) {
69: if(!valid(fcurrent)) {
70: throw Exception(PARSER_RUNTIME, 0, "invalid current row");
71: }
72: element_type row=get(fcurrent);
73: while(row->count()<=column){
74: *row+=&String::Empty;
75: }
76: row->put(column, value);
77: }
78:
1.66 ! moko 79: void Table::remove_current(){
! 80: if(!valid(fcurrent)) {
! 81: throw Exception(PARSER_RUNTIME, 0, "invalid current row");
! 82: }
! 83: remove(fcurrent);
! 84: if(fcurrent==count() && count()>0){
! 85: fcurrent--;
! 86: }
! 87: }
! 88:
1.54 paf 89: #ifndef DOXYGEN
90: struct Locate_int_string_info {
91: int column;
92: const String* value;
93: };
94: #endif
1.57 paf 95: bool locate_int_string(Table& self, Locate_int_string_info* info) {
96: const String *item_value=self.item(info->column);
97: return item_value && *item_value==*info->value;
1.54 paf 98: }
99:
100: bool Table::locate(int column, const String& value,
101: Table::Action_options& options) {
102: Locate_int_string_info info={column, &value};
1.56 paf 103: return table_first_that(locate_int_string, &info, options);
1.54 paf 104: }
105:
106: bool Table::locate(const String& column, const String& value,
107: Table::Action_options& options) {
108: return locate(column_name2index(column, true), value, options);
1.22 paf 109: }
110:
1.41 paf 111: void Table::offset(bool absolute, int offset) {
1.65 moko 112: // not +lcount, but "if either operand is unsigned, the other shall be converted to unsigned" C++ rule works here
1.56 paf 113: if(size_t lcount=count())
114: fcurrent=((absolute?0:fcurrent)+offset+lcount)%lcount;
1.1 paf 115: }
E-mail: