Annotation of parser3/src/main/pa_table.C, revision 1.19
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.19 ! paf 8: $Id: pa_table.C,v 1.18 2001/03/25 09:10:30 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.7 paf 34: const Array &Table::at(int index) {
1.16 paf 35: return *const_cast<const Array *>(static_cast<Array *>(get(index)));
1.2 paf 36: }
37:
1.12 paf 38: const String *Table::item(const String& column_name) {
1.2 paf 39: int column_index;
1.12 paf 40: if(fcolumns) { // named
1.2 paf 41: int found_index=name2number.get_int(column_name);
42: if(found_index)
43: column_index=found_index-1;
44: else
1.9 paf 45: THROW(0, 0,
1.6 paf 46: &column_name,
47: "column not found");
1.12 paf 48: } else { // nameless
1.2 paf 49: column_index=atoi(column_name.cstr());
1.13 paf 50: if(!valid(fcurrent))
51: return 0; // it's OK we don't have row, just return nothing
1.7 paf 52: const Array& row=at(fcurrent);
1.12 paf 53: if(column_index<0 || column_index>=row.size()) // read past proper index?
1.13 paf 54: return 0; // it's OK we don't have column, just return nothing
1.2 paf 55: }
56:
57: return item(column_index);
1.1 paf 58: }
1.19 ! paf 59:
! 60: void Table::save(bool nameless_save, const String& file_spec) {
! 61: String sdata(pool());
! 62: if(!nameless_save) { // not nameless=named output
! 63: // write out names line
! 64: if(fcolumns) { // named table
! 65: for(int column=0; column<fcolumns->size(); column++) {
! 66: if(column)
! 67: sdata.APPEND_CONST("\t");
! 68: sdata.append(*static_cast<String *>(fcolumns->quick_get(column)),
! 69: String::UL_TABLE);
! 70: }
! 71: } else { // nameless table
! 72: int lsize=size()?static_cast<Array *>(get(0))->size():0;
! 73: if(lsize)
! 74: for(int column=0; column<lsize; column++) {
! 75: char *cindex_tab=(char *)malloc(MAX_NUMBER);
! 76: snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
! 77: sdata.APPEND_CONST(cindex_tab);
! 78: }
! 79: else
! 80: sdata.APPEND_CONST("empty nameless table");
! 81: }
! 82: sdata.APPEND_CONST("\n");
! 83: }
! 84: // data lines
! 85: for(int index=0; index<size(); index++) {
! 86: Array *row=static_cast<Array *>(quick_get(index));
! 87: for(int column=0; column<row->size(); column++) {
! 88: if(column)
! 89: sdata.APPEND_CONST("\t");
! 90: sdata.append(*static_cast<String *>(row->quick_get(column)),
! 91: String::UL_TABLE);
! 92: }
! 93: sdata.APPEND_CONST("\n");
! 94: }
! 95:
! 96: // write
! 97: char *cdata=sdata.cstr();
! 98: file_write(pool(), file_spec, cdata, strlen(cdata), true);
! 99: }
E-mail: