Annotation of parser3/src/main/pa_table.C, revision 1.12
1.1 paf 1: /*
1.10 paf 2: Parser
3: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.11 paf 4: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.10 paf 5:
1.12 ! paf 6: $Id: pa_table.C,v 1.11 2001/03/11 08:16:35 paf Exp $
1.1 paf 7: */
8:
1.2 paf 9: #include <stdlib.h>
10:
1.1 paf 11: #include "pa_table.h"
12: #include "pa_pool.h"
13:
1.12 ! paf 14: Table::Table(Pool& apool,
1.1 paf 15: char *afile, uint aline,
16: Array *acolumns,
17: int initial_rows) :
1.12 ! paf 18: Array(apool, initial_rows),
1.5 paf 19: fcurrent(0),
20: fcolumns(acolumns),
1.12 ! paf 21: name2number(pool(), false) {
1.1 paf 22: #ifndef NO_STRING_ORIGIN
1.5 paf 23: forigin.file=afile;
24: forigin.line=aline;
1.1 paf 25: #endif
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.7 paf 34: const Array &Table::at(int index) {
1.2 paf 35: if(index<0 || index>=size())
1.9 paf 36: THROW(0, 0,
1.6 paf 37: 0,
1.7 paf 38: "table row index %d is out of range [0..%d]",
1.2 paf 39: index, size()-1);
40:
1.7 paf 41: return *static_cast<const Array *>(get(index));
1.2 paf 42: }
43:
1.12 ! paf 44: const String *Table::item(const String& column_name) {
1.2 paf 45: int column_index;
1.12 ! paf 46: if(fcolumns) { // named
1.2 paf 47: int found_index=name2number.get_int(column_name);
48: if(found_index)
49: column_index=found_index-1;
50: else
1.9 paf 51: THROW(0, 0,
1.6 paf 52: &column_name,
53: "column not found");
1.12 ! paf 54: } else { // nameless
1.2 paf 55: column_index=atoi(column_name.cstr());
1.7 paf 56: const Array& row=at(fcurrent);
1.12 ! paf 57: if(column_index<0 || column_index>=row.size()) // read past proper index?
! 58: return 0; // it's OK, just return nothing
1.2 paf 59: }
60:
61: return item(column_index);
1.1 paf 62: }
E-mail: