Annotation of parser3/src/main/pa_table.C, revision 1.52
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.52 ! paf 8: static const char* IDENT_TABLE_C="$Date: 2002/12/05 11:23:53 $";
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.50 paf 34: Table::Table(Pool& apool, const Table& source, int offset, int limit) :
35: Array(apool, 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.50 paf 42: append_array(source, offset, limit);
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.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: