Annotation of parser3/src/main/pa_table.C, revision 1.21
1.14 paf 1: /** @file
1.15 paf 2: Parser: table class.
3:
1.10 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.15 paf 5:
1.11 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.10 paf 7:
1.21 ! paf 8: $Id: pa_table.C,v 1.20 2001/03/27 13:47:31 paf Exp $
1.1 paf 9: */
10:
1.2 paf 11: #include <stdlib.h>
12:
1.1 paf 13: #include "pa_table.h"
14: #include "pa_pool.h"
15:
1.12 paf 16: Table::Table(Pool& apool,
1.18 paf 17: const String *aorigin_string,
1.1 paf 18: Array *acolumns,
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.12 paf 25: name2number(pool(), false) {
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.2 paf 32: }
33:
1.21 ! paf 34: int Table::column_name2index(const String& column_name) const {
! 35: int result;
1.12 paf 36: if(fcolumns) { // named
1.21 ! paf 37: int column_index=name2number.get_int(column_name);
! 38: if(column_index)
! 39: result=column_index-1;
1.2 paf 40: else
1.9 paf 41: THROW(0, 0,
1.6 paf 42: &column_name,
43: "column not found");
1.12 paf 44: } else { // nameless
1.21 ! paf 45: result=atoi(column_name.cstr());
! 46: }
! 47: return result;
! 48: }
! 49:
! 50: const String *Table::item(int column_index) const {
! 51: if(valid(fcurrent)) {
1.7 paf 52: const Array& row=at(fcurrent);
1.21 ! paf 53: if(column_index>=0 && column_index<row.size()) // proper index?
! 54: return row.get_string(column_index);
! 55: }
! 56: return 0; // it's OK we don't have row|column, just return nothing
! 57: }
! 58:
! 59: bool Table::locate(const String& column, const String& value) const {
! 60: int key_index=column_name2index(column);
! 61: for(int fcurrent=0; fcurrent<size(); fcurrent++) {
! 62: const String *item_value=item(key_index);
! 63: if(item_value && *item_value==value)
! 64: return true;
1.2 paf 65: }
66:
1.21 ! paf 67: fcurrent=0;
! 68: return false;
1.1 paf 69: }
E-mail: