Annotation of parser3/src/main/pa_table.C, revision 1.53.2.1
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.53.2.1! paf 8: static const char* IDENT_TABLE_C="$Date: 2003/01/21 15:51:15 $";
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.53.2.1! paf 16: Table::Table(
! 17: object_ptr<const String> aorigin_string,
! 18: object_ptr<const Array<const String> > acolumns,
! 19: int initial_rows):
! 20: Array<element_type>(initial_rows),
1.13 paf 21:
1.18 paf 22: forigin_string(aorigin_string),
1.5 paf 23: fcurrent(0),
24: fcolumns(acolumns),
1.53.2.1! paf 25: name2number(object_ptr<name2number_hash_class>(new name2number_hash_class)) {
1.1 paf 26:
1.53.2.1! paf 27: if(fcolumns.get())
! 28: for(int i=0; i<fcolumns->count(); i++) {
! 29: const String& name=fcolumns->get(i);
! 30: name2number->put(name, i+1);
1.1 paf 31: }
1.42 paf 32: }
33:
1.53.2.1! paf 34: Table::Table(const Table& src, int offset, int limit) :
! 35: Array<element_type>(limit/*may be more than needed, no harm done*/),
1.42 paf 36:
1.53.2.1! paf 37: forigin_string(src.forigin_string),
1.51 paf 38: fcurrent(0),
1.53.2.1! paf 39: fcolumns(src.fcolumns),
! 40: name2number(src.name2number) {
1.42 paf 41:
1.53.2.1! paf 42: append(src, offset, limit);
1.2 paf 43: }
44:
1.53.2.1! paf 45: int Table::column_name2index(object_ptr<const String> column_name, bool bark) const {
! 46: if(fcolumns.get()) {// named
! 47: int result=name2number->get(*column_name.get())-1; // -1 = column not found
1.29 paf 48: if(bark && result<0)
1.45 paf 49: throw Exception("parser.runtime",
1.53.2.1! paf 50: column_name,
1.29 paf 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.23 paf 77: bool Table::locate(int column, const String& value) {
1.31 parser 78: int scurrent=fcurrent;
1.22 paf 79: for(fcurrent=0; fcurrent<size(); fcurrent++) {
1.23 paf 80: const String *item_value=item(column);
1.21 paf 81: if(item_value && *item_value==value)
82: return true;
1.2 paf 83: }
84:
1.31 parser 85: fcurrent=scurrent;
1.21 paf 86: return false;
1.28 paf 87: }
88:
89: bool Table::locate(const String& column, const String& value) {
1.29 paf 90: return locate(column_name2index(column, true), value);
1.22 paf 91: }
92:
1.41 paf 93: void Table::offset(bool absolute, int offset) {
1.22 paf 94: if(size())
1.41 paf 95: fcurrent=((absolute?0:fcurrent)+offset+size())%size();
1.1 paf 96: }
E-mail: