Annotation of parser3/src/main/pa_table.C, revision 1.49
1.14 paf 1: /** @file
1.15 paf 2: Parser: table class.
3:
1.43 paf 4: Copyright (c) 2001, 2002 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.49 ! paf 8: static const char* IDENT_TABLE_C="$Date: pa_table.C,v 1.48 2002/08/01 11:26:51 paf Exp $";
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:
34: Table::Table(const Table& source) :
35: Array(source),
36:
37: forigin_string(source.forigin_string),
38: fcurrent(source.fcurrent),
39: fcolumns(source.fcolumns),
40: name2number(source.name2number) {
41:
1.46 paf 42: }
43:
44: Table::Table(Pool& apool, const Table& source, int offset) :
45: Array(apool),
46:
47: forigin_string(source.forigin_string),
48: fcurrent(source.fcurrent),
49: fcolumns(source.fcolumns),
50: name2number(source.name2number) {
51:
52: append_array(source, offset);
1.2 paf 53: }
54:
1.29 paf 55: int Table::column_name2index(const String& column_name, bool bark) const {
56: if(fcolumns) {// named
57: int result=name2number.get_int(column_name)-1; // -1 = column not found
58: if(bark && result<0)
1.45 paf 59: throw Exception("parser.runtime",
1.29 paf 60: &column_name,
61: "column not found");
62: return result;
63: } else { // nameless
1.36 parser 64: char *error_pos;
65: const char *cstr=column_name.cstr();
66: int result=(int)strtol(cstr, &error_pos, 0);
1.37 parser 67: if(*error_pos/*not EOS*/) {
68: result=-1;
1.30 paf 69: if(bark)
1.45 paf 70: throw Exception("parser.runtime",
1.30 paf 71: &column_name,
72: "invalid column number");
73: }
1.24 paf 74: return result;
75: }
1.21 paf 76: }
77:
1.22 paf 78: const String *Table::item(int column) const {
1.21 paf 79: if(valid(fcurrent)) {
1.7 paf 80: const Array& row=at(fcurrent);
1.22 paf 81: if(column>=0 && column<row.size()) // proper index?
82: return row.get_string(column);
1.21 paf 83: }
84: return 0; // it's OK we don't have row|column, just return nothing
85: }
86:
1.23 paf 87: bool Table::locate(int column, const String& value) {
1.31 parser 88: int scurrent=fcurrent;
1.22 paf 89: for(fcurrent=0; fcurrent<size(); fcurrent++) {
1.23 paf 90: const String *item_value=item(column);
1.21 paf 91: if(item_value && *item_value==value)
92: return true;
1.2 paf 93: }
94:
1.31 parser 95: fcurrent=scurrent;
1.21 paf 96: return false;
1.28 paf 97: }
98:
99: bool Table::locate(const String& column, const String& value) {
1.29 paf 100: return locate(column_name2index(column, true), value);
1.22 paf 101: }
102:
1.41 paf 103: void Table::offset(bool absolute, int offset) {
1.22 paf 104: if(size())
1.41 paf 105: fcurrent=((absolute?0:fcurrent)+offset+size())%size();
1.1 paf 106: }
E-mail: